From 5b23e672dc87c8643d6025cba9d5a29bce17eb2e Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Mon, 17 Feb 2020 19:16:30 +0200 Subject: Simplify the HUD API --- .../cottonmc/cotton/gui/client/CottonHud.java | 84 ++++++---------------- 1 file changed, 22 insertions(+), 62 deletions(-) (limited to 'src/main/java') 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 a9d1d9b..5173383 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 @@ -20,64 +20,46 @@ public enum CottonHud implements HudRenderCallback { HudRenderCallback.EVENT.register(INSTANCE); } - 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) { - widgets.add(widget); - } + private final Map widgets = new HashMap<>(); /** * Adds a new widget to the HUD at the specified offsets. * + *

The offsets wrap around the screen. Use negative values for bottom/right edges. + * * @param widget the widget * @param x the x offset * @param y the y offset - * @see Positioner#of documentation about the offsets */ public void add(WWidget widget, int x, int y) { - add(widget, Positioner.of(x, y)); + widgets.put(widget, new Position(x, y)); } /** * Adds a new widget to the HUD at the specified offsets and resizes it. * + *

The offsets wrap around the screen. Use negative values for bottom/right edges. + * * @param widget the widget * @param x the x offset * @param y the y offset * @param width the width of the widget - * @param height the heigh of the widget - * @see Positioner#of documentation about the offsets + * @param height the height of the widget */ public void add(WWidget widget, int x, int y, int width, int height) { - add(widget, Positioner.of(x, y)); + add(widget, x, y); widget.setSize(width, height); } - /** - * Adds a new widget to the HUD with a custom positioner. - * - * @param widget the widget - * @param positioner the positioner - */ - public void add(WWidget widget, Positioner positioner) { - widgets.add(widget); - setPositioner(widget, positioner); - } - /** * Sets the positioner of the widget. * * @param widget the widget - * @param positioner the positioner */ - public void setPositioner(WWidget widget, Positioner positioner) { - positioners.put(widget, positioner); + public void setPosition(WWidget widget, int x, int y) { + Position pos = widgets.get(widget); + pos.x = x; + pos.y = y; } /** @@ -94,43 +76,21 @@ public enum CottonHud implements HudRenderCallback { 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); - } - + for (Map.Entry entry : widgets.entrySet()) { + WWidget widget = entry.getKey(); + Position pos = entry.getValue(); + widget.setLocation((hudWidth + pos.x) % hudWidth, (hudHeight + pos.y) % hudHeight); widget.paintBackground(widget.getX(), widget.getY(), -1, -1); } } - /** - * Positioners can be used to change the position of a widget based on the window dimensions. - */ - @FunctionalInterface - public interface Positioner { - /** - * Repositions the widget according to the HUD dimensions. - * - * @param widget the widget - * @param hudWidth the width of the HUD - * @param hudHeight the height of the HUD - */ - void reposition(WWidget widget, int hudWidth, int hudHeight); + private static final class Position { + int x; + int y; - /** - * Creates a new positioner that offsets widgets. - * - *

If an offset is negative, the offset is subtracted from the HUD dimension on that axis. - * - * @param x the x offset - * @param y the y offset - * @return an offsetting positioner - */ - static Positioner of(int x, int y) { - return (widget, hudWidth, hudHeight) -> { - widget.setLocation((hudWidth + x) % hudWidth, (hudHeight + y) % hudHeight); - }; + Position(int x, int y) { + this.x = x; + this.y = y; } } } -- cgit