From 5b40050073518b31f5381f51f86b1c87d4f60e1c Mon Sep 17 00:00:00 2001 From: VADemon Date: Tue, 10 Dec 2019 03:49:05 +0100 Subject: [PATCH] Activemods: add align and offset options --- .../com/matt/forgehax/mods/ActiveModList.java | 69 +++++++++++++------ .../forgehax/util/draw/SurfaceHelper.java | 8 ++- .../matt/forgehax/util/math/AlignHelper.java | 59 ++++++++++++++++ 3 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/matt/forgehax/util/math/AlignHelper.java diff --git a/src/main/java/com/matt/forgehax/mods/ActiveModList.java b/src/main/java/com/matt/forgehax/mods/ActiveModList.java index bc633e6a6..6b7015e72 100644 --- a/src/main/java/com/matt/forgehax/mods/ActiveModList.java +++ b/src/main/java/com/matt/forgehax/mods/ActiveModList.java @@ -5,6 +5,8 @@ import com.matt.forgehax.mods.services.TickRateService; import com.matt.forgehax.util.color.Colors; import com.matt.forgehax.util.command.Setting; +import com.matt.forgehax.util.math.AlignHelper; +import com.matt.forgehax.util.math.AlignHelper.Align; import com.matt.forgehax.util.draw.SurfaceHelper; import com.matt.forgehax.util.mod.BaseMod; import com.matt.forgehax.util.mod.Category; @@ -13,12 +15,22 @@ import java.util.Comparator; import java.util.concurrent.atomic.AtomicInteger; import net.minecraft.client.gui.GuiChat; +import net.minecraft.client.gui.ScaledResolution; import net.minecraftforge.client.event.RenderGameOverlayEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; @RegisterMod public class ActiveModList extends ToggleMod { + private final Setting alignment = + getCommandStub() + .builders() + .newSettingEnumBuilder() + .name("alignment") + .description("align to corner") + .defaultTo(Align.TOPLEFT) + .build(); + private final Setting tps_meter = getCommandStub() .builders() @@ -66,6 +78,24 @@ public class ActiveModList extends ToggleMod { .defaultTo(SortMode.ALPHABETICAL) .build(); + private final Setting offsetX = + getCommandStub() + .builders() + .newSettingBuilder() + .name("x-offset") + .description("shift on X-axis") + .defaultTo(0) + .build(); + + private final Setting offsetY = + getCommandStub() + .builders() + .newSettingBuilder() + .name("y-offset") + .description("shift on Y-axis") + .defaultTo(0) + .build(); + public ActiveModList() { super(Category.RENDER, "ActiveMods", true, "Shows list of all active mods"); } @@ -89,9 +119,7 @@ private String generateTickRateText() { builder.append(" ("); builder.append(data.getSampleSize()); builder.append(")"); - if (sections > 0) { - builder.append(", "); - } + if (sections > 0) builder.append(", "); } if (sections > 0) { for (int i = sections; i > 0; i--) { @@ -101,9 +129,7 @@ private String generateTickRateText() { builder.append(" ("); builder.append(at); builder.append(")"); - if ((i - 1) != 0) { - builder.append(", "); - } + if ((i - 1) != 0) builder.append(", "); } } } @@ -123,12 +149,20 @@ private String generateTickRateText() { @SubscribeEvent public void onRenderScreen(RenderGameOverlayEvent.Text event) { - int posX = 1; - final AtomicInteger posY = new AtomicInteger(1); + int align = alignment.get().ordinal(); + final int dirSignX = AlignHelper.getFlowDirX2(align); + final int dirSignY = AlignHelper.getFlowDirY2(align); + final int shiftLineBy = (SurfaceHelper.getTextHeight()+1) * dirSignY; + + ScaledResolution scaledRes = new ScaledResolution(MC); + int posX = dirSignX + offsetX.get() * dirSignX + (scaledRes.getScaledWidth() * AlignHelper.getPosX(align)) / 2; + final AtomicInteger posY = new AtomicInteger(offsetY.get() * dirSignY + scaledRes.getScaledHeight() * AlignHelper.getPosY(align) / 2); + + if (dirSignY == -1) posY.addAndGet(shiftLineBy); // will also maintain 1px margin to border + if (tps_meter.get()) { - SurfaceHelper - .drawTextShadow(generateTickRateText(), posX, posY.get(), Colors.WHITE.toBuffer()); - posY.addAndGet(SurfaceHelper.getTextHeight() + 1); + SurfaceHelper.drawTextShadowAlignH(generateTickRateText(), posX, posY.get(), Colors.WHITE.toBuffer(), align); + posY.addAndGet(shiftLineBy); } if (MC.currentScreen instanceof GuiChat || MC.gameSettings.showDebugInfo) { @@ -138,8 +172,8 @@ public void onRenderScreen(RenderGameOverlayEvent.Text event) { .filter(BaseMod::isEnabled) .filter(mod -> !mod.isHidden()) .count(); - SurfaceHelper.drawTextShadow(enabledMods + " mods enabled", - posX, posY.get(), Colors.WHITE.toBuffer()); + SurfaceHelper.drawTextShadowAlignH(enabledMods + " mods enabled", + posX, posY.get(), Colors.WHITE.toBuffer(), align); } else { getModManager() .getMods() @@ -150,15 +184,10 @@ public void onRenderScreen(RenderGameOverlayEvent.Text event) { .sorted(sortMode.get().getComparator()) .forEach( name -> { - SurfaceHelper.drawTextShadow(">" + name, posX, posY.get(), Colors.WHITE.toBuffer()); - posY.addAndGet(SurfaceHelper.getTextHeight() + 1); + SurfaceHelper.drawTextShadowAlignH(">" + name, posX, posY.get(), Colors.WHITE.toBuffer(), align); + posY.addAndGet(shiftLineBy); }); } - /* - posY += (Render2DUtils.getTextHeight() + 1) * 2; - Render2DUtils.drawTextShadow(String.format("Pitch: %.4f", MC.thePlayer.rotationPitch), posX, posY, Utils.toRGBA(255, 255, 255, 255)); - posY += Render2DUtils.getTextHeight() + 1; - Render2DUtils.drawTextShadow(String.format("Yaw: %.4f", MC.thePlayer.rotationYaw), posX, posY, Utils.toRGBA(255, 255, 255, 255));*/ } private enum SortMode { diff --git a/src/main/java/com/matt/forgehax/util/draw/SurfaceHelper.java b/src/main/java/com/matt/forgehax/util/draw/SurfaceHelper.java index 1dbd94ec8..62dc4c617 100644 --- a/src/main/java/com/matt/forgehax/util/draw/SurfaceHelper.java +++ b/src/main/java/com/matt/forgehax/util/draw/SurfaceHelper.java @@ -5,6 +5,7 @@ import com.matt.forgehax.Globals; import com.matt.forgehax.Helper; import com.matt.forgehax.util.draw.font.MinecraftFontRenderer; +import com.matt.forgehax.util.math.AlignHelper; import javax.annotation.Nullable; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; @@ -177,7 +178,12 @@ public static void drawTextShadowCentered(String msg, float x, float y, int colo float offsetY = getTextHeight() / 2f; MC.fontRenderer.drawStringWithShadow(msg, x - offsetX, y - offsetY, color); } - + + public static void drawTextShadowAlignH(String msg, int x, int y, int color, int alignmask) { + final int offsetX = getTextWidth(msg) * AlignHelper.getPosX(alignmask) / 2; + MC.fontRenderer.drawStringWithShadow(msg, x - offsetX, y, color); + } + public static void drawText(String msg, int x, int y, int color, double scale, boolean shadow) { GlStateManager.pushMatrix(); GlStateManager.disableDepth(); diff --git a/src/main/java/com/matt/forgehax/util/math/AlignHelper.java b/src/main/java/com/matt/forgehax/util/math/AlignHelper.java new file mode 100644 index 000000000..60448f49c --- /dev/null +++ b/src/main/java/com/matt/forgehax/util/math/AlignHelper.java @@ -0,0 +1,59 @@ +package com.matt.forgehax.util.math; + +public class AlignHelper { + public enum Align { + TOPLEFT, TOP, TOPRIGHT, + CENTERLEFT, CENTER, CENTERRIGHT, + BOTTOMLEFT, BOTTOM, BOTTOMRIGHT + } + /* + Horizontal + 0 1 2 % 3 = 0,1,2 + 3 4 5 % 3 = 0,1,2 + 6 7 8 % 3 = 0,1,2 + Vertical + 0 1 2 // 3 = 0,0,0 + 3 4 5 // 3 = 1,1,1 + 6 7 8 // 3 = 2,2,2 + */ + + // returns either left or right (text flow) direction. Middle is considered to continue right + public static int getFlowDirX2(Align mask) { return getFlowDirX2(mask.ordinal()); } + public static int getFlowDirX2(int mask) { return mask % 3 < 2 ? 1 : -1; } + public static int getFlowDirY2(Align mask) { return getFlowDirY2(mask.ordinal()); } + public static int getFlowDirY2(int mask) { return mask / 3 < 2 ? 1 : -1; } + + public static int getPosX(int mask) { return mask % 3; } + public static int getPosY(int mask) { return mask / 3; } + + // returns direction relative to center (cartesian) + public static int getSignumX(int mask) { return mask % 3 - 1; } + public static int getSignumY(int mask) { return mask / 3 - 1; } + + public static boolean isLeft(Align mask) { return isLeft(mask.ordinal()); } + public static boolean isLeft(int mask) { return mask % 3 == 0; } + + public static boolean isMiddleH(Align mask) { return isMiddleH(mask.ordinal()); } + public static boolean isMiddleH(int mask) { return mask % 3 == 1; } + + public static boolean isRight(Align mask) { return isRight(mask.ordinal()); } + public static boolean isRight(int mask) { return mask % 3 == 2; } + + public static boolean isTop(Align mask) { return isTop(mask.ordinal()); } + public static boolean isTop(int mask) { return mask / 3 == 0; } + + public static boolean isMiddleV(Align mask) { return isMiddleV(mask.ordinal()); } + public static boolean isMiddleV(int mask) { return mask / 3 == 1; } + + public static boolean isBottom(Align mask) { return mask.ordinal() / 3 == 2; } + public static boolean isBottom(int mask) { return mask / 3 == 2; } + + public static boolean isCenter(Align mask) { return isMiddleH(mask.ordinal()) && isMiddleV(mask.ordinal()); } + + public static boolean isTopLeft(Align mask) { return isTop(mask.ordinal()) && isLeft(mask.ordinal()); } + + public static boolean isTopRight(Align mask) { return isTop(mask.ordinal()) && isRight(mask.ordinal()); } + + public static boolean isBottomLeft(Align mask) { return isBottom(mask.ordinal()) && isLeft(mask.ordinal()); } + + public static boolean isBottomRight(Align mask) { return isBottom(mask.ordinal()) && isRight(mask.ordinal()); } }