fix: improved and fixed Align and Text utils

This commit is contained in:
əlemi 2023-01-30 02:19:39 +01:00
parent 52c5b2d205
commit 2aa64795f5
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E
4 changed files with 58 additions and 128 deletions

View file

@ -1,88 +0,0 @@
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;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.Color;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.Style;
import net.minecraft.util.text.TextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.client.event.RenderGameOverlayEvent.ElementType;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class Hud extends Module {
private final ForgeConfigSpec.ConfigValue<Boolean> coordinates;
private final ForgeConfigSpec.ConfigValue<Boolean> activemods;
private final ForgeConfigSpec.ConfigValue<Integer> size;
public Hud(ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) {
super("Hud", Group.HUD, builder, dispatcher);
this.coordinates = this.option(
"coords", "show coordinates on screen", true,
BoolArgumentType.bool(), Boolean.class,
builder, dispatcher
);
this.activemods = this.option(
"modules", "show active modules on screen", true,
BoolArgumentType.bool(), Boolean.class,
builder, dispatcher
);
this.size = this.option(
"size", "font size for text", 12,
IntegerArgumentType.integer(), Integer.class,
builder, dispatcher
);
}
@SubscribeEvent
public void onRenderOverlay(RenderGameOverlayEvent event) {
Minecraft mc = BoSCoVicino.minecraft;
if (event.getType() == ElementType.TEXT) {
if (this.coordinates.get() && BoSCoVicino.minecraft.player != null) {
float height = 1.0f;
if (mc.screen != null && mc.screen instanceof ChatScreen) {
height = 16.0f;
}
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()) {
TextBuilder()
.txt(String.format("%s <", m.name))
.anchor(Anchor.BOTTOMLEFT)
.x(1.0f)
.y(offset)
.render(event.getMatrixStack(), event.getWindow());
offset += mc.font.lineHeight;
}
}
}
}
}
}

View file

@ -1,37 +0,0 @@
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);
}
}
}

View file

@ -0,0 +1,50 @@
package co.fantabos.bscv.tools;
import co.fantabos.bscv.BoSCoVicino;
import net.minecraft.client.MainWindow;
import net.minecraft.client.gui.screen.ChatScreen;
import net.minecraft.util.math.vector.Vector2f;
public enum Anchor {
TOPLEFT("TOPLEFT"), TOPCENTER("TOPCENTER"), TOPRIGHT("TOPRIGHT"),
MIDDLELEFT("MIDDLELEFT"), MIDDLECENTER("MIDDLECENTER"), MIDDLERIGHT("MIDDLERIGHT"),
BOTTOMLEFT("BOTTOMLEFT"), BOTTOMCENTER("BOTTOMCENTER"), BOTTOMRIGHT("BOTTOMRIGHT");
private Anchor(String in) { }
public Vector2f translate(Vector2f in, int textWidth, int lineHeight, MainWindow window) {
int offset = 0;
switch (this) {
case BOTTOMLEFT:
case BOTTOMCENTER:
case BOTTOMRIGHT:
if (BoSCoVicino.minecraft.screen instanceof ChatScreen) {
offset = 15;
}
default:
}
switch (this) {
case TOPLEFT:
return new Vector2f(in.x, in.y);
case TOPCENTER:
return new Vector2f((window.getWidth()/4.0f) + in.x - (textWidth / 2), in.y);
case TOPRIGHT:
return new Vector2f((window.getWidth()/2.0f) - (in.x + textWidth), in.y);
case MIDDLELEFT:
return new Vector2f(in.x, (window.getHeight()/4.0f) + in.y - (lineHeight / 2));
case MIDDLECENTER:
return new Vector2f((window.getWidth()/4.0f) + in.x - (textWidth / 2), (window.getHeight()/4.0f) + in.y - (lineHeight / 2));
case MIDDLERIGHT:
return new Vector2f((window.getWidth()/2.0f) - (in.x + textWidth), (window.getHeight()/4.0f) + in.y - (lineHeight / 2));
case BOTTOMLEFT:
return new Vector2f(in.x, (window.getHeight()/2.0f) - (in.y + lineHeight + offset));
case BOTTOMCENTER:
return new Vector2f((window.getWidth()/4.0f) + in.x - (textWidth / 2), (window.getHeight()/2.0f) - (in.y + lineHeight + offset));
case BOTTOMRIGHT:
return new Vector2f((window.getWidth()/2.0f) - (in.x + textWidth), (window.getHeight()/2.0f) - (in.y + lineHeight + offset));
default:
return new Vector2f(0.0f, 0.0f);
}
}
}

View file

@ -3,7 +3,7 @@ package co.fantabos.bscv.tools;
import com.mojang.blaze3d.matrix.MatrixStack;
import co.fantabos.bscv.BoSCoVicino;
import co.fantabos.bscv.tools.Align.Anchor;
import co.fantabos.bscv.tools.Anchor;
import net.minecraft.client.MainWindow;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.math.vector.Vector2f;
@ -23,7 +23,7 @@ public final class Text {
private Text() {
this.text = "";
this.anchor = Anchor.TOPLEFT;
this.style = Style.EMPTY;
this.style = Style.EMPTY.withColor(Color.fromRgb(16777215));
this.x = 0.0f;
this.y = 0.0f;
}
@ -60,7 +60,12 @@ public final class Text {
public void render(MatrixStack stack, MainWindow window) {
FontRenderer font = BoSCoVicino.minecraft.font;
ITextComponent text = new StringTextComponent(this.text).setStyle(this.style);
Vector2f abs_coords = Align.translate(this.anchor, window, new Vector2f(this.x, this.y));
Vector2f abs_coords = this.anchor.translate(
new Vector2f(this.x, this.y),
font.width(text),
font.lineHeight,
window
);
font.drawShadow(
stack,
text,