aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
blob: 2e32d8af11aebf69d3689e0d081e9f6f8436c7ac (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
package cc.polyfrost.oneconfig.hud;

import cc.polyfrost.oneconfig.config.annotations.Dropdown;
import cc.polyfrost.oneconfig.config.annotations.Switch;
import cc.polyfrost.oneconfig.config.annotations.Text;

import java.util.Collections;
import java.util.List;

public abstract class SingleTextHud extends TextHud {
    public SingleTextHud(String title, boolean enabled) {
        this(title, enabled, 0, 0);
    }

    public SingleTextHud(String title, boolean enabled, int x, int y) {
        super(enabled, x, y);
        this.title = title;
    }

    /**
     * This function is called every tick
     *
     * @return The new text
     */
    protected abstract String getText();

    /**
     * This function is called every frame
     *
     * @return The new text, null to use the cached value
     */
    protected String getTextFrequent() {
        return null;
    }

    /**
     * This function is called every tick in the move GUI
     *
     * @return The new text
     */
    protected String getExampleText() {
        return getText();
    }

    /**
     * This function is called every frame in the move GUI
     *
     * @return The new text, null to use the cached value
     */
    protected String getExampleTextFrequent() {
        return getTextFrequent();
    }

    @Override
    protected List<String> update() {
        return Collections.singletonList(getCompleteText(getText()));
    }

    @Override
    protected List<String> updateFrequent() {
        String text = getTextFrequent();
        if (text == null) return null;
        return Collections.singletonList(getCompleteText(text));
    }

    @Override
    protected List<String> updateExampleFrequent() {
        String text = getExampleTextFrequent();
        if (text == null) return null;
        return Collections.singletonList(getCompleteText(text));
    }

    @Override
    protected List<String> updateExample() {
        return Collections.singletonList(getCompleteText(getExampleText()));
    }

    protected final String getCompleteText(String text) {
        boolean showTitle = !title.trim().isEmpty();
        StringBuilder builder = new StringBuilder();
        if (brackets) {
            builder.append("[");
        }

        if (showTitle && titleLocation == 0) {
            builder.append(title).append(": ");
        }

        builder.append(text);

        if (showTitle && titleLocation == 1) {
            builder.append(" ").append(title);
        }

        if (brackets) {
            builder.append("]");
        }
        return builder.toString();
    }


    @Switch(
            name = "Brackets"
    )
    public boolean brackets = false;

    @Text(
            name = "Title"
    )
    public String title;

    @Dropdown(
            name = "Title Location",
            options = {"Left", "Right"}
    )
    public int titleLocation = 0;
}