From 3027527637373d7df682bf56368cf176ffcc19a8 Mon Sep 17 00:00:00 2001 From: xander Date: Sat, 3 Sep 2022 14:08:53 +0100 Subject: pass through type class to option for controllers to use --- src/main/java/dev/isxander/yacl/api/Option.java | 14 +++++++++++--- .../dev/isxander/yacl/gui/controllers/EnumController.java | 12 ++++-------- src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java | 5 +++++ src/main/java/dev/isxander/yacl/impl/OptionImpl.java | 11 ++++++++++- 4 files changed, 30 insertions(+), 12 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java index 65eae23..9d6ebe2 100644 --- a/src/main/java/dev/isxander/yacl/api/Option.java +++ b/src/main/java/dev/isxander/yacl/api/Option.java @@ -39,6 +39,12 @@ public interface Option { */ @NotNull Binding binding(); + /** + * Class of the option type. + * Used by some controllers. + */ + @NotNull Class typeClass(); + /** * Checks if the pending value is not equal to the current set value */ @@ -77,7 +83,7 @@ public interface Option { * @param typeClass used to capture the type */ static Builder createBuilder(Class typeClass) { - return new Builder<>(); + return new Builder<>(typeClass); } class Builder { @@ -89,8 +95,10 @@ public interface Option { private Binding binding; - private Builder() { + private final Class typeClass; + private Builder(Class typeClass) { + this.typeClass = typeClass; } /** @@ -177,7 +185,7 @@ public interface Option { concatenatedTooltip.append(line); } - return new OptionImpl<>(name, concatenatedTooltip, controlGetter, binding); + return new OptionImpl<>(name, concatenatedTooltip, controlGetter, binding, typeClass); } } } 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 5bb3caf..75d2bca 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java @@ -22,7 +22,6 @@ import java.util.function.Function; public class EnumController> implements Controller { private final Option option; private final Function valueFormatter; - private final Class enumClass; /** * Constructs a cycling enum controller with a default value formatter. @@ -30,10 +29,9 @@ public class EnumController> implements Controller { * enum is a {@link NameableEnum} else, just use {@link Enum#name()} * * @param option bound option - * @param enumClass class of enum */ - public EnumController(Option option, Class enumClass) { - this(option, enumClass, value -> { + public EnumController(Option option) { + this(option, value -> { if (value instanceof NameableEnum nameableEnum) return nameableEnum.getDisplayName(); return Text.of(value.name()); @@ -44,13 +42,11 @@ public class EnumController> implements Controller { * Constructs a cycling enum controller. * * @param option bound option - * @param enumClass class of enum * @param valueFormatter format the enum into any {@link Text} */ - public EnumController(Option option, Class enumClass, Function valueFormatter) { + public EnumController(Option option, Function valueFormatter) { this.option = option; this.valueFormatter = valueFormatter; - this.enumClass = enumClass; } /** @@ -74,7 +70,7 @@ public class EnumController> implements Controller { */ @Override public ControllerWidget> provideWidget(YACLScreen screen, Dimension widgetDimension) { - return new EnumControllerElement<>(this, screen, widgetDimension, enumClass.getEnumConstants()); + return new EnumControllerElement<>(this, screen, widgetDimension, option().typeClass().getEnumConstants()); } @ApiStatus.Internal diff --git a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java index 748dd0c..d3fbaa6 100644 --- a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java @@ -58,6 +58,11 @@ public class ButtonOptionImpl implements ButtonOption { return binding; } + @Override + public @NotNull Class> typeClass() { + throw new UnsupportedOperationException(); + } + @Override public boolean changed() { return false; diff --git a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java index 176bf93..2d66aee 100644 --- a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java @@ -17,18 +17,22 @@ public class OptionImpl implements Option { private final Controller controller; private final Binding binding; + private final Class typeClass; + private T pendingValue; public OptionImpl( @NotNull Text name, @Nullable Text tooltip, @NotNull Function, Controller> controlGetter, - @NotNull Binding binding + @NotNull Binding binding, + @NotNull Class typeClass ) { this.name = name; this.tooltip = tooltip; this.controller = controlGetter.apply(this); this.binding = binding; + this.typeClass = typeClass; this.pendingValue = binding().getValue(); } @@ -52,6 +56,11 @@ public class OptionImpl implements Option { return binding; } + @Override + public @NotNull Class typeClass() { + return typeClass; + } + @Override public boolean changed() { return !binding().getValue().equals(pendingValue); -- cgit