From 9b16647ea9e11ff947efad58b23343d7a8208eb9 Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sun, 27 Jun 2021 11:46:35 +0300 Subject: Fix WBar.withConstantMaximum and clean up the class --- .../io/github/cottonmc/cotton/gui/widget/WBar.java | 146 ++++++++++++++------- 1 file changed, 95 insertions(+), 51 deletions(-) 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 60fb7a9..13ff5e1 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 @@ -44,106 +44,134 @@ public class WBar extends WWidget { /** * The ID of the property representing the maximum value of the {@link #field}. * - *
If {@code max} is 0, the {@link #maxValue} constant will be used instead. + *
If {@code max} is negative, the {@link #maxValue} constant will be used instead. */ protected final int max; /** * The constant maximum value of the {@link #field}. * - *
This constant will only be used if {@link #max} is 0. + *
This constant will only be used if {@link #max} is negative. * * @see #withConstantMaximum(Identifier, Identifier, int, int, Direction) */ protected int maxValue; + + /** + * The properties used for painting this bar. + * + *
The current value is read from the property with ID {@link #field}, + * and the maximum value is usually read from the property with ID {@link #max}. + */ protected PropertyDelegate properties; private boolean manuallySetProperties = false; + + /** + * The direction of this bar, representing where the bar will grow + * when the field increases. + */ protected final Direction direction; + + /** + * The translation key of the tooltip. + * + * @see #withTooltip(String) formatting instructions + */ protected String tooltipLabel; + + /** + * A tooltip text component. This can be used instead of {@link #tooltipLabel}, + * or together with it. In that case, this component will be drawn after the other label. + */ protected Text tooltipTextComponent; - - public WBar(Texture bg, Texture bar, int field, int maxfield) { - this(bg, bar, field, maxfield, Direction.UP); + + public WBar(@Nullable Texture bg, @Nullable Texture bar, int field, int maxField) { + this(bg, bar, field, maxField, Direction.UP); } - public WBar(Texture bg, Texture bar, int field, int maxfield, Direction dir) { + public WBar(@Nullable Texture bg, @Nullable Texture bar, int field, int maxField, Direction dir) { this.bg = bg; this.bar = bar; this.field = field; - this.max = maxfield; + this.max = maxField; this.maxValue = 0; this.direction = dir; } - public WBar(Identifier bg, Identifier bar, int field, int maxfield) { - this(bg, bar, field, maxfield, Direction.UP); + public WBar(Identifier bg, Identifier bar, int field, int maxField) { + this(bg, bar, field, maxField, Direction.UP); } - public WBar(Identifier bg, Identifier bar, int field, int maxfield, Direction dir) { - this(new Texture(bg), new Texture(bar), field, maxfield, dir); + public WBar(Identifier bg, Identifier bar, int field, int maxField, Direction dir) { + this(new Texture(bg), new Texture(bar), field, maxField, dir); } /** * Adds a tooltip to the WBar. * - * Formatting Guide: The tooltip label is passed into {@code String.format} and can receive two integers + *
Formatting Guide: The tooltip label is passed into {@code String.format} and can receive two integers * (%d) - the first is the current value of the bar's focused field, and the second is the * bar's focused maximum. * - * @param label Translation key of the string to render on the tooltip. - * @return WBar with tooltip enabled and set. + * @param label the translation key of the string to render on the tooltip + * @return this bar with tooltip enabled and set */ public WBar withTooltip(String label) { this.tooltipLabel = label; return this; } - - - + + /** + * Adds a tooltip {@link Text} to the WBar. + * + * @param label the added tooltip label + * @return this bar + */ public WBar withTooltip(Text label) { this.tooltipTextComponent = label; return this; } - + @Override public boolean canResize() { return true; } - + @Environment(EnvType.CLIENT) @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { - if (bg!=null) { + if (bg != null) { ScreenDrawing.texturedRect(matrices, x, y, getWidth(), getHeight(), bg, 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(matrices, x, y, getWidth(), getHeight(), ScreenDrawing.colorAtOpacity(0x000000, 0.25f)); } - - float percent = properties.get(field) / (float) properties.get(max); + + int maxVal = max >= 0 ? properties.get(max) : maxValue; + float percent = properties.get(field) / (float) maxVal; if (percent < 0) percent = 0f; if (percent > 1) percent = 1f; - + int barMax = getWidth(); if (direction == Direction.DOWN || direction == Direction.UP) barMax = getHeight(); percent = ((int) (percent * barMax)) / (float) barMax; //Quantize to bar size - + int barSize = (int) (barMax * percent); if (barSize <= 0) return; - - switch(direction) { //anonymous blocks in this switch statement are to sandbox variables + + switch (direction) { //anonymous blocks in this switch statement are to sandbox variables case UP: { int left = x; int top = y + getHeight(); top -= barSize; - if (bar!=null) { + if (bar != null) { ScreenDrawing.texturedRect(matrices, left, top, getWidth(), barSize, bar.image(), bar.u1(), MathHelper.lerp(percent, bar.v2(), bar.v1()), bar.u2(), bar.v2(), 0xFFFFFFFF); } else { - ScreenDrawing.coloredRect(matrices, left, top, getWidth(), barSize, ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); + ScreenDrawing.coloredRect(matrices, left, top, getWidth(), barSize, ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); } break; } case RIGHT: { - if (bar!=null) { + if (bar != null) { ScreenDrawing.texturedRect(matrices, x, y, barSize, getHeight(), bar.image(), bar.u1(), bar.v1(), MathHelper.lerp(percent, bar.u1(), bar.u2()), bar.v2(), 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(matrices, x, y, barSize, getHeight(), ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); @@ -151,7 +179,7 @@ public class WBar extends WWidget { break; } case DOWN: { - if (bar!=null) { + if (bar != null) { ScreenDrawing.texturedRect(matrices, x, y, getWidth(), barSize, bar.image(), bar.u1(), bar.v1(), bar.u2(), MathHelper.lerp(percent, bar.v1(), bar.v2()), 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(matrices, x, y, getWidth(), barSize, ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); @@ -162,7 +190,7 @@ public class WBar extends WWidget { int left = x + getWidth(); int top = y; left -= barSize; - if (bar!=null) { + if (bar != null) { ScreenDrawing.texturedRect(matrices, left, top, barSize, getHeight(), bar.image(), MathHelper.lerp(percent, bar.u2(), bar.u1()), bar.v1(), bar.u2(), bar.v2(), 0xFFFFFFFF); } else { ScreenDrawing.coloredRect(matrices, left, top, barSize, getHeight(), ScreenDrawing.colorAtOpacity(0xFFFFFF, 0.5f)); @@ -175,18 +203,12 @@ public class WBar extends WWidget { @Environment(EnvType.CLIENT) @Override public void addTooltip(TooltipBuilder information) { - if (tooltipLabel!=null) { - int value = (field>=0) ? properties.get(field) : 0; - int valMax = (max>=0) ? properties.get(max) : maxValue; - Text formatted; - try { - formatted = new TranslatableText(tooltipLabel, Integer.valueOf(value), Integer.valueOf(valMax)); - } catch (Throwable t) { - formatted = new LiteralText(t.getLocalizedMessage()); - } //Fallback to raw tooltipLabel - information.add(formatted); + if (tooltipLabel != null) { + int value = (field >= 0) ? properties.get(field) : 0; + int valMax = (max >= 0) ? properties.get(max) : maxValue; + information.add(new TranslatableText(tooltipLabel, Integer.valueOf(value), Integer.valueOf(valMax))); } - if (tooltipTextComponent!=null) { + if (tooltipTextComponent != null) { try { information.add(tooltipTextComponent); } catch (Throwable t) { @@ -194,11 +216,11 @@ public class WBar extends WWidget { } } } - + @Override public void validate(GuiDescription host) { super.validate(host); - if (properties==null || !manuallySetProperties) properties = host.getPropertyDelegate(); + if (properties == null || !manuallySetProperties) properties = host.getPropertyDelegate(); } /** @@ -228,12 +250,13 @@ public class WBar extends WWidget { /** * 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 - * @param bar the foreground image that represents the filled bar - * @param field the field index for bar values - * @param maxValue the constant maximum value for the bar - * @param dir the direction the bar should grow towards - * @return a new WBar with a constant maximum value. + * + * @param bg the background image to use for the bar + * @param bar the foreground image that represents the filled bar + * @param field the field index for bar values + * @param maxValue the constant maximum value for the bar + * @param dir the direction the bar should grow towards + * @return a new WBar with a constant maximum value */ public static WBar withConstantMaximum(Identifier bg, Identifier bar, int field, int maxValue, Direction dir) { WBar result = new WBar(bg, bar, field, -1, dir); @@ -241,7 +264,28 @@ public class WBar extends WWidget { return result; } - public static enum Direction { + /** + * 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 + * @param bar the foreground image that represents the filled bar + * @param field the field index for bar values + * @param maxValue the constant maximum value for the bar + * @param dir the direction the bar should grow towards + * @return a new WBar with a constant maximum value + * @since 4.1.0 + */ + public static WBar withConstantMaximum(Texture bg, Texture bar, int field, int maxValue, Direction dir) { + WBar result = new WBar(bg, bar, field, -1, dir); + result.maxValue = maxValue; + return result; + } + + /** + * The direction of a {@link WBar}, representing where the bar will + * grown when its field increases. + */ + public enum Direction { UP, RIGHT, DOWN, -- cgit