diff options
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/gui/YACLTooltipPositioner.java')
-rw-r--r-- | common/src/main/java/dev/isxander/yacl/gui/YACLTooltipPositioner.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/gui/YACLTooltipPositioner.java b/common/src/main/java/dev/isxander/yacl/gui/YACLTooltipPositioner.java new file mode 100644 index 0000000..d6e6220 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl/gui/YACLTooltipPositioner.java @@ -0,0 +1,48 @@ +package dev.isxander.yacl.gui; + +import net.minecraft.client.gui.navigation.ScreenRectangle; +import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipPositioner; +import net.minecraft.util.Mth; +import org.joml.Vector2i; +import org.joml.Vector2ic; + +import java.util.function.Supplier; + +public class YACLTooltipPositioner implements ClientTooltipPositioner { + private final Supplier<ScreenRectangle> buttonDimensions; + + public YACLTooltipPositioner(net.minecraft.client.gui.components.AbstractWidget widget) { + this.buttonDimensions = widget::getRectangle; + } + + public YACLTooltipPositioner(dev.isxander.yacl.gui.AbstractWidget widget) { + this.buttonDimensions = () -> { + var dim = widget.getDimension(); + return new ScreenRectangle(dim.x(), dim.y(), dim.width(), dim.height()); + }; + } + + public YACLTooltipPositioner(Supplier<ScreenRectangle> buttonDimensions) { + this.buttonDimensions = buttonDimensions; + } + + @Override + public Vector2ic positionTooltip(int guiWidth, int guiHeight, int x, int y, int width, int height) { + ScreenRectangle buttonDimensions = this.buttonDimensions.get(); + + int centerX = buttonDimensions.left() + buttonDimensions.width() / 2; + int aboveY = buttonDimensions.top() - height - 4; + int belowY = buttonDimensions.top() + buttonDimensions.height() + 4; + + int maxBelow = guiHeight - (belowY + height); + int minAbove = aboveY - height; + + int yResult = aboveY; + if (minAbove < 8) + yResult = maxBelow > minAbove ? belowY : aboveY; + + int xResult = Mth.clamp(centerX - width / 2, -4, guiWidth - width - 4); + + return new Vector2i(xResult, yResult); + } +} |