aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/gui/widgets/GT_GuiTooltip.java
blob: 326e74438276f01cf43a51f4fc0784d5d561964c (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package gregtech.api.gui.widgets;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.lwjgl.input.Keyboard;

import gregtech.api.util.GT_TooltipDataCache.TooltipData;

public class GT_GuiTooltip {

    protected Rectangle bounds;
    protected TooltipData data;
    private List<String> displayedText;
    public boolean enabled = true;

    /**
     * Used to create a tooltip that will appear over the specified bounds. This will initially be a "static" tooltip
     * that doesn't respect verbosity levels or respond to the shift key.
     *
     * @param bounds
     * @param text
     */
    public GT_GuiTooltip(Rectangle bounds, String... text) {
        this.bounds = bounds;
        setToolTipText(text);
    }

    /**
     * Used to create a tooltip that will appear over the specified bounds. This will initially be a "dynamic" tooltip
     * that respects verbosity levels and responds to the shift key.
     *
     * @param bounds
     * @param data
     */
    public GT_GuiTooltip(Rectangle bounds, TooltipData data) {
        this.bounds = bounds;
        // Trust that the tooltips have already been formatted and colored, just make sure it has no nulls
        this.data = sanitizeTooltipData(data);
    }

    private TooltipData sanitizeTooltipData(TooltipData data) {
        if (data.text == null) {
            data.text = Collections.emptyList();
        }
        if (data.shiftText == null) {
            data.shiftText = Collections.emptyList();
        }
        return data;
    }

    /**
     * Called before the tooltip manager checks whether this tooltip is enabled
     */
    protected void onTick() {
        // Switch which of our 2 stored texts we're displaying now.
        this.displayedText = Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) ? this.data.shiftText : this.data.text;
        // If this text is empty, let's not display a tooltip at all.
        this.enabled = this.displayedText.size() != 0;
    }

    /**
     * Called once this tooltip has been determined to be enabled
     */
    protected void updateText() {}

    /**
     * Used to set a "static" tooltip that doesn't respect verbosity levels or respond to the shift key
     *
     * @param text
     */
    public void setToolTipText(String... text) {
        this.data = formatTooltip(text);
        this.displayedText = data.text;
    }

    /**
     * Used to set a "dynamic" tooltip that respects verbosity levels and responds to the shift key
     *
     * @param data
     */
    public void setToolTipText(TooltipData data) {
        // Trust that the tooltips have already been formatted and colored, just make sure it has no nulls
        this.data = sanitizeTooltipData(data);
    }

    /**
     * Apply tooltip colors in case the text doesn't contain them and return as tooltip data
     *
     * @param text
     * @return colored tooltip lines as list
     */
    protected TooltipData formatTooltip(String[] text) {
        List<String> list;
        if (text != null) {
            list = new ArrayList<>(text.length);
            for (String s : text) {
                if (s == null) continue;
                if (list.isEmpty()) list.add("\u00a7f" + s);
                else list.add("\u00a77" + s);
            }
        } else {
            list = Collections.emptyList();
        }
        return new TooltipData(list, list);
    }

    public List<String> getToolTipText() {
        return this.displayedText;
    }

    public Rectangle getBounds() {
        return bounds;
    }

    public boolean isDelayed() {
        return true;
    }
}