aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-06-27 03:54:28 +0700
committerWyvest <45589059+Wyvest@users.noreply.github.com>2022-06-27 03:54:28 +0700
commit9afbfbc4c41bf8f90b5de901797e18375da2c01a (patch)
tree125282f723f1e17a8a077c0a3222f777fd74ca06 /src/main/java/cc/polyfrost/oneconfig/hud
parentf6bebfc3d14812dfac4592c1b35becf83d46cf76 (diff)
downloadOneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.tar.gz
OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.tar.bz2
OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.zip
cache text
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/hud')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java9
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java3
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/Hud.java2
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java54
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java70
5 files changed, 127 insertions, 11 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java b/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java
new file mode 100644
index 0000000..c4ff151
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java
@@ -0,0 +1,9 @@
+package cc.polyfrost.oneconfig.hud;
+
+import cc.polyfrost.oneconfig.config.elements.BasicOption;
+
+import java.util.ArrayList;
+
+public interface Cacheable {
+ void addCacheOptions(String category, String subcategory, ArrayList<BasicOption> options);
+}
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
index e91a7ce..babb810 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
@@ -23,6 +23,9 @@ public class HUDUtils {
options.add(new ConfigHeader(field, hud, hudAnnotation.name(), category, subcategory, 2));
options.add(new ConfigSwitch(hud.getClass().getField("enabled"), hud, "Enabled", category, subcategory, 2));
options.addAll(ConfigUtils.getClassOptions(hud));
+ if (hud instanceof Cacheable) {
+ ((Cacheable) hud).addCacheOptions(category, subcategory, options);
+ }
options.add(new ConfigCheckbox(hud.getClass().getField("rounded"), hud, "Rounded corners", category, subcategory, 1));
options.get(options.size() - 1).addDependency(() -> hud.enabled);
options.add(new ConfigCheckbox(hud.getClass().getField("border"), hud, "Outline/border", category, subcategory, 1));
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java
index 71c6c38..61e960d 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java
@@ -30,7 +30,7 @@ import cc.polyfrost.oneconfig.config.Config;
* * @literal @HUD(
* * name = "HUD Element"
* * )
- * * public YourHudElement hudElement = new YourHudElement(false);
+ * * public YourHudElement hudElement = new YourHudElement("Title");
* * }
* * }</pre>
*/
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java
index 5440f10..5dbff4c 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java
@@ -1,26 +1,63 @@
package cc.polyfrost.oneconfig.hud;
+import cc.polyfrost.oneconfig.config.annotations.Exclude;
+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;
import java.util.List;
-public abstract class MultiTextHud extends TextHud {
+public abstract class MultiTextHud extends TextHud implements Cacheable {
private transient int width = 100;
private transient int height;
+ private transient List<String> cachedStrings = null;
+ private transient final boolean caching;
public MultiTextHud(boolean enabled) {
this(enabled, 0, 0);
}
+ public MultiTextHud(boolean enabled, boolean caching) {
+ this(enabled, 0, 0, caching);
+ }
+
public MultiTextHud(boolean enabled, int x, int y) {
+ this(enabled, x, y, true);
+ }
+
+ public MultiTextHud(boolean enabled, int x, int y, boolean caching) {
super(enabled, x, y);
+ this.caching = caching;
+ if (caching) {
+ EventManager.INSTANCE.register(new TextCacher());
+ }
}
@Override
+ public void addCacheOptions(String category, String subcategory, ArrayList<BasicOption> options) {
+ if (caching) {
+ try {
+ System.out.println("AIJSIOJ!!");
+ 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;
+
+ @Override
public int getWidth(float scale) {
return (int) (width * scale);
}
@@ -38,7 +75,7 @@ public abstract class MultiTextHud extends TextHud {
int textY = y;
width = 0;
- for (String line : getLines()) {
+ for (String line : cachedStrings == null ? getLines() : cachedStrings) {
RenderManager.drawScaledString(line, x, textY, color.getRGB(), RenderManager.TextType.toType(textType), scale);
width = Math.max(width, UMinecraft.getFontRenderer().getStringWidth(line));
textY += 12 * scale;
@@ -63,4 +100,17 @@ public abstract class MultiTextHud extends TextHud {
public List<String> getExampleLines() {
return getLines();
}
+
+ private class TextCacher {
+ @Subscribe
+ private void onTick(TickEvent event) {
+ if (event.stage == Stage.START) {
+ if (cacheText) {
+ cachedStrings = getLines();
+ } else {
+ cachedStrings = null;
+ }
+ }
+ }
+ }
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
index 983047a..f1a4125 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
@@ -1,24 +1,67 @@
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;
-public abstract class SingleTextHud extends TextHud {
+import java.util.ArrayList;
- public SingleTextHud(boolean enabled) {
- this(enabled, 0, 0);
+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(boolean enabled, int x, int y) {
+ 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"
)
@@ -27,7 +70,7 @@ public abstract class SingleTextHud extends TextHud {
@Text(
name = "Title"
)
- public String title = getDefaultTitle();
+ public String title;
@Dropdown(
name = "Title Location",
@@ -35,8 +78,6 @@ public abstract class SingleTextHud extends TextHud {
)
public int titleLocation = 0;
- public abstract String getDefaultTitle();
-
@Override
public int getWidth(float scale) {
return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(false)) * scale);
@@ -77,7 +118,7 @@ public abstract class SingleTextHud extends TextHud {
builder.append(title).append(": ");
}
- builder.append(example ? getExampleText() : getText());
+ builder.append(example ? getExampleText() : (cachedString == null ? getText() : cachedString));
if (showTitle && titleLocation == 1) {
builder.append(" ").append(title);
@@ -94,4 +135,17 @@ public abstract class SingleTextHud extends TextHud {
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;
+ }
+ }
+ }
+ }
}