diff options
| author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-06-28 17:29:26 +0700 |
|---|---|---|
| committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-06-28 17:29:26 +0700 |
| commit | e85b11665a92e8a6bffb11b76b45ee3c97138bd2 (patch) | |
| tree | 1e85d3673d6511df53b96deb8fc23989cd304722 /src/main/java/cc/polyfrost/oneconfig/hud | |
| parent | 61861be3663dc05883c387070c81eae9154444ef (diff) | |
| download | OneConfig-e85b11665a92e8a6bffb11b76b45ee3c97138bd2.tar.gz OneConfig-e85b11665a92e8a6bffb11b76b45ee3c97138bd2.tar.bz2 OneConfig-e85b11665a92e8a6bffb11b76b45ee3c97138bd2.zip | |
revert funny diamond hud stuff
add ExcludeType again
fix INSTANCE field stuff
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/hud')
5 files changed, 235 insertions, 137 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Conditional.java b/src/main/java/cc/polyfrost/oneconfig/hud/Conditional.java new file mode 100644 index 0000000..d9f9ca9 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/hud/Conditional.java @@ -0,0 +1,12 @@ +package cc.polyfrost.oneconfig.hud; + +import cc.polyfrost.oneconfig.config.elements.BasicOption; + +import java.util.ArrayList; + +/** + * Marks that the HUD element has options to add programmatically. + */ +public interface Conditional { + void addNewOptions(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..fb6f755 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 Conditional) { + ((Conditional) hud).addNewOptions(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/MultiTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java new file mode 100644 index 0000000..17e27ec --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java @@ -0,0 +1,120 @@ +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.elements.config.ConfigSwitch; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; +import cc.polyfrost.oneconfig.libs.universal.UMinecraft; +import cc.polyfrost.oneconfig.renderer.RenderManager; + +import java.util.ArrayList; +import java.util.List; + +public abstract class MultiTextHud extends TextHud implements Conditional { + 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 final void addNewOptions(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; + + @Override + public final int getWidth(float scale) { + return (int) (width * scale); + } + + @Override + public final int getHeight(float scale) { + return (int) (height * scale); + } + + @Override + public final void draw(int x, int y, float scale) { + update(x, y, scale); + 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 final void drawExample(int x, int y, float scale) { + update(x, y, 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); + } + + protected abstract List<String> getLines(); + + protected List<String> getExampleLines() { + return getLines(); + } + + /** + * Ran before the HUD is drawn. + * + * Can be used to update values but not necessarily process / render them. + * This should only be used if absolutely necessary. + */ + protected void update(int x, int y, float scale) { + + } + + 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 2e32d8a..3c357e0 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java @@ -1,81 +1,108 @@ 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.elements.config.ConfigSwitch; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; +import cc.polyfrost.oneconfig.libs.universal.UMinecraft; +import cc.polyfrost.oneconfig.renderer.RenderManager; -import java.util.Collections; -import java.util.List; +import java.util.ArrayList; + +public abstract class SingleTextHud extends TextHud implements Conditional { + + private transient String cachedString = null; + private transient final boolean caching; + + public SingleTextHud(String title) { + this(title, true); + } -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()); + } } - /** - * 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; + @Override + public void addNewOptions(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 in the move GUI - * - * @return The new text - */ - protected String getExampleText() { - return getText(); - } + @Exclude(type = Exclude.ExcludeType.HUD) + public boolean cacheText = true; - /** - * 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(); + @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 - protected List<String> update() { - return Collections.singletonList(getCompleteText(getText())); + public int getExampleWidth(float scale) { + return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(true)) * scale); } @Override - protected List<String> updateFrequent() { - String text = getTextFrequent(); - if (text == null) return null; - return Collections.singletonList(getCompleteText(text)); + public int getHeight(float scale) { + return (int) (UMinecraft.getFontRenderer().FONT_HEIGHT * scale); } @Override - protected List<String> updateExampleFrequent() { - String text = getExampleTextFrequent(); - if (text == null) return null; - return Collections.singletonList(getCompleteText(text)); + public void draw(int x, int y, float scale) { + update(x, y, scale); + RenderManager.drawScaledString(getCompleteText(false), x, y, color.getRGB(), RenderManager.TextType.toType(textType), scale); } @Override - protected List<String> updateExample() { - return Collections.singletonList(getCompleteText(getExampleText())); + public void drawExample(int x, int y, float scale) { + update(x, y, scale); + RenderManager.drawScaledString(getCompleteText(true), x, y, color.getRGB(), RenderManager.TextType.toType(textType), scale); } - protected final String getCompleteText(String text) { + protected final String getCompleteText(boolean example) { boolean showTitle = !title.trim().isEmpty(); StringBuilder builder = new StringBuilder(); if (brackets) { @@ -86,7 +113,7 @@ public abstract class SingleTextHud extends TextHud { builder.append(title).append(": "); } - builder.append(text); + builder.append(example ? getExampleText() : (cachedString == null ? getText() : cachedString)); if (showTitle && titleLocation == 1) { builder.append(" ").append(title); @@ -98,20 +125,32 @@ public abstract class SingleTextHud extends TextHud { return builder.toString(); } + public abstract String getText(); - @Switch( - name = "Brackets" - ) - public boolean brackets = false; + public String getExampleText() { + return getText(); + } - @Text( - name = "Title" - ) - public String title; + /** + * Ran before the HUD is drawn. + * + * Can be used to update values but not necessarily process / render them. + * This should only be used if absolutely necessary. + */ + protected void update(int x, int y, float scale) { - @Dropdown( - name = "Title Location", - options = {"Left", "Right"} - ) - public int titleLocation = 0; + } + + private class TextCacher { + @Subscribe + private void onTick(TickEvent event) { + if (event.stage == Stage.START) { + if (cacheText) { + cachedString = getText(); + } else { + cachedString = null; + } + } + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java index 7895f26..74a7602 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java @@ -3,17 +3,8 @@ 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; +abstract class TextHud extends Hud { @Color( name = "Text Color" ) @@ -25,75 +16,8 @@ public abstract class TextHud extends Hud { ) 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(); - } } |
