forked from Chisel-Team/Chisel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Skiftcha/chisel-gui-scroll
Chisel gui scroll
- Loading branch information
Showing
7 changed files
with
322 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package team.chisel.client.gui; | ||
|
||
import net.minecraft.util.ResourceLocation; | ||
import org.lwjgl.opengl.GL11; | ||
import team.chisel.inventory.ContainerChisel; | ||
import team.chisel.network.PacketHandler; | ||
import team.chisel.network.message.MessageScrollUpdate; | ||
|
||
public class GuiScrollbar { | ||
public static final ResourceLocation TEXTURE_RESOURCE_LOCATION = new ResourceLocation("minecraft", "textures/gui/container/creative_inventory/tabs.png"); | ||
|
||
private int displayX = 0; | ||
private int displayY = 0; | ||
private int width = 12; | ||
private int height = 15; | ||
|
||
private int rowSize = 10; | ||
private int rowsOnPage = 6; | ||
|
||
private int scrollHeight = height; | ||
private ContainerChisel container; | ||
|
||
public void draw(GuiChisel g) { | ||
// chisel2GuiScroll scroll button texture coordinates work strange (maybe because of alignment) so taking vanilla one | ||
g.mc.getTextureManager().bindTexture(TEXTURE_RESOURCE_LOCATION); | ||
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | ||
|
||
if (getMaxScroll() == 0) { | ||
g.drawTexturedModalRect(displayX, displayY, 232 + width, 0, width, height); | ||
} else { | ||
int offset = getCurrentScroll() * (scrollHeight - height) / getMaxScroll(); | ||
g.drawTexturedModalRect(displayX, offset + displayY, 232, 0, width, height); | ||
} | ||
} | ||
|
||
public GuiScrollbar setScrollHeight(int scrollHeight) { | ||
this.scrollHeight = scrollHeight; | ||
return this; | ||
} | ||
|
||
public GuiScrollbar setLeft(int displayX) { | ||
this.displayX = displayX; | ||
return this; | ||
} | ||
|
||
public GuiScrollbar setTop(int displayY) { | ||
this.displayY = displayY; | ||
return this; | ||
} | ||
|
||
private int getMaxScroll() { | ||
int maxScroll = (container.inventory.getMaxScroll() + rowSize - 1) / rowSize - rowsOnPage; | ||
|
||
if (maxScroll <= 0) { | ||
maxScroll = 0; | ||
} | ||
|
||
return maxScroll; | ||
} | ||
|
||
public int getCurrentScroll() { | ||
return container.inventory.getCurrentScroll() / rowSize; | ||
} | ||
|
||
private void setCurrentScroll(int scroll) { | ||
container.inventory.setCurrentScroll(scroll * rowSize); | ||
PacketHandler.INSTANCE.sendToServer(new MessageScrollUpdate(scroll * rowSize)); | ||
} | ||
|
||
private int applyRange(int scroll) { | ||
return Math.max(Math.min(scroll, getMaxScroll()), 0); | ||
} | ||
|
||
void click(int x, int y) { | ||
if (getMaxScroll() == 0) { | ||
return; | ||
} | ||
|
||
if (x > displayX && x <= displayX + width) { | ||
if (y > displayY && y <= displayY + scrollHeight) { | ||
int currentScroll = (y - displayY - height / 2); | ||
currentScroll = Math.round(1.0f * currentScroll * getMaxScroll() / (scrollHeight - height)); | ||
setCurrentScroll(applyRange(currentScroll)); | ||
} | ||
} | ||
} | ||
|
||
void wheel(int delta) { | ||
setCurrentScroll(applyRange(getCurrentScroll() + Math.max(Math.min(-delta, 1), -1))); | ||
} | ||
|
||
public void setContainerChisel(ContainerChisel container) { | ||
this.container = container; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.