diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/hud')
6 files changed, 533 insertions, 238 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/BasicHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/BasicHud.java new file mode 100644 index 0000000..cb8a2d4 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/hud/BasicHud.java @@ -0,0 +1,116 @@ +package cc.polyfrost.oneconfig.hud; + +import cc.polyfrost.oneconfig.config.core.OneColor; +import cc.polyfrost.oneconfig.libs.universal.UMatrixStack; +import cc.polyfrost.oneconfig.renderer.RenderManager; + +public abstract class BasicHud extends Hud { + protected boolean rounded; + protected boolean border; + protected OneColor bgColor; + protected OneColor borderColor; + protected float cornerRadius; + protected float borderSize; + protected float paddingX; + protected float paddingY; + + /** + * @param enabled If the hud is enabled + * @param x X-coordinate of hud on a 1080p display + * @param y Y-coordinate of hud on a 1080p display + * @param scale Scale of the hud + * @param rounded If the corner is rounded or not + * @param cornerRadius Radius of the corner + * @param paddingX Horizontal background padding + * @param paddingY Vertical background padding + * @param bgColor Background color + * @param border If the hud has a border or not + * @param borderSize Thickness of the border + * @param borderColor The color of the border + */ + public BasicHud(boolean enabled, float x, float y, float scale, boolean rounded, float cornerRadius, float paddingX, float paddingY, OneColor bgColor, boolean border, float borderSize, OneColor borderColor) { + super(enabled, x, y, scale); + this.rounded = rounded; + this.cornerRadius = cornerRadius; + this.paddingX = paddingX; + this.paddingY = paddingY; + this.bgColor = bgColor; + this.border = border; + this.borderSize = borderSize; + this.borderColor = borderColor; + position.setSize(getWidth(scale, true) + paddingX * scale * 2f, getHeight(scale, true) + paddingY * scale * 2f); + } + + /** + * @param enabled If the hud is enabled + * @param x X-coordinate of hud on a 1080p display + * @param y Y-coordinate of hud on a 1080p display + * @param scale Scale of the hud + */ + public BasicHud(boolean enabled, float x, float y, float scale) { + this(enabled, x, y, scale, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + } + + /** + * @param enabled If the hud is enabled + * @param x X-coordinate of hud on a 1080p display + * @param y Y-coordinate of hud on a 1080p display + */ + public BasicHud(boolean enabled, float x, float y) { + this(enabled, x, y, 1, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + } + + /** + * @param enabled If the hud is enabled + */ + public BasicHud(boolean enabled) { + this(enabled, 0, 0, 1, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + } + + public BasicHud() { + this(false, 0, 0, 1, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + } + + @Override + public void drawAll(UMatrixStack matrices, boolean example) { + if (!example && !shouldShow()) return; + preRender(example); + position.setSize(getWidth(scale, example) + paddingX * scale * 2f, getHeight(scale, example) + paddingY * scale * 2f); + if (shouldDrawBackground()) + drawBackground(position.getX(), position.getY(), position.getWidth(), position.getHeight(), scale); + draw(matrices, position.getX() + paddingX * scale, position.getY() + paddingY * scale, scale, example); + } + + /** + * Set a new scale value + * + * @param scale The new scale + * @param example If the HUD is being rendered in example form + */ + @Override + public void setScale(float scale, boolean example) { + this.scale = scale; + position.updateSizePosition(getWidth(scale, example) + paddingX * scale * 2f, getHeight(scale, example) + paddingY * scale * 2f); + } + + /** + * @return If the background should be drawn + */ + protected boolean shouldDrawBackground() { + return true; + } + + protected void drawBackground(float x, float y, float width, float height, float scale) { + RenderManager.setupAndDraw(true, (vg) -> { + if (rounded) { + RenderManager.drawRoundedRect(vg, x, y, width, height, bgColor.getRGB(), cornerRadius * scale); + if (border) + RenderManager.drawHollowRoundRect(vg, x - borderSize * scale, y - borderSize * scale, width + borderSize * scale, height + borderSize * scale, borderColor.getRGB(), cornerRadius * scale, borderSize * scale); + } else { + RenderManager.drawRect(vg, x, y, width, height, bgColor.getRGB()); + if (border) + RenderManager.drawHollowRoundRect(vg, x - borderSize * scale, y - borderSize * scale, width + borderSize * scale, height + borderSize * scale, borderColor.getRGB(), 0, borderSize * scale); + } + }); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java index a67177c..bff1b70 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java @@ -6,42 +6,52 @@ import cc.polyfrost.oneconfig.config.core.ConfigUtils; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.config.elements.OptionPage; import cc.polyfrost.oneconfig.gui.elements.config.*; +import cc.polyfrost.oneconfig.hud.BasicHud; +import cc.polyfrost.oneconfig.hud.Hud; import cc.polyfrost.oneconfig.internal.hud.HudCore; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; public class HUDUtils { public static void addHudOptions(OptionPage page, Field field, Object instance, Config config) { HUD hudAnnotation = field.getAnnotation(HUD.class); + field.setAccessible(true); Hud hud = (Hud) ConfigUtils.getField(field, instance); if (hud == null) return; hud.setConfig(config); HudCore.huds.add(hud); String category = hudAnnotation.category(); String subcategory = hudAnnotation.subcategory(); - ArrayList<BasicOption> options = ConfigUtils.getSubCategory(page, hudAnnotation.category(), hudAnnotation.subcategory()).options; + ArrayList<BasicOption> options = new ArrayList<>(); try { + ArrayList<Field> fieldArrayList = ConfigUtils.getClassFields(hud.getClass()); + HashMap<String, Field> fields = new HashMap<>(); + for (Field f : fieldArrayList) fields.put(f.getName(), f); 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.add(new ConfigSwitch(fields.get("enabled"), hud, "Enabled", category, subcategory, 2)); options.addAll(ConfigUtils.getClassOptions(hud)); - options.add(new ConfigCheckbox(hud.getClass().getField("rounded"), hud, "Rounded corners", category, subcategory, 1)); - options.get(options.size() - 1).addDependency(hud::isEnabled); - options.add(new ConfigCheckbox(hud.getClass().getField("border"), hud, "Outline/border", category, subcategory, 1)); - options.get(options.size() - 1).addDependency(hud::isEnabled); - options.add(new ConfigColorElement(hud.getClass().getField("bgColor"), hud, "Background color:", category, subcategory, 1, true)); - options.get(options.size() - 1).addDependency(hud::isEnabled); - options.add(new ConfigColorElement(hud.getClass().getField("borderColor"), hud, "Border color:", category, subcategory, 1, true)); - options.get(options.size() - 1).addDependency(() -> hud.isEnabled() && hud.border); - options.add(new ConfigSlider(hud.getClass().getField("cornerRadius"), hud, "Corner radius:", category, subcategory, 0, 10, 0)); - options.get(options.size() - 1).addDependency(() -> hud.isEnabled() && hud.rounded); - options.add(new ConfigSlider(hud.getClass().getField("borderSize"), hud, "Border thickness:", category, subcategory, 0, 10, 0)); - options.get(options.size() - 1).addDependency(() -> hud.isEnabled() && hud.border); - options.add(new ConfigSlider(hud.getClass().getField("paddingX"), hud, "X-Padding", category, subcategory, 0, 50, 0)); - options.get(options.size() - 1).addDependency(hud::isEnabled); - options.add(new ConfigSlider(hud.getClass().getField("paddingY"), hud, "Y-Padding", category, subcategory, 0, 50, 0)); - options.get(options.size() - 1).addDependency(hud::isEnabled); - } catch (NoSuchFieldException ignored) { + if (hud instanceof BasicHud) { + options.add(new ConfigCheckbox(fields.get("rounded"), hud, "Rounded corners", category, subcategory, 1)); + options.add(new ConfigCheckbox(fields.get("border"), hud, "Outline/border", category, subcategory, 1)); + options.add(new ConfigColorElement(fields.get("bgColor"), hud, "Background color:", category, subcategory, 1, true)); + options.add(new ConfigColorElement(fields.get("borderColor"), hud, "Border color:", category, subcategory, 1, true)); + options.get(options.size() - 1).addDependency(() -> ((BasicHud) hud).border); + options.add(new ConfigSlider(fields.get("cornerRadius"), hud, "Corner radius:", category, subcategory, 0, 10, 0)); + options.get(options.size() - 1).addDependency(() -> ((BasicHud) hud).rounded); + options.add(new ConfigSlider(fields.get("borderSize"), hud, "Border thickness:", category, subcategory, 0, 10, 0)); + options.get(options.size() - 1).addDependency(() -> ((BasicHud) hud).border); + options.add(new ConfigSlider(fields.get("paddingX"), hud, "X-Padding", category, subcategory, 0, 50, 0)); + options.add(new ConfigSlider(fields.get("paddingY"), hud, "Y-Padding", category, subcategory, 0, 50, 0)); + } + for (BasicOption option : options) { + if (option.name.equals("Enabled")) continue; + option.addDependency(hud::isEnabled); + } + } catch (Exception ignored) { } + ConfigUtils.getSubCategory(page, hudAnnotation.category(), hudAnnotation.subcategory()).options.addAll(options); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java index 05a4f76..a8c51bd 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java @@ -2,11 +2,9 @@ package cc.polyfrost.oneconfig.hud; import cc.polyfrost.oneconfig.config.Config; 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.UMatrixStack; import cc.polyfrost.oneconfig.platform.Platform; -import cc.polyfrost.oneconfig.renderer.RenderManager; /** * Represents a HUD element in OneConfig. @@ -15,7 +13,7 @@ import cc.polyfrost.oneconfig.renderer.RenderManager; * 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(UMatrixStack, int, int, float)} with the width, height, and the drawing code respectively. + * If you want to display something else, extend this class and override {@link Hud#getWidth(float, boolean)}, {@link Hud#getHeight(float, boolean)}, and {@link Hud#draw(UMatrixStack, float, float, float, boolean)} with the width, height, and the drawing code respectively. * </p> * <p> * It should also be noted that additional options to the HUD can be added simply by declaring them. @@ -39,50 +37,10 @@ import cc.polyfrost.oneconfig.renderer.RenderManager; * * }</pre> */ public abstract class Hud { - public boolean enabled; + protected boolean enabled; transient private Config config; - public boolean rounded; - public boolean border; - public OneColor bgColor; - public OneColor borderColor; - public float cornerRadius; - public float borderSize; - public double xUnscaled; - public double yUnscaled; - public float scale; - public float paddingX; - public float paddingY; - - /** - * @param enabled If the hud is enabled - * @param x X-coordinate of hud on a 1080p display - * @param y Y-coordinate of hud on a 1080p display - * @param scale Scale of the hud - * @param rounded If the corner is rounded or not - * @param cornerRadius Radius of the corner - * @param paddingX Horizontal background padding - * @param paddingY Vertical background padding - * @param bgColor Background color - * @param border If the hud has a border or not - * @param borderSize Thickness of the border - * @param borderColor The color of the border - */ - public Hud(boolean enabled, int x, int y, float scale, boolean rounded, int cornerRadius, int paddingX, int paddingY, OneColor bgColor, boolean border, float borderSize, OneColor borderColor) { - this.enabled = enabled; - this.scale = scale; - this.rounded = rounded; - this.cornerRadius = cornerRadius; - this.paddingX = paddingX; - this.paddingY = paddingY; - this.bgColor = bgColor; - this.border = border; - this.borderSize = borderSize; - this.borderColor = borderColor; - if (x / 1920d <= 0.5d) xUnscaled = x / 1920d; - else xUnscaled = (x + getWidth(scale)) / 1920d; - if (y / 1080d <= 0.5d) yUnscaled = y / 1080d; - else yUnscaled = (y + getHeight(scale)) / 1090d; - } + public final Position position; + protected float scale; /** * @param enabled If the hud is enabled @@ -90,8 +48,10 @@ public abstract class Hud { * @param y Y-coordinate of hud on a 1080p display * @param scale Scale of the hud */ - public Hud(boolean enabled, int x, int y, int scale) { - this(enabled, x, y, scale, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + public Hud(boolean enabled, float x, float y, float scale) { + this.enabled = enabled; + this.scale = scale; + position = new Position(x, y, getWidth(scale, true), getHeight(scale, true)); } /** @@ -99,153 +59,103 @@ public abstract class Hud { * @param x X-coordinate of hud on a 1080p display * @param y Y-coordinate of hud on a 1080p display */ - public Hud(boolean enabled, int x, int y) { - this(enabled, x, y, 1, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + public Hud(boolean enabled, float x, float y) { + this(enabled, x, y, 1); } /** * @param enabled If the hud is enabled */ public Hud(boolean enabled) { - this(enabled, 0, 0, 1, false, 2, 5, 5, new OneColor(0, 0, 0, 120), false, 2, new OneColor(0, 0, 0)); + this(enabled, 0, 0, 1); } - /** - * Function called when drawing the hud - * - * @param x Top left x-coordinate of the hud - * @param y Top left y-coordinate of the hud - * @param scale Scale of the hud - */ - public abstract void draw(UMatrixStack matrices, int x, int y, float scale); + public Hud() { + this(false, 0, 0, 1); + } /** - * Function called when drawing the example version of the hud. - * This is used in for example, the hud editor gui. + * Function called when drawing the hud * - * @param x Top left x-coordinate of the hud - * @param y Top left y-coordinate of the hud - * @param scale Scale of the hud + * @param matrices The UMatrixStack used for rendering in higher versions + * @param x Top left x-coordinate of the hud + * @param y Top left y-coordinate of the hud + * @param scale Scale of the hud + * @param example If the HUD is being rendered in example form */ - public void drawExample(UMatrixStack matrices, int x, int y, float scale) { - draw(matrices, x, y, scale); - } + protected abstract void draw(UMatrixStack matrices, float x, float y, float scale, boolean example); /** - * @param scale Scale of the hud + * @param scale Scale of the hud + * @param example If the HUD is being rendered in example form * @return The width of the hud */ - public abstract int getWidth(float scale); + protected abstract float getWidth(float scale, boolean example); /** - * @param scale Scale of the hud + * @param scale Scale of the hud + * @param example If the HUD is being rendered in example form * @return The height of the hud */ - public abstract int getHeight(float scale); - - /** - * @param scale Scale of the hud - * @return The width of the example version of the hud - */ - public int getExampleWidth(float scale) { - return getWidth(scale); - } - - /** - * @param scale Scale of the hud - * @return The height of the example version of the hud - */ - public int getExampleHeight(float scale) { - return getHeight(scale); - } + protected abstract float getHeight(float scale, boolean example); /** - * @return If the background should be drawn + * Function to do things before rendering anything + * + * @param example If the HUD is being rendered in example form */ - public boolean drawBackground() { - return true; + protected void preRender(boolean example) { } /** * Draw the background, the hud and all childed huds, used by HudCore - * - * @param x X-coordinate - * @param y Y-coordinate - * @param scale Scale of the hud - * @param background If background should be drawn or not */ - public void drawAll(UMatrixStack matrices, float x, float y, float scale, boolean background) { - if (!showInGuis && Platform.getGuiPlatform().getCurrentScreen() != null && !(Platform.getGuiPlatform().getCurrentScreen() instanceof OneConfigGui)) return; - if (!showInChat && Platform.getGuiPlatform().isInChat()) return; - if (!showInDebug && Platform.getGuiPlatform().isInDebug()) return; - if (background && drawBackground()) drawBackground(x, y, getWidth(scale), getHeight(scale), scale); - draw(matrices, (int) (x + paddingX * scale / 2f), (int) (y + paddingY * scale / 2f), scale); + public void drawAll(UMatrixStack matrices, boolean example) { + if (!example && !shouldShow()) return; + preRender(example); + position.setSize(getWidth(scale, example), getHeight(scale, example)); + draw(matrices, position.getX(), position.getY(), scale, example); } - /** - * Draw example version of the background, the hud and all childed huds, used by HudGui - * - * @param x X-coordinate - * @param y Y-coordinate - * @param scale Scale of the hud - * @param background If background should be drawn or not - */ - public void drawExampleAll(UMatrixStack matrices, float x, float y, float scale, boolean background) { - if (background) drawBackground(x, y, getWidth(scale), getHeight(scale), scale); - drawExample(matrices, (int) (x + paddingX * scale / 2f), (int) (y + paddingY * scale / 2f), scale); + protected boolean shouldShow() { + if (!showInGuis && Platform.getGuiPlatform().getCurrentScreen() != null && !(Platform.getGuiPlatform().getCurrentScreen() instanceof OneConfigGui)) + return false; + if (!showInChat && Platform.getGuiPlatform().isInChat()) return false; + return showInDebug || !Platform.getGuiPlatform().isInDebug(); } /** - * Draw example version of the background, the hud and all childed huds, used by HudGui - * - * @param x X-coordinate - * @param y Y-coordinate - * @param width Width of the hud - * @param height Height of the hud - * @param scale Scale of the hud + * @return If the hud is enabled */ - private void drawBackground(float x, float y, float width, float height, float scale) { - RenderManager.setupAndDraw(true, (vg) -> { - if (rounded) { - RenderManager.drawRoundedRect(vg, x, y, (width + paddingX * scale), (height + paddingY * scale), bgColor.getRGB(), cornerRadius * scale); - if (border) - RenderManager.drawHollowRoundRect(vg, x - borderSize * scale, y - borderSize * scale, (width + paddingX * scale) + borderSize * scale, (height + paddingY * scale) + borderSize * scale, borderColor.getRGB(), cornerRadius * scale, borderSize * scale); - } else { - RenderManager.drawRect(vg, x, y, (width + paddingX * scale), (height + paddingY * scale), bgColor.getRGB()); - if (border) - RenderManager.drawHollowRoundRect(vg, x - borderSize * scale, y - borderSize * scale, (width + paddingX * scale) + borderSize * scale, (height + paddingY * scale) + borderSize * scale, borderColor.getRGB(), 0, borderSize * scale); - } - }); + public boolean isEnabled() { + return enabled && (config == null || config.enabled); } /** - * @param screenWidth width of the screen - * @return X-coordinate of the hud + * Set the config to disable accordingly, intended for internal use + * + * @param config The config instance */ - public float getXScaled(int screenWidth) { - if (xUnscaled <= 0.5) - return (int) (screenWidth * xUnscaled); - return (float) (screenWidth - (1d - xUnscaled) * screenWidth - (getWidth(scale) + paddingX * scale)); + public void setConfig(Config config) { + this.config = config; } /** - * @param screenHeight height of the screen - * @return Y-coordinate of the hud + * @return The scale of the Hud */ - public float getYScaled(int screenHeight) { - if (yUnscaled <= 0.5) return (int) (screenHeight * yUnscaled); - return (float) (screenHeight - (1d - yUnscaled) * screenHeight - (getHeight(scale) + paddingY * scale)); + public float getScale() { + return scale; } /** - * @return If the hud is enabled + * Set a new scale value + * + * @param scale The new scale + * @param example If the HUD is being rendered in example form */ - public boolean isEnabled() { - return enabled && (config == null || config.enabled); - } - - public void setConfig(Config config) { - this.config = config; + public void setScale(float scale, boolean example) { + this.scale = scale; + position.updateSizePosition(getWidth(scale, example), getHeight(scale, example)); } @Switch( diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Position.java b/src/main/java/cc/polyfrost/oneconfig/hud/Position.java new file mode 100644 index 0000000..1b3bbb7 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/hud/Position.java @@ -0,0 +1,279 @@ +package cc.polyfrost.oneconfig.hud; + +import cc.polyfrost.oneconfig.config.annotations.Exclude; +import cc.polyfrost.oneconfig.libs.universal.UResolution; +import com.google.gson.annotations.SerializedName; + +public class Position { + private AnchorPosition anchor; + private float x; + private float y; + @Exclude + private float width; + @Exclude + private float height; + + /** + * Position object used for huds + * + * @param x The X coordinate + * @param y The Y coordinate + * @param width The width of the HUD + * @param height The height of the HUD + * @param screenWidth The width of the screen to initialize the position width + * @param screenHeight The height of the screen to initialize the position width + */ + public Position(float x, float y, float width, float height, float screenWidth, float screenHeight) { + setSize(width, height); + setPosition(x, y, screenWidth, screenHeight); + } + + /** + * Position object used for huds + * + * @param x The X coordinate + * @param y The Y coordinate + * @param width The width of the HUD + * @param height The height of the HUD + */ + public Position(float x, float y, float width, float height) { + this(x, y, width, height, 1920, 1080); + } + + /** + * Set the position + * + * @param x The X coordinate + * @param y The Y coordinate + * @param screenWidth The screen width + * @param screenHeight The screen height + */ + public void setPosition(float x, float y, float screenWidth, float screenHeight) { + float rightX = x + width; + float bottomY = y + height; + + if (x <= screenWidth / 3f && y <= screenHeight / 3f) + this.anchor = AnchorPosition.TOP_LEFT; + else if (rightX >= screenWidth / 3f * 2f && y <= screenHeight / 3f) + this.anchor = AnchorPosition.TOP_RIGHT; + else if (x <= screenWidth / 3f && bottomY >= screenHeight / 3f * 2f) + this.anchor = AnchorPosition.BOTTOM_LEFT; + else if (rightX >= screenWidth / 3f * 2f && bottomY >= screenHeight / 3f * 2f) + this.anchor = AnchorPosition.BOTTOM_RIGHT; + else if (y <= screenHeight / 3f) + this.anchor = AnchorPosition.TOP_CENTER; + else if (x <= screenWidth / 3f) + this.anchor = AnchorPosition.MIDDLE_LEFT; + else if (rightX >= screenWidth / 3f * 2f) + this.anchor = AnchorPosition.MIDDLE_RIGHT; + else if (bottomY >= screenHeight / 3f * 2f) + this.anchor = AnchorPosition.BOTTOM_CENTER; + else + this.anchor = AnchorPosition.MIDDLE_CENTER; + + this.x = x - getAnchorX(screenWidth) + getAnchorX(width); + this.y = y - getAnchorY(screenHeight) + getAnchorY(height); + } + + /** + * Set the position + * + * @param x The X coordinate + * @param y The Y coordinate + */ + public void setPosition(float x, float y) { + setPosition(x, y, UResolution.getScaledWidth(), UResolution.getScaledHeight()); + } + + /** + * Set the size of the position + * + * @param width The width + * @param height The height + */ + public void setSize(float width, float height) { + this.width = width; + this.height = height; + } + + /** + * Update the position so the top left corner stays in the same spot + * + * @param width The width + * @param height The height + */ + public void updateSizePosition(float width, float height) { + float x = getX(); + float y = getY(); + setSize(width, height); + setPosition(x, y); + } + + /** + * Get the X coordinate scaled to the size of the screen + * + * @param screenWidth The width of the screen + * @return The X coordinate + */ + public float getX(float screenWidth) { + return x + getAnchorX(screenWidth) - getAnchorX(width); + } + + /** + * Get the X coordinate scaled to the size of the screen + * + * @return The X coordinate + */ + public float getX() { + return getX(UResolution.getScaledWidth()); + } + + /** + * Get the Y coordinate scaled to the size of the screen + * + * @param screenHeight The height of the screen + * @return The Y coordinate + */ + public float getY(float screenHeight) { + return y + getAnchorY(screenHeight) - getAnchorY(height); + } + + /** + * Get the Y coordinate scaled to the size of the screen + * + * @return The Y coordinate + */ + public float getY() { + return getY(UResolution.getScaledHeight()); + } + + /** + * Get the X coordinate scaled to the size of the screen of the right corner + * + * @param screenWidth The width of the screen + * @return The X coordinate of the right corner + */ + public float getRightX(float screenWidth) { + return getX(screenWidth) + width; + } + + /** + * Get the X coordinate scaled to the size of the screen of the right corner + * + * @return The X coordinate of the right corner + */ + public float getRightX() { + return getRightX(UResolution.getScaledWidth()); + } + + /** + * Get the Y coordinate scaled to the size of the screen of the bottom corner + * + * @param screenHeight The width of the screen + * @return The Y coordinate of the bottom corner + */ + public float getBottomY(float screenHeight) { + return getY(screenHeight) + height; + } + + /** + * Get the Y coordinate scaled to the size of the screen of the bottom corner + * + * @return The Y coordinate of the bottom corner + */ + public float getBottomY() { + return getBottomY(UResolution.getScaledHeight()); + } + + /** + * Get the center X coordinate + * + * @param screenWidth The width of the screen + * @return The center X coordinate + */ + public float getCenterX(float screenWidth) { + return getX(screenWidth) + width / 2f; + } + + /** + * Get the center X coordinate + * + * @return The center X coordinate + */ + public float getCenterX() { + return getCenterX(UResolution.getScaledWidth()); + } + + /** + * Get the center Y coordinate + * + * @param screenHeight The width of the screen + * @return The center Y coordinate + */ + public float getCenterY(float screenHeight) { + return getY(screenHeight) + height / 2f; + } + + /** + * Get the center Y coordinate + * + * @return The center Y coordinate + */ + public float getCenterY() { + return getCenterY(UResolution.getScaledHeight()); + } + + /** + * @return The width of the position + */ + public float getWidth() { + return width; + } + + /** + * @return The height of the position + */ + public float getHeight() { + return height; + } + + private float getAnchorX(float value) { + return value * anchor.x; + } + + private float getAnchorY(float value) { + return value * anchor.y; + } + + /** + * Position of the anchors were the position is relative too + */ + public enum AnchorPosition { + @SerializedName("0") + TOP_LEFT(0f, 0f), + @SerializedName("1") + TOP_CENTER(0.5f, 0f), + @SerializedName("2") + TOP_RIGHT(1f, 0f), + @SerializedName("3") + MIDDLE_LEFT(0f, 0.5f), + @SerializedName("4") + MIDDLE_CENTER(0.5f, 0.5f), + @SerializedName("5") + MIDDLE_RIGHT(1f, 0.5f), + @SerializedName("6") + BOTTOM_LEFT(0f, 1f), + @SerializedName("7") + BOTTOM_CENTER(0.5f, 1f), + @SerializedName("8") + BOTTOM_RIGHT(1f, 1f); + + public final float x; + public final float y; + + AnchorPosition(float x, float y) { + this.x = x; + this.y = y; + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java index d3ce8f2..5380be5 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java @@ -22,56 +22,25 @@ public abstract class SingleTextHud extends TextHud { * * @return The new text */ - protected abstract String getText(); + protected abstract String getText(boolean example); /** * This function is called every frame * * @return The new text, null to use the cached value */ - protected String getTextFrequent() { + protected String getTextFrequent(boolean example) { return null; } - /** - * This function is called every tick in the move GUI - * - * @return The new text - */ - protected String getExampleText() { - return getText(); - } - - /** - * 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 - protected void getLines(List<String> lines) { - lines.add(getCompleteText(getText())); - } - - @Override - protected void getLinesFrequent(List<String> lines) { - String text = getTextFrequent(); - if (text == null) return; - lines.clear(); - lines.add(getCompleteText(text)); - } - @Override - protected void getExampleLines(List<String> lines) { - lines.add(getCompleteText(getExampleText())); + protected void getLines(List<String> lines, boolean example) { + lines.add(getCompleteText(getText(example))); } @Override - protected void getExampleLinesFrequent(List<String> lines) { - String text = getExampleTextFrequent(); + protected void getLinesFrequent(List<String> lines, boolean example) { + String text = getTextFrequent(example); if (text == null) return; lines.clear(); lines.add(getCompleteText(text)); @@ -104,16 +73,16 @@ public abstract class SingleTextHud extends TextHud { @Switch( name = "Brackets" ) - public boolean brackets = false; + protected boolean brackets = false; @Text( name = "Title" ) - public String title; + protected String title; @Dropdown( name = "Title Location", options = {"Left", "Right"} ) - public int titleLocation = 0; + protected int titleLocation = 0; }
\ No newline at end of file diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java index 8dd8ac3..b4857d8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java @@ -15,21 +15,20 @@ import cc.polyfrost.oneconfig.renderer.RenderManager; import java.util.ArrayList; import java.util.List; -public abstract class TextHud extends Hud { +public abstract class TextHud extends BasicHud { protected transient List<String> lines = new ArrayList<>(); private transient int width; - private transient int height; @Color( name = "Text Color" ) - public OneColor color = new OneColor(255, 255, 255); + protected OneColor color = new OneColor(255, 255, 255); @Dropdown( name = "Text Type", options = {"No Shadow", "Shadow", "Full Shadow"} ) - public int textType = 0; + protected int textType = 0; public TextHud(boolean enabled, int x, int y) { super(enabled, x, y); @@ -45,59 +44,72 @@ public abstract class TextHud extends Hud { * * @param lines Empty ArrayList to add your hud text too */ - protected abstract void getLines(List<String> lines); + protected abstract void getLines(List<String> lines, boolean example); /** * This function is called every frame * * @param lines The current lines of the hud */ - protected void getLinesFrequent(List<String> lines) { + protected void getLinesFrequent(List<String> lines, boolean example) { + } + + @Override + public void draw(UMatrixStack matrices, float x, float y, float scale, boolean example) { + if (lines == null || lines.size() == 0) return; + float textY = y; + for (String line : lines) { + drawLine(line, x, textY, scale); + textY += 12 * scale; + } } /** - * This function is called every tick in the move GUI + * Function that can be overwritten to implement different behavior easily * - * @param lines Empty ArrayList to add your hud text too + * @param line The line + * @param x The X coordinate + * @param y The Y coordinate + * @param scale The scale */ - protected void getExampleLines(List<String> lines) { - getLines(lines); + protected void drawLine(String line, float x, float y, float scale) { + RenderManager.drawScaledString(line, x, y, color.getRGB(), RenderManager.TextType.toType(textType), scale); } /** - * This function is called every frame in the move GUI + * Function that can be overwritten to implement different behavior easily * - * @param lines The current lines of the hud + * @param line The line + * @return The width of the line (scaled accordingly) */ - protected void getExampleLinesFrequent(List<String> lines) { - getLinesFrequent(lines); + protected float getLineWidth(String line, float scale) { + return Platform.getGLPlatform().getStringWidth(line) * scale; } @Override - public void draw(UMatrixStack matrices, int x, int y, float scale) { - if (!HudCore.editing) getLinesFrequent(lines); - else getExampleLinesFrequent(lines); - if (lines == null) return; + protected void preRender(boolean example) { + getLinesFrequent(lines, example); + } - int textY = y; - width = 0; + @Override + protected float getWidth(float scale, boolean example) { + if (lines == null) return 0; + float width = 0; for (String line : lines) { - RenderManager.drawScaledString(line, x, textY, color.getRGB(), RenderManager.TextType.toType(textType), scale); - width = Math.max(width, Platform.getGLPlatform().getStringWidth(line)); - textY += 12 * scale; + width = Math.max(width, getLineWidth(line, scale)); } - height = (int) ((textY - y) / scale - 3); + return width; } @Override - public int getWidth(float scale) { - return (int) (width * scale); + protected float getHeight(float scale, boolean example) { + return lines == null ? 0 : (lines.size() * 12 - 4) * scale; } @Override - public int getHeight(float scale) { - return (int) (height * scale); + public boolean shouldDrawBackground() { + return super.shouldDrawBackground() && lines != null && lines.size() > 0; } private class TickHandler { @@ -105,8 +117,7 @@ public abstract class TextHud extends Hud { private void onTick(TickEvent event) { if (event.stage != Stage.START || !isEnabled()) return; lines.clear(); - if (!HudCore.editing) getLines(lines); - else getExampleLines(lines); + getLines(lines, HudCore.editing); } } }
\ No newline at end of file |