diff --git a/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java b/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java index 6c138b25c..b035b84be 100644 --- a/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java +++ b/src/main/java/noppes/npcs/client/gui/mainmenu/GuiNpcDisplay.java @@ -7,6 +7,7 @@ import kamkeel.npcs.network.packets.request.mainmenu.MainmenuDisplayGetPacket; import kamkeel.npcs.network.packets.request.mainmenu.MainmenuDisplaySavePacket; import net.minecraft.client.gui.GuiButton; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.ResourceLocation; import noppes.npcs.DataDisplay; import noppes.npcs.client.NoppesUtil; @@ -41,6 +42,8 @@ public class GuiNpcDisplay extends GuiNPCInterface2 implements ITextfieldListene this.addButton(new GuiNpcButton(0, guiLeft + 253+52, y , 110, 20, new String[]{"display.show","display.hide","display.showAttacking"} ,display.showName)); this.addButton(new GuiNpcButton(14, guiLeft + 259, y , 20, 20, Character.toString('\u21bb'))); + getButton(14).setIconTexture(new ResourceLocation("customnpcs", "textures/gui/slot.png")); + this.addButton(new GuiNpcButton(15, guiLeft + 259 + 22, y , 20, 20, Character.toString('\u22EE'))); y+=23; diff --git a/src/main/java/noppes/npcs/client/gui/util/GuiNpcButton.java b/src/main/java/noppes/npcs/client/gui/util/GuiNpcButton.java index bd3e3b8f0..dc568c1e5 100644 --- a/src/main/java/noppes/npcs/client/gui/util/GuiNpcButton.java +++ b/src/main/java/noppes/npcs/client/gui/util/GuiNpcButton.java @@ -4,6 +4,7 @@ import kamkeel.npcs.util.TextSplitter; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.util.ResourceLocation; import net.minecraft.util.StatCollector; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL12; @@ -12,97 +13,141 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -public class GuiNpcButton extends GuiButton{ +public class GuiNpcButton extends GuiButton { - protected String[] display; - private int displayValue = 0; - public int id; + protected String[] display; + private int displayValue = 0; + public int id; public String hoverableText = ""; public int oldHover; public int hoverCount = 0; boolean hasSubGUI = false; - public GuiNpcButton(int i, int j, int k, String s) { - super(i, j, k, StatCollector.translateToLocal(s)); - id = i; - } - public GuiNpcButton(int i, int j, int k, String[] display, int val) { - this(i, j, k, display[val]); - this.display = display; - this.displayValue = val; - } - public GuiNpcButton(int i, int j, int k, int l, int m, String string) { - super(i, j, k, l, m, StatCollector.translateToLocal(string)); - id = i; - } - public GuiNpcButton(int i, int j, int k, int l, int m, String[] display, - int val) { - this(i, j, k, l, m, display.length == 0?"":display[val % display.length]); - this.display = display; - this.displayValue = display.length == 0?0:val % display.length; - } + // Fields for icon texture and its dimensions. + private ResourceLocation iconTexture = null; + private int iconWidth = 16; + private int iconHeight = 16; - public GuiNpcButton(int i, int j, int k, int l, int m, Enum[] displayEnums, - int val) { + public GuiNpcButton(int i, int j, int k, String s) { + super(i, j, k, StatCollector.translateToLocal(s)); + id = i; + } + + public GuiNpcButton(int i, int j, int k, String[] display, int val) { + this(i, j, k, display[val]); + this.display = display; + this.displayValue = val; + } + + public GuiNpcButton(int i, int j, int k, int l, int m, String string) { + super(i, j, k, l, m, StatCollector.translateToLocal(string)); + id = i; + } + + public GuiNpcButton(int i, int j, int k, int l, int m, String[] display, int val) { + this(i, j, k, l, m, display.length == 0 ? "" : display[val % display.length]); + this.display = display; + this.displayValue = display.length == 0 ? 0 : val % display.length; + } + + public GuiNpcButton(int i, int j, int k, int l, int m, Enum[] displayEnums, int val) { this(i, j, k, l, m, displayEnums.length == 0 ? "" : displayEnums[val % displayEnums.length].toString()); - ArrayList strings = new ArrayList<>(); for (Enum e : displayEnums) { strings.add(e.toString()); } - this.display = strings.toArray(new String[0]); - this.displayValue = display.length == 0?0:val % display.length; + this.displayValue = display.length == 0 ? 0 : val % display.length; } - public void setDisplayText(String text){ - this.displayString = StatCollector.translateToLocal(text); - } - public void setHoverText(String text){ + public void setDisplayText(String text) { + this.displayString = StatCollector.translateToLocal(text); + } + + public void setHoverText(String text) { this.hoverableText = StatCollector.translateToLocal(text); } - public int getValue(){ - return displayValue; - } - public void setEnabled(boolean bo){ - this.enabled = bo; - } - public void setVisible(boolean b) { - this.visible = b; - } - public boolean getVisible() { - return visible; - } - - public void setDisplay(int value){ - this.displayValue = value; - this.setDisplayText(display[value]); + public int getValue() { + return displayValue; } - public void setTextColor(int color){ - this.packedFGColour = color; + public void setEnabled(boolean bo) { + this.enabled = bo; + } + + public void setVisible(boolean b) { + this.visible = b; + } + + public boolean getVisible() { + return visible; + } + + public void setDisplay(int value) { + this.displayValue = value; + this.setDisplayText(display[value]); + } + + public void setTextColor(int color) { + this.packedFGColour = color; + } + + /** + * Sets the icon texture for this button. + * The texture should be 16x16 by default, unless you adjust the icon dimensions. + */ + public void setIconTexture(ResourceLocation texture) { + this.iconTexture = texture; + } + + /** + * Sets the icon dimensions. + */ + public void setIconSize(int width, int height) { + this.iconWidth = width; + this.iconHeight = height; } @Override - public boolean mousePressed(Minecraft minecraft, int i, int j) - { - boolean bo = super.mousePressed(minecraft, i, j); - if(bo && display != null && display.length != 0){ - displayValue = (displayValue+1) % display.length; - this.setDisplayText(display[displayValue]); - } - return bo; + public boolean mousePressed(Minecraft minecraft, int i, int j) { + boolean bo = super.mousePressed(minecraft, i, j); + if (bo && display != null && display.length != 0) { + displayValue = (displayValue + 1) % display.length; + this.setDisplayText(display[displayValue]); + } + return bo; } - public int getWidth() { - return width; - } + public int getWidth() { + return width; + } + + @Override + public void drawButton(Minecraft mc, int mouseX, int mouseY) { + if (!this.visible) + return; + // First, draw the button normally using the superclass method. + super.drawButton(mc, mouseX, mouseY); + // Then, if an icon texture is set, draw it. + if (iconTexture != null) { + mc.getTextureManager().bindTexture(iconTexture); + GL11.glPushMatrix(); + RenderHelper.enableGUIStandardItemLighting(); + // Calculate the icon position so that it's centered in the button. + // (Assumes the button's (xPosition,yPosition) is its top left.) + int iconX = this.xPosition + (this.width - iconWidth) / 2; + int iconY = this.yPosition + (this.height - iconHeight) / 2; + // Draw the texture; we assume texture coordinates start at (0,0) with the desired size. + this.drawTexturedModalRect(iconX, iconY, 0, 0, iconWidth, iconHeight); + RenderHelper.disableStandardItemLighting(); + GL11.glPopMatrix(); + } + } public void drawHover(int i, int j, boolean hasSubGui) { if (hasSubGui || !visible || hoverableText.isEmpty()) return; - int hoverState = this.getHoverState(this.field_146123_n); if (oldHover != hoverState) { oldHover = hoverState; @@ -111,7 +156,6 @@ public class GuiNpcButton extends GuiButton{ if (hoverCount < 110) hoverCount++; } - if (hoverState == 2 && hoverCount > 100) { GL11.glPushMatrix(); Minecraft mc = Minecraft.getMinecraft(); @@ -127,7 +171,6 @@ public class GuiNpcButton extends GuiButton{ protected void drawHoveringText(List textLines, int x, int y, Minecraft mc) { if (mc.fontRenderer == null || textLines.isEmpty()) return; - GL11.glDisable(GL12.GL_RESCALE_NORMAL); RenderHelper.disableStandardItemLighting(); GL11.glDisable(GL11.GL_LIGHTING); @@ -151,26 +194,21 @@ public class GuiNpcButton extends GuiButton{ maxHeight += 2 + (textLines.size() - 1) * 10; } - // Calculate the maximum x-coordinate to ensure the tooltip stays within the game window int gameWidth = mc.displayWidth; - int maxTooltipX = gameWidth - maxWidth - 4; // Subtract the tooltip width + int maxTooltipX = gameWidth - maxWidth - 4; - // Adjust tooltip position if it goes off the screen to the right if (j2 > maxTooltipX) { int diff = j2 - maxTooltipX; - j2 -= diff; // Shift tooltip to the left - GL11.glTranslatef(-300, 0, 0); // Apply translation + j2 -= diff; + GL11.glTranslatef(-300, 0, 0); } if (k2 + maxHeight + 6 > mc.displayHeight) { k2 = mc.displayHeight - maxHeight - 6; } - // Draw the tooltip this.zLevel = 300.0F; int j1 = -267386864; - // Draw the gradient background - // (Ensure to adjust the coordinates according to the translation) this.drawGradientRect(j2 - 3, k2 - 4, j2 + maxWidth + 3, k2 - 3, j1, j1); this.drawGradientRect(j2 - 3, k2 + maxHeight + 3, j2 + maxWidth + 3, k2 + maxHeight + 4, j1, j1); this.drawGradientRect(j2 - 3, k2 - 3, j2 + maxWidth + 3, k2 + maxHeight + 3, j1, j1); @@ -183,7 +221,6 @@ public class GuiNpcButton extends GuiButton{ this.drawGradientRect(j2 - 3, k2 - 3, j2 + maxWidth + 3, k2 - 3 + 1, k1, k1); this.drawGradientRect(j2 - 3, k2 + maxHeight + 2, j2 + maxWidth + 3, k2 + maxHeight + 3, l1, l1); - // Draw the text lines for (int i2 = 0; i2 < textLines.size(); ++i2) { String s1 = (String) textLines.get(i2); mc.fontRenderer.drawStringWithShadow(s1, j2, k2, -1); @@ -201,28 +238,21 @@ public class GuiNpcButton extends GuiButton{ } @Override - public int getHoverState(boolean mouseOver) - { + public int getHoverState(boolean mouseOver) { byte b0 = 1; - if (!this.enabled) - { + if (!this.enabled) { b0 = 0; - } - else if (mouseOver && !this.hasSubGUI) - { + } else if (mouseOver && !this.hasSubGUI) { b0 = 2; } - return b0; } - public void updateSubGUI(boolean hasSubGUI) - { + public void updateSubGUI(boolean hasSubGUI) { this.hasSubGUI = hasSubGUI; } - public boolean getHasSubGUI() - { + public boolean getHasSubGUI() { return this.hasSubGUI; } }