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

import cc.polyfrost.oneconfig.config.annotations.Color;
import cc.polyfrost.oneconfig.config.annotations.Dropdown;
import cc.polyfrost.oneconfig.config.core.OneColor;
import cc.polyfrost.oneconfig.internal.hud.HudCore;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import cc.polyfrost.oneconfig.renderer.RenderManager;

import java.util.List;

public abstract class TextHud extends Hud {
    protected transient List<String> lines = null;
    private transient int width;
    private transient int height;

    @Color(
            name = "Text Color"
    )
    public OneColor color = new OneColor(255, 255, 255);

    @Dropdown(
            name = "Text Type",
            options = {"No Shadow", "Shadow", "Full Shadow"}
    )
    public int textType = 0;

    public TextHud(boolean enabled, int x, int y) {
        super(enabled, x, y);
    }
    public TextHud(boolean enabled) {
        this(enabled, 0, 0);
    }

    /**
     * This function is called every tick
     *
     * @return The new lines
     */
    protected abstract List<String> update();

    /**
     * This function is called every frame
     *
     * @return The new lines, null if you want to use the cached lines
     */
    protected List<String> updateFrequent() {
        return null;
    }

    /**
     * This function is called every tick in the move GUI
     *
     * @return The new lines
     */
    protected List<String> updateExample() {
        return update();
    }

    /**
     * This function is called every frame in the move GUI
     *
     * @return The new lines, null if you want to use the cached lines
     */
    protected List<String> updateExampleFrequent() {
        return updateFrequent();
    }

    @Override
    public void draw(int x, int y, float scale) {
        List<String> frequentLines = HudCore.editing ? updateExampleFrequent() : updateFrequent();
        if (frequentLines != null) lines = frequentLines;
        if (lines == null) return;

        int textY = y;
        width = 0;
        for (String line : lines) {
            RenderManager.drawScaledString(line, x, textY, color.getRGB(), RenderManager.TextType.toType(textType), scale);
            width = Math.max(width, UMinecraft.getFontRenderer().getStringWidth(line));
            textY += 12 * scale;
        }
        height = (int) ((textY - y) / scale - 3);
    }

    @Override
    public int getWidth(float scale) {
        return (int) (width * scale);
    }

    @Override
    public int getHeight(float scale) {
        return (int) (height * scale);
    }

    public void tick() {
        if (!HudCore.editing) lines = update();
        else lines = updateExample();
    }
}