From 96106900a36ae66dee0673b2e44086c40c9f2f38 Mon Sep 17 00:00:00 2001 From: slprime <31038811+slprime@users.noreply.github.com> Date: Sat, 5 Oct 2024 13:21:04 +0300 Subject: [PATCH] fix tooltip position (#587) Co-authored-by: slprime Co-authored-by: Martin Robertz --- .../java/appeng/client/gui/AEBaseGui.java | 180 ++++++++---------- .../java/appeng/client/gui/AEBaseMEGui.java | 4 +- .../gui/implementations/GuiCraftConfirm.java | 10 +- .../gui/implementations/GuiCraftingCPU.java | 4 +- .../implementations/GuiInterfaceTerminal.java | 4 +- .../gui/implementations/GuiMEMonitorable.java | 4 +- .../gui/implementations/GuiNetworkStatus.java | 6 +- .../implementations/GuiOptimizePatterns.java | 5 +- .../gui/widgets/GuiCraftingCPUTable.java | 2 +- .../client/gui/widgets/GuiCraftingTree.java | 4 +- 10 files changed, 96 insertions(+), 127 deletions(-) diff --git a/src/main/java/appeng/client/gui/AEBaseGui.java b/src/main/java/appeng/client/gui/AEBaseGui.java index d8dfa2657..ffa35c0d1 100644 --- a/src/main/java/appeng/client/gui/AEBaseGui.java +++ b/src/main/java/appeng/client/gui/AEBaseGui.java @@ -13,6 +13,7 @@ package appeng.client.gui; import java.io.IOException; import java.text.DecimalFormat; import java.text.ParseException; +import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; @@ -23,7 +24,6 @@ import java.util.concurrent.TimeUnit; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.gui.ScaledResolution; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.RenderHelper; import net.minecraft.client.renderer.Tessellator; @@ -32,6 +32,7 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.Slot; import net.minecraft.item.ItemStack; +import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.MinecraftForge; @@ -80,18 +81,63 @@ import appeng.integration.IntegrationType; import appeng.integration.abstraction.INEI; import appeng.util.Platform; import appeng.util.item.AEItemStack; +import codechicken.lib.gui.GuiDraw; import codechicken.nei.guihook.GuiContainerManager; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.ObfuscationReflectionHelper; public abstract class AEBaseGui extends GuiContainer { + private static class AEGuiTooltip { + + private int shiftX = 0; + private int shiftY = 0; + + private int x = 0; + private int y = 0; + public String[] lines = null; + + public void set(final int x, final int y, final String message) { + set(x, y, message != null && !message.isEmpty() ? message.split("\n") : null); + } + + public void set(final int x, final int y, final String[] lines) { + this.lines = lines; + this.x = this.shiftX + x; + this.y = this.shiftY + y; + } + + public void shift(int shiftX, int shiftY) { + this.shiftX = shiftX; + this.shiftY = shiftY; + } + + public void draw() { + if (!isEmpty()) { + List list = new ArrayList<>(); + list.add(this.lines[0] + GuiDraw.TOOLTIP_LINESPACE); + + for (int i = 1; i < this.lines.length; i++) { + list.add(EnumChatFormatting.GRAY + this.lines[i].replace("\u00a0", " ")); + } + + GuiDraw.drawMultilineTip(this.x + 12, this.y - 12, list); + this.lines = null; + } + } + + public boolean isEmpty() { + return this.lines == null || this.lines.length == 0; + } + } + private static boolean switchingGuis; private final List meSlots = new LinkedList<>(); // drag y private final Set drag_click = new HashSet<>(); public static final AppEngRenderItem aeRenderItem = new AppEngRenderItem(); public static final TranslatedRenderItem translatedRenderItem = new TranslatedRenderItem(); + private final AEGuiTooltip currentToolTip = new AEGuiTooltip(); private GuiScrollbar scrollBar = null; private boolean disableShiftClick = false; private Stopwatch dbl_clickTimer = Stopwatch.createStarted(); @@ -129,7 +175,7 @@ public abstract class AEBaseGui extends GuiContainer { super.initGui(); final List slots = this.getInventorySlots(); - slots.removeIf(slot -> slot instanceof SlotME); + slots.removeIf(SlotME.class::isInstance); for (final InternalSlotME me : this.meSlots) { slots.add(new SlotME(me)); @@ -150,115 +196,37 @@ public abstract class AEBaseGui extends GuiContainer { handleTooltip(mouseX, mouseY, (ITooltip) c); } } + + this.currentToolTip.draw(); } protected void handleTooltip(int mouseX, int mouseY, ITooltip tooltip) { - final int x = tooltip.xPos(); // ((GuiImgButton) c).xPosition; - int y = tooltip.yPos(); // ((GuiImgButton) c).yPosition; + final int x = tooltip.xPos(); + final int y = tooltip.yPos(); - if (x < mouseX && x + tooltip.getWidth() > mouseX && tooltip.isVisible()) { - if (y < mouseY && y + tooltip.getHeight() > mouseY) { - if (y < 15) { - y = 15; - } - - final String msg = tooltip.getMessage(); - if (msg != null && !msg.isEmpty()) { - this.drawTooltip(x + 11, y + 4, 0, msg); - } - } + if (tooltip.isVisible() && x < mouseX + && x + tooltip.getWidth() > mouseX + && y < mouseY + && y + tooltip.getHeight() > mouseY) { + drawTooltip(x + 11, Math.max(y, 15) + 4, tooltip.getMessage()); } } + @Deprecated public void drawTooltip(final int x, final int y, final int forceWidth, final String message) { - GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS); - GL11.glDisable(GL12.GL_RESCALE_NORMAL); - RenderHelper.disableStandardItemLighting(); - GL11.glDisable(GL11.GL_LIGHTING); - GL11.glDisable(GL11.GL_DEPTH_TEST); - final String[] lines = message.split("\n"); + drawTooltip(x, y, message); + } - if (lines.length > 0) { - int width = 0; - int left; - int top; - - for (left = 0; left < lines.length; ++left) { - top = this.fontRendererObj.getStringWidth(lines[left]); - - if (top > width) { - width = top; - } - } - - left = x + 12; - top = y - 12; - int height = 8; - - if (lines.length > 1) { - height += 2 + (lines.length - 1) * 10; - } - - ScaledResolution scaledresolution = new ScaledResolution( - this.mc, - this.mc.displayWidth, - this.mc.displayHeight); - - if (top + height + 6 > scaledresolution.getScaledHeight()) { - top = scaledresolution.getScaledHeight() - height - 6; - } - - if (forceWidth > 0) { - width = forceWidth; - } - - if (left + width + 6 > scaledresolution.getScaledWidth()) { - left = scaledresolution.getScaledWidth() - width - 6; - } - - this.zLevel = 300.0F; - itemRender.zLevel = 300.0F; - final int color1 = 0xF0100010; - this.drawGradientRect(left - 3, top - 4, left + width + 3, top - 3, color1, color1); - this.drawGradientRect(left - 3, top + height + 3, left + width + 3, top + height + 4, color1, color1); - this.drawGradientRect(left - 3, top - 3, left + width + 3, top + height + 3, color1, color1); - this.drawGradientRect(left - 4, top - 3, left - 3, top + height + 3, color1, color1); - this.drawGradientRect(left + width + 3, top - 3, left + width + 4, top + height + 3, color1, color1); - final int color2 = 0x505000FF; - final int color3 = 0x5028007F; // (color2 & 16711422) >> 1 | color2 & -16777216; - this.drawGradientRect(left - 3, top - 3 + 1, left - 3 + 1, top + height + 3 - 1, color2, color3); - this.drawGradientRect( - left + width + 2, - top - 3 + 1, - left + width + 3, - top + height + 3 - 1, - color2, - color3); - this.drawGradientRect(left - 3, top - 3, left + width + 3, top - 3 + 1, color2, color2); - this.drawGradientRect(left - 3, top + height + 2, left + width + 3, top + height + 3, color3, color3); - - for (int var13 = 0; var13 < lines.length; ++var13) { - String var14 = lines[var13]; - - if (var13 == 0) { - var14 = '\u00a7' + Integer.toHexString(15) + var14; - } else { - var14 = "\u00a77" + var14; - } - - this.fontRendererObj.drawStringWithShadow(var14, left, top, -1); - - if (var13 == 0) { - top += 2; - } - - top += 10; - } - - this.zLevel = 0.0F; - itemRender.zLevel = 0.0F; + public void drawTooltip(final int x, final int y, final String message) { + if (message != null && !message.isEmpty()) { + drawTooltip(x, y, message.split("\n")); + } + } + + public void drawTooltip(final int x, final int y, final String[] lines) { + if (lines != null && lines.length > 0) { + this.currentToolTip.set(x, y, lines); } - GL11.glPopAttrib(); } /** @@ -336,23 +304,25 @@ public abstract class AEBaseGui extends GuiContainer { @Override protected final void drawGuiContainerForegroundLayer(final int x, final int y) { - final int ox = this.guiLeft; // (width - xSize) / 2; - final int oy = this.guiTop; // (height - ySize) / 2; + final int ox = this.guiLeft; + final int oy = this.guiTop; GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); if (this.getScrollBar() != null) { this.getScrollBar().draw(this); } + this.currentToolTip.shift(ox, oy); this.drawFG(ox, oy, x, y); + this.currentToolTip.shift(0, 0); } public abstract void drawFG(int offsetX, int offsetY, int mouseX, int mouseY); @Override protected final void drawGuiContainerBackgroundLayer(final float f, final int x, final int y) { - final int ox = this.guiLeft; // (width - xSize) / 2; - final int oy = this.guiTop; // (height - ySize) / 2; + final int ox = this.guiLeft; + final int oy = this.guiTop; GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); this.drawBG(ox, oy, x, y); diff --git a/src/main/java/appeng/client/gui/AEBaseMEGui.java b/src/main/java/appeng/client/gui/AEBaseMEGui.java index e3af442a0..34c44bb6f 100644 --- a/src/main/java/appeng/client/gui/AEBaseMEGui.java +++ b/src/main/java/appeng/client/gui/AEBaseMEGui.java @@ -109,12 +109,12 @@ public abstract class AEBaseMEGui extends AEBaseGui implements IGuiTooltipHandle + NumberFormat.getNumberInstance(Locale.US).format(myStack.getCountRequestable())); } - this.drawTooltip(x, y, 0, join(currentToolTip, "\n")); + this.drawTooltip(x, y, currentToolTip.toArray(new String[0])); } else if (stack.stackSize > BigNumber) { final List var4 = stack .getTooltip(this.mc.thePlayer, this.mc.gameSettings.advancedItemTooltips); var4.add("Items Stored: " + NumberFormat.getNumberInstance(Locale.US).format(stack.stackSize)); - this.drawTooltip(x, y, 0, join(var4, "\n")); + this.drawTooltip(x, y, var4.toArray(new String[0])); return; } } diff --git a/src/main/java/appeng/client/gui/implementations/GuiCraftConfirm.java b/src/main/java/appeng/client/gui/implementations/GuiCraftConfirm.java index 0647cd86d..b2456770f 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiCraftConfirm.java +++ b/src/main/java/appeng/client/gui/implementations/GuiCraftConfirm.java @@ -321,12 +321,12 @@ public class GuiCraftConfirm extends AEBaseGui implements ICraftingCPUTableHolde == DisplayMode.LIST); this.takeScreenshot.visible = (displayMode == DisplayMode.TREE); - super.drawScreen(mouseX, mouseY, btn); - switch (displayMode) { case LIST -> drawListScreen(mouseX, mouseY, btn); case TREE -> drawTreeScreen(mouseX, mouseY, btn); } + + super.drawScreen(mouseX, mouseY, btn); } private void drawListScreen(final int mouseX, final int mouseY, final float btn) { @@ -605,18 +605,18 @@ public class GuiCraftConfirm extends AEBaseGui implements ICraftingCPUTableHolde } if (this.tooltip >= 0 && !dspToolTip.isEmpty()) { - this.drawTooltip(toolPosX, toolPosY + 10, 0, dspToolTip); + this.drawTooltip(toolPosX, toolPosY + 10, dspToolTip); } } private void drawTreeFG(final int offsetX, final int offsetY, final int mouseX, final int mouseY) { final CraftingJobV2 jobTree = this.jobTree; if (jobTree == null) { - this.drawTooltip(16, 48, 0, GuiText.NoCraftingTreeReceived.getLocal()); + this.drawTooltip(16, 48, GuiText.NoCraftingTreeReceived.getLocal()); return; } if (jobTree.getOutput() == null) { - this.drawTooltip(16, 48, 0, GuiText.Nothing.getLocal()); + this.drawTooltip(16, 48, GuiText.Nothing.getLocal()); return; } diff --git a/src/main/java/appeng/client/gui/implementations/GuiCraftingCPU.java b/src/main/java/appeng/client/gui/implementations/GuiCraftingCPU.java index 7178f10ab..9f89075c3 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiCraftingCPU.java +++ b/src/main/java/appeng/client/gui/implementations/GuiCraftingCPU.java @@ -277,8 +277,8 @@ public class GuiCraftingCPU extends AEBaseGui implements ISortSource, IGuiToolti } } - super.drawScreen(mouseX, mouseY, btn); this.handleTooltip(mouseX, mouseY, remainingOperations); + super.drawScreen(mouseX, mouseY, btn); } private void updateRemainingOperations() { @@ -464,7 +464,7 @@ public class GuiCraftingCPU extends AEBaseGui implements ISortSource, IGuiToolti } if (this.tooltip >= 0 && dspToolTip.length() > 0) { - this.drawTooltip(toolPosX, toolPosY + 10, 0, dspToolTip); + this.drawTooltip(toolPosX, toolPosY + 10, dspToolTip); } } diff --git a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java index 46c197a1e..87a85d092 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java +++ b/src/main/java/appeng/client/gui/implementations/GuiInterfaceTerminal.java @@ -307,11 +307,11 @@ public class GuiInterfaceTerminal extends AEBaseGui terminalStyleBox.set(AEConfig.instance.settings.getSetting(Settings.TERMINAL_STYLE)); - super.drawScreen(mouseX, mouseY, btn); - handleTooltip(mouseX, mouseY, searchFieldInputs); handleTooltip(mouseX, mouseY, searchFieldOutputs); handleTooltip(mouseX, mouseY, searchFieldNames); + + super.drawScreen(mouseX, mouseY, btn); } @Override diff --git a/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java b/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java index 7d9a1f0cd..c7ddadc85 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java +++ b/src/main/java/appeng/client/gui/implementations/GuiMEMonitorable.java @@ -592,9 +592,9 @@ public class GuiMEMonitorable extends AEBaseMEGui implements ISortSource, IConfi @Override public void drawScreen(final int mouseX, final int mouseY, final float btn) { - super.drawScreen(mouseX, mouseY, btn); - handleTooltip(mouseX, mouseY, searchField); + + super.drawScreen(mouseX, mouseY, btn); } public boolean isOverTextField(final int mousex, final int mousey) { diff --git a/src/main/java/appeng/client/gui/implementations/GuiNetworkStatus.java b/src/main/java/appeng/client/gui/implementations/GuiNetworkStatus.java index 84a4ee617..6ccc75475 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiNetworkStatus.java +++ b/src/main/java/appeng/client/gui/implementations/GuiNetworkStatus.java @@ -232,7 +232,7 @@ public class GuiNetworkStatus extends AEBaseGui implements ISortSource { } catch (final Throwable ignore) {} if (myStack != null) { - final List currentToolTip = stack + final List currentToolTip = stack .getTooltip(this.mc.thePlayer, this.mc.gameSettings.advancedItemTooltips); while (currentToolTip.size() > 1) { @@ -246,7 +246,7 @@ public class GuiNetworkStatus extends AEBaseGui implements ISortSource { GuiText.EnergyDrain.getLocal() + ": " + Platform.formatPowerLong(myStack.getCountRequestable(), true)); - this.drawTooltip(x, y, 0, join(currentToolTip, "\n")); + this.drawTooltip(x, y, currentToolTip.toArray(new String[0])); } } // super.drawItemStackTooltip( stack, x, y ); @@ -403,7 +403,7 @@ public class GuiNetworkStatus extends AEBaseGui implements ISortSource { } if (this.tooltip >= 0 && toolTip.length() > 0) { - this.drawTooltip(toolPosX, toolPosY + 10, 0, toolTip); + this.drawTooltip(toolPosX, toolPosY + 10, toolTip); } } diff --git a/src/main/java/appeng/client/gui/implementations/GuiOptimizePatterns.java b/src/main/java/appeng/client/gui/implementations/GuiOptimizePatterns.java index f6d3151d9..e4e9093dd 100644 --- a/src/main/java/appeng/client/gui/implementations/GuiOptimizePatterns.java +++ b/src/main/java/appeng/client/gui/implementations/GuiOptimizePatterns.java @@ -149,8 +149,6 @@ public class GuiOptimizePatterns extends AEBaseGui implements IGuiTooltipHandler @Override public void drawScreen(int mouseX, int mouseY, float btn) { - super.drawScreen(mouseX, mouseY, btn); - final int gx = (this.width - this.xSize) / 2; final int gy = (this.height - this.ySize) / 2; @@ -178,6 +176,7 @@ public class GuiOptimizePatterns extends AEBaseGui implements IGuiTooltipHandler } } + super.drawScreen(mouseX, mouseY, btn); } @Override @@ -318,7 +317,7 @@ public class GuiOptimizePatterns extends AEBaseGui implements IGuiTooltipHandler } if (this.tooltip >= 0 && !dspToolTip.isEmpty()) { - this.drawTooltip(toolPosX, toolPosY + 10, 0, dspToolTip); + this.drawTooltip(toolPosX, toolPosY + 10, dspToolTip); } } diff --git a/src/main/java/appeng/client/gui/widgets/GuiCraftingCPUTable.java b/src/main/java/appeng/client/gui/widgets/GuiCraftingCPUTable.java index 649cd2c32..1df8f1cee 100644 --- a/src/main/java/appeng/client/gui/widgets/GuiCraftingCPUTable.java +++ b/src/main/java/appeng/client/gui/widgets/GuiCraftingCPUTable.java @@ -222,7 +222,7 @@ public class GuiCraftingCPUTable { tooltip.append('\n'); } if (tooltip.length() > 0) { - parent.drawTooltip(mouseX - offsetX, mouseY - offsetY, 0, tooltip.toString()); + parent.drawTooltip(mouseX - offsetX, mouseY - offsetY, tooltip.toString()); } } } diff --git a/src/main/java/appeng/client/gui/widgets/GuiCraftingTree.java b/src/main/java/appeng/client/gui/widgets/GuiCraftingTree.java index 89e8344c4..e45540663 100644 --- a/src/main/java/appeng/client/gui/widgets/GuiCraftingTree.java +++ b/src/main/java/appeng/client/gui/widgets/GuiCraftingTree.java @@ -130,7 +130,7 @@ public class GuiCraftingTree { if (tooltip == null) { tooltip = request.getTooltipText(); } - parent.drawTooltip(mouseX, mouseY, 0, tooltip); + parent.drawTooltip(mouseX, mouseY, tooltip); } } @@ -181,7 +181,7 @@ public class GuiCraftingTree { if (tooltip == null) { tooltip = resolver.task.getTooltipText(); } - parent.drawTooltip(mouseX, mouseY, 0, tooltip); + parent.drawTooltip(mouseX, mouseY, tooltip); } }