From f729d5199a2acd9264f9e4244d598debdc53cbcc Mon Sep 17 00:00:00 2001 From: Moulberry Date: Sun, 9 Aug 2020 07:02:36 +1000 Subject: 1.1.1 --- .../notenoughupdates/mbgui/MBGuiGroupFloating.java | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java b/src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java index 91cca2d7..6bdd25fd 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/mbgui/MBGuiGroupFloating.java @@ -1,5 +1,9 @@ package io.github.moulberry.notenoughupdates.mbgui; +import io.github.moulberry.notenoughupdates.util.Utils; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.inventory.GuiContainer; import org.lwjgl.util.vector.Vector2f; import java.util.*; @@ -7,6 +11,8 @@ import java.util.*; public class MBGuiGroupFloating extends MBGuiGroup { private LinkedHashMap children; + private GuiContainer lastContainer = null; + private HashMap childrenPositionOffset = new HashMap<>(); public MBGuiGroupFloating(int width, int height, LinkedHashMap children) { this.width = width; @@ -19,8 +25,54 @@ public class MBGuiGroupFloating extends MBGuiGroup { return Collections.unmodifiableMap(children); } + @Override + public Map getChildrenPosition() { + GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen; + + if(currentScreen instanceof GuiContainer) { + GuiContainer currentContainer = (GuiContainer) currentScreen; + if(lastContainer != currentContainer) { + lastContainer = currentContainer; + for(Map.Entry entry : children.entrySet()) { + MBGuiElement child = entry.getKey(); + MBAnchorPoint anchorPoint = entry.getValue(); + + Vector2f childPos; + if(childrenPosition.containsKey(child)) { + childPos = new Vector2f(childrenPosition.get(child)); + } else { + childPos = new Vector2f(); + } + + if(anchorPoint.anchorPoint == MBAnchorPoint.AnchorPoint.INV_BOTMID) { + try { + int xSize = (int) Utils.getField(GuiContainer.class, currentContainer, "xSize", "field_146999_f"); + int ySize = (int) Utils.getField(GuiContainer.class, currentContainer, "ySize", "field_147000_g"); + int guiLeft = (int) Utils.getField(GuiContainer.class, currentContainer, "guiLeft", "field_147003_i"); + int guiTop = (int) Utils.getField(GuiContainer.class, currentContainer, "guiTop", "field_147009_r"); + + int defGuiLeft = (this.width - xSize) / 2; + int defGuiTop = (this.height - ySize) / 2; + + childPos.x += guiLeft-defGuiLeft + (anchorPoint.anchorPoint.x-0.5f)*xSize; + childPos.y += guiTop-defGuiTop + (anchorPoint.anchorPoint.y-0.5f)*ySize; + } catch(Exception ignored) { + } + } + + childrenPositionOffset.put(child, childPos); + } + } + return Collections.unmodifiableMap(childrenPositionOffset); + } else { + return Collections.unmodifiableMap(childrenPosition); + } + } + @Override public void recalculate() { + lastContainer = null; + for(MBGuiElement child : children.keySet()) { child.recalculate(); } @@ -30,6 +82,12 @@ public class MBGuiGroupFloating extends MBGuiGroup { MBAnchorPoint anchorPoint = entry.getValue(); float x = anchorPoint.anchorPoint.x * width - anchorPoint.anchorPoint.x * child.getWidth() + anchorPoint.offset.x; float y = anchorPoint.anchorPoint.y * height - anchorPoint.anchorPoint.y * child.getHeight() + anchorPoint.offset.y; + + if(anchorPoint.anchorPoint == MBAnchorPoint.AnchorPoint.INV_BOTMID) { + x = width*0.5f + anchorPoint.offset.x; + y = height*0.5f + anchorPoint.offset.y; + } + childrenPosition.put(child, new Vector2f(x, y)); } } -- cgit