aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/thatgravyboat/skyblockhud/core/GuiElementBoolean.java
blob: 8daf4b1e021a05c040a5bbda8efc029ef48c9722 (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
package com.thatgravyboat.skyblockhud.core;

import com.thatgravyboat.skyblockhud.GuiTextures;
import com.thatgravyboat.skyblockhud.core.util.lerp.LerpUtils;
import com.thatgravyboat.skyblockhud.core.util.render.RenderUtils;
import java.util.function.Consumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Mouse;

public class GuiElementBoolean extends GuiElement {

    public int x;
    public int y;
    private boolean value;
    private int clickRadius;
    private Consumer<Boolean> toggleCallback;

    private boolean previewValue;
    private int animation = 0;
    private long lastMillis = 0;

    private static final int xSize = 48;
    private static final int ySize = 14;

    public GuiElementBoolean(int x, int y, boolean value, Consumer<Boolean> toggleCallback) {
        this(x, y, value, 0, toggleCallback);
    }

    public GuiElementBoolean(int x, int y, boolean value, int clickRadius, Consumer<Boolean> toggleCallback) {
        this.x = x;
        this.y = y;
        this.value = value;
        this.previewValue = value;
        this.clickRadius = clickRadius;
        this.toggleCallback = toggleCallback;
        this.lastMillis = System.currentTimeMillis();

        if (value) animation = 36;
    }

    @Override
    public void render() {
        GlStateManager.color(1, 1, 1, 1);
        Minecraft.getMinecraft().getTextureManager().bindTexture(GuiTextures.BAR);
        RenderUtils.drawTexturedRect(x, y, xSize, ySize);

        ResourceLocation buttonLoc = GuiTextures.ON;
        long currentMillis = System.currentTimeMillis();
        long deltaMillis = currentMillis - lastMillis;
        lastMillis = currentMillis;
        boolean passedLimit = false;
        if (previewValue != value) {
            if ((previewValue && animation > 12) || (!previewValue && animation < 24)) {
                passedLimit = true;
            }
        }
        if (previewValue != passedLimit) {
            animation += deltaMillis / 10;
        } else {
            animation -= deltaMillis / 10;
        }
        lastMillis -= deltaMillis % 10;

        if (previewValue == value) {
            animation = Math.max(0, Math.min(36, animation));
        } else if (!passedLimit) {
            if (previewValue) {
                animation = Math.max(0, Math.min(12, animation));
            } else {
                animation = Math.max(24, Math.min(36, animation));
            }
        } else {
            if (previewValue) {
                animation = Math.max(12, animation);
            } else {
                animation = Math.min(24, animation);
            }
        }

        int animation = (int) (LerpUtils.sigmoidZeroOne(this.animation / 36f) * 36);
        if (animation < 3) {
            buttonLoc = GuiTextures.OFF;
        } else if (animation < 13) {
            buttonLoc = GuiTextures.ONE;
        } else if (animation < 23) {
            buttonLoc = GuiTextures.TWO;
        } else if (animation < 33) {
            buttonLoc = GuiTextures.THREE;
        }

        Minecraft.getMinecraft().getTextureManager().bindTexture(buttonLoc);
        RenderUtils.drawTexturedRect(x + animation, y, 12, 14);
    }

    @Override
    public boolean mouseInput(int mouseX, int mouseY) {
        if (mouseX > x - clickRadius && mouseX < x + xSize + clickRadius && mouseY > y - clickRadius && mouseY < y + ySize + clickRadius) {
            if (Mouse.getEventButton() == 0) {
                if (Mouse.getEventButtonState()) {
                    previewValue = !value;
                } else if (previewValue == !value) {
                    value = !value;
                    toggleCallback.accept(value);
                }
            }
        } else {
            previewValue = value;
        }
        return false;
    }

    @Override
    public boolean keyboardInput() {
        return false;
    }
}