diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java | 109 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java | 26 |
2 files changed, 134 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 new file mode 100644 index 0000000..255c8bd --- /dev/null +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonHud.java @@ -0,0 +1,109 @@ +package io.github.cottonmc.cotton.gui.client; + +import io.github.cottonmc.cotton.gui.widget.WWidget; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.util.Window; + +import java.util.*; + +/** + * Manages widgets that are painted on the in-game HUD. + */ +@Environment(EnvType.CLIENT) +public enum CottonHud implements HudRenderCallback { + INSTANCE; + + static { + HudRenderCallback.EVENT.register(INSTANCE); + } + + private final Set<WWidget> widgets = new HashSet<>(); + private final Map<WWidget, Positioner> positioners = new HashMap<>(); + + /** + * Adds a new widget to the HUD. + * + * @param widget the widget + */ + public void add(WWidget widget) { + widgets.add(widget); + } + + /** + * 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 setPositioner(WWidget widget, Positioner positioner) { + positioners.put(widget, positioner); + } + + /** + * Removes the widget from the HUD. + * + * @param widget the widget + */ + public void remove(WWidget widget) { + widgets.remove(widget); + } + + @Override + public void onHudRender(float tickDelta) { + Window window = MinecraftClient.getInstance().getWindow(); + int hudWidth = window.getScaledWidth(); + int hudHeight = window.getScaledHeight(); + 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); + } + } + + /** + * 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); + + /** + * 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); + }; + } + } +} diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java index 3b634b4..d3bb769 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBar.java @@ -11,6 +11,8 @@ import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Identifier; +import javax.annotation.Nullable; + public class WBar extends WWidget { protected final Identifier bg; protected final Identifier bar; @@ -149,7 +151,29 @@ public class WBar extends WWidget { public void createPeers(GuiDescription c) { if (properties==null) properties = c.getPropertyDelegate(); } - + + /** + * Gets the current properties of this bar. + * + * @return the current property delegate, or null if not initialized yet + */ + @Nullable + public PropertyDelegate getProperties() { + return properties; + } + + /** + * Sets the current properties of this bar. + * + * <p>This method is meant for situations when a GUI description is unavailable (such as HUDs). + * {@link GuiDescription#getPropertyDelegate()} should be preferred over this if available. + * + * @param properties the properties + */ + public void setProperties(PropertyDelegate properties) { + this.properties = properties; + } + /** * Creates a WBar that has a constant maximum-value instead of getting the maximum from a field. * @param bg the background image to use for the bar |