diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-02-17 19:16:30 +0200 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-02-17 19:16:30 +0200 |
commit | 5b23e672dc87c8643d6025cba9d5a29bce17eb2e (patch) | |
tree | d49eb1afcb7da7ede519e0ca67ff1ce16ec54f5c | |
parent | a799905e5e8a88bff003ebc5e9ed442af11c5598 (diff) | |
download | LibGui-5b23e672dc87c8643d6025cba9d5a29bce17eb2e.tar.gz LibGui-5b23e672dc87c8643d6025cba9d5a29bce17eb2e.tar.bz2 LibGui-5b23e672dc87c8643d6025cba9d5a29bce17eb2e.zip |
Simplify the HUD API
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java | 84 |
1 files changed, 22 insertions, 62 deletions
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<WWidget> widgets = new HashSet<>(); - private final Map<WWidget, Positioner> 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<WWidget, Position> widgets = new HashMap<>(); /** * Adds a new widget to the HUD at the specified offsets. * + * <p>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. * + * <p>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<WWidget, Position> 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. - * - * <p>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; } } } |