aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2020-02-17 19:34:31 +0200
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2020-02-17 19:34:31 +0200
commit4aadba3408fbfaf6816c6e917bfb6196bec16403 (patch)
tree2e098e4db853f30866a5ebe661d9dfc57f999f42 /src/main/java/io
parentf0cda5093d8d5d9024ea226fb043cf7da2283466 (diff)
downloadLibGui-4aadba3408fbfaf6816c6e917bfb6196bec16403.tar.gz
LibGui-4aadba3408fbfaf6816c6e917bfb6196bec16403.tar.bz2
LibGui-4aadba3408fbfaf6816c6e917bfb6196bec16403.zip
Add horizontally centering positioner
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java30
1 files changed, 29 insertions, 1 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..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) {
@@ -71,6 +71,20 @@ public enum CottonHud implements HudRenderCallback {
}
/**
+ * 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.
*
* @param widget 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.
+ *
+ * <p>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);
+ };
+ }
}
}