From a43a1123fdcd88eb49276cc523713d1204215891 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Thu, 26 Nov 2020 22:32:12 +0200 Subject: Switch CottonHud to use static methods --- .../cottonmc/test/client/LibGuiTestClient.java | 2 +- .../cottonmc/cotton/gui/client/CottonHud.java | 57 ++++++++++------------ 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java index 65efe75..22a1bb8 100644 --- a/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java +++ b/GuiTest/src/main/java/io/github/cottonmc/test/client/LibGuiTestClient.java @@ -16,7 +16,7 @@ public class LibGuiTestClient implements ClientModInitializer { (desc, inventory, title) -> new CottonInventoryScreen<>(desc, inventory.player, title) ); - CottonHud.INSTANCE.add(new WHudTest(), 10, -20, 10, 10); + CottonHud.add(new WHudTest(), 10, -20, 10, 10); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java index 79111d2..f69bb65 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java @@ -6,7 +6,6 @@ import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.minecraft.client.MinecraftClient; import net.minecraft.client.util.Window; -import net.minecraft.client.util.math.MatrixStack; import io.github.cottonmc.cotton.gui.widget.WWidget; @@ -19,27 +18,40 @@ import java.util.Set; * Manages widgets that are painted on the in-game HUD. */ @Environment(EnvType.CLIENT) -public enum CottonHud implements HudRenderCallback { - INSTANCE; // TODO (4.0): Migrate from singleton to static methods +public final class CottonHud { + private CottonHud() {} + + private static final Set widgets = new HashSet<>(); + private static final Map positioners = new HashMap<>(); static { - HudRenderCallback.EVENT.register(INSTANCE); + HudRenderCallback.EVENT.register((matrices, tickDelta) -> { + Window window = MinecraftClient.getInstance().getWindow(); + int hudWidth = window.getScaledWidth(); + int hudHeight = window.getScaledHeight(); + for (WWidget widget : widgets) { + Positioner positioner = positioners.get(widget); + if (positioner != null) { + positioner.reposition(widget, hudWidth, hudHeight); + } + + widget.paint(matrices, widget.getX(), widget.getY(), -1, -1); + } + }); + ClientTickEvents.END_CLIENT_TICK.register(client -> { - for (WWidget widget : INSTANCE.widgets) { + for (WWidget widget : widgets) { widget.tick(); } }); } - private final Set widgets = new HashSet<>(); - private final Map positioners = new HashMap<>(); - /** * Adds a new widget to the HUD. * * @param widget the widget */ - public void add(WWidget widget) { + public static void add(WWidget widget) { widgets.add(widget); } @@ -51,7 +63,7 @@ public enum CottonHud implements HudRenderCallback { * @param y the y offset * @see Positioner#of documentation about the offsets */ - public void add(WWidget widget, int x, int y) { + public static void add(WWidget widget, int x, int y) { add(widget, Positioner.of(x, y)); } @@ -65,7 +77,7 @@ public enum CottonHud implements HudRenderCallback { * @param height the height of the widget * @see Positioner#of documentation about the offsets */ - public void add(WWidget widget, int x, int y, int width, int height) { + public static void add(WWidget widget, int x, int y, int width, int height) { add(widget, Positioner.of(x, y)); widget.setSize(width, height); } @@ -76,7 +88,7 @@ public enum CottonHud implements HudRenderCallback { * @param widget the widget * @param positioner the positioner */ - public void add(WWidget widget, Positioner positioner) { + public static void add(WWidget widget, Positioner positioner) { widgets.add(widget); setPositioner(widget, positioner); } @@ -89,7 +101,7 @@ public enum CottonHud implements HudRenderCallback { * @param width the width of the widget * @param height the height of the widget */ - public void add(WWidget widget, Positioner positioner, int width, int height) { + public static void add(WWidget widget, Positioner positioner, int width, int height) { widgets.add(widget); widget.setSize(width, height); setPositioner(widget, positioner); @@ -101,7 +113,7 @@ public enum CottonHud implements HudRenderCallback { * @param widget the widget * @param positioner the positioner */ - public void setPositioner(WWidget widget, Positioner positioner) { + public static void setPositioner(WWidget widget, Positioner positioner) { positioners.put(widget, positioner); } @@ -110,25 +122,10 @@ public enum CottonHud implements HudRenderCallback { * * @param widget the widget */ - public void remove(WWidget widget) { + public static void remove(WWidget widget) { widgets.remove(widget); } - @Override - public void onHudRender(MatrixStack matrices, float tickDelta) { - Window window = MinecraftClient.getInstance().getWindow(); - int hudWidth = window.getScaledWidth(); - int hudHeight = window.getScaledHeight(); - for (WWidget widget : widgets) { - Positioner positioner = positioners.get(widget); - if (positioner != null) { - positioner.reposition(widget, hudWidth, hudHeight); - } - - widget.paint(matrices, widget.getX(), widget.getY(), -1, -1); - } - } - /** * Positioners can be used to change the position of a widget based on the window dimensions. */ -- cgit