Skip to content

Commit

Permalink
Revert "Simplify the HUD API"
Browse files Browse the repository at this point in the history
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 5b23e67.
  • Loading branch information
Juuxel committed Feb 17, 2020
1 parent 5b23e67 commit f0cda50
Showing 1 changed file with 62 additions and 22 deletions.
84 changes: 62 additions & 22 deletions src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -76,21 +94,43 @@ public void onHudRender(float tickDelta) {
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);
};
}
}
}

0 comments on commit f0cda50

Please sign in to comment.