feat: prefix correctly depending on anchoring

This commit is contained in:
əlemi 2023-03-12 00:16:19 +01:00
parent ccb2f2eae8
commit 53e24b2e08
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 106 additions and 16 deletions

View file

@ -17,6 +17,8 @@ public abstract class HudModule extends AbstractModule {
return "HUD";
}
// TODO there should be a way for HUD mods to specify their defaults for x/y and anchor settings
protected HudModule() {
super();
this.x = Setting.Number.builder()
@ -44,6 +46,13 @@ public abstract class HudModule extends AbstractModule {
.build(this);
}
protected String affixed(String in, Object... args) {
Anchor anchor = this.anchor.get();
String prefix = anchor.isLeft() ? "> " : "";
String postfix = anchor.isRight() ? " <" : "";
return String.format(prefix + in + postfix, args);
}
protected boolean shouldHide() {
return ICommons.MC.options.renderDebug;
}

View file

@ -2,7 +2,6 @@ package ftbsc.bscv.modules.hud;
import com.google.auto.service.AutoService;
import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.api.IModule;
import ftbsc.bscv.modules.HudModule;
@ -13,7 +12,7 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import static ftbsc.bscv.tools.Text.TextBuilder;
@AutoService(ILoadable.class)
public class ActiveModules extends HudModule implements ICommons {
public class ActiveModules extends HudModule {
@SubscribeEvent
public void onRenderOverlay(RenderGameOverlayEvent event) {
if (event.getType() != ElementType.TEXT) return;
@ -22,7 +21,7 @@ public class ActiveModules extends HudModule implements ICommons {
for (IModule m : Boscovicino.modManager.mods) {
if (m.isEnabled() && !m.getGroup().equalsIgnoreCase("HUD")) {
TextBuilder()
.txt(String.format("%s <", m.getName()))
.txt(this.affixed(m.getName()))
.anchor(this.anchor.get())
.x(this.x.get())
.y(this.y.get() + offset)

View file

@ -1,7 +1,6 @@
package ftbsc.bscv.modules.hud;
import com.google.auto.service.AutoService;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.HudModule;
import ftbsc.bscv.tools.Setting;
@ -22,7 +21,7 @@ import java.util.stream.Collectors;
import static ftbsc.bscv.tools.Text.TextBuilder;
@AutoService(ILoadable.class)
public class EntityList extends HudModule implements ICommons {
public class EntityList extends HudModule {
public final ForgeConfigSpec.ConfigValue<String> search;
@ -60,7 +59,7 @@ public class EntityList extends HudModule implements ICommons {
int offset = 0;
for (String u : uniques) {
TextBuilder()
.txt(String.format("%s", u))
.txt(this.affixed(u))
.anchor(this.anchor.get())
.x(this.x.get())
.y(this.y.get() + offset)

View file

@ -15,7 +15,6 @@ import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.TickEvent.Phase;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
@ -31,6 +30,7 @@ public class InfoDisplay extends HudModule implements ICommons {
public final ForgeConfigSpec.ConfigValue<Boolean> logo;
public final ForgeConfigSpec.ConfigValue<Boolean> speed;
public final ForgeConfigSpec.ConfigValue<Boolean> age;
public final ForgeConfigSpec.ConfigValue<Boolean> time;
public final ForgeConfigSpec.ConfigValue<Boolean> fps;
// public final ForgeConfigSpec.ConfigValue<Boolean> biome;
@ -60,6 +60,12 @@ public class InfoDisplay extends HudModule implements ICommons {
.fallback(true)
.build(this);
this.age = Setting.Bool.builder()
.name("age")
.comment("show age of the world")
.fallback(true)
.build(this);
this.time = Setting.Bool.builder()
.name("time")
.comment("show world time")
@ -98,6 +104,11 @@ public class InfoDisplay extends HudModule implements ICommons {
double buf = 0.0;
for (double v : this.history_speed) { buf += v; }
this.average_speed = buf / this.history_speed.size();
if (this.last_fps_string != MC.fpsString) {
this.last_fps_string = MC.fpsString;
this.curr_fps = this.last_fps_string.split(" ")[0];
}
}
@SubscribeEvent
@ -125,13 +136,16 @@ public class InfoDisplay extends HudModule implements ICommons {
offset += MC.font.lineHeight * scale * 4.0;
}
if (this.time.get()) {
long daytime = 0;
if (MC.level != null) {
daytime = MC.level.dayTime();
}
long day = 0;
long time = 0;
if (MC.level != null) {
day = MC.level.dayTime() / 24000L;
time = MC.level.dayTime() % 24000L;
}
if (this.fps.get()) {
TextBuilder()
.txt(String.format("> time: %d/1200 (%d day)", (daytime / 20) % 1200, daytime / (20 * 1200) ))
.txt(this.affixed("fps: %s", this.curr_fps))
.anchor(this.anchor.get())
.x(this.x.get())
.y(this.y.get() + offset)
@ -142,7 +156,7 @@ public class InfoDisplay extends HudModule implements ICommons {
if (this.speed.get()) {
TextBuilder()
.txt(String.format("> speed: %.1f [%.1f] m/s", this.instant_speed * 20.0, this.average_speed * 20.0))
.txt(this.affixed("speed: %.1f [%.1f] m/s", this.instant_speed * 20.0, this.average_speed * 20.0))
.anchor(this.anchor.get())
.x(this.x.get())
.y(this.y.get() + offset)
@ -151,9 +165,20 @@ public class InfoDisplay extends HudModule implements ICommons {
offset += MC.font.lineHeight * scale;
}
if (this.fps.get()) {
if (this.age.get()) {
TextBuilder()
.txt("> " + MC.fpsString)
.txt(this.affixed("age: %d (~%d days)", day, day / (3 * 24) )) // 3 mc days last 1 hour
.anchor(this.anchor.get())
.x(this.x.get())
.y(this.y.get() + offset)
.scale(scale)
.render(event.getMatrixStack(), event.getWindow());
offset += MC.font.lineHeight * scale;
}
if (this.time.get()) {
TextBuilder()
.txt(this.affixed("time: %d/%d (%s)", (time / TPS), this.getNextStep(time) / TPS, this.getTimePhase(time) ))
.anchor(this.anchor.get())
.x(this.x.get())
.y(this.y.get() + offset)
@ -162,4 +187,28 @@ public class InfoDisplay extends HudModule implements ICommons {
offset += MC.font.lineHeight * scale;
}
}
private String last_fps_string;
private String curr_fps = "0";
// Time utils
private String getTimePhase(long time) {
if (time > 23000) return "Dawn";
if (time > 18500) return "Night";
if (time > 17500) return "Midnight";
if (time > 13000) return "Evening";
if (time > 12000) return "Dusk";
if (time > 6500) return "Afternoon";
if (time > 5500) return "Noon";
return "Morning";
}
private int getNextStep(long time) {
if (time > 23000) return 24000;
if (time > 13000) return 23000;
if (time > 12000) return 13000;
return 12000;
}
private final int TPS = 20;
}

View file

@ -13,6 +13,40 @@ public enum Anchor implements ICommons {
private Anchor(String in) { }
public boolean isLeft() {
switch (this) {
case BOTTOMLEFT:
case MIDDLELEFT:
case TOPLEFT:
return true;
case BOTTOMRIGHT:
case MIDDLERIGHT:
case TOPRIGHT:
case BOTTOMCENTER:
case MIDDLECENTER:
case TOPCENTER:
default:
return false;
}
}
public boolean isRight() {
switch (this) {
case BOTTOMRIGHT:
case MIDDLERIGHT:
case TOPRIGHT:
return true;
case BOTTOMLEFT:
case MIDDLELEFT:
case TOPLEFT:
case BOTTOMCENTER:
case MIDDLECENTER:
case TOPCENTER:
default:
return false;
}
}
public Vector2f translate(Vector2f in, int textWidth, int lineHeight, float scale, MainWindow window) {
int offset = 0;
switch (this) {