From ded0634bf20af7444884b73ca4b646bab5a91e66 Mon Sep 17 00:00:00 2001 From: OutsiderH Date: Thu, 15 Feb 2024 14:51:07 +0800 Subject: [PATCH] add altitude indicator --- .../DoABarrelRollWithHud.java | 3 +- .../mixin/InGameHudMixin.java | 1 + .../renderer/AltitudeIndicator.java | 16 ++ .../renderer/Renderer.java | 156 ++++++++++++++++-- .../renderer/SpeedIndicator.java | 6 +- .../renderer/TextAlign.java | 10 +- .../utils/FlightComputer.java | 12 +- 7 files changed, 183 insertions(+), 21 deletions(-) create mode 100644 src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/AltitudeIndicator.java diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/DoABarrelRollWithHud.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/DoABarrelRollWithHud.java index 3f170a0..2bf51e8 100644 --- a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/DoABarrelRollWithHud.java +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/DoABarrelRollWithHud.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.github.outsiderh.doabarrelrollwithhud.renderer.AltitudeIndicator; import com.github.outsiderh.doabarrelrollwithhud.renderer.Renderer; import com.github.outsiderh.doabarrelrollwithhud.renderer.SpeedIndicator; import com.github.outsiderh.doabarrelrollwithhud.utils.FlightComputer; @@ -14,7 +15,7 @@ public class DoABarrelRollWithHud implements ClientModInitializer { public static final String modId = "doabarrelrollwithhud"; public static final Logger loggerSource = LoggerFactory.getLogger(modId); - public static final List renderers = List.of(new SpeedIndicator()); + public static final List renderers = List.of(new SpeedIndicator(), new AltitudeIndicator()); public static final FlightComputer fc = new FlightComputer(); @Override public void onInitializeClient() { diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/mixin/InGameHudMixin.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/mixin/InGameHudMixin.java index 3df13b4..77064b9 100644 --- a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/mixin/InGameHudMixin.java +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/mixin/InGameHudMixin.java @@ -26,6 +26,7 @@ public void afterRender(MatrixStack matrices, float tickDelta, CallbackInfo ci) return; } DoABarrelRollWithHud.fc.eval(client.player); + Renderer.updateSize(client); for (final Renderer item : DoABarrelRollWithHud.renderers) { item.render(client, matrices); } diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/AltitudeIndicator.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/AltitudeIndicator.java new file mode 100644 index 0000000..9426150 --- /dev/null +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/AltitudeIndicator.java @@ -0,0 +1,16 @@ +package com.github.outsiderh.doabarrelrollwithhud.renderer; + +import com.github.outsiderh.doabarrelrollwithhud.DoABarrelRollWithHud; +import com.github.outsiderh.doabarrelrollwithhud.utils.Vector2Int; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.util.math.MatrixStack; + +public class AltitudeIndicator extends Renderer { + @Override + public void render(MinecraftClient client, MatrixStack mat) { + Vector2Int altitudePos = new Vector2Int(hudEnd.x, (hudBegin.y + hudEnd.y) / 2); + drawTextWithFixedBox(client.textRenderer, mat, String.format("%.1f", DoABarrelRollWithHud.fc.altitudeInUnit()), color, altitudePos, TextAlign.Left, "xxxx.x"); + drawVerticalLine(mat, hudEnd.x, hudBegin.y, hudEnd.y, color); + } +} diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/Renderer.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/Renderer.java index 100f055..81d8131 100644 --- a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/Renderer.java +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/Renderer.java @@ -7,33 +7,163 @@ import net.minecraft.client.util.math.MatrixStack; public abstract class Renderer extends DrawableHelper { - protected Vector2Int hudStart = new Vector2Int(); - protected Vector2Int hudEnd = new Vector2Int(); - protected void updateSize(MinecraftClient client) { + protected static final int color = 0xFF00FF00; + protected static final Vector2Int hudBegin = new Vector2Int(); + protected static final Vector2Int hudEnd = new Vector2Int(); + public static void updateSize(MinecraftClient client) { Vector2Int windowSize = new Vector2Int(client.getWindow().getWidth(), client.getWindow().getHeight()); - hudStart.x = windowSize.x / 4; - hudStart.y = windowSize.y / 4; - hudEnd.x = hudStart.x * 3; - hudEnd.y = hudStart.y * 3; + hudBegin.x = windowSize.x / 8; + hudBegin.y = windowSize.y / 8; + hudEnd.x = hudBegin.x * 3; + hudEnd.y = hudBegin.y * 3; } protected void drawText(TextRenderer tr, MatrixStack mat, String text, int color, Vector2Int pos, TextAlign align) { switch (align) { - case Left: + case UpLeft: { break; - case Middle: + } + case Up: { pos.x -= tr.getWidth(text) / 2; break; - case Right: + } + case UpRight: { pos.x -= tr.getWidth(text); break; - default: + } + case Left: { + pos.y -= tr.fontHeight / 2; break; + } + case Central: { + pos.x -= tr.getWidth(text) / 2; + pos.y -= tr.fontHeight / 2; + break; + } + case Right: { + pos.x -= tr.getWidth(text); + pos.y -= tr.fontHeight / 2; + break; + } + case DownLeft: { + pos.y -= tr.fontHeight; + break; + } + case Down: { + pos.x -= tr.getWidth(text) / 2; + pos.y -= tr.fontHeight; + break; + } + case DownRight: { + pos.x -= tr.getWidth(text); + pos.y -= tr.fontHeight; + break; + } + default: { + break; + } } tr.draw(mat, text, pos.x, pos.y, color); } - protected void drawTextCenterY(TextRenderer tr, MatrixStack mat, String text, int color, Vector2Int pos, TextAlign align) { - pos.y -= tr.fontHeight / 2; + protected void drawTextWithFixedBox(TextRenderer tr, MatrixStack mat, String text, int color, Vector2Int pos, TextAlign align, String size) { + Vector2Int begin = new Vector2Int(); + Vector2Int end = new Vector2Int(); + switch (align) { + case UpLeft: { + begin.x = pos.x; + begin.y = pos.y; + end.x = begin.x + tr.getWidth(size); + end.y = begin.y + tr.fontHeight; + pos.x += 2; + end.x += 2; + begin.y -= 2; + break; + } + case Up: { + int width = tr.getWidth(size); + begin.x = pos.x - width / 2; + begin.y = pos.y; + end.x = begin.x + width; + end.y = begin.y + tr.fontHeight; + begin.x -= 2; + begin.y -= 2; + break; + } + case UpRight: { + begin.x = pos.x - tr.getWidth(size); + begin.y = pos.y; + end.x = pos.x; + end.y = begin.y + tr.fontHeight; + begin.x -= 2; + begin.y -= 2; + break; + } + case Left: { + begin.x = pos.x; + begin.y = pos.y - tr.fontHeight / 2; + end.x = begin.x + tr.getWidth(size); + end.y = begin.y + tr.fontHeight; + pos.x += 2; + end.x += 2; + begin.y -= 2; + break; + } + case Central: { + int width = tr.getWidth(size); + begin.x = pos.x - width / 2; + begin.y = pos.y - tr.fontHeight / 2; + end.x = begin.x + width; + end.y = begin.y + tr.fontHeight; + begin.x -= 2; + begin.y -= 2; + break; + } + case Right: { + begin.x = pos.x - tr.getWidth(size); + begin.y = pos.y - tr.fontHeight / 2; + end.x = pos.x; + end.y = begin.y + tr.fontHeight; + begin.x -= 2; + begin.y -= 2; + break; + } + case DownLeft: { + begin.x = pos.x; + begin.y = pos.y - tr.fontHeight; + end.x = begin.x + tr.getWidth(size); + end.y = pos.y; + pos.x += 2; + end.x += 2; + begin.y -= 2; + break; + } + case Down: { + int width = tr.getWidth(size); + begin.x = pos.x - width / 2; + begin.y = pos.y - tr.fontHeight; + end.x = begin.x + width; + end.y = pos.y; + begin.x -= 2; + begin.y -= 2; + break; + } + case DownRight: { + begin.x = pos.x - tr.getWidth(size); + begin.y = pos.y - tr.fontHeight; + end.x = pos.x; + end.y = pos.y; + begin.x -= 2; + begin.y -= 2; + break; + } + default: { + break; + } + } drawText(tr, mat, text, color, pos, align); + drawHorizontalLine(mat, begin.x, end.x, begin.y, color); + drawHorizontalLine(mat, begin.x, end.x, end.y, color); + drawVerticalLine(mat, begin.x, begin.y, end.y, color); + drawVerticalLine(mat, end.x, begin.y, end.y, color); } public abstract void render(MinecraftClient client, MatrixStack mat); } diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/SpeedIndicator.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/SpeedIndicator.java index a0ed885..fdf850e 100644 --- a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/SpeedIndicator.java +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/SpeedIndicator.java @@ -9,8 +9,8 @@ public class SpeedIndicator extends Renderer { @Override public void render(MinecraftClient client, MatrixStack mat) { - updateSize(client); - Vector2Int airSpeedPos = new Vector2Int((hudStart.x + hudEnd.x) / 4, (hudStart.y + hudEnd.y) / 4); - drawTextCenterY(client.textRenderer, mat, String.format("%.1f", DoABarrelRollWithHud.fc.airSpeed), 0x0000FF00, airSpeedPos, TextAlign.Right); + Vector2Int airSpeedPos = new Vector2Int(hudBegin.x, (hudBegin.y + hudEnd.y) / 2); + drawTextWithFixedBox(client.textRenderer, mat, String.format("%.1f", DoABarrelRollWithHud.fc.airSpeedInUnit()), color, airSpeedPos, TextAlign.Right, "xxx.x"); + drawVerticalLine(mat, hudBegin.x, hudBegin.y, hudEnd.y, color); } } diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/TextAlign.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/TextAlign.java index c5db126..a614d56 100644 --- a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/TextAlign.java +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/renderer/TextAlign.java @@ -1,7 +1,13 @@ package com.github.outsiderh.doabarrelrollwithhud.renderer; public enum TextAlign { + UpLeft, + Up, + UpRight, Left, - Middle, - Right + Central, + Right, + DownLeft, + Down, + DownRight } diff --git a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/utils/FlightComputer.java b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/utils/FlightComputer.java index b920a00..7995551 100644 --- a/src/client/java/com/github/outsiderh/doabarrelrollwithhud/utils/FlightComputer.java +++ b/src/client/java/com/github/outsiderh/doabarrelrollwithhud/utils/FlightComputer.java @@ -4,8 +4,16 @@ import net.minecraft.util.math.Vec3d; public class FlightComputer { - public double airSpeed; + public float airSpeed; + public float altitude; + public float airSpeedInUnit() { + return airSpeed / 1.7f * 100f; + } + public float altitudeInUnit() { + return altitude - 63f; + } public void eval(PlayerEntity player) { - airSpeed = player.getVelocity().distanceTo(Vec3d.ZERO); + airSpeed = (float)player.getVelocity().distanceTo(Vec3d.ZERO); + altitude = (float)player.getPos().y; } }