From 52c5b2d205812f8c3033ce31d212f6ef4a9f4d9a Mon Sep 17 00:00:00 2001 From: "dev@ftbsc" Date: Sun, 29 Jan 2023 20:24:53 +0100 Subject: [PATCH] feat: added TextBuilder, converted Hud module --- .../java/co/fantabos/bscv/modules/Hud.java | 26 +++++--- .../java/co/fantabos/bscv/tools/Align.java | 37 +++++++++++ .../java/co/fantabos/bscv/tools/Text.java | 61 +++++++++++++++++-- 3 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 src/main/java/co/fantabos/bscv/tools/Align.java diff --git a/src/main/java/co/fantabos/bscv/modules/Hud.java b/src/main/java/co/fantabos/bscv/modules/Hud.java index 4b54adf..d82b5ec 100644 --- a/src/main/java/co/fantabos/bscv/modules/Hud.java +++ b/src/main/java/co/fantabos/bscv/modules/Hud.java @@ -1,11 +1,14 @@ package co.fantabos.bscv.modules; +import static co.fantabos.bscv.tools.Text.TextBuilder; + import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; import co.fantabos.bscv.BoSCoVicino; import co.fantabos.bscv.Module; +import co.fantabos.bscv.tools.Align.Anchor; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screen.ChatScreen; import net.minecraft.command.CommandSource; @@ -27,7 +30,7 @@ public class Hud extends Module { private final ForgeConfigSpec.ConfigValue size; public Hud(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { - super("Hud", Group.CORE, builder, dispatcher); + super("Hud", Group.HUD, builder, dispatcher); this.coordinates = this.option( "coords", "show coordinates on screen", true, @@ -53,24 +56,29 @@ public class Hud extends Module { Minecraft mc = BoSCoVicino.minecraft; if (event.getType() == ElementType.TEXT) { if (this.coordinates.get() && BoSCoVicino.minecraft.player != null) { - Vector3d position = mc.player.getPosition(0.0f); - TextComponent text = new StringTextComponent(String.format("[ X %.1f | %.1f Z ] %.1f Y", position.x(), position.z(), position.y())); - text.setStyle(Style.EMPTY.withColor(Color.fromLegacyFormat(TextFormatting.WHITE))); float height = 1.0f; if (mc.screen != null && mc.screen instanceof ChatScreen) { height = 16.0f; } - mc.font.drawShadow(event.getMatrixStack(), text, 1.0f, (event.getWindow().getHeight() / 2) - (mc.font.lineHeight + height), this.size.get()); + Vector3d position = mc.player.position(); + TextBuilder() + .txt(String.format("[ X %.1f | %.1f Z ] %.1f Y", position.x(), position.z(), position.y())) + .anchor(Anchor.BOTTOMLEFT) + .x(1.0f) + .y(height) + .render(event.getMatrixStack(), event.getWindow()); } if (this.activemods.get()) { float offset = 27.0f; for (Module m : BoSCoVicino.mods) { if (m.enabled.get()) { - TextComponent text = new StringTextComponent(String.format("%s <", m.name)); - text.setStyle(Style.EMPTY.withColor(Color.fromLegacyFormat(TextFormatting.WHITE))); - int textWidth = BoSCoVicino.minecraft.font.width(String.format("%s <", m.name)); - mc.font.drawShadow(event.getMatrixStack(), text, (event.getWindow().getWidth() / 2) - (textWidth + 1), offset, this.size.get()); + TextBuilder() + .txt(String.format("%s <", m.name)) + .anchor(Anchor.BOTTOMLEFT) + .x(1.0f) + .y(offset) + .render(event.getMatrixStack(), event.getWindow()); offset += mc.font.lineHeight; } } diff --git a/src/main/java/co/fantabos/bscv/tools/Align.java b/src/main/java/co/fantabos/bscv/tools/Align.java new file mode 100644 index 0000000..495f214 --- /dev/null +++ b/src/main/java/co/fantabos/bscv/tools/Align.java @@ -0,0 +1,37 @@ +package co.fantabos.bscv.tools; + +import net.minecraft.client.MainWindow; +import net.minecraft.util.math.vector.Vector2f; + +public final class Align { + public enum Anchor { + TOPLEFT, TOPCENTER, TOPRIGHT, + MIDDLELEFT, MIDDLECENTER, MIDDLERIGHT, + BOTTOMLEFT, BOTTOMCENTER, BOTTOMRIGHT + } + + public static Vector2f translate(Anchor anchor, MainWindow window, Vector2f in) { + switch (anchor) { + case TOPLEFT: + return new Vector2f(in.x, in.y); + case TOPCENTER: + return new Vector2f((window.getWidth()/4.0f) + in.x, in.y); + case TOPRIGHT: + return new Vector2f((window.getWidth()/2.0f) - in.x, in.y); + case MIDDLELEFT: + return new Vector2f(in.x, (window.getHeight()/4.0f) + in.y); + case MIDDLECENTER: + return new Vector2f((window.getWidth()/4.0f) + in.x, (window.getHeight()/4.0f) + in.y); + case MIDDLERIGHT: + return new Vector2f((window.getWidth()/2.0f) - in.x, (window.getHeight()/4.0f) + in.y); + case BOTTOMLEFT: + return new Vector2f(in.x, (window.getHeight()/2.0f) - in.y); + case BOTTOMCENTER: + return new Vector2f((window.getWidth()/4.0f) + in.x, (window.getHeight()/2.0f) - in.y); + case BOTTOMRIGHT: + return new Vector2f((window.getWidth()/2.0f) - in.x, (window.getHeight()/2.0f) - in.y); + default: + return new Vector2f(0.0f, 0.0f); + } + } +} diff --git a/src/main/java/co/fantabos/bscv/tools/Text.java b/src/main/java/co/fantabos/bscv/tools/Text.java index b5ae83b..a861281 100644 --- a/src/main/java/co/fantabos/bscv/tools/Text.java +++ b/src/main/java/co/fantabos/bscv/tools/Text.java @@ -3,19 +3,70 @@ package co.fantabos.bscv.tools; import com.mojang.blaze3d.matrix.MatrixStack; import co.fantabos.bscv.BoSCoVicino; +import co.fantabos.bscv.tools.Align.Anchor; +import net.minecraft.client.MainWindow; import net.minecraft.client.gui.FontRenderer; +import net.minecraft.util.math.vector.Vector2f; +import net.minecraft.util.text.Color; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import net.minecraft.util.text.Style; + public final class Text { - final String text; + String text; + Anchor anchor; + Style style; + float x; + float y; - public Text(String text) { - this.text = text; + private Text() { + this.text = ""; + this.anchor = Anchor.TOPLEFT; + this.style = Style.EMPTY; + this.x = 0.0f; + this.y = 0.0f; } + public static Text TextBuilder() { + return new Text(); + } + public Text txt(String txt) { + this.text = txt; + return this; + } - public void render(MatrixStack stack) { + public Text anchor(Anchor a) { + this.anchor = a; + return this; + } + + public Text style(Style in) { + this.style = in; + return this; + } + + public Text x(float x) { + this.x = x; + return this; + } + + public Text y(float y) { + this.y = y; + return this; + } + + public void render(MatrixStack stack, MainWindow window) { FontRenderer font = BoSCoVicino.minecraft.font; - font.drawShadow(stack, ); + ITextComponent text = new StringTextComponent(this.text).setStyle(this.style); + Vector2f abs_coords = Align.translate(this.anchor, window, new Vector2f(this.x, this.y)); + font.drawShadow( + stack, + text, + abs_coords.x, + abs_coords.y, + 0 // ???? TODO! + ); } }