From 442f48a6e28196910e92f460d3d677e3e47cbfc0 Mon Sep 17 00:00:00 2001 From: Kevin <92656833+kevinthegreat1@users.noreply.github.com> Date: Sat, 18 Nov 2023 04:07:39 -0500 Subject: Add Enum Dropdown Controller (#117) --- .../java/dev/isxander/yacl3/api/NameableEnum.java | 2 +- .../controller/EnumDropdownControllerBuilder.java | 10 +++ .../dropdown/AbstractDropdownController.java | 6 +- .../dropdown/EnumDropdownController.java | 92 ++++++++++++++++++++++ .../dropdown/EnumDropdownControllerElement.java | 25 ++++++ .../EnumDropdownControllerBuilderImpl.java | 27 +++++++ 6 files changed, 160 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/dev/isxander/yacl3/api/controller/EnumDropdownControllerBuilder.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownController.java create mode 100644 common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownControllerElement.java create mode 100644 common/src/main/java/dev/isxander/yacl3/impl/controller/EnumDropdownControllerBuilderImpl.java (limited to 'common/src/main/java') diff --git a/common/src/main/java/dev/isxander/yacl3/api/NameableEnum.java b/common/src/main/java/dev/isxander/yacl3/api/NameableEnum.java index 5a50207..425623f 100644 --- a/common/src/main/java/dev/isxander/yacl3/api/NameableEnum.java +++ b/common/src/main/java/dev/isxander/yacl3/api/NameableEnum.java @@ -3,7 +3,7 @@ package dev.isxander.yacl3.api; import net.minecraft.network.chat.Component; /** - * Used for the default value formatter of {@link dev.isxander.yacl3.gui.controllers.cycling.EnumController} + * Used for the default value formatter of {@link dev.isxander.yacl3.gui.controllers.cycling.EnumController} and {@link dev.isxander.yacl3.gui.controllers.dropdown.EnumDropdownController} */ public interface NameableEnum { Component getDisplayName(); diff --git a/common/src/main/java/dev/isxander/yacl3/api/controller/EnumDropdownControllerBuilder.java b/common/src/main/java/dev/isxander/yacl3/api/controller/EnumDropdownControllerBuilder.java new file mode 100644 index 0000000..0814cc6 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/api/controller/EnumDropdownControllerBuilder.java @@ -0,0 +1,10 @@ +package dev.isxander.yacl3.api.controller; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.impl.controller.EnumDropdownControllerBuilderImpl; + +public interface EnumDropdownControllerBuilder> extends ValueFormattableController> { + static > EnumDropdownControllerBuilder create(Option option) { + return new EnumDropdownControllerBuilderImpl<>(option); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/AbstractDropdownController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/AbstractDropdownController.java index b4e4304..b913d15 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/AbstractDropdownController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/AbstractDropdownController.java @@ -26,8 +26,12 @@ public abstract class AbstractDropdownController implements IStringController this.allowAnyValue = allowAnyValue; } + protected AbstractDropdownController(Option option, List allowedValues) { + this(option, allowedValues, false, false); + } + protected AbstractDropdownController(Option option) { - this(option, Collections.emptyList(), false, false); + this(option, Collections.emptyList()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownController.java new file mode 100644 index 0000000..8e6e0e6 --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownController.java @@ -0,0 +1,92 @@ +package dev.isxander.yacl3.gui.controllers.dropdown; + +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.ValueFormatter; +import dev.isxander.yacl3.api.utils.Dimension; +import dev.isxander.yacl3.gui.AbstractWidget; +import dev.isxander.yacl3.gui.YACLScreen; +import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.NotNull; + +import java.util.Arrays; +import java.util.stream.Stream; + +public class EnumDropdownController> extends AbstractDropdownController { + /** + * The function used to convert enum constants to strings used for display, suggestion, and validation. Defaults to {@link Enum#toString}. + */ + protected final ValueFormatter formatter; + + public EnumDropdownController(Option option, ValueFormatter formatter) { + super(option, Arrays.stream(option.pendingValue().getDeclaringClass().getEnumConstants()).map(formatter::format).map(Component::getString).toList()); + this.formatter = formatter; + } + + @Override + public String getString() { + return formatter.format(option().pendingValue()).getString(); + } + + @Override + public void setFromString(String value) { + option().requestSet(getEnumFromString(value)); + } + + /** + * Searches through enum constants for one whose {@link #formatter} result equals {@code value} + * + * @return The enum constant associated with the {@code value} or the pending value if none are found + * @implNote The return value of {@link #formatter} on each enum constant should be unique in order to ensure accuracy + */ + private E getEnumFromString(String value) { + value = value.toLowerCase(); + for (E constant : option().pendingValue().getDeclaringClass().getEnumConstants()) { + if (formatter.format(constant).getString().toLowerCase().equals(value)) return constant; + } + + return option().pendingValue(); + } + + @Override + public boolean isValueValid(String value) { + value = value.toLowerCase(); + for (String constant : getAllowedValues()) { + if (constant.equals(value)) return true; + } + + return false; + } + + @Override + protected String getValidValue(String value, int offset) { + return getValidEnumConstants(value) + .skip(offset) + .findFirst() + .orElseGet(this::getString); + } + + /** + * Filters and sorts through enum constants for those whose {@link #formatter} result equals {@code value} + * + * @return a sorted stream containing enum constants associated with the {@code value} + * @implNote The return value of {@link #formatter} on each enum constant should be unique in order to ensure accuracy + */ + @NotNull + protected Stream getValidEnumConstants(String value) { + String valueLowerCase = value.toLowerCase(); + return getAllowedValues().stream() + .filter(constant -> constant.toLowerCase().contains(valueLowerCase)) + .sorted((s1, s2) -> { + String s1LowerCase = s1.toLowerCase(); + String s2LowerCase = s2.toLowerCase(); + if (s1LowerCase.startsWith(valueLowerCase) && !s2LowerCase.startsWith(valueLowerCase)) return -1; + if (!s1LowerCase.startsWith(valueLowerCase) && s2LowerCase.startsWith(valueLowerCase)) return 1; + return s1.compareTo(s2); + }); + } + + @Override + public AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { + return new EnumDropdownControllerElement<>(this, screen, widgetDimension); + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownControllerElement.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownControllerElement.java new file mode 100644 index 0000000..2df6f6b --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/dropdown/EnumDropdownControllerElement.java @@ -0,0 +1,25 @@ +package dev.isxander.yacl3.gui.controllers.dropdown; + +import dev.isxander.yacl3.api.utils.Dimension; +import dev.isxander.yacl3.gui.YACLScreen; + +import java.util.List; + +public class EnumDropdownControllerElement> extends AbstractDropdownControllerElement { + private final EnumDropdownController controller; + + public EnumDropdownControllerElement(EnumDropdownController control, YACLScreen screen, Dimension dim) { + super(control, screen, dim); + this.controller = control; + } + + @Override + public List computeMatchingValues() { + return controller.getValidEnumConstants(inputField).toList(); + } + + @Override + public String getString(String object) { + return object; + } +} diff --git a/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumDropdownControllerBuilderImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumDropdownControllerBuilderImpl.java new file mode 100644 index 0000000..4ac063f --- /dev/null +++ b/common/src/main/java/dev/isxander/yacl3/impl/controller/EnumDropdownControllerBuilderImpl.java @@ -0,0 +1,27 @@ +package dev.isxander.yacl3.impl.controller; + +import dev.isxander.yacl3.api.Controller; +import dev.isxander.yacl3.api.Option; +import dev.isxander.yacl3.api.controller.EnumDropdownControllerBuilder; +import dev.isxander.yacl3.api.controller.ValueFormatter; +import dev.isxander.yacl3.gui.controllers.cycling.EnumController; +import dev.isxander.yacl3.gui.controllers.dropdown.EnumDropdownController; + +public class EnumDropdownControllerBuilderImpl> extends AbstractControllerBuilderImpl implements EnumDropdownControllerBuilder { + private ValueFormatter formatter = EnumController.getDefaultFormatter()::apply; + + public EnumDropdownControllerBuilderImpl(Option option) { + super(option); + } + + @Override + public EnumDropdownControllerBuilder formatValue(ValueFormatter formatter) { + this.formatter = formatter; + return this; + } + + @Override + public Controller build() { + return new EnumDropdownController<>(option, formatter); + } +} -- cgit