aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/config/interfaces/Option.java
blob: 5980a63af3396e8b7176a805f0b3209f193ac444 (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
package io.polyfrost.oneconfig.config.interfaces;

import java.lang.reflect.Field;

@SuppressWarnings({"unused"})
public abstract class Option {
    protected final Field field;
    protected final String name;
    protected final String description;

    /**
     * Initialize option
     *
     * @param field       variable attached to option (null for category)
     * @param name        name of option
     * @param description description of option
     */
    public Option(Field field, String name, String description) {
        this.field = field;
        this.name = name;
        this.description = description;
        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 x      x position
     * @param y      y position
     * @param width  width of menu
     * @param mouseX x position of mouse
     * @param mouseY y position of mouse
     */
    public abstract void draw(int x, int y, int width, int mouseX, int mouseY);

    /**
     * Function that gets called when mouse is clicked
     *
     * @param mouseX      x position of mouse
     * @param mouseY      y position of mouse
     * @param mouseButton button that got pressed
     */
    protected void onMouseClicked(int mouseX, int mouseY, int mouseButton) {
    }

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