From 4aadba3408fbfaf6816c6e917bfb6196bec16403 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Mon, 17 Feb 2020 19:34:31 +0200 Subject: Add horizontally centering positioner --- .../cottonmc/cotton/gui/client/CottonHud.java | 30 +++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/main') 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..6294ac6 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 @@ -51,7 +51,7 @@ public enum CottonHud implements HudRenderCallback { * @param x the x offset * @param y the y offset * @param width the width of the widget - * @param height the heigh of the widget + * @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) { @@ -70,6 +70,20 @@ public enum CottonHud implements HudRenderCallback { setPositioner(widget, positioner); } + /** + * Adds a new widget to the HUD with a custom positioner and resizes it. + * + * @param widget the widget + * @param positioner the positioner + * @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) { + widgets.add(widget); + widget.setSize(width, height); + setPositioner(widget, positioner); + } + /** * Sets the positioner of the widget. * @@ -132,5 +146,19 @@ public enum CottonHud implements HudRenderCallback { widget.setLocation((hudWidth + x) % hudWidth, (hudHeight + y) % hudHeight); }; } + + /** + * Creates a new positioner that centers widgets on the X axis and offsets them on the Y axis. + * + *
If the Y offset is negative, the offset is subtracted from the HUD height. + * + * @param y the y offset + * @return a centering positioner + */ + static Positioner horizontallyCentered(int y) { + return (widget, hudWidth, hudHeight) -> { + widget.setLocation((hudWidth - widget.getWidth()) / 2, (hudHeight + y) % hudHeight); + }; + } } } -- cgit