aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
blob: f1a41252630117509ab0363c052770b323186a1d (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cc.polyfrost.oneconfig.hud;

import cc.polyfrost.oneconfig.config.annotations.Dropdown;
import cc.polyfrost.oneconfig.config.annotations.Exclude;
import cc.polyfrost.oneconfig.config.annotations.Switch;
import cc.polyfrost.oneconfig.config.annotations.Text;
import cc.polyfrost.oneconfig.config.elements.BasicOption;
import cc.polyfrost.oneconfig.events.EventManager;
import cc.polyfrost.oneconfig.events.event.Stage;
import cc.polyfrost.oneconfig.events.event.TickEvent;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
import cc.polyfrost.oneconfig.gui.elements.config.ConfigSwitch;
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import cc.polyfrost.oneconfig.libs.universal.UScreen;
import cc.polyfrost.oneconfig.renderer.RenderManager;
import net.minecraft.client.gui.GuiChat;

import java.util.ArrayList;

public abstract class SingleTextHud extends TextHud implements Cacheable {

    private transient String cachedString = null;
    private transient final boolean caching;

    public SingleTextHud(String title) {
        this(title, true);
    }

    public SingleTextHud(String title, boolean enabled) {
        this(title, enabled, 0, 0);
    }

    public SingleTextHud(String title, boolean enabled, boolean caching) {
        this(title, enabled, 0, 0, caching);
    }

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

    public SingleTextHud(String title, boolean enabled, int x, int y, boolean caching) {
        super(enabled, x, y);
        this.title = title;
        this.caching = caching;
        if (caching) {
            EventManager.INSTANCE.register(new TextCacher());
        }
    }

    @Override
    public void addCacheOptions(String category, String subcategory, ArrayList<BasicOption> options) {
        if (caching) {
            try {
                options.add(new ConfigSwitch(getClass().getField("cacheText"), this, "Cache Text", category, subcategory, 1));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
    }

    @Exclude(type = Exclude.ExcludeType.HUD)
    public boolean cacheText = true;

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

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

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

    @Override
    public int getWidth(float scale) {
        return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(false)) * scale);
    }

    @Override
    public int getExampleWidth(float scale) {
        return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(true)) * scale);
    }

    @Override
    public int getHeight(float scale) {
        return (int) (UMinecraft.getFontRenderer().FONT_HEIGHT * scale);
    }

    @Override
    public void draw(int x, int y, float scale) {
        if (!showInGuis && UScreen.getCurrentScreen() != null && !(UScreen.getCurrentScreen() instanceof OneConfigGui)) return;
        if (!showInChat && UScreen.getCurrentScreen() instanceof GuiChat) return;
        if (!showInDebug && UMinecraft.getSettings().showDebugInfo) return;

        RenderManager.drawScaledString(getCompleteText(false), x, y, color.getRGB(), RenderManager.TextType.toType(textType), scale);
    }

    @Override
    public void drawExample(int x, int y, float scale) {
        RenderManager.drawScaledString(getCompleteText(true), x, y, color.getRGB(), RenderManager.TextType.toType(textType), scale);
    }

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

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

        builder.append(example ? getExampleText() : (cachedString == null ? getText() : cachedString));

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

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

    public abstract String getText();

    public String getExampleText() {
        return getText();
    }

    private class TextCacher {
        @Subscribe
        private void onTick(TickEvent event) {
            if (event.stage == Stage.START) {
                if (cacheText) {
                    cachedString = getText();
                } else {
                    cachedString = null;
                }
            }
        }
    }
}