diff options
author | isXander <xandersmith2008@gmail.com> | 2022-11-27 18:17:36 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-11-27 18:17:36 +0000 |
commit | d163b9128d760e53e34fd6c08dbf782fa3d50c51 (patch) | |
tree | b9ea79bb99bf52ea7de9d059cc05900270ff88d4 /src/client/java/dev/isxander/yacl/gui/controllers/cycling | |
parent | c26d3a89caae20f1a91898202d91de8dc90da5ad (diff) | |
download | YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.tar.gz YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.tar.bz2 YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.zip |
split sourcesets
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui/controllers/cycling')
4 files changed, 237 insertions, 0 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingControllerElement.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingControllerElement.java new file mode 100644 index 0000000..ab0a9c3 --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingControllerElement.java @@ -0,0 +1,60 @@ +package dev.isxander.yacl.gui.controllers.cycling; + +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.YACLScreen; +import dev.isxander.yacl.gui.controllers.ControllerWidget; +import net.minecraft.client.gui.screen.Screen; +import org.lwjgl.glfw.GLFW; + +public class CyclingControllerElement extends ControllerWidget<ICyclingController<?>> { + + public CyclingControllerElement(ICyclingController<?> control, YACLScreen screen, Dimension<Integer> dim) { + super(control, screen, dim); + } + + public void cycleValue(int increment) { + int targetIdx = control.getPendingValue() + increment; + if (targetIdx >= control.getCycleLength()) { + targetIdx -= control.getCycleLength(); + } else if (targetIdx < 0) { + targetIdx += control.getCycleLength(); + } + control.setPendingValue(targetIdx); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (!isMouseOver(mouseX, mouseY) || (button != 0 && button != 1) || !isAvailable()) + return false; + + playDownSound(); + cycleValue(button == 1 || Screen.hasShiftDown() || Screen.hasControlDown() ? -1 : 1); + + return true; + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (!focused) + return false; + + switch (keyCode) { + case GLFW.GLFW_KEY_LEFT, GLFW.GLFW_KEY_DOWN -> + cycleValue(-1); + case GLFW.GLFW_KEY_RIGHT, GLFW.GLFW_KEY_UP -> + cycleValue(1); + case GLFW.GLFW_KEY_ENTER, GLFW.GLFW_KEY_SPACE, GLFW.GLFW_KEY_KP_ENTER -> + cycleValue(Screen.hasControlDown() || Screen.hasShiftDown() ? -1 : 1); + default -> { + return false; + } + } + + return true; + } + + @Override + protected int getHoveredControlWidth() { + return getUnhoveredControlWidth(); + } +} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingListController.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingListController.java new file mode 100644 index 0000000..3b14066 --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingListController.java @@ -0,0 +1,79 @@ +package dev.isxander.yacl.gui.controllers.cycling; + +import com.google.common.collect.ImmutableList; +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; + +import java.util.function.Function; + +/** + * A controller where once clicked, cycles through elements + * in the provided list. + */ +public class CyclingListController<T> implements ICyclingController<T> { + private final Option<T> option; + private final Function<T, Text> valueFormatter; + private final ImmutableList<T> values; + + /** + * Constructs a {@link CyclingListController}, with a default + * value formatter of {@link Object#toString()}. + * @param option option of which to bind the controller to + * @param values the values to cycle through + */ + public CyclingListController(Option<T> option, Iterable<T> values) { + this(option, values, value -> Text.of(value.toString())); + } + + /** + * Constructs a {@link CyclingListController} + * @param option option of which to bind the controller to + * @param values the values to cycle through + * @param valueFormatter function of how to convert each value to a string to display + */ + public CyclingListController(Option<T> option, Iterable<T> values, Function<T, Text> valueFormatter) { + this.option = option; + this.valueFormatter = valueFormatter; + this.values = ImmutableList.copyOf(values); + } + + /** + * {@inheritDoc} + */ + @Override + public Option<T> option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + /** + * {@inheritDoc} + */ + @Override + public void setPendingValue(int ordinal) { + option().requestSet(values.get(ordinal)); + } + + /** + * {@inheritDoc} + */ + @Override + public int getPendingValue() { + return values.indexOf(option().pendingValue()); + } + + /** + * {@inheritDoc} + */ + @Override + public int getCycleLength() { + return values.size(); + } +} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/EnumController.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/EnumController.java new file mode 100644 index 0000000..bc9f46d --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/EnumController.java @@ -0,0 +1,60 @@ +package dev.isxander.yacl.gui.controllers.cycling; + +import dev.isxander.yacl.api.NameableEnum; +import dev.isxander.yacl.api.Option; +import net.minecraft.text.Text; +import net.minecraft.util.TranslatableOption; + +import java.util.Arrays; +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>> extends CyclingListController<T> { + 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.toString()); + }; + } + + /** + * 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} or {@link TranslatableOption} else, just uses {@link Enum#toString()} + * + * @param option bound option + */ + public EnumController(Option<T> option) { + this(option, getDefaultFormatter()); + } + + /** + * 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) { + super(option, Arrays.asList(availableValues), valueFormatter); + } +} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/ICyclingController.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/ICyclingController.java new file mode 100644 index 0000000..081b572 --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/ICyclingController.java @@ -0,0 +1,38 @@ +package dev.isxander.yacl.gui.controllers.cycling; + +import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.AbstractWidget; +import dev.isxander.yacl.gui.YACLScreen; + +/** + * This interface simply generifies setting and getting of + * the pending value, using an ordinal so elements can cycle through + * without knowing the content. + */ +public interface ICyclingController<T> extends Controller<T> { + /** + * Sets the pending value to whatever corresponds to the ordinal + * @param ordinal index of element to set + */ + void setPendingValue(int ordinal); + + /** + * Gets the pending ordinal that corresponds to the actual value + * @return ordinal + */ + int getPendingValue(); + + /** + * Allows the element when it should wrap-around back to zeroth ordinal + */ + int getCycleLength(); + + /** + * {@inheritDoc} + */ + @Override + default AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { + return new CyclingControllerElement(this, screen, widgetDimension); + } +} |