Skip to content

Commit

Permalink
Title hud refactor (#951)
Browse files Browse the repository at this point in the history
* Refactor TitleContainer and fix bugs

* Sync with hud refactor

* Update translatable key checks

* TABS

* Remove extra renderBackground

* Add EnumUtils

* Migrate to EnumUtils
  • Loading branch information
kevinthegreat1 authored Dec 30, 2024
1 parent 4434668 commit 145ba09
Show file tree
Hide file tree
Showing 11 changed files with 487 additions and 503 deletions.
253 changes: 132 additions & 121 deletions src/main/java/de/hysky/skyblocker/config/HudConfigScreen.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.hysky.skyblocker.config;

import de.hysky.skyblocker.skyblock.tabhud.widget.HudWidget;
import de.hysky.skyblocker.utils.render.RenderHelper;
import de.hysky.skyblocker.utils.render.gui.AbstractWidget;
import it.unimi.dsi.fastutil.ints.IntIntMutablePair;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
Expand All @@ -13,129 +13,140 @@
/**
* A screen for configuring the positions of HUD widgets.
* <p>
* Note: This is currently only used for title container. There is a new system for other HUD widgets, see {@link de.hysky.skyblocker.skyblock.tabhud.config.WidgetsConfigurationScreen}
* <p>
* This class takes care of rendering the widgets, dragging them, and resetting their positions.
* Create one subclass for each collection of HUD widgets that are displayed at the same time.
* (i.e. one for dwarven mines, one for the end, etc.) See an implementation for an example.
*/
public abstract class HudConfigScreen extends Screen {
private final Screen parent;
private final List<HudWidget> widgets;

private HudWidget draggingWidget;
private double mouseClickRelativeX;
private double mouseClickRelativeY;

/**
* Creates a new HudConfigScreen with the passed title, parent, and widget
* @param title the title of the screen
* @param parent the parent screen
* @param widget the widget to configure
*/
public HudConfigScreen(Text title, Screen parent, HudWidget widget) {
this(title, parent, List.of(widget));
}

/**
* Creates a new HudConfigScreen with the passed title, parent, and widgets
* @param title the title of the screen
* @param parent the parent screen
* @param widgets the widgets to configure
*/
public HudConfigScreen(Text title, Screen parent, List<HudWidget> widgets) {
super(title);
this.parent = parent;
this.widgets = widgets;
resetPos();
}

@Override
public final void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
renderWidget(context, widgets);
context.drawCenteredTextWithShadow(textRenderer, "Right Click To Reset Position", width / 2, height / 2, Color.GRAY.getRGB());
}

/**
* Renders the widgets using the default {@link HudWidget#render(DrawContext)} method. Override to change the behavior.
* @param context the context to render in
* @param widgets the widgets to render
*/
protected void renderWidget(DrawContext context, List<HudWidget> widgets) {
for (HudWidget widget : widgets) {
widget.render(context);
}
}

@Override
public final boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (button == 0 && draggingWidget != null) {
draggingWidget.setX((int) Math.clamp(mouseX - mouseClickRelativeX, 0, this.width - draggingWidget.getWidth()));
draggingWidget.setY((int) Math.clamp(mouseY - mouseClickRelativeY, 0, this.height - draggingWidget.getHeight()));
}
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
}

@Override
public final boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) {
for (HudWidget widget : widgets) {
if (RenderHelper.pointIsInArea(mouseX, mouseY, widget.getX(), widget.getY(), widget.getX() + widget.getWidth(), widget.getY() + widget.getHeight())) {
draggingWidget = widget;
mouseClickRelativeX = mouseX - widget.getX();
mouseClickRelativeY = mouseY - widget.getY();
break;
}
}
} else if (button == 1) {
resetPos();
}
return super.mouseClicked(mouseX, mouseY, button);
}

@Override
public final boolean mouseReleased(double mouseX, double mouseY, int button) {
draggingWidget = null;
return super.mouseReleased(mouseX, mouseY, button);
}

/**
* Resets the positions of the widgets to the positions in the config. Override to change the behavior.
*/
protected void resetPos() {
List<IntIntMutablePair> configPositions = getConfigPos(SkyblockerConfigManager.get());
if (configPositions.size() != widgets.size()) {
throw new IllegalStateException("The number of positions (" + configPositions.size() + ") does not match the number of widgets (" + widgets.size() + ")");
}
for (int i = 0; i < widgets.size(); i++) {
HudWidget widget = widgets.get(i);
IntIntMutablePair configPos = configPositions.get(i);
widget.setX(configPos.leftInt());
widget.setY(configPos.rightInt());
}
}

/**
* Returns the positions of the widgets in the config
* @param config the config to get the positions from
* @return the positions of the widgets
*/
protected abstract List<IntIntMutablePair> getConfigPos(SkyblockerConfig config);

@Override
public final void close() {
SkyblockerConfig skyblockerConfig = SkyblockerConfigManager.get();
savePos(skyblockerConfig, widgets);
SkyblockerConfigManager.save();

client.setScreen(parent);
}

/**
* Saves the passed positions to the config.
* <p>
* NOTE: The parent class will call {@link SkyblockerConfigManager#save()} right after this method
* @param configManager the config so you don't have to get it
* @param widgets the widgets to save
*/
protected abstract void savePos(SkyblockerConfig configManager, List<HudWidget> widgets);
protected final Screen parent;
protected final List<AbstractWidget> widgets;

private AbstractWidget draggingWidget;
private double mouseClickRelativeX;
private double mouseClickRelativeY;

/**
* Creates a new HudConfigScreen with the passed title, parent, and widget
*
* @param title the title of the screen
* @param parent the parent screen
* @param widget the widget to configure
*/
public HudConfigScreen(Text title, Screen parent, AbstractWidget widget) {
this(title, parent, List.of(widget));
}

/**
* Creates a new HudConfigScreen with the passed title, parent, and widgets
*
* @param title the title of the screen
* @param parent the parent screen
* @param widgets the widgets to configure
*/
public HudConfigScreen(Text title, Screen parent, List<AbstractWidget> widgets) {
super(title);
this.parent = parent;
this.widgets = widgets;
resetPos();
}

@Override
public final void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
renderWidget(context, widgets, delta);
context.drawCenteredTextWithShadow(textRenderer, "Right Click To Reset Position", width / 2, height / 2, Color.GRAY.getRGB());
}

/**
* Renders the widgets using the default {@link AbstractWidget#render(DrawContext, int, int, float)} method. Override to change the behavior.
*
* @param context the context to render in
* @param widgets the widgets to render
*/
protected void renderWidget(DrawContext context, List<AbstractWidget> widgets, float delta) {
for (AbstractWidget widget : widgets) {
widget.render(context, -1, -1, delta);
}
}

@Override
public final boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
if (button == 0 && draggingWidget != null) {
draggingWidget.setX((int) Math.clamp(mouseX - mouseClickRelativeX, 0, this.width - draggingWidget.getWidth()) - getWidgetXOffset(draggingWidget));
draggingWidget.setY((int) Math.clamp(mouseY - mouseClickRelativeY, 0, this.height - draggingWidget.getHeight()));
}
return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
}

@Override
public final boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) {
for (AbstractWidget widget : widgets) {
if (RenderHelper.pointIsInArea(mouseX, mouseY, widget.getX() + getWidgetXOffset(widget), widget.getY(), widget.getX() + getWidgetXOffset(widget) + widget.getWidth(), widget.getY() + widget.getHeight())) {
draggingWidget = widget;
mouseClickRelativeX = mouseX - widget.getX() - getWidgetXOffset(widget);
mouseClickRelativeY = mouseY - widget.getY();
break;
}
}
} else if (button == 1) {
resetPos();
}
return super.mouseClicked(mouseX, mouseY, button);
}

@Override
public final boolean mouseReleased(double mouseX, double mouseY, int button) {
draggingWidget = null;
return super.mouseReleased(mouseX, mouseY, button);
}

protected int getWidgetXOffset(AbstractWidget widget) {
return 0;
}

/**
* Resets the positions of the widgets to the positions in the config. Override to change the behavior.
*/
protected void resetPos() {
List<IntIntMutablePair> configPositions = getConfigPos(SkyblockerConfigManager.get());
if (configPositions.size() != widgets.size()) {
throw new IllegalStateException("The number of positions (" + configPositions.size() + ") does not match the number of widgets (" + widgets.size() + ")");
}
for (int i = 0; i < widgets.size(); i++) {
AbstractWidget widget = widgets.get(i);
IntIntMutablePair configPos = configPositions.get(i);
widget.setX(configPos.leftInt());
widget.setY(configPos.rightInt());
}
}

/**
* Returns the positions of the widgets in the config
*
* @param config the config to get the positions from
* @return the positions of the widgets
*/
protected abstract List<IntIntMutablePair> getConfigPos(SkyblockerConfig config);

@Override
public final void close() {
SkyblockerConfig skyblockerConfig = SkyblockerConfigManager.get();
savePos(skyblockerConfig, widgets);
SkyblockerConfigManager.save();

client.setScreen(parent);
}

/**
* Saves the passed positions to the config.
* <p>
* NOTE: The parent class will call {@link SkyblockerConfigManager#save()} right after this method
*
* @param configManager the config so you don't have to get it
* @param widgets the widgets to save
*/
protected abstract void savePos(SkyblockerConfig configManager, List<AbstractWidget> widgets);
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ public static class TitleContainer {
public int y = 10;

@SerialEntry
public Direction direction = Direction.HORIZONTAL;
public Direction direction = Direction.VERTICAL;

@SerialEntry
public Alignment alignment = Alignment.MIDDLE;

public float getRenderScale() {
return titleContainerScale * 0.03f;
}
}

public enum Direction {
Expand All @@ -147,7 +151,7 @@ public String toString() {
}

public enum Alignment {
LEFT, RIGHT, MIDDLE;
LEFT, MIDDLE, RIGHT;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public DungeonMapConfigScreen(Screen parent) {
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context, mouseX, mouseY, delta);
renderBackground(context, mouseX, mouseY, delta);
renderHUDMap(context, mapX, mapY);
renderHUDScore(context, scoreX, scoreY);
context.drawCenteredTextWithShadow(textRenderer, "Right Click To Reset Position", width >> 1, height >> 1, Color.GRAY.getRGB());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.hysky.skyblocker.skyblock.fancybars;

import de.hysky.skyblocker.utils.EnumUtils;
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
Expand Down Expand Up @@ -211,7 +212,7 @@ public void setCurrent(T current) {

@Override
public void onClick(double mouseX, double mouseY) {
current = values[(current.ordinal() + 1) % values.length];
current = EnumUtils.cycle(current);
if (onChange != null) onChange.accept(current);
super.onClick(mouseX, mouseY);
}
Expand Down Expand Up @@ -334,4 +335,4 @@ protected int getContentsHeightWithPadding() {
protected double getDeltaYPerScroll() {
return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.hysky.skyblocker.skyblock.item;

import com.mojang.serialization.Codec;
import de.hysky.skyblocker.utils.EnumUtils;
import net.minecraft.util.Formatting;
import net.minecraft.util.StringIdentifiable;

Expand Down Expand Up @@ -41,6 +42,6 @@ public String asString() {
}

public SkyblockItemRarity next() {
return values()[(ordinal() + 1) % values().length];
return EnumUtils.cycle(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.hysky.skyblocker.skyblock.tabhud.util.PlayerListMgr;
import de.hysky.skyblocker.skyblock.tabhud.widget.HudWidget;
import de.hysky.skyblocker.skyblock.tabhud.widget.TabHudWidget;
import de.hysky.skyblocker.utils.EnumUtils;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Location;
import de.hysky.skyblocker.utils.render.gui.DropdownWidget;
Expand Down Expand Up @@ -307,8 +308,7 @@ void onHudWidgetSelected(@Nullable HudWidget hudWidget) {
widgetOptions.addWidget(ButtonWidget.builder(Text.literal(ye), button -> {
ScreenBuilder builder = ScreenMaster.getScreenBuilder(getCurrentLocation());
PositionRule rule = builder.getPositionRuleOrDefault(hudWidget.getInternalID());
ScreenMaster.ScreenLayer[] values = ScreenMaster.ScreenLayer.values();
ScreenMaster.ScreenLayer newLayer = values[(rule.screenLayer().ordinal() + 1) % values.length];
ScreenMaster.ScreenLayer newLayer = EnumUtils.cycle(rule.screenLayer());

PositionRule newRule = new PositionRule(
rule.parent(),
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/de/hysky/skyblocker/utils/EnumUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.hysky.skyblocker.utils;

public class EnumUtils {
public static <T extends Enum<T>> T cycle(T current) {
T[] values = current.getDeclaringClass().getEnumConstants();
return values[(current.ordinal() + 1) % values.length];
}

public static <T extends Enum<T>> T cycleBackwards(T current) {
T[] values = current.getDeclaringClass().getEnumConstants();
return values[(current.ordinal() - 1 + values.length) % values.length];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.hysky.skyblocker.utils.render.gui;

import net.minecraft.client.gui.DrawContext;

public class EmptyWidget extends AbstractWidget {
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {}
}
Loading

0 comments on commit 145ba09

Please sign in to comment.