aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorJuuxel <6596629+Juuxel@users.noreply.github.com>2020-02-17 19:21:55 +0200
committerJuuxel <6596629+Juuxel@users.noreply.github.com>2020-02-17 19:21:55 +0200
commitf0cda5093d8d5d9024ea226fb043cf7da2283466 (patch)
treeefcd594fb7fe6b000956297140fce735c2634a71 /src/main/java
parent5b23e672dc87c8643d6025cba9d5a29bce17eb2e (diff)
downloadLibGui-f0cda5093d8d5d9024ea226fb043cf7da2283466.tar.gz
LibGui-f0cda5093d8d5d9024ea226fb043cf7da2283466.tar.bz2
LibGui-f0cda5093d8d5d9024ea226fb043cf7da2283466.zip
Revert "Simplify the HUD API"
Positioners actually have another use case -- centering widgets. Centering is not possible with just x/y as it also needs the widget and the height. This reverts commit 5b23e672.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java84
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);
+ };
}
}
}