Skip to content

Commit

Permalink
Add horizontally centering positioner
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Feb 17, 2020
1 parent f0cda50 commit 4aadba3
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void add(WWidget widget, int x, int y) {
* @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) {
Expand All @@ -70,6 +70,20 @@ public void add(WWidget widget, Positioner positioner) {
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.
*
Expand Down Expand Up @@ -132,5 +146,19 @@ static Positioner of(int x, int y) {
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);
};
}
}
}

0 comments on commit 4aadba3

Please sign in to comment.