aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/gui/widget/ClickableLabelWidget.java
blob: 20945eb9bc60e60c7f8b120eae35217dca2909e5 (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
package me.shedaniel.rei.gui.widget;

import java.awt.*;

public abstract class ClickableLabelWidget extends LabelWidget {
    
    public static final int hoveredColor = (new Color(102, 255, 204)).getRGB();
    public boolean focused;
    
    public ClickableLabelWidget(int x, int y, String text) {
        super(x, y, text);
    }
    
    @Override
    public void render(int mouseX, int mouseY, float partialTicks) {
        int colour = -1;
        if (isHovered(mouseX, mouseY))
            colour = hoveredColor;
        drawStringCentered(textRenderer, (isHovered(mouseX, mouseY) ? "§n" : "") + text, x, y, colour);
    }
    
    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        if (button == 0 && isHighlighted(mouseX, mouseY)) {
            onLabelClicked();
            return true;
        }
        return false;
    }
    
    @Override
    public boolean keyPressed(int int_1, int int_2, int int_3) {
        if (int_1 != 257 && int_1 != 32 && int_1 != 335) {
            return false;
        } else {
            this.onLabelClicked();
            return true;
        }
    }
    
    @Override
    public boolean isPartOfFocusCycle() {
        return true;
    }
    
    public boolean isHovered(int mouseX, int mouseY) {
        return isHighlighted(mouseX, mouseY) || focused;
    }
    
    @Override
    public void onFocusChanged(boolean boolean_1) {
        focused = boolean_1;
    }
    
    public abstract void onLabelClicked();
    
}