aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl/gui/TooltipButtonWidget.java
blob: 0f34e61efa69cc5e14dae74f7cbf031c55f77223 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package dev.isxander.yacl.gui;

import dev.isxander.yacl.impl.YACLConstants;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.MultilineText;
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 {
    protected float hoveredTicks = 0;
    protected int prevMouseX, prevMouseY;

    protected final Screen screen;
    protected MultilineText 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 renderHoveredTooltip(MatrixStack matrices, int mouseX, int mouseY) {
        if (hoveredTicks >= YACLConstants.HOVER_TICKS) {
            YACLScreen.renderMultilineTooltip(matrices, MinecraftClient.getInstance().textRenderer, wrappedDescription, mouseX, mouseY, screen.width, screen.height);
        }
    }

    public void setTooltip(Text tooltip) {
        wrappedDescription = MultilineText.create(MinecraftClient.getInstance().textRenderer, tooltip, screen.width / 2);
    }
}