Port over scrollable GUIs

This commit is contained in:
somehussar committed 2024-05-19 02:27:03 +02:00
1 parent f8c1c18e45
commit 3c464c52fd
2 files changed
+161 -7

No files matched your search

@@ -29,13 +29,15 @@ public abstract class GuiNPCInterface extends GuiScreen
public boolean drawDefaultBackground = true;
public EntityNPCInterface npc;
protected HashMap<Integer,GuiNpcButton> buttons = new HashMap<Integer,GuiNpcButton>();
private HashMap<Integer,GuiMenuTopButton> topbuttons = new HashMap<Integer,GuiMenuTopButton>();
private HashMap<Integer,GuiMenuSideButton> sidebuttons = new HashMap<Integer,GuiMenuSideButton>();
private HashMap<Integer,GuiNpcTextField> textfields = new HashMap<Integer,GuiNpcTextField>();
private HashMap<Integer,GuiNpcLabel> labels = new HashMap<Integer,GuiNpcLabel>();
private HashMap<Integer,GuiCustomScroll> scrolls = new HashMap<Integer,GuiCustomScroll>();
private HashMap<Integer,GuiNpcSlider> sliders = new HashMap<Integer,GuiNpcSlider>();
private HashMap<Integer,GuiScreen> extra = new HashMap<Integer,GuiScreen>();
protected HashMap<Integer,GuiMenuTopButton> topbuttons = new HashMap<Integer,GuiMenuTopButton>();
protected HashMap<Integer,GuiMenuSideButton> sidebuttons = new HashMap<Integer,GuiMenuSideButton>();
protected HashMap<Integer,GuiNpcTextField> textfields = new HashMap<Integer,GuiNpcTextField>();
protected HashMap<Integer,GuiNpcLabel> labels = new HashMap<Integer,GuiNpcLabel>();
protected HashMap<Integer,GuiCustomScroll> scrolls = new HashMap<Integer,GuiCustomScroll>();
protected HashMap<Integer,GuiNpcSlider> sliders = new HashMap<Integer,GuiNpcSlider>();
protected HashMap<Integer,GuiScreen> extra = new HashMap<Integer,GuiScreen>();
protected HashMap<Integer, GuiScrollable> 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<GuiNpcTextField>(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<GuiCustomScroll>(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);
@@ -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);
}
}