diff options
Diffstat (limited to 'src/main')
7 files changed, 39 insertions, 15 deletions
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<T extends Controller<?>> extends AbstractWidget { protected final T control; @@ -18,7 +19,8 @@ public abstract class ControllerWidget<T extends Controller<?>> 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<Integer> dim) { super(dim); @@ -26,14 +28,15 @@ public abstract class ControllerWidget<T extends Controller<?>> 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<T extends Controller<?>> 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<T extends Enum<T>> implements Controller<T> { private final Option<T> option; private final Function<T, Text> valueFormatter; + private final T[] availableValues; + + public static <T extends Enum<T>> Function<T, Text> 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<T> 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<T> option, Function<T, Text> 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<T> option, Function<T, Text> valueFormatter, T[] availableValues) { this.option = option; this.valueFormatter = valueFormatter; + this.availableValues = availableValues; } /** @@ -74,7 +91,7 @@ public class EnumController<T extends Enum<T>> implements Controller<T> { */ @Override public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new EnumControllerElement<>(this, screen, widgetDimension, option().typeClass().getEnumConstants()); + return new EnumControllerElement<>(this, screen, widgetDimension, availableValues); } public static class EnumControllerElement<T extends Enum<T>> extends ControllerWidget<EnumController<T>> { 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<Double> { public DoubleSliderController(Option<Double> option, double min, double max, double interval, Function<Double, Text> 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<Float> { public FloatSliderController(Option<Float> option, float min, float max, float interval, Function<Float, Text> 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<Integer> { public IntegerSliderController(Option<Integer> option, int min, int max, int interval, Function<Integer, Text> 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<Long> { public LongSliderController(Option<Long> option, long min, long max, long interval, Function<Long, Text> 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<ISliderController< } protected void calculateInterpolation() { - interpolation = (float) ((control.pendingValue() - control.min()) * 1 / control.range()); + interpolation = MathHelper.clamp((float) ((control.pendingValue() - control.min()) * 1 / control.range()), 0f, 1f); } @Override |