From dd29c979e6092325ee064d3b8cf33816e7e93aa4 Mon Sep 17 00:00:00 2001 From: xander Date: Fri, 23 Sep 2022 21:01:00 +0100 Subject: 1.5.0 --- .../yacl/gui/controllers/ControllerWidget.java | 11 ++++--- .../yacl/gui/controllers/EnumController.java | 37 ++++++++++++++++------ .../controllers/slider/DoubleSliderController.java | 1 + .../controllers/slider/FloatSliderController.java | 1 + .../slider/IntegerSliderController.java | 1 + .../controllers/slider/LongSliderController.java | 1 + .../slider/SliderControllerElement.java | 2 +- 7 files changed, 39 insertions(+), 15 deletions(-) (limited to 'src/main') diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java index 6ef7e56..34efe4f 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java @@ -9,6 +9,7 @@ import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; +import net.minecraft.util.Formatting; public abstract class ControllerWidget> extends AbstractWidget { protected final T control; @@ -18,7 +19,8 @@ public abstract class ControllerWidget> extends Abstract protected boolean focused = false; protected boolean hovered = false; - protected final String optionName; + protected final Text modifiedOptionName; + protected final String optionNameString; public ControllerWidget(T control, YACLScreen screen, Dimension dim) { super(dim); @@ -26,14 +28,15 @@ public abstract class ControllerWidget> extends Abstract this.screen = screen; control.option().addListener((opt, pending) -> updateTooltip()); updateTooltip(); - this.optionName = control.option().name().getString().toLowerCase(); + this.modifiedOptionName = control.option().name().copy().formatted(Formatting.ITALIC); + this.optionNameString = control.option().name().getString().toLowerCase(); } @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { hovered = isMouseOver(mouseX, mouseY); - Text name = control.option().name(); + Text name = control.option().changed() ? modifiedOptionName : control.option().name(); String nameString = name.getString(); boolean firstIter = true; @@ -148,7 +151,7 @@ public abstract class ControllerWidget> extends Abstract @Override public boolean matchesSearch(String query) { - return optionName.contains(query.toLowerCase()); + return optionNameString.contains(query.toLowerCase()); } @Override diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java index 5cf680d..57c6c18 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java @@ -24,33 +24,50 @@ import java.util.function.Function; public class EnumController> implements Controller { private final Option option; private final Function valueFormatter; + private final T[] availableValues; + + public static > Function getDefaultFormatter() { + return value -> { + if (value instanceof NameableEnum nameableEnum) + return nameableEnum.getDisplayName(); + if (value instanceof TranslatableOption translatableOption) + return translatableOption.getText(); + return Text.of(value.name()); + }; + } /** - * Constructs a cycling enum controller with a default value formatter. + * Constructs a cycling enum controller with a default value formatter and all values being available. * The default value formatter first searches if the * enum is a {@link NameableEnum} else, just use {@link Enum#name()} * * @param option bound option */ public EnumController(Option option) { - this(option, value -> { - if (value instanceof NameableEnum nameableEnum) - return nameableEnum.getDisplayName(); - if (value instanceof TranslatableOption translatableOption) - return translatableOption.getText(); - return Text.of(value.name()); - }); + this(option, getDefaultFormatter()); } /** - * Constructs a cycling enum controller. + * Constructs a cycling enum controller with all values being available. * * @param option bound option * @param valueFormatter format the enum into any {@link Text} */ public EnumController(Option option, Function valueFormatter) { + this(option, valueFormatter, option.typeClass().getEnumConstants()); + } + + /** + * Constructs a cycling enum controller. + * + * @param option bound option + * @param valueFormatter format the enum into any {@link Text} + * @param availableValues all enum constants that can be cycled through + */ + public EnumController(Option option, Function valueFormatter, T[] availableValues) { this.option = option; this.valueFormatter = valueFormatter; + this.availableValues = availableValues; } /** @@ -74,7 +91,7 @@ public class EnumController> implements Controller { */ @Override public AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { - return new EnumControllerElement<>(this, screen, widgetDimension, option().typeClass().getEnumConstants()); + return new EnumControllerElement<>(this, screen, widgetDimension, availableValues); } public static class EnumControllerElement> extends ControllerWidget> { diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java index ed78abd..b530e8c 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java @@ -46,6 +46,7 @@ public class DoubleSliderController implements ISliderController { public DoubleSliderController(Option option, double min, double max, double interval, Function valueFormatter) { Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); Validate.isTrue(interval > 0, "`interval` must be more than 0"); + Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); this.option = option; this.min = min; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java index 4297271..d7c203e 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java @@ -46,6 +46,7 @@ public class FloatSliderController implements ISliderController { public FloatSliderController(Option option, float min, float max, float interval, Function valueFormatter) { Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); Validate.isTrue(interval > 0, "`interval` must be more than 0"); + Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); this.option = option; this.min = min; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java index 38efed5..a8bca7c 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java @@ -43,6 +43,7 @@ public class IntegerSliderController implements ISliderController { public IntegerSliderController(Option option, int min, int max, int interval, Function valueFormatter) { Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); Validate.isTrue(interval > 0, "`interval` must be more than 0"); + Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); this.option = option; this.min = min; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java index 67b6a0e..50559d5 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java @@ -43,6 +43,7 @@ public class LongSliderController implements ISliderController { public LongSliderController(Option option, long min, long max, long interval, Function valueFormatter) { Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); Validate.isTrue(interval > 0, "`interval` must be more than 0"); + Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); this.option = option; this.min = min; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java index 4a0061b..913cc00 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java @@ -135,7 +135,7 @@ public class SliderControllerElement extends ControllerWidget