From fcf74094f023408c683ac94fb4d99fc4e8a1fdb2 Mon Sep 17 00:00:00 2001 From: somehussar <148253652+somehussar@users.noreply.github.com> Date: Tue, 21 May 2024 03:28:25 +0200 Subject: [PATCH] Add scrollbars --- .../gui/util/GuiScrollableComponent.java | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiScrollableComponent.java b/src/main/java/noppes/npcs/client/gui/util/GuiScrollableComponent.java index 38cc087e0..48ce510cf 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiScrollableComponent.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiScrollableComponent.java @@ -3,6 +3,7 @@ package noppes.npcs.client.gui.util; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.ScaledResolution; import net.minecraft.util.ChatComponentText; +import net.minecraft.util.ResourceLocation; import noppes.npcs.entity.EntityNPCInterface; import noppes.npcs.scripted.CustomNPCsException; import org.lwjgl.input.Mouse; @@ -10,6 +11,7 @@ import org.lwjgl.opengl.GL11; public class GuiScrollableComponent extends GuiNPCInterface{ + public static final ResourceLocation resource = new ResourceLocation("customnpcs","textures/gui/misc.png"); protected ScaledResolution scaledResolution; public int clipWidth; public int clipHeight; @@ -66,12 +68,16 @@ public class GuiScrollableComponent extends GuiNPCInterface{ //Enable clipping GL11.glEnable(GL11.GL_SCISSOR_TEST); - setClip(); + setClip(xPos, yPos, clipWidth, clipHeight); this.drawDefaultBackground = false; if(drawBackground) this.drawDefaultBackground(); + + if(maxScrollY > clipHeight/2) + setClip(xPos, yPos, clipWidth-13, clipHeight); + GL11.glTranslatef(xPos, yPos+scrollY, 0); super.drawScreen(mouseX-xPos, (int) (mouseY-scrollY-yPos), partialTicks); @@ -79,7 +85,13 @@ public class GuiScrollableComponent extends GuiNPCInterface{ //Disable clipping (VERY IMPORTANT) GL11.glDisable(GL11.GL_SCISSOR_TEST); - GL11.glTranslatef(0, 0, 0); + GL11.glTranslatef(-xPos, -yPos-scrollY, 0); + + + + if(maxScrollY > clipHeight/2){ + drawScrollBar(); + } GL11.glPopMatrix(); } @@ -90,6 +102,22 @@ public class GuiScrollableComponent extends GuiNPCInterface{ this.drawScreen(mouseX, mouseY, partialTicks); } + private void drawScrollBar() + { + mc.renderEngine.bindTexture(resource); + GL11.glColor4f(1, 1, 1, 1); + + int maxSize = (int) ((clipHeight) * ( (double) (clipHeight)/ maxScrollY))/2; + + int x = xPos + clipWidth - 9; + int y = (int) (yPos + 4 + (scrollY/maxScrollY * (clipHeight-maxSize-8))); + + for(int k = y; k < y+maxSize && k-clipHeight+maxSize < clipHeight;k++){ + drawTexturedModalRect(x, k, 176, 9, 5, 1); + } + + } + public void adjustScroll(int mouseScroll) { nextScrollY -= (float) mouseScroll/25; if(nextScrollY < 0) @@ -125,15 +153,14 @@ public class GuiScrollableComponent extends GuiNPCInterface{ return mouseX >= xPos && mouseX <= clipWidth+xPos && mouseY >= yPos && mouseY <= clipHeight+yPos; } - protected void setClip(){ - setClip(xPos, yPos, clipWidth, clipHeight); - } - protected void setClip(int x, int y, int width, int height){ if(scaledResolution == null) return; int scaleFactor = scaledResolution.getScaleFactor(); +// if(drawingScrollbar) +// width-=10; + //Correct the positions for Screen Space in OpenGL x*=scaleFactor; y*=scaleFactor;