From 9b6d1fdfd354b62eb2a332337384c5bcdb7fd4db Mon Sep 17 00:00:00 2001 From: Matthias Mann Date: Tue, 14 Apr 2015 22:11:23 +0200 Subject: [PATCH] vertical scrollbar widget --- .../java/crazypants/enderio/gui/IconEIO.java | 11 ++ .../crazypants/enderio/gui/VScrollbarEIO.java | 181 ++++++++++++++++++ .../java/crazypants/gui/GuiContainerBase.java | 76 +++++++- 3 files changed, 262 insertions(+), 6 deletions(-) create mode 100644 src/main/java/crazypants/enderio/gui/VScrollbarEIO.java diff --git a/src/main/java/crazypants/enderio/gui/IconEIO.java b/src/main/java/crazypants/enderio/gui/IconEIO.java index 71d6f8bd..94336173 100644 --- a/src/main/java/crazypants/enderio/gui/IconEIO.java +++ b/src/main/java/crazypants/enderio/gui/IconEIO.java @@ -111,6 +111,17 @@ public final class IconEIO { public static final IconEIO MINUS_BUT_HOVER = new IconEIO(208, 56, 8, 8); public static final IconEIO MINUS_BUT_HOVER_PRESSED = new IconEIO(216, 56, 8, 8); + public static final IconEIO UP_ARROW_OFF = new IconEIO(201, 64, 11, 8); + public static final IconEIO UP_ARROW_ON = new IconEIO(212, 64, 11, 8); + public static final IconEIO UP_ARROW_HOVER_OFF = new IconEIO(234, 64, 11, 8); + public static final IconEIO UP_ARROW_HOVER_ON = new IconEIO(245, 64, 11, 8); + public static final IconEIO DOWN_ARROW_OFF = new IconEIO(201, 72, 11, 8); + public static final IconEIO DOWN_ARROW_ON = new IconEIO(212, 72, 11, 8); + public static final IconEIO DOWN_ARROW_HOVER_OFF = new IconEIO(234, 72, 11, 8); + public static final IconEIO DOWN_ARROW_HOVER_ON = new IconEIO(245, 72, 11, 8); + public static final IconEIO VSCROLL_THUMB = new IconEIO(223, 64, 11, 8); + public static final IconEIO VSCROLL_THUMB_HOVER = new IconEIO(223, 72, 11, 8); + public static final IconEIO SOUND = new IconEIO(176, 208); public static final IconEIO XP = new IconEIO(128, 224); diff --git a/src/main/java/crazypants/enderio/gui/VScrollbarEIO.java b/src/main/java/crazypants/enderio/gui/VScrollbarEIO.java new file mode 100644 index 00000000..58e3b066 --- /dev/null +++ b/src/main/java/crazypants/enderio/gui/VScrollbarEIO.java @@ -0,0 +1,181 @@ +package crazypants.enderio.gui; + +import crazypants.gui.IGuiScreen; +import crazypants.render.RenderUtil; +import java.awt.Rectangle; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.Tessellator; +import org.lwjgl.opengl.GL11; + +public class VScrollbarEIO { + + protected final IGuiScreen gui; + + protected int xOrigin; + protected int yOrigin; + protected int height; + + protected int xPosition; + protected int yPosition; + protected Rectangle wholeArea; + protected Rectangle btnUp; + protected Rectangle btnDown; + protected Rectangle thumbArea; + + protected int scrollPos; + protected int scrollMax; + + protected boolean pressedUp; + protected boolean pressedDown; + protected boolean pressedThumb; + protected int scrollDir; + protected long timeNextScroll; + + public VScrollbarEIO(IGuiScreen gui, int xOrigin, int yOrigin, int height) { + this.gui = gui; + this.xOrigin = xOrigin; + this.yOrigin = yOrigin; + this.height = height; + } + + public void adjustPosition() { + xPosition = xOrigin + gui.getGuiLeft(); + yPosition = yOrigin + gui.getGuiTop(); + wholeArea = new Rectangle(xPosition, yPosition, + (int)IconEIO.VSCROLL_THUMB.width, height); + btnUp = new Rectangle(xPosition, yPosition, + (int)IconEIO.UP_ARROW_OFF.width, (int)IconEIO.UP_ARROW_OFF.height); + btnDown = new Rectangle(xPosition, yPosition + Math.max(0, height - (int)IconEIO.DOWN_ARROW_OFF.height), + (int)IconEIO.DOWN_ARROW_OFF.width, (int)IconEIO.DOWN_ARROW_OFF.height); + thumbArea = new Rectangle(xPosition, yPosition + btnUp.height, + (int)IconEIO.VSCROLL_THUMB.width, Math.max(0, height - (btnUp.height + btnDown.height))); + } + + public int getScrollPos() { + return scrollPos; + } + + public void setScrollPos(int scrollPos) { + this.scrollPos = limitPos(scrollPos); + } + + public void scrollBy(int amount) { + setScrollPos(scrollPos + amount); + } + + public int getScrollMax() { + return scrollMax; + } + + public void setScrollMax(int scrollMax) { + this.scrollMax = scrollMax; + setScrollPos(scrollPos); + } + + public void drawScrollbar(int mouseX, int mouseY) { + boolean hoverUp = btnUp.contains(mouseX, mouseY); + boolean hoverDown = btnDown.contains(mouseX, mouseY); + + IconEIO iconUp; + if(pressedUp) { + iconUp = hoverUp ? IconEIO.UP_ARROW_HOVER_ON : IconEIO.UP_ARROW_ON; + } else { + iconUp = hoverUp ? IconEIO.UP_ARROW_HOVER_OFF : IconEIO.UP_ARROW_OFF; + } + + IconEIO iconDown; + if(pressedDown) { + iconDown = hoverDown ? IconEIO.DOWN_ARROW_HOVER_ON : IconEIO.DOWN_ARROW_ON; + } else { + iconDown = hoverDown ? IconEIO.DOWN_ARROW_HOVER_OFF : IconEIO.DOWN_ARROW_OFF; + } + + if(scrollDir != 0) { + long time = Minecraft.getSystemTime(); + if((timeNextScroll - time) <= 0) { + timeNextScroll = time + 100; + scrollBy(scrollDir); + } + } + + RenderUtil.bindTexture(IconEIO.TEXTURE); + GL11.glColor3f(1, 1, 1); + + Tessellator tes = Tessellator.instance; + tes.startDrawingQuads(); + + iconUp.renderIcon(btnUp.x, btnUp.y, false); + iconDown.renderIcon(btnDown.x, btnDown.y, false); + + if(scrollMax > 0) { + int thumbPos = getThumbPosition(); + boolean hoverThumb = thumbArea.contains(mouseX, mouseY) && + mouseY >= thumbPos && mouseY < thumbPos + (int)IconEIO.VSCROLL_THUMB.height; + + IconEIO iconThumb = hoverThumb ? IconEIO.VSCROLL_THUMB_HOVER : IconEIO.VSCROLL_THUMB; + iconThumb.renderIcon(thumbArea.x, thumbPos, false); + } + + tes.draw(); + GL11.glPopAttrib(); + } + + public boolean mouseClicked(int x, int y, int button) { + if(button == 0) { + if(scrollMax > 0 && thumbArea.contains(x, y)) { + int thumbPos = getThumbPosition(); + pressedUp = y < thumbPos; + pressedDown = y >= thumbPos + (int)IconEIO.VSCROLL_THUMB.height; + pressedThumb = !pressedUp && !pressedDown; + } else { + pressedUp = btnUp.contains(x, y); + pressedDown = btnDown.contains(x, y); + pressedThumb = false; + } + + scrollDir = (pressedDown ? 1 : 0) - (pressedUp ? 1 : 0); + if(scrollDir != 0) { + timeNextScroll = Minecraft.getSystemTime() + 200; + scrollBy(scrollDir); + } + } + return isDragActive(); + } + + public boolean mouseClickMove(int x, int y, int button, long time) { + if(pressedThumb) { + int pos = y - (thumbArea.y + (int)IconEIO.VSCROLL_THUMB.height/2); + int len = thumbArea.height - (int)IconEIO.VSCROLL_THUMB.height; + if(len > 0) { + setScrollPos(Math.round(pos * (float)scrollMax / (float)len)); + } + return true; + } + return false; + } + + public void mouseMovedOrUp(int x, int y, int button) { + pressedUp = false; + pressedDown = false; + pressedThumb = false; + scrollDir = 0; + } + + public void mouseWheel(int x, int y, int delta) { + if(!isDragActive() && wholeArea.contains(x, y)) { + scrollBy(-Integer.signum(delta)); + } + } + + public boolean isDragActive() { + return pressedUp || pressedDown || pressedThumb; + } + + protected int getThumbPosition() { + return thumbArea.y + (thumbArea.height - (int)IconEIO.VSCROLL_THUMB.height) * scrollPos / scrollMax; + } + + protected int limitPos(int pos) { + return Math.max(0, Math.min(pos, scrollMax)); + } +} diff --git a/src/main/java/crazypants/gui/GuiContainerBase.java b/src/main/java/crazypants/gui/GuiContainerBase.java index 23c9476c..984e8097 100644 --- a/src/main/java/crazypants/gui/GuiContainerBase.java +++ b/src/main/java/crazypants/gui/GuiContainerBase.java @@ -29,6 +29,7 @@ import cpw.mods.fml.common.Optional; import crazypants.enderio.gui.IGuiOverlay; import crazypants.enderio.gui.IconButtonEIO; import crazypants.enderio.gui.TextFieldEIO; +import crazypants.enderio.gui.VScrollbarEIO; import crazypants.gui.ToolTipManager.ToolTipRenderer; import crazypants.render.RenderUtil; @@ -40,9 +41,11 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe protected ToolTipManager ttMan = new ToolTipManager(); protected List overlays = new ArrayList(); protected List textFields = Lists.newArrayList(); + protected List scrollbars = new ArrayList(); protected List ghostSlots = new ArrayList(); protected GhostSlot hoverGhostSlot; + protected VScrollbarEIO draggingScrollbar; protected GuiContainerBase(Container par1Container) { super(par1Container); @@ -131,6 +134,10 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe return; } } + int delta = Mouse.getEventDWheel(); + if(delta != 0) { + mouseWheel(x, y, delta); + } super.handleMouseInput(); } @@ -156,6 +163,18 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe for (GuiTextField f : textFields) { f.mouseClicked(x, y, button); } + if(!scrollbars.isEmpty()) { + if(draggingScrollbar != null) { + draggingScrollbar.mouseClicked(x, y, button); + return; + } + for(VScrollbarEIO vs : scrollbars) { + if(vs.mouseClicked(x, y, button)) { + draggingScrollbar = vs; + return; + } + } + } if(!ghostSlots.isEmpty()) { GhostSlot slot = getGhostSlot(x, y); if(slot != null) { @@ -178,6 +197,32 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe super.mouseClicked(x, y, button); } + @Override + protected void mouseMovedOrUp(int x, int y, int button) { + if(draggingScrollbar != null) { + draggingScrollbar.mouseMovedOrUp(x, y, button); + draggingScrollbar = null; + } + super.mouseMovedOrUp(x, y, button); + } + + @Override + protected void mouseClickMove(int x, int y, int button, long time) { + if(draggingScrollbar != null) { + draggingScrollbar.mouseClickMove(x, y, button, time); + return; + } + super.mouseClickMove(x, y, button, time); + } + + protected void mouseWheel(int x, int y, int delta) { + if(!scrollbars.isEmpty()) { + for(VScrollbarEIO vs : scrollbars) { + vs.mouseWheel(x, y, delta); + } + } + } + protected void actionPerformedButton(IconButtonEIO btn, int mouseButton) { actionPerformed(btn); } @@ -190,6 +235,18 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe overlays.remove(overlay); } + public void addScrollbar(VScrollbarEIO vs) { + scrollbars.add(vs); + vs.adjustPosition(); + } + + public void removeScrollbar(VScrollbarEIO vs) { + scrollbars.remove(vs); + if(draggingScrollbar == vs) { + draggingScrollbar = null; + } + } + private int realMx, realMy; @Override @@ -217,6 +274,11 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe for (GuiTextField f : textFields) { f.drawTextBox(); } + if(!scrollbars.isEmpty()) { + for(VScrollbarEIO vs : scrollbars) { + vs.drawScrollbar(mouseX, mouseY); + } + } drawGhostSlots(mouseX, mouseY); } @@ -235,14 +297,16 @@ public abstract class GuiContainerBase extends GuiContainer implements ToolTipRe super.drawScreen(mx, my, par3); - if(hoverGhostSlot != null && mc.thePlayer.inventory.getItemStack() == null) { - ItemStack stack = hoverGhostSlot.getStack(); - if(stack != null) { - renderToolTip(stack, par1, par2); + if(draggingScrollbar == null) { + if(hoverGhostSlot != null && mc.thePlayer.inventory.getItemStack() == null) { + ItemStack stack = hoverGhostSlot.getStack(); + if(stack != null) { + renderToolTip(stack, par1, par2); + } } - } - ttMan.drawTooltips(this, par1, par2); + ttMan.drawTooltips(this, par1, par2); + } } // copied from super with hate