aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorxander <xander@isxander.dev>2022-09-03 14:08:53 +0100
committerxander <xander@isxander.dev>2022-09-03 14:08:53 +0100
commit3027527637373d7df682bf56368cf176ffcc19a8 (patch)
tree7d06ac25b09b1944626e3007ef6831f455c36ec0 /src/main/java
parentbe878535f0cd82fe3a0a7c7268e1e6fff5c76a2e (diff)
downloadYetAnotherConfigLib-3027527637373d7df682bf56368cf176ffcc19a8.tar.gz
YetAnotherConfigLib-3027527637373d7df682bf56368cf176ffcc19a8.tar.bz2
YetAnotherConfigLib-3027527637373d7df682bf56368cf176ffcc19a8.zip
pass through type class to option for controllers to use
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/dev/isxander/yacl/api/Option.java14
-rw-r--r--src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java12
-rw-r--r--src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java5
-rw-r--r--src/main/java/dev/isxander/yacl/impl/OptionImpl.java11
4 files changed, 30 insertions, 12 deletions
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
@@ -40,6 +40,12 @@ public interface Option<T> {
@NotNull Binding<T> binding();
/**
+ * Class of the option type.
+ * Used by some controllers.
+ */
+ @NotNull Class<T> typeClass();
+
+ /**
* Checks if the pending value is not equal to the current set value
*/
boolean changed();
@@ -77,7 +83,7 @@ public interface Option<T> {
* @param typeClass used to capture the type
*/
static <T> Builder<T> createBuilder(Class<T> typeClass) {
- return new Builder<>();
+ return new Builder<>(typeClass);
}
class Builder<T> {
@@ -89,8 +95,10 @@ public interface Option<T> {
private Binding<T> binding;
- private Builder() {
+ private final Class<T> typeClass;
+ private Builder(Class<T> typeClass) {
+ this.typeClass = typeClass;
}
/**
@@ -177,7 +185,7 @@ public interface Option<T> {
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<T extends Enum<T>> implements Controller<T> {
private final Option<T> option;
private final Function<T, Text> valueFormatter;
- private final Class<T> enumClass;
/**
* Constructs a cycling enum controller with a default value formatter.
@@ -30,10 +29,9 @@ public class EnumController<T extends Enum<T>> implements Controller<T> {
* enum is a {@link NameableEnum} else, just use {@link Enum#name()}
*
* @param option bound option
- * @param enumClass class of enum
*/
- public EnumController(Option<T> option, Class<T> enumClass) {
- this(option, enumClass, value -> {
+ public EnumController(Option<T> option) {
+ this(option, value -> {
if (value instanceof NameableEnum nameableEnum)
return nameableEnum.getDisplayName();
return Text.of(value.name());
@@ -44,13 +42,11 @@ public class EnumController<T extends Enum<T>> implements Controller<T> {
* 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<T> option, Class<T> enumClass, Function<T, Text> valueFormatter) {
+ public EnumController(Option<T> option, Function<T, Text> valueFormatter) {
this.option = option;
this.valueFormatter = valueFormatter;
- this.enumClass = enumClass;
}
/**
@@ -74,7 +70,7 @@ public class EnumController<T extends Enum<T>> implements Controller<T> {
*/
@Override
public ControllerWidget<EnumController<T>> provideWidget(YACLScreen screen, Dimension<Integer> 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
@@ -59,6 +59,11 @@ public class ButtonOptionImpl implements ButtonOption {
}
@Override
+ public @NotNull Class<Consumer<YACLScreen>> 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<T> implements Option<T> {
private final Controller<T> controller;
private final Binding<T> binding;
+ private final Class<T> typeClass;
+
private T pendingValue;
public OptionImpl(
@NotNull Text name,
@Nullable Text tooltip,
@NotNull Function<Option<T>, Controller<T>> controlGetter,
- @NotNull Binding<T> binding
+ @NotNull Binding<T> binding,
+ @NotNull Class<T> typeClass
) {
this.name = name;
this.tooltip = tooltip;
this.controller = controlGetter.apply(this);
this.binding = binding;
+ this.typeClass = typeClass;
this.pendingValue = binding().getValue();
}
@@ -53,6 +57,11 @@ public class OptionImpl<T> implements Option<T> {
}
@Override
+ public @NotNull Class<T> typeClass() {
+ return typeClass;
+ }
+
+ @Override
public boolean changed() {
return !binding().getValue().equals(pendingValue);
}