From 3c464c52fdebcee5331388e6fa77c45b50178324 Mon Sep 17 00:00:00 2001 From: somehussar <148253652+somehussar@users.noreply.github.com> Date: Sun, 19 May 2024 02:27:03 +0200 Subject: [PATCH] Port over scrollable GUIs --- .../npcs/client/gui/util/GuiNPCInterface.java | 35 ++++- .../npcs/client/gui/util/GuiScrollable.java | 133 ++++++++++++++++++ 2 files changed, 161 insertions(+), 7 deletions(-) create mode 100644 src/main/java/noppes/npcs/client/gui/util/GuiScrollable.java diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java b/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java index a2144bc81..1ee84436d 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiNPCInterface.java @@ -29,13 +29,15 @@ public abstract class GuiNPCInterface extends GuiScreen public boolean drawDefaultBackground = true; public EntityNPCInterface npc; protected HashMap buttons = new HashMap(); - private HashMap topbuttons = new HashMap(); - private HashMap sidebuttons = new HashMap(); - private HashMap textfields = new HashMap(); - private HashMap labels = new HashMap(); - private HashMap scrolls = new HashMap(); - private HashMap sliders = new HashMap(); - private HashMap extra = new HashMap(); + protected HashMap topbuttons = new HashMap(); + protected HashMap sidebuttons = new HashMap(); + protected HashMap textfields = new HashMap(); + protected HashMap labels = new HashMap(); + protected HashMap scrolls = new HashMap(); + protected HashMap sliders = new HashMap(); + protected HashMap extra = new HashMap(); + protected HashMap scrollableGuiInserts = new HashMap<>(); + public String title; private ResourceLocation background = null; public boolean closeOnEsc = false; @@ -105,6 +107,11 @@ public abstract class GuiNPCInterface extends GuiScreen extra.put(gui.id, gui); } + public void addScrollableGui(int id, GuiScrollable gui){ + gui.setWorldAndResolution(mc, width, height); + extra.put(id, gui); + } + public void mouseClicked(int i, int j, int k) { if(subgui != null) @@ -113,6 +120,12 @@ public abstract class GuiNPCInterface extends GuiScreen for(GuiNpcTextField tf : new ArrayList(textfields.values())) if(tf.enabled) tf.mouseClicked(i, j, k); + + for(GuiScrollable guiScrollable : scrollableGuiInserts.values()) { + if(guiScrollable.isMouseOver(i, j)) + guiScrollable.mouseClicked(i, j, k); + } + if (k == 0){ for(GuiCustomScroll scroll : new ArrayList(scrolls.values())){ scroll.mouseClicked(i, j, k); @@ -189,6 +202,11 @@ public abstract class GuiNPCInterface extends GuiScreen return labels.get(i); } + public GuiScrollable getScrollableGui(int i) { + return scrollableGuiInserts.get(i); + } + + public void addSlider(GuiNpcSlider slider){ sliders.put(slider.id,slider); buttonList.add(slider); @@ -241,6 +259,9 @@ public abstract class GuiNPCInterface extends GuiScreen button.drawHover(i, j, subGui); } } + for(GuiScrollable guiScrollable : scrollableGuiInserts.values()){ + guiScrollable.drawScreen(i, j, f, guiScrollable.isMouseOver(i,j)?Mouse.getDWheel():0); + } if(subgui != null) subgui.drawScreen(i,j,f); diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiScrollable.java b/src/main/java/noppes/npcs/client/gui/util/GuiScrollable.java new file mode 100644 index 000000000..da03b96fb --- /dev/null +++ b/src/main/java/noppes/npcs/client/gui/util/GuiScrollable.java @@ -0,0 +1,133 @@ +package noppes.npcs.client.gui.util; + +import net.minecraft.client.gui.ScaledResolution; +import noppes.npcs.entity.EntityNPCInterface; +import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.GL11; + +public class GuiScrollable extends GuiNPCInterface{ + + protected ScaledResolution scaledResolution; + public int clipWidth; + public int clipHeight; + + protected int maxScrollY = 0; + protected float scrollY = 0; + protected float nextScrollY = 0; + + private double lerp(double a, double b, double lambda){ + return a + lambda * (b - a); + } + + public GuiScrollable(EntityNPCInterface npc, int posX, int posY, int clipWidth, int clipHeight, int maxScroll){ + super(npc); + this.guiLeft = posX; + this.guiTop = posY; + this.clipWidth = clipWidth; + this.clipHeight = clipHeight; + this.maxScrollY = maxScroll; + } + + @Override + public void save() { + + } + + @Override + public void initGui(){ + super.initGui(); + + + + float scrollPerc = 0; + if((guiTop+clipHeight) != 0){ + scrollPerc = (nextScrollY) / (clipHeight); + } + + nextScrollY = scrollPerc * (clipHeight); + } + + @Override + public void drawScreen(int mouseX, int mouseY, float partialTicks){ + scrollY = (float) lerp(scrollY, nextScrollY, partialTicks); + if(Float.isNaN(scrollY)) + scrollY = 0; + + boolean drawBackground = this.drawDefaultBackground; + + GL11.glPushMatrix(); + + //Enable clipping + GL11.glEnable(GL11.GL_SCISSOR_TEST); + setClip(); + + this.drawDefaultBackground = false; + if(drawBackground) + this.drawDefaultBackground(); + + GL11.glTranslatef(guiLeft, guiTop+scrollY, 0); + super.drawScreen(mouseX-guiLeft, (int) (mouseY-scrollY-guiTop), partialTicks); + + this.drawDefaultBackground = drawBackground; + + //Disable clipping (VERY IMPORTANT) + GL11.glDisable(GL11.GL_SCISSOR_TEST); + GL11.glTranslatef(0, 0, 0); + + GL11.glPopMatrix(); + } + + public void drawScreen(int mouseX, int mouseY, float partialTicks, int mouseScroll){ + nextScrollY -= (float) mouseScroll/25; + if(nextScrollY < 0) + nextScrollY = 0; + if(nextScrollY > clipHeight) { + nextScrollY = clipHeight; + } + this.drawScreen(mouseX, mouseY, partialTicks); + } + + @Override + public void mouseClicked(int mouseX, int mouseY, int mouseButton){ + super.mouseClicked(mouseX-guiLeft, (int) (mouseY-scrollY-guiTop), mouseButton); + } + + @Override + public void handleMouseInput(){ + super.handleMouseInput(); + + nextScrollY -= (float) Mouse.getEventDWheel()/25; + if(nextScrollY < 0) + nextScrollY = 0; + if(nextScrollY > maxScrollY){ + nextScrollY = maxScrollY; + } + + } + + public boolean isMouseOver(int mouseX, int mouseY){ + return mouseX >= guiLeft && mouseX <= xSize && mouseY >= guiTop && mouseY <= ySize; + } + + protected void setClip(){ + setClip(guiLeft, guiTop, clipWidth, clipHeight); + } + + protected void setClip(int x, int y, int width, int height){ + if(scaledResolution == null) + return; + int scaleFactor = scaledResolution.getScaleFactor(); + + //Correct the positions for Screen Space in OpenGL + x*=scaleFactor; + y*=scaleFactor; + width*=scaleFactor; + height*=scaleFactor; + + //Adjust position to Top-Left origin (OpenGL window/screen space uses bottom-left origin) + y=mc.displayHeight-y; + + //Set clip + GL11.glScissor(x, y-height, width, height); + } +}