diff options
author | xander <xander@isxander.dev> | 2022-09-03 13:42:49 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-03 13:42:49 +0100 |
commit | b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f (patch) | |
tree | 55c1944af1e8453766703537807701712452a1a2 /src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java | |
parent | 8a29f957e99c957efd11d6c279e5f97639f05d9e (diff) | |
download | YetAnotherConfigLib-b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f.tar.gz YetAnotherConfigLib-b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f.tar.bz2 YetAnotherConfigLib-b775bae81d3ec0c8fb64b0c680e375b6bca4fe5f.zip |
add tooltips to action buttons and messages
Diffstat (limited to 'src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java')
-rw-r--r-- | src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java b/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java new file mode 100644 index 0000000..b8abbbd --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java @@ -0,0 +1,49 @@ +package dev.isxander.yacl.gui; + +import dev.isxander.yacl.impl.YACLConstants; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Text; + +import java.util.List; + +public class TooltipButtonWidget extends ButtonWidget { + private float hoveredTicks = 0; + private int prevMouseX, prevMouseY; + + private final Screen screen; + private List<OrderedText> wrappedDescription; + + public TooltipButtonWidget(Screen screen, int x, int y, int width, int height, Text message, Text tooltip, PressAction onPress) { + super(x, y, width, height, message, onPress); + this.screen = screen; + setTooltip(tooltip); + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + super.render(matrices, mouseX, mouseY, delta); + + if (isHovered() && (!YACLConstants.HOVER_MOUSE_RESET || (prevMouseX == mouseX && prevMouseY == mouseY))) { + hoveredTicks += delta; + } else { + hoveredTicks = 0; + } + + prevMouseX = mouseX; + prevMouseY = mouseY; + } + + public void renderTooltip(MatrixStack matrices, int mouseX, int mouseY) { + if (hoveredTicks >= YACLConstants.HOVER_TICKS) { + screen.renderOrderedTooltip(matrices, wrappedDescription, mouseX, mouseY); + } + } + + public void setTooltip(Text tooltip) { + wrappedDescription = MinecraftClient.getInstance().textRenderer.wrapLines(tooltip, screen.width / 2); + } +} |