aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/config/interfaces/BasicOption.java
blob: c036408497c50ea37a8e43ea74b1d4ee618f7269 (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
package cc.polyfrost.oneconfig.config.interfaces;

import java.lang.reflect.Field;
import java.util.function.Supplier;

@SuppressWarnings({"unused"})
public abstract class BasicOption {
    protected final Field field;
    protected final String name;
    public final int size;
    private Supplier<Boolean> dependency;

    /**
     * Initialize option
     *
     * @param field variable attached to option (null for category)
     * @param name  name of option
     * @param size  size of option, 0 for single column, 1 for double.
     */
    public BasicOption(Field field, String name, int size) {
        this.field = field;
        this.name = name;
        this.size = size;
        if (field != null) field.setAccessible(true);
    }

    /**
     * @param object Java object to set the variable to
     */
    protected void set(Object object) throws IllegalAccessException {
        if (field == null) return;
        field.set(null, object);
    }

    /**
     * @return value of variable as Java object
     */
    protected Object get() throws IllegalAccessException {
        if (field == null) return null;
        return field.get(null);
    }

    /**
     * @return height of option to align other options accordingly
     */
    public abstract int getHeight();

    /**
     * Function that gets called when drawing option
     *
     * @param vg NanoVG context
     * @param x  x position
     * @param y  y position
     */
    public abstract void draw(long vg, int x, int y);

    /**
     * Function that gets called last drawing option,
     * should be used for things that draw above other options
     *
     * @param vg NanoVG context
     * @param x  x position
     * @param y  y position
     */
    public void drawLast(long vg, int x, int y) {
    }

    /**
     * Function that gets called when a key is typed
     *
     * @param key     char that has been typed
     * @param keyCode code of key
     */
    public void keyTyped(char key, int keyCode) {
    }

    /**
     * @return If the component has an option to render at half size
     */
    public boolean hasHalfSize() {
        return true;
    }

    public String getName() {
        return name;
    }

    public void setDependency(Supplier<Boolean> supplier) {
        this.dependency = supplier;
    }

    protected boolean isEnabled() {
        return dependency == null || dependency.get();
    }
}