diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java | 84 |
1 files changed, 62 insertions, 22 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 5173383..a9d1d9b 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,46 +20,64 @@ public enum CottonHud implements HudRenderCallback { HudRenderCallback.EVENT.register(INSTANCE); } - private final Map<WWidget, Position> widgets = new HashMap<>(); + private final Set<WWidget> widgets = new HashSet<>(); + private final Map<WWidget, Positioner> positioners = new HashMap<>(); /** - * Adds a new widget to the HUD at the specified offsets. + * Adds a new widget to the HUD. * - * <p>The offsets wrap around the screen. Use negative values for bottom/right edges. + * @param widget the widget + */ + public void add(WWidget widget) { + widgets.add(widget); + } + + /** + * Adds a new widget to the HUD at the specified offsets. * * @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) { - widgets.put(widget, new Position(x, y)); + add(widget, Positioner.of(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 height of the widget + * @param height the heigh of the widget + * @see Positioner#of documentation about the offsets */ public void add(WWidget widget, int x, int y, int width, int height) { - add(widget, x, y); + add(widget, Positioner.of(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 setPosition(WWidget widget, int x, int y) { - Position pos = widgets.get(widget); - pos.x = x; - pos.y = y; + public void setPositioner(WWidget widget, Positioner positioner) { + positioners.put(widget, positioner); } /** @@ -76,21 +94,43 @@ public enum CottonHud implements HudRenderCallback { Window window = MinecraftClient.getInstance().getWindow(); int hudWidth = window.getScaledWidth(); int hudHeight = window.getScaledHeight(); - 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); + for (WWidget widget : widgets) { + Positioner positioner = positioners.get(widget); + if (positioner != null) { + positioner.reposition(widget, hudWidth, hudHeight); + } + widget.paintBackground(widget.getX(), widget.getY(), -1, -1); } } - private static final class Position { - int x; - int y; + /** + * 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); - Position(int x, int y) { - this.x = x; - this.y = 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); + }; } } } |