aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java
diff options
context:
space:
mode:
authorxander <xander@isxander.dev>2022-09-01 11:58:49 +0100
committerxander <xander@isxander.dev>2022-09-01 11:58:49 +0100
commit4d977cc9764ecf0073650f126700f6ff638fa06b (patch)
tree883e68bbd80874c048b3e34db59bf0aa926b489b /src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java
parente63a3c989e3a899bdc81558dd2e4c5cc2c659bde (diff)
downloadYetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.tar.gz
YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.tar.bz2
YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.zip
javadoc!
added LongSliderController renamed Control -> Controller add minecraft simple option binding constructor
Diffstat (limited to 'src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java')
-rw-r--r--src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java
new file mode 100644
index 0000000..d88401c
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java
@@ -0,0 +1,110 @@
+package dev.isxander.yacl.gui.controllers;
+
+import dev.isxander.yacl.api.Controller;
+import dev.isxander.yacl.api.NameableEnum;
+import dev.isxander.yacl.api.Option;
+import dev.isxander.yacl.api.utils.Dimension;
+import net.minecraft.client.gui.screen.Screen;
+import net.minecraft.text.Text;
+import org.jetbrains.annotations.ApiStatus;
+
+import java.util.function.Function;
+
+/**
+ * Simple controller type that displays the enum on the right.
+ * <p>
+ * Cycles forward with left click, cycles backward with right click or when shift is held
+ *
+ * @param <T> enum type
+ */
+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.
+ * The default value formatter first searches if the
+ * 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 -> {
+ if (value instanceof NameableEnum nameableEnum)
+ return nameableEnum.getDisplayName();
+ return Text.of(value.name());
+ });
+ }
+
+ /**
+ * 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) {
+ this.option = option;
+ this.valueFormatter = valueFormatter;
+ this.enumClass = enumClass;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Option<T> option() {
+ return option;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Text formatValue() {
+ return valueFormatter.apply(option().pendingValue());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public ControllerWidget<EnumController<T>> provideWidget(Screen screen, Dimension<Integer> widgetDimension) {
+ return new EnumControllerElement<>(this, screen, widgetDimension, enumClass.getEnumConstants());
+ }
+
+ @ApiStatus.Internal
+ public static class EnumControllerElement<T extends Enum<T>> extends ControllerWidget<EnumController<T>> {
+ private final T[] values;
+
+ public EnumControllerElement(EnumController<T> control, Screen screen, Dimension<Integer> dim, T[] values) {
+ super(control, screen, dim);
+ this.values = values;
+ }
+
+ @Override
+ public boolean mouseClicked(double mouseX, double mouseY, int button) {
+ if (!isMouseOver(mouseX, mouseY) || (button != 0 && button != 1))
+ return false;
+
+ playDownSound();
+
+ int change = button == 1 || Screen.hasShiftDown() ? -1 : 1;
+ int targetIdx = control.option().pendingValue().ordinal() + change;
+ if (targetIdx >= values.length) {
+ targetIdx -= values.length;
+ } else if (targetIdx < 0) {
+ targetIdx += values.length;
+ }
+ control.option().requestSet(values[targetIdx]);
+ return true;
+ }
+
+ @Override
+ protected int getHoveredControlWidth() {
+ return getUnhoveredControlWidth();
+ }
+ }
+}