aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud
diff options
context:
space:
mode:
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.java102
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java116
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java156
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java96
6 files changed, 171 insertions, 311 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java b/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java
deleted file mode 100644
index c4ff151..0000000
--- a/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java
+++ /dev/null
@@ -1,9 +0,0 @@
-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 babb810..e91a7ce 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java
@@ -23,9 +23,6 @@ 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 61e960d..62cfb2e 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java
@@ -1,14 +1,19 @@
package cc.polyfrost.oneconfig.hud;
+import cc.polyfrost.oneconfig.config.annotations.Switch;
import cc.polyfrost.oneconfig.config.core.OneColor;
+import cc.polyfrost.oneconfig.gui.OneConfigGui;
+import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
+import cc.polyfrost.oneconfig.libs.universal.UScreen;
import cc.polyfrost.oneconfig.renderer.RenderManager;
import cc.polyfrost.oneconfig.config.Config;
+import net.minecraft.client.gui.GuiChat;
/**
* Represents a HUD element in OneConfig.
* A HUD element can be used to display useful information to the user, like FPS or CPS.
* <p>
- * If you simply want to display text, extend {@link SingleTextHud} or {@link MultiTextHud},
+ * If you simply want to display text, extend {@link TextHud} or {@link SingleTextHud},
* whichever applies to the use case. Then, override the required methods.
* <p>
* If you want to display something else, extend this class and override {@link Hud#getWidth(float)}, {@link Hud#getHeight(float)}, and {@link Hud#draw(int, int, float)} with the width, height, and the drawing code respectively.
@@ -47,9 +52,6 @@ public abstract class Hud {
public float scale;
public float paddingX;
public float paddingY;
- public Hud parent;
- public Hud childRight;
- public Hud childBottom;
/**
* @param enabled If the hud is enabled
@@ -158,6 +160,13 @@ public abstract class Hud {
}
/**
+ * @return If the background should be drawn
+ */
+ public boolean drawBackground() {
+ return true;
+ }
+
+ /**
* Draw the background, the hud and all childed huds, used by HudCore
*
* @param x X-coordinate
@@ -166,12 +175,11 @@ public abstract class Hud {
* @param background If background should be drawn or not
*/
public void drawAll(float x, float y, float scale, boolean background) {
- if (background) drawBackground(x, y, getTotalWidth(scale), getTotalHeight(scale), scale);
+ if (!showInGuis && UScreen.getCurrentScreen() != null && !(UScreen.getCurrentScreen() instanceof OneConfigGui)) return;
+ if (!showInChat && UScreen.getCurrentScreen() instanceof GuiChat) return;
+ if (!showInDebug && UMinecraft.getSettings().showDebugInfo) return;
+ if (background && drawBackground()) drawBackground(x, y, getWidth(scale), getHeight(scale), scale);
draw((int) (x + paddingX * scale / 2f), (int) (y + paddingY * scale / 2f), scale);
- if (childRight != null)
- childRight.drawAll((int) x + paddingX * scale / 2f + getWidth(scale), (int) y, childRight.scale, false);
- if (childBottom != null)
- childBottom.drawAll((int) x, (int) y + paddingY * scale / 2f + getHeight(scale), childBottom.scale, false);
}
/**
@@ -183,12 +191,8 @@ public abstract class Hud {
* @param background If background should be drawn or not
*/
public void drawExampleAll(float x, float y, float scale, boolean background) {
- if (background) drawBackground(x, y, getTotalExampleWidth(scale), getTotalExampleHeight(scale), scale);
+ if (background) drawBackground(x, y, getWidth(scale), getHeight(scale), scale);
drawExample((int) (x + paddingX * scale / 2f), (int) (y + paddingY * scale / 2f), scale);
- if (childRight != null)
- childRight.drawExampleAll((int) x + paddingX * scale / 2f + getWidth(scale), (int) y, childRight.scale, false);
- if (childBottom != null)
- childBottom.drawExampleAll((int) x, (int) y + paddingY * scale / 2f + getHeight(scale), childBottom.scale, false);
}
/**
@@ -219,14 +223,8 @@ public abstract class Hud {
* @return X-coordinate of the hud
*/
public float getXScaled(int screenWidth) {
- if (parent != null && parent.childRight == this) {
- return parent.getXScaled(screenWidth) + parent.getWidth(parent.scale) + parent.paddingX * parent.scale / 2f;
- } else if (parent != null) {
- return parent.getXScaled(screenWidth);
- }
- if (xUnscaled <= 0.5) {
+ if (xUnscaled <= 0.5)
return (int) (screenWidth * xUnscaled);
- }
return (float) (screenWidth - (1d - xUnscaled) * screenWidth - (getWidth(scale) + paddingX * scale));
}
@@ -235,58 +233,22 @@ public abstract class Hud {
* @return Y-coordinate of the hud
*/
public float getYScaled(int screenHeight) {
- if (parent != null && parent.childBottom == this) {
- return parent.getYScaled(screenHeight) + parent.getHeight(parent.scale) + parent.paddingY * parent.scale / 2f;
- } else if (parent != null) {
- return parent.getYScaled(screenHeight);
- }
- if (yUnscaled <= 0.5) {
- return (int) (screenHeight * yUnscaled);
- }
+ if (yUnscaled <= 0.5) return (int) (screenHeight * yUnscaled);
return (float) (screenHeight - (1d - yUnscaled) * screenHeight - (getHeight(scale) + paddingY * scale));
}
- /**
- * @param scale Scale of the hud
- * @return The width of the hud and all childed huds
- */
- public float getTotalWidth(float scale) {
- float width = getWidth(scale);
- if (childRight != null) width += childRight.getTotalWidth(childRight.scale) + paddingY * scale / 2f;
- if (childBottom != null) width = Math.max(childBottom.getTotalWidth(childBottom.scale), width);
- return width;
- }
+ @Switch(
+ name = "Show in Chat"
+ )
+ public boolean showInChat = true;
- /**
- * @param scale Scale of the hud
- * @return The height of the hud and all childed huds
- */
- public float getTotalHeight(float scale) {
- float height = getHeight(scale);
- if (childBottom != null) height += childBottom.getTotalHeight(childBottom.scale) + paddingY * scale / 2f;
- if (childRight != null) height = Math.max(childRight.getTotalHeight(childRight.scale), height);
- return height;
- }
-
- /**
- * @param scale Scale of the hud
- * @return The example width of the hud and all childed huds
- */
- public float getTotalExampleWidth(float scale) {
- float width = getExampleWidth(scale);
- if (childRight != null) width += childRight.getTotalExampleWidth(childRight.scale) + paddingX * scale / 2f;
- if (childBottom != null) width = Math.max(childBottom.getTotalExampleWidth(childBottom.scale), width);
- return width;
- }
+ @Switch(
+ name = "Show in F3 (Debug)"
+ )
+ public boolean showInDebug = false;
- /**
- * @param scale Scale of the hud
- * @return The example height of the hud and all childed huds
- */
- public float getTotalExampleHeight(float scale) {
- float height = getExampleHeight(scale);
- if (childBottom != null) height += childBottom.getTotalExampleHeight(childBottom.scale) + paddingY * scale / 2f;
- if (childRight != null) height = Math.max(childRight.getTotalExampleHeight(childRight.scale), height);
- return height;
- }
+ @Switch(
+ name = "Show in GUIs"
+ )
+ public boolean showInGuis = true;
} \ No newline at end of file
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java
deleted file mode 100644
index 5dbff4c..0000000
--- a/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java
+++ /dev/null
@@ -1,116 +0,0 @@
-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 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);
- }
-
- @Override
- public int getHeight(float scale) {
- return (int) (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;
-
- int textY = y;
- width = 0;
- 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;
- }
- height = (int) ((textY - y) / scale - 3);
- }
-
- @Override
- public void drawExample(int x, int y, float scale) {
- int textY = y;
- width = 0;
- for (String line : getExampleLines()) {
- 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);
- }
-
- public abstract List<String> getLines();
-
- 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 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;
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java
index 7a05ef7..7895f26 100644
--- a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java
+++ b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java
@@ -2,38 +2,98 @@ package cc.polyfrost.oneconfig.hud;
import cc.polyfrost.oneconfig.config.annotations.Color;
import cc.polyfrost.oneconfig.config.annotations.Dropdown;
-import cc.polyfrost.oneconfig.config.annotations.Switch;
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;
-abstract class TextHud extends Hud {
@Color(
name = "Text Color"
)
public OneColor color = new OneColor(255, 255, 255);
- @Switch(
- name = "Show in Chat"
- )
- public boolean showInChat;
-
- @Switch(
- name = "Show in F3 (Debug)"
- )
- public boolean showInDebug;
-
- @Switch(
- name = "Show in GUIs"
- )
- public boolean showInGuis = true;
-
@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();
+ }
}