Skip to content

Commit

Permalink
Activemods: add align and offset options
Browse files Browse the repository at this point in the history
  • Loading branch information
VADemon committed Dec 10, 2019
1 parent b7537dd commit 5b40050
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 21 deletions.
69 changes: 49 additions & 20 deletions src/main/java/com/matt/forgehax/mods/ActiveModList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Align> alignment =
getCommandStub()
.builders()
.<Align>newSettingEnumBuilder()
.name("alignment")
.description("align to corner")
.defaultTo(Align.TOPLEFT)
.build();

private final Setting<Boolean> tps_meter =
getCommandStub()
.builders()
Expand Down Expand Up @@ -66,6 +78,24 @@ public class ActiveModList extends ToggleMod {
.defaultTo(SortMode.ALPHABETICAL)
.build();

private final Setting<Integer> offsetX =
getCommandStub()
.builders()
.<Integer>newSettingBuilder()
.name("x-offset")
.description("shift on X-axis")
.defaultTo(0)
.build();

private final Setting<Integer> offsetY =
getCommandStub()
.builders()
.<Integer>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");
}
Expand All @@ -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--) {
Expand All @@ -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(", ");
}
}
}
Expand All @@ -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) {
Expand All @@ -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()
Expand All @@ -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 {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/matt/forgehax/util/draw/SurfaceHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/matt/forgehax/util/math/AlignHelper.java
Original file line number Diff line number Diff line change
@@ -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()); } }

0 comments on commit 5b40050

Please sign in to comment.