aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/gui/elements/Slider.java
blob: 15293a5de5a46b54ba421c1313ce3700c7fdac98 (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 cc.polyfrost.oneconfig.gui.elements;

import cc.polyfrost.oneconfig.config.OneConfigConfig;
import cc.polyfrost.oneconfig.lwjgl.RenderManager;
import cc.polyfrost.oneconfig.utils.InputUtils;
import org.lwjgl.input.Mouse;

public class Slider extends BasicElement {
    private final float min, max;
    protected float value;
    protected float currentDragPoint;

    public Slider(int length, float min, float max, float startValue) {
        super(length, 8, false);
        this.min = min;
        this.max = max;
        setValue(startValue);
    }

    @Override
    public void draw(long vg, int x, int y) {
        update(x, y);
        RenderManager.drawRoundedRect(vg, x, y + 2, width, height - 4, OneConfigConfig.GRAY_300, 3f);
        RenderManager.drawRoundedRect(vg, x, y + 2, width * value, height - 4, OneConfigConfig.BLUE_500, 3f);
        RenderManager.drawRoundedRect(vg, currentDragPoint - 12, y - 8, 24, 24, OneConfigConfig.WHITE, 12f);


    }

    public void update(int x, int y) {
        super.update(x, y);
        if (InputUtils.isAreaHovered((int) ((x + width * value) - 8f), y - 12, 32, 32)) {
            boolean drag = Mouse.isButtonDown(0);
            if (drag) {
                value = ((float) InputUtils.mouseX() - x) / width;
            }
        } else if (InputUtils.isAreaHovered(x - 20, y - 4, width + 40, height + 8)) {
            if (InputUtils.isClicked()) {
                value = ((float) InputUtils.mouseX() - x) / width;
            }
        }

        if (value < 0) value = 0;
        if (value > 1) value = 1;

        currentDragPoint = x + width * value;

    }

    public float getValue() {
        return value * (max - min) + min;
    }

    public void setValue(float value) {
        this.value = (value - min) / (max - min);
    }
}