Add option to change the hud position

This commit is contained in:
Sollace 2024-12-20 15:53:36 +01:00
parent ed15923f28
commit 449862ca7e
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
5 changed files with 138 additions and 11 deletions

View file

@ -7,6 +7,7 @@ import com.google.gson.GsonBuilder;
import com.minelittlepony.common.util.GamePaths; import com.minelittlepony.common.util.GamePaths;
import com.minelittlepony.common.util.registry.RegistryTypeAdapter; import com.minelittlepony.common.util.registry.RegistryTypeAdapter;
import com.minelittlepony.common.util.settings.*; import com.minelittlepony.common.util.settings.*;
import com.minelittlepony.unicopia.client.gui.HudPosition;
public class Config extends com.minelittlepony.common.util.settings.Config { public class Config extends com.minelittlepony.common.util.settings.Config {
public final Setting<Set<String>> speciesWhiteList = value("server", "speciesWhiteList", (Set<String>)new HashSet<String>()) public final Setting<Set<String>> speciesWhiteList = value("server", "speciesWhiteList", (Set<String>)new HashSet<String>())
@ -42,6 +43,9 @@ public class Config extends com.minelittlepony.common.util.settings.Config {
public final Setting<Integer> hudPage = value("client", "hudActivePage", 0) public final Setting<Integer> hudPage = value("client", "hudActivePage", 0)
.addComment("The page of abilities currently visible in the HUD. You can change this in-game using the PG_UP and PG_DWN keys (configurable)"); .addComment("The page of abilities currently visible in the HUD. You can change this in-game using the PG_UP and PG_DWN keys (configurable)");
public final Setting<HudPosition> hudPosition = value("client", "hudPosition", HudPosition.MAIN_HAND)
.addComment("Location of the HUD on-screen");
public final Setting<Boolean> disableWaterPlantsFix = value("compatibility", "disableWaterPlantsFix", false) public final Setting<Boolean> disableWaterPlantsFix = value("compatibility", "disableWaterPlantsFix", false)
.addComment("Disables this mod's built in fix for making sea plants waterlogged") .addComment("Disables this mod's built in fix for making sea plants waterlogged")
.addComment("Turn this ON if you're using another mod that does something similar of if you encounter copatibility issues with other mods."); .addComment("Turn this ON if you're using another mod that does something similar of if you encounter copatibility issues with other mods.");

View file

@ -0,0 +1,73 @@
package com.minelittlepony.unicopia.client.gui;
import net.minecraft.util.math.MathHelper;
public enum HudPosition {
MAIN_HAND(Alignment.UNSET, Alignment.UNSET),
OFF_HAND(Alignment.UNSET, Alignment.UNSET),
TOP_LEFT(Alignment.START, Alignment.START),
TOP_CENTER(Alignment.MIDDLE, Alignment.START),
TOP_RIGHT(Alignment.END, Alignment.START),
CENTER_LEFT(Alignment.START, Alignment.MIDDLE),
CENTER_CENTER(Alignment.MIDDLE, Alignment.MIDDLE),
CENTER_RIGHT(Alignment.END, Alignment.MIDDLE),
BOTTOM_LEFT(Alignment.START, Alignment.END),
BOTTOM_CENTER(Alignment.MIDDLE, Alignment.END),
BOTTOM_RIGHT(Alignment.END, Alignment.END);
private final Alignment horizontal;
private final Alignment vertical;
HudPosition(Alignment horizontal, Alignment vertical) {
this.horizontal = horizontal;
this.vertical = vertical;
}
public Alignment getHorizontal() {
return horizontal;
}
public Alignment getVertical() {
return vertical;
}
public enum Alignment {
UNSET,
START,
MIDDLE,
END;
public int getSignum() {
return this == UNSET ? 0 : this == START ? -1 : 1;
}
public Alignment opposite() {
return (this == UNSET || this == MIDDLE) ? this : this == START ? END : START;
}
public Alignment or(Alignment fallback) {
return this == UNSET ? fallback : this;
}
public int pick(int start, int middle, int end, int unset) {
return switch(this) {
case UNSET -> unset;
case START -> start;
case MIDDLE -> middle;
case END -> end;
};
}
public int pick(int start, int end, int unset) {
return switch(this) {
case UNSET -> unset;
case START -> start;
case MIDDLE -> MathHelper.lerp(0.5F, start, end);
case END -> end;
};
}
}
}

View file

@ -18,7 +18,9 @@ import net.minecraft.client.gui.screen.Screen;
import net.minecraft.server.integrated.IntegratedServer; import net.minecraft.server.integrated.IntegratedServer;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Arm;
import net.minecraft.util.Formatting; import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;
public class SettingsScreen extends GameGui { public class SettingsScreen extends GameGui {
private final Config config = Unicopia.getConfig(); private final Config config = Unicopia.getConfig();
@ -28,6 +30,8 @@ public class SettingsScreen extends GameGui {
@Nullable @Nullable
private Style mineLpStatus; private Style mineLpStatus;
private long drawingHudOutline;
public SettingsScreen(Screen parent) { public SettingsScreen(Screen parent) {
super(Text.translatable("unicopia.options.title"), parent); super(Text.translatable("unicopia.options.title"), parent);
@ -68,16 +72,25 @@ public class SettingsScreen extends GameGui {
}) })
.getStyle().setText("unicopia.options.ignore_mine_lp"); .getStyle().setText("unicopia.options.ignore_mine_lp");
row += 10;
mineLpStatus = content.addButton(new Label(LEFT, row += 10)).getStyle().setText(getMineLPStatus());
content.addButton(new Toggle(LEFT, row += 20, config.toggleAbilityKeys.get())) content.addButton(new Toggle(LEFT, row += 20, config.toggleAbilityKeys.get()))
.onChange(config.toggleAbilityKeys) .onChange(config.toggleAbilityKeys)
.getStyle().setText("unicopia.options.toggle_ability_keys"); .getStyle().setText("unicopia.options.toggle_ability_keys");
mineLpStatus = content.addButton(new Label(LEFT, row += 10)).getStyle().setText(getMineLPStatus()); content.addButton(new EnumSlider<>(LEFT, row += 20, config.hudPosition))
.onChange(v -> {
drawingHudOutline = System.currentTimeMillis() + 1000L;
return config.hudPosition.set(v);
})
.setTextFormat(v -> Text.translatable("unicopia.options.hud_position", v.getValue().name()));
RegistryIndexer<Race> races = RegistryIndexer.of(Race.REGISTRY); RegistryIndexer<Race> races = RegistryIndexer.of(Race.REGISTRY);
content.addButton(new Slider(LEFT, row += 25, 0, races.size(), races.indexOf(config.preferredRace.get()))) content.addButton(new Slider(LEFT, row += 25, 0, races.size(), races.indexOf(config.preferredRace.get())))
.onChange(races.createSetter(config.preferredRace::set)) .onChange(races.createSetter(config.preferredRace))
.setTextFormat(v -> Text.translatable("unicopia.options.preferred_race", races.valueOf(v.getValue()).getDisplayName())); .setTextFormat(v -> Text.translatable("unicopia.options.preferred_race", races.valueOf(v.getValue()).getDisplayName()));
IntegratedServer server = client.getServer(); IntegratedServer server = client.getServer();
@ -112,6 +125,31 @@ public class SettingsScreen extends GameGui {
public void render(DrawContext context, int mouseX, int mouseY, float tickDelta) { public void render(DrawContext context, int mouseX, int mouseY, float tickDelta) {
super.render(context, mouseX, mouseY, tickDelta); super.render(context, mouseX, mouseY, tickDelta);
content.render(context, mouseX, mouseY, tickDelta); content.render(context, mouseX, mouseY, tickDelta);
if (drawingHudOutline > System.currentTimeMillis()) {
int scaledWidth = client.getWindow().getScaledWidth();
int scaledHeight = client.getWindow().getScaledHeight();
HudPosition selectedPos = config.hudPosition.get();
for (var hudPos : HudPosition.values()) {
HudPosition.Alignment armAlignment = client.options.getMainArm().getValue() == Arm.LEFT ? HudPosition.Alignment.START : HudPosition.Alignment.END;
if (hudPos == HudPosition.OFF_HAND) {
armAlignment = armAlignment.opposite();
}
int left = hudPos.getHorizontal().pick(2, scaledWidth - 50, ((scaledWidth - 50) / 2) + (109 * armAlignment.getSignum()));
int top = hudPos.getVertical().pick(12, scaledHeight - 50, scaledHeight - 50);
if (hudPos == HudPosition.BOTTOM_CENTER) {
top -= 22;
}
context.getMatrices().push();
context.getMatrices().translate(left + UHud.PRIMARY_SLOT_SIZE / 2, top + UHud.PRIMARY_SLOT_SIZE / 2, 0);
DrawableUtil.drawArc(context.getMatrices(), 0, UHud.PRIMARY_SLOT_SIZE / 2, 0, MathHelper.TAU, hudPos == selectedPos ? 0xEEFF00A5 : 0xEEFFFF25);
context.getMatrices().pop();
}
}
} }
@Override @Override

View file

@ -97,7 +97,14 @@ public class UHud {
} }
font = client.textRenderer; font = client.textRenderer;
xDirection = client.player.getMainArm() == Arm.LEFT ? -1 : 1;
HudPosition hudPos = Unicopia.getConfig().hudPosition.get();
HudPosition.Alignment armAlignment = client.player.getMainArm() == Arm.LEFT ? HudPosition.Alignment.START : HudPosition.Alignment.END;
if (hudPos == HudPosition.OFF_HAND) {
armAlignment = armAlignment.opposite();
}
xDirection = hudPos.getHorizontal().or(armAlignment.opposite()).opposite().getSignum();
matrices.push(); matrices.push();
matrices.translate(scaledWidth / 2, scaledHeight / 2, 0); matrices.translate(scaledWidth / 2, scaledHeight / 2, 0);
@ -111,11 +118,13 @@ public class UHud {
matrices.pop(); matrices.pop();
matrices.push(); matrices.push();
int hudX = ((scaledWidth - 50) / 2) + (109 * xDirection); int hudX = hudPos.getHorizontal().pick(2, scaledWidth - 50, ((scaledWidth - 50) / 2) + (109 * armAlignment.getSignum()));
int hudY = scaledHeight - 50; int hudY = hudPos.getVertical().pick(12, scaledHeight - 50, scaledHeight - 50);
if (hudPos == HudPosition.BOTTOM_CENTER) {
hudY -= 22;
}
int hudZ = hotbarZ; int hudZ = hotbarZ;
float exhaustion = pony.getMagicalReserves().getExhaustion().getPercentFill(); float exhaustion = pony.getMagicalReserves().getExhaustion().getPercentFill();
if (exhaustion > 0.5F || EquinePredicates.RAGING.test(client.player)) { if (exhaustion > 0.5F || EquinePredicates.RAGING.test(client.player)) {
@ -205,12 +214,14 @@ public class UHud {
int progress = Math.min(255, (int)(time * 255F / 20F)); int progress = Math.min(255, (int)(time * 255F / 20F));
if (progress > 8) { if (progress > 8) {
int color = Colors.WHITE; int color = ColorHelper.Argb.withAlpha(progress, Colors.WHITE);
int alpha = progress << 24 & -16777216;
color |= alpha; HudPosition hudPos = Unicopia.getConfig().hudPosition.get();
context.drawCenteredTextWithShadow(font, message, 25, -15, color); int messageWidth = font.getWidth(message);
int messageX = hudPos.getHorizontal().pick(0, 25 - messageWidth/2, -messageWidth + 45, 25 - messageWidth/2);
int messageY = hudPos.getVertical().pick(55, -17, -17, -17);
context.drawText(font, message, messageX, messageY, color, true);
} }
} }
@ -374,7 +385,7 @@ public class UHud {
} }
public void tick() { public void tick() {
if (messageTime > 0) { if (!client.isPaused() && messageTime > 0) {
messageTime--; messageTime--;
} }
} }

View file

@ -1568,6 +1568,7 @@
"unicopia.options.ignore_mine_lp.detected": "* Your detected race is %s", "unicopia.options.ignore_mine_lp.detected": "* Your detected race is %s",
"unicopia.options.ignore_mine_lp.undetected": "* We will not use your detected race", "unicopia.options.ignore_mine_lp.undetected": "* We will not use your detected race",
"unicopia.options.preferred_race": "Preferred Race: %s", "unicopia.options.preferred_race": "Preferred Race: %s",
"unicopia.options.hud_position": "HUD Position: %s",
"unicopia.options.whitelist": "Enable Whitelist", "unicopia.options.whitelist": "Enable Whitelist",
"unicopia.options.whitelist.race": "Allow %ss", "unicopia.options.whitelist.race": "Allow %ss",
"unicopia.options.whitelist.details": "* Select the races you wish to allow", "unicopia.options.whitelist.details": "* Select the races you wish to allow",