aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/gui/modularui/widget/CoverCycleButtonWidget.java
blob: 28b495ce426f0613f39479fb97fbcc9e7148769e (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
package gregtech.common.gui.modularui.widget;

import org.lwjgl.opengl.GL11;

import com.gtnewhorizons.modularui.api.drawable.IDrawable;
import com.gtnewhorizons.modularui.api.drawable.UITexture;
import com.gtnewhorizons.modularui.common.widget.CycleButtonWidget;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.gui.modularui.GT_UITextures;

/**
 * Fires click action on mouse release, not on press. Draws different backgrounds depending on whether the mouse is
 * being pressed or the widget is hovered.
 */
public class CoverCycleButtonWidget extends CycleButtonWidget {

    private static final UITexture BUTTON_NORMAL_NOT_PRESSED = GT_UITextures.BUTTON_COVER_NORMAL.getSubArea(
            0,
            0,
            1,
            0.5f);
    private static final UITexture BUTTON_NORMAL_PRESSED = GT_UITextures.BUTTON_COVER_NORMAL.getSubArea(0, 0.5f, 1, 1);
    private static final UITexture BUTTON_HOVERED_NOT_PRESSED = GT_UITextures.BUTTON_COVER_NORMAL_HOVERED.getSubArea(
            0,
            0,
            1,
            0.5f);
    private static final UITexture BUTTON_HOVERED_PRESSED = GT_UITextures.BUTTON_COVER_NORMAL_HOVERED.getSubArea(
            0,
            0.5f,
            1,
            1);

    private boolean clickPressed;

    private static final int TOOLTIP_DELAY = 5;

    public CoverCycleButtonWidget() {
        setSize(16, 16);
        setTooltipShowUpDelay(TOOLTIP_DELAY);
    }

    @Override
    public ClickResult onClick(int buttonId, boolean doubleClick) {
        updateState();
        if (!canClick()) return ClickResult.REJECT;
        clickPressed = true;
        return ClickResult.SUCCESS;
    }

    @Override
    public boolean onClickReleased(int buttonId) {
        clickPressed = false;
        updateState();
        if (!isHovering() || !canClick()) return false;
        return onClickImpl(buttonId);
    }

    protected boolean onClickImpl(int buttonId) {
        super.onClick(buttonId, false);
        return true;
    }

    @SuppressWarnings("BooleanMethodIsAlwaysInverted")
    protected boolean canClick() {
        return true;
    }

    @SideOnly(Side.CLIENT)
    protected void updateState() {}

    public boolean isClickPressed() {
        return clickPressed;
    }

    @Override
    public void drawBackground(float partialTicks) {
        GL11.glColor4f(1, 1, 1, 1);
        super.drawBackground(partialTicks);
    }

    @Override
    public IDrawable[] getBackground() {
        if (isHovering()) {
            if (clickPressed) {
                return new IDrawable[] { BUTTON_HOVERED_PRESSED };
            } else {
                return new IDrawable[] { BUTTON_HOVERED_NOT_PRESSED };
            }
        } else {
            if (clickPressed) {
                return new IDrawable[] { BUTTON_NORMAL_PRESSED };
            } else {
                return new IDrawable[] { BUTTON_NORMAL_NOT_PRESSED };
            }
        }
    }
}