aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java156
1 files changed, 61 insertions, 95 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
index f1a4125..2e32d8a 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
@@ -1,113 +1,81 @@
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);
- }
+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, 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();
- }
- }
+ /**
+ * 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;
}
- @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;
+ /**
+ * This function is called every tick in the move GUI
+ *
+ * @return The new text
+ */
+ protected String getExampleText() {
+ return getText();
+ }
- @Override
- public int getWidth(float scale) {
- return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(false)) * scale);
+ /**
+ * 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
- public int getExampleWidth(float scale) {
- return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(true)) * scale);
+ protected List<String> update() {
+ return Collections.singletonList(getCompleteText(getText()));
}
@Override
- public int getHeight(float scale) {
- return (int) (UMinecraft.getFontRenderer().FONT_HEIGHT * scale);
+ protected List<String> updateFrequent() {
+ String text = getTextFrequent();
+ if (text == null) return null;
+ return Collections.singletonList(getCompleteText(text));
}
@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);
+ protected List<String> updateExampleFrequent() {
+ String text = getExampleTextFrequent();
+ if (text == null) return null;
+ return Collections.singletonList(getCompleteText(text));
}
@Override
- public void drawExample(int x, int y, float scale) {
- RenderManager.drawScaledString(getCompleteText(true), x, y, color.getRGB(), RenderManager.TextType.toType(textType), scale);
+ protected List<String> updateExample() {
+ return Collections.singletonList(getCompleteText(getExampleText()));
}
- protected final String getCompleteText(boolean example) {
+ protected final String getCompleteText(String text) {
boolean showTitle = !title.trim().isEmpty();
StringBuilder builder = new StringBuilder();
if (brackets) {
@@ -118,7 +86,7 @@ public abstract class SingleTextHud extends TextHud implements Cacheable {
builder.append(title).append(": ");
}
- builder.append(example ? getExampleText() : (cachedString == null ? getText() : cachedString));
+ builder.append(text);
if (showTitle && titleLocation == 1) {
builder.append(" ").append(title);
@@ -130,22 +98,20 @@ public abstract class SingleTextHud extends TextHud implements Cacheable {
return builder.toString();
}
- public abstract String getText();
- public String getExampleText() {
- return getText();
- }
+ @Switch(
+ name = "Brackets"
+ )
+ public boolean brackets = false;
- private class TextCacher {
- @Subscribe
- private void onTick(TickEvent event) {
- if (event.stage == Stage.START) {
- if (cacheText) {
- cachedString = getText();
- } else {
- cachedString = null;
- }
- }
- }
- }
+ @Text(
+ name = "Title"
+ )
+ public String title;
+
+ @Dropdown(
+ name = "Title Location",
+ options = {"Left", "Right"}
+ )
+ public int titleLocation = 0;
}