From ea226c9343303483f13f7bc2e098209d4a6b4d02 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 18 Nov 2024 15:49:10 +0000 Subject: [PATCH] Add a selector element (Cycler but it holds generic values) --- .../common/client/gui/element/Cycler.java | 2 - .../common/client/gui/element/Selector.java | 77 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/minelittlepony/common/client/gui/element/Selector.java diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java b/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java index 0d1480e2..766428ce 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Cycler.java @@ -9,8 +9,6 @@ /** * Represents a toggle button that switches between different * styles as you toggle through its different states. - *

- * "Iconic" here refers to how it uses an icon instead of text * * @author Sollace * diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Selector.java b/src/main/java/com/minelittlepony/common/client/gui/element/Selector.java new file mode 100644 index 00000000..cef7d55c --- /dev/null +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Selector.java @@ -0,0 +1,77 @@ +package com.minelittlepony.common.client.gui.element; + +import java.util.function.Function; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import com.minelittlepony.common.client.gui.IField; +import com.minelittlepony.common.client.gui.style.IMultiStyled; +import com.minelittlepony.common.client.gui.style.Style; + +/** + * Represents a toggle button that switches between different + * values as you toggle through its different states. + * + * @author Sollace + * + */ +public class Selector extends Button implements IMultiStyled>, IField> { + private final Style defaultStyle; + private final T defaultValue; + private T value; + + @NotNull + private IChangeCallback action = IChangeCallback::none; + + private final Function styleFunction; + + public Selector(int x, int y, int width, int height, T value, Function styleFunction) { + super(x, y, width, height); + this.defaultValue = value; + this.value = value; + this.styleFunction = styleFunction; + this.defaultStyle = styleFunction.apply(value); + setStyle(this.defaultStyle); + } + + @Override + public Selector onChange(IChangeCallback action) { + this.action = action; + + return this; + } + + @Override + public T getValue() { + return value; + } + + @Override + public Selector setValue(@Nullable T value) { + this.value = value == null ? defaultValue : value; + this.setStyle(styleFunction.apply(this.value)); + + return this; + } + + /** + * Sets the styles to use for each state this toggle is able to be in. + * The number of styles here determines the number of possible states + * and the value is the index to the array of styles. + */ + @Override + public Selector setStyles(Style... styles) { + return this; + } + + @Override + public Style[] getStyles() { + return new Style[] {getStyle()}; + } + + @Override + public void onPress() { + setValue(action.perform(getValue())); + super.onPress(); + } +}