diff options
author | xander <xander@isxander.dev> | 2022-09-01 11:58:49 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-01 11:58:49 +0100 |
commit | 4d977cc9764ecf0073650f126700f6ff638fa06b (patch) | |
tree | 883e68bbd80874c048b3e34db59bf0aa926b489b /src/main/java/dev/isxander/yacl/gui/controllers | |
parent | e63a3c989e3a899bdc81558dd2e4c5cc2c659bde (diff) | |
download | YetAnotherConfigLib-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')
15 files changed, 567 insertions, 205 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ActionControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/ActionControl.java deleted file mode 100644 index 66e8e35..0000000 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ActionControl.java +++ /dev/null @@ -1,58 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import dev.isxander.yacl.api.ButtonOption; -import dev.isxander.yacl.api.Control; -import dev.isxander.yacl.api.utils.Dimension; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; - -public class ActionControl implements Control<Runnable> { - private final ButtonOption option; - private final Text executeText; - - public ActionControl(ButtonOption option) { - this(option, Text.translatable("yacl.control.action.execute")); - } - - public ActionControl(ButtonOption option, Text executeText) { - this.option = option; - this.executeText = executeText; - - } - - @Override - public ButtonOption option() { - return option; - } - - @Override - public Text formatValue() { - return executeText; - } - - @Override - public ControlWidget<ActionControl> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { - return new ActionControlElement(this, screen, widgetDimension); - } - - public static class ActionControlElement extends ControlWidget<ActionControl> { - public ActionControlElement(ActionControl control, Screen screen, Dimension<Integer> dim) { - super(control, screen, dim); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (isMouseOver(mouseX, mouseY)) { - playDownSound(); - control.option().action().run(); - return true; - } - return false; - } - - @Override - protected int getHoveredControlWidth() { - return getUnhoveredControlWidth(); - } - } -} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java b/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java new file mode 100644 index 0000000..b632c5f --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java @@ -0,0 +1,87 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.ButtonOption; +import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.utils.Dimension; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; +import org.jetbrains.annotations.ApiStatus; + +/** + * Simple controller that simply runs the button action on press + * and renders a {@link} Text on the right. + */ +public class ActionController implements Controller<Runnable> { + public static final Text DEFAULT_TEXT = Text.translatable("yacl.control.action.execute"); + + private final ButtonOption option; + private final Text text; + + /** + * Constructs an action controller + * with the default formatter of {@link ActionController#DEFAULT_TEXT} + * + * @param option bound option + */ + public ActionController(ButtonOption option) { + this(option, DEFAULT_TEXT); + } + + /** + * Constructs an action controller + * + * @param option bound option + * @param text text to display + */ + public ActionController(ButtonOption option, Text text) { + this.option = option; + this.text = text; + + } + + /** + * {@inheritDoc} + */ + @Override + public ButtonOption option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return text; + } + + /** + * {@inheritDoc} + */ + @Override + public ControllerWidget<ActionController> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new ActionControllerElement(this, screen, widgetDimension); + } + + @ApiStatus.Internal + public static class ActionControllerElement extends ControllerWidget<ActionController> { + public ActionControllerElement(ActionController control, Screen screen, Dimension<Integer> dim) { + super(control, screen, dim); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (isMouseOver(mouseX, mouseY)) { + playDownSound(); + control.option().action().run(); + return true; + } + return false; + } + + @Override + protected int getHoveredControlWidth() { + return getUnhoveredControlWidth(); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ControlWidget.java b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java index be05ba4..416ee42 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ControlWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java @@ -1,6 +1,6 @@ package dev.isxander.yacl.gui.controllers; -import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.gui.AbstractWidget; import net.minecraft.client.gui.DrawableHelper; @@ -12,9 +12,9 @@ import net.minecraft.text.Text; import java.util.List; -public abstract class ControlWidget<T extends Control<?>> extends AbstractWidget { +public abstract class ControllerWidget<T extends Controller<?>> extends AbstractWidget { protected final T control; - protected final List<OrderedText> wrappedDescription; + protected final List<OrderedText> wrappedTooltip; public Dimension<Integer> dim; protected final Screen screen; @@ -22,11 +22,11 @@ public abstract class ControlWidget<T extends Control<?>> extends AbstractWidget protected boolean hovered = false; protected float hoveredTicks = 0; - public ControlWidget(T control, Screen screen, Dimension<Integer> dim) { + public ControllerWidget(T control, Screen screen, Dimension<Integer> dim) { this.control = control; this.dim = dim; this.screen = screen; - this.wrappedDescription = textRenderer.wrapLines(control.option().tooltip(), screen.width / 2); + this.wrappedTooltip = textRenderer.wrapLines(control.option().tooltip(), screen.width / 2); } @Override @@ -63,7 +63,7 @@ public abstract class ControlWidget<T extends Control<?>> extends AbstractWidget } if (hoveredTicks > 40) { - screen.renderOrderedTooltip(matrices, wrappedDescription, mouseX, mouseY); + screen.renderOrderedTooltip(matrices, wrappedTooltip, mouseX, mouseY); } } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/EnumControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java index d032937..d88401c 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/EnumControl.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java @@ -1,20 +1,36 @@ package dev.isxander.yacl.gui.controllers; -import dev.isxander.yacl.api.Control; +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; -public class EnumControl<T extends Enum<T>> implements Control<T> { +/** + * 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; - public EnumControl(Option<T> option, 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(); @@ -22,31 +38,48 @@ public class EnumControl<T extends Enum<T>> implements Control<T> { }); } - public EnumControl(Option<T> option, Class<T> enumClass, Function<T, Text> valueFormatter) { + /** + * 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 ControlWidget<EnumControl<T>> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { - return new EnumControlElement<>(this, screen, widgetDimension, enumClass.getEnumConstants()); + public ControllerWidget<EnumController<T>> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new EnumControllerElement<>(this, screen, widgetDimension, enumClass.getEnumConstants()); } - public static class EnumControlElement<T extends Enum<T>> extends ControlWidget<EnumControl<T>> { + @ApiStatus.Internal + public static class EnumControllerElement<T extends Enum<T>> extends ControllerWidget<EnumController<T>> { private final T[] values; - public EnumControlElement(EnumControl<T> control, Screen screen, Dimension<Integer> dim, T[] values) { + public EnumControllerElement(EnumController<T> control, Screen screen, Dimension<Integer> dim, T[] values) { super(control, screen, dim); this.values = values; } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java index fb0e595..3bc5c22 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxControl.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java @@ -1,6 +1,6 @@ package dev.isxander.yacl.gui.controllers; -import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.Option; import dev.isxander.yacl.api.utils.Dimension; import net.minecraft.client.gui.DrawableHelper; @@ -8,10 +8,14 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; -public class TickBoxControl implements Control<Boolean> { +/** + * On hover, this controller renders a tickbox, otherwise, formatted {@link Text} + */ +public class TickBoxController implements Controller<Boolean> { public static final Function<Boolean, Text> ON_OFF_FORMATTER = (state) -> state @@ -31,33 +35,55 @@ public class TickBoxControl implements Control<Boolean> { private final Option<Boolean> option; private final Function<Boolean, Text> valueFormatter; - public TickBoxControl(Option<Boolean> option) { + /** + * Constructs a tickbox controller + * with the default value formatter of {@link TickBoxController#ON_OFF_FORMATTER} + * + * @param option bound option + */ + public TickBoxController(Option<Boolean> option) { this(option, ON_OFF_FORMATTER); } - public TickBoxControl(Option<Boolean> option, Function<Boolean, Text> valueFormatter) { + /** + * Constructs a tickbox controller + * + * @param option bound option + * @param valueFormatter format value into any {@link Text} + */ + public TickBoxController(Option<Boolean> option, Function<Boolean, Text> valueFormatter) { this.option = option; this.valueFormatter = valueFormatter; } + /** + * {@inheritDoc} + */ @Override public Option<Boolean> option() { return option; } + /** + * {@inheritDoc} + */ @Override public Text formatValue() { return valueFormatter.apply(option().pendingValue()); } + /** + * {@inheritDoc} + */ @Override - public ControlWidget<TickBoxControl> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { - return new TickBoxControlElement(this, screen, widgetDimension); + public ControllerWidget<TickBoxController> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new TickBoxControllerElement(this, screen, widgetDimension); } - public static class TickBoxControlElement extends ControlWidget<TickBoxControl> { - private TickBoxControlElement(TickBoxControl control, Screen screen, Dimension<Integer> dim) { + @ApiStatus.Internal + public static class TickBoxControllerElement extends ControllerWidget<TickBoxController> { + private TickBoxControllerElement(TickBoxController control, Screen screen, Dimension<Integer> dim) { super(control, screen, dim); } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/package-info.java b/src/main/java/dev/isxander/yacl/gui/controllers/package-info.java new file mode 100644 index 0000000..fcfc8e5 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/package-info.java @@ -0,0 +1,11 @@ +/** + * This package contains all {@link dev.isxander.yacl.api.Controller} implementations + * + * <ul> + * <li>For numbers: {@link dev.isxander.yacl.gui.controllers.slider}</li> + * <li>For booleans: {@link dev.isxander.yacl.gui.controllers.TickBoxController}</li> + * <li>For enums: {@link dev.isxander.yacl.gui.controllers.EnumController}</li> + * <li>For {@link dev.isxander.yacl.api.ButtonOption}: {@link dev.isxander.yacl.gui.controllers.ActionController}</li> + * </ul> + */ +package dev.isxander.yacl.gui.controllers; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java index 5aaa6c4..8d74ceb 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java @@ -6,7 +6,13 @@ import org.apache.commons.lang3.Validate; import java.util.function.Function; -public class DoubleSliderControl implements ISliderControl<Double> { +/** + * {@link ISliderController} for doubles. + */ +public class DoubleSliderController implements ISliderController<Double> { + /** + * Formats doubles to two decimal places + */ public static final Function<Double, Text> DEFAULT_FORMATTER = value -> Text.of(String.format("%.2f", value)); private final Option<Double> option; @@ -15,11 +21,29 @@ public class DoubleSliderControl implements ISliderControl<Double> { private final Function<Double, Text> valueFormatter; - public DoubleSliderControl(Option<Double> option, double min, double max, double interval) { + /** + * Constructs a {@link ISliderController} for doubles + * using the default value formatter {@link DoubleSliderController#DEFAULT_FORMATTER}. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + */ + public DoubleSliderController(Option<Double> option, double min, double max, double interval) { this(option, min, max, interval, DEFAULT_FORMATTER); } - public DoubleSliderControl(Option<Double> option, double min, double max, double interval, Function<Double, Text> valueFormatter) { + /** + * Constructs a {@link ISliderController} for doubles. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + * @param valueFormatter format the value into any {@link Text} + */ + public DoubleSliderController(Option<Double> option, double min, double max, double interval, Function<Double, Text> valueFormatter) { Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); Validate.isTrue(interval > 0, "`interval` must be more than 0"); @@ -30,43 +54,60 @@ public class DoubleSliderControl implements ISliderControl<Double> { this.valueFormatter = valueFormatter; } + /** + * {@inheritDoc} + */ @Override public Option<Double> option() { return option; } + /** + * {@inheritDoc} + */ @Override public Text formatValue() { return valueFormatter.apply(option().pendingValue()); } + /** + * {@inheritDoc} + */ @Override public double min() { return min; } + /** + * {@inheritDoc} + */ @Override public double max() { return max; } + /** + * {@inheritDoc} + */ @Override public double interval() { return interval; } + /** + * {@inheritDoc} + */ @Override public void setPendingValue(double value) { option().requestSet(value); } + /** + * {@inheritDoc} + */ @Override public double pendingValue() { return option().pendingValue(); } - @Override - public Text getValueText(double value) { - return valueFormatter.apply(value); - } } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java index d74ec33..6f4192b 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java @@ -6,7 +6,13 @@ import org.apache.commons.lang3.Validate; import java.util.function.Function; -public class FloatSliderControl implements ISliderControl<Float> { +/** + * {@link ISliderController} for floats. + */ +public class FloatSliderController implements ISliderController<Float> { + /** + * Formats floats to one decimal place + */ public static final Function<Float, Text> DEFAULT_FORMATTER = value -> Text.of(String.format("%.1f", value)); private final Option<Float> option; @@ -15,11 +21,29 @@ public class FloatSliderControl implements ISliderControl<Float> { private final Function<Float, Text> valueFormatter; - public FloatSliderControl(Option<Float> option, float min, float max, float interval) { + /** + * Constructs a {@link ISliderController} for floats + * using the default value formatter {@link FloatSliderController#DEFAULT_FORMATTER}. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + */ + public FloatSliderController(Option<Float> option, float min, float max, float interval) { this(option, min, max, interval, DEFAULT_FORMATTER); } - public FloatSliderControl(Option<Float> option, float min, float max, float interval, Function<Float, Text> valueFormatter) { + /** + * Constructs a {@link ISliderController} for floats. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + * @param valueFormatter format the value into any {@link Text} + */ + public FloatSliderController(Option<Float> option, float min, float max, float interval, Function<Float, Text> valueFormatter) { Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); Validate.isTrue(interval > 0, "`interval` must be more than 0"); @@ -30,43 +54,60 @@ public class FloatSliderControl implements ISliderControl<Float> { this.valueFormatter = valueFormatter; } + /** + * {@inheritDoc} + */ @Override public Option<Float> option() { return option; } + /** + * {@inheritDoc} + */ @Override public Text formatValue() { return valueFormatter.apply(option().pendingValue()); } + /** + * {@inheritDoc} + */ @Override public double min() { return min; } + /** + * {@inheritDoc} + */ @Override public double max() { return max; } + /** + * {@inheritDoc} + */ @Override public double interval() { return interval; } + /** + * {@inheritDoc} + */ @Override public void setPendingValue(double value) { option().requestSet((float) value); } + /** + * {@inheritDoc} + */ @Override public double pendingValue() { return option().pendingValue(); } - @Override - public Text getValueText(double value) { - return valueFormatter.apply((float) value); - } } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java deleted file mode 100644 index 8c1cdf8..0000000 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java +++ /dev/null @@ -1,29 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Control; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.controllers.ControlWidget; -import net.minecraft.client.gui.screen.Screen; -import net.minecraft.text.Text; - -public interface ISliderControl<T extends Number> extends Control<T> { - double min(); - - double max(); - - double interval(); - - default double range() { - return max() - min(); - } - - void setPendingValue(double value); - double pendingValue(); - - Text getValueText(double value); - - @Override - default ControlWidget<?> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { - return new SliderControlElement(this, screen, widgetDimension, min(), max(), interval()); - } -} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java new file mode 100644 index 0000000..0f6c2d5 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java @@ -0,0 +1,54 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.controllers.ControllerWidget; +import net.minecraft.client.gui.screen.Screen; + +/** + * Simple custom slider implementation that shifts the current value across when shown. + * <p> + * For simplicity, {@link SliderControllerElement} works in doubles so each + * {@link ISliderController} must cast to double. This is to get around re-writing the element for every type. + */ +public interface ISliderController<T extends Number> extends Controller<T> { + /** + * Gets the minimum value for the slider + */ + double min(); + + /** + * Gets the maximum value for the slider + */ + double max(); + + /** + * Gets the interval (or step size) for the slider. + */ + double interval(); + + /** + * Gets the range of the slider. + */ + default double range() { + return max() - min(); + } + + /** + * Sets the {@link dev.isxander.yacl.api.Option}'s pending value + */ + void setPendingValue(double value); + + /** + * Gets the {@link dev.isxander.yacl.api.Option}'s pending value + */ + double pendingValue(); + + /** + * {@inheritDoc} + */ + @Override + default ControllerWidget<?> provideWidget(Screen screen, Dimension<Integer> widgetDimension) { + return new SliderControllerElement(this, screen, widgetDimension, min(), max(), interval()); + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java deleted file mode 100644 index 99c8137..0000000 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java +++ /dev/null @@ -1,72 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Option; -import net.minecraft.text.Text; -import org.apache.commons.lang3.Validate; - -import java.util.function.Function; - -public class IntegerSliderControl implements ISliderControl<Integer> { - public static final Function<Integer, Text> DEFAULT_FORMATTER = value -> Text.of(String.valueOf(value)); - - private final Option<Integer> option; - - private final int min, max, interval; - - private final Function<Integer, Text> valueFormatter; - - public IntegerSliderControl(Option<Integer> option, int min, int max, int interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - public IntegerSliderControl(Option<Integer> option, int min, int max, int interval, Function<Integer, Text> valueFormatter) { - Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); - Validate.isTrue(interval > 0, "`interval` must be more than 0"); - - this.option = option; - this.min = min; - this.max = max; - this.interval = interval; - this.valueFormatter = valueFormatter; - } - - @Override - public Option<Integer> option() { - return option; - } - - @Override - public Text formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - @Override - public double min() { - return min; - } - - @Override - public double max() { - return max; - } - - @Override - public double interval() { - return interval; - } - - @Override - public void setPendingValue(double value) { - option().requestSet((int) value); - } - - @Override - public double pendingValue() { - return option().pendingValue(); - } - - @Override - public Text getValueText(double value) { - return valueFormatter.apply((int) value); - } -} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java new file mode 100644 index 0000000..0d0d7b9 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java @@ -0,0 +1,111 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.impl.utils.NumberFormatHelper; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; + +import java.util.function.Function; + +/** + * {@link ISliderController} for integers. + */ +public class IntegerSliderController implements ISliderController<Integer> { + public static final Function<Integer, Text> DEFAULT_FORMATTER = value -> Text.of(NumberFormatHelper.formatSmaller(value)); + + private final Option<Integer> option; + + private final int min, max, interval; + + private final Function<Integer, Text> valueFormatter; + + /** + * Constructs a {@link ISliderController} for integers + * using the default value formatter {@link IntegerSliderController#DEFAULT_FORMATTER}. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + */ + public IntegerSliderController(Option<Integer> option, int min, int max, int interval) { + this(option, min, max, interval, DEFAULT_FORMATTER); + } + + /** + * Constructs a {@link ISliderController} for integers. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + * @param valueFormatter format the value into any {@link Text} + */ + public IntegerSliderController(Option<Integer> option, int min, int max, int interval, Function<Integer, Text> valueFormatter) { + Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); + Validate.isTrue(interval > 0, "`interval` must be more than 0"); + + this.option = option; + this.min = min; + this.max = max; + this.interval = interval; + this.valueFormatter = valueFormatter; + } + + /** + * {@inheritDoc} + */ + @Override + public Option<Integer> option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + /** + * {@inheritDoc} + */ + @Override + public double min() { + return min; + } + + /** + * {@inheritDoc} + */ + @Override + public double max() { + return max; + } + + /** + * {@inheritDoc} + */ + @Override + public double interval() { + return interval; + } + + /** + * {@inheritDoc} + */ + @Override + public void setPendingValue(double value) { + option().requestSet((int) value); + } + + /** + * {@inheritDoc} + */ + @Override + public double pendingValue() { + return option().pendingValue(); + } + +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java new file mode 100644 index 0000000..d4c71d1 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java @@ -0,0 +1,111 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.impl.utils.NumberFormatHelper; +import net.minecraft.text.Text; +import org.apache.commons.lang3.Validate; + +import java.util.function.Function; + +/** + * {@link ISliderController} for longs. + */ +public class LongSliderController implements ISliderController<Long> { + public static final Function<Long, Text> DEFAULT_FORMATTER = value -> Text.of(NumberFormatHelper.formatSmaller(value)); + + private final Option<Long> option; + + private final long min, max, interval; + + private final Function<Long, Text> valueFormatter; + + /** + * Constructs a {@link ISliderController} for longs + * using the default value formatter {@link LongSliderController#DEFAULT_FORMATTER}. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + */ + public LongSliderController(Option<Long> option, long min, long max, long interval) { + this(option, min, max, interval, DEFAULT_FORMATTER); + } + + /** + * Constructs a {@link ISliderController} for longs. + * + * @param option bound option + * @param min minimum slider value + * @param max maximum slider value + * @param interval step size (or increments) for the slider + * @param valueFormatter format the value into any {@link Text} + */ + public LongSliderController(Option<Long> option, long min, long max, long interval, Function<Long, Text> valueFormatter) { + Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); + Validate.isTrue(interval > 0, "`interval` must be more than 0"); + + this.option = option; + this.min = min; + this.max = max; + this.interval = interval; + this.valueFormatter = valueFormatter; + } + + /** + * {@inheritDoc} + */ + @Override + public Option<Long> option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return valueFormatter.apply(option().pendingValue()); + } + + /** + * {@inheritDoc} + */ + @Override + public double min() { + return min; + } + + /** + * {@inheritDoc} + */ + @Override + public double max() { + return max; + } + + /** + * {@inheritDoc} + */ + @Override + public double interval() { + return interval; + } + + /** + * {@inheritDoc} + */ + @Override + public void setPendingValue(double value) { + option().requestSet((long) value); + } + + /** + * {@inheritDoc} + */ + @Override + public double pendingValue() { + return option().pendingValue(); + } + +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java index b8dab71..139ef39 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java @@ -1,14 +1,15 @@ package dev.isxander.yacl.gui.controllers.slider; import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.controllers.ControlWidget; +import dev.isxander.yacl.gui.controllers.ControllerWidget; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; +import org.jetbrains.annotations.ApiStatus; -public class SliderControlElement extends ControlWidget<ISliderControl<?>> { +@ApiStatus.Internal +public class SliderControllerElement extends ControllerWidget<ISliderController<?>> { private final double min, max, interval; private float interpolation; @@ -17,7 +18,7 @@ public class SliderControlElement extends ControlWidget<ISliderControl<?>> { private boolean mouseDown = false; - public SliderControlElement(ISliderControl<?> option, Screen screen, Dimension<Integer> dim, double min, double max, double interval) { + public SliderControllerElement(ISliderController<?> option, Screen screen, Dimension<Integer> dim, double min, double max, double interval) { super(option, screen, dim); this.min = min; this.max = max; @@ -103,18 +104,13 @@ public class SliderControlElement extends ControlWidget<ISliderControl<?>> { @Override protected int getHoveredControlWidth() { - int textWidth = textRenderer.getWidth(getValueText()); - return hovered ? sliderBounds.width() + textWidth + 6 + getThumbWidth() / 2 : textWidth ; + return sliderBounds.width() + getUnhoveredControlWidth() + 6 + getThumbWidth() / 2; } private void calculateInterpolation() { interpolation = (float) ((control.pendingValue() - control.min()) * 1 / control.range()); } - private Text getValueText() { - return control.getValueText(control.pendingValue()); - } - private int getThumbX() { return (int) (sliderBounds.x() + sliderBounds.width() * interpolation); } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/package-info.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/package-info.java new file mode 100644 index 0000000..bff0d57 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/package-info.java @@ -0,0 +1,10 @@ +/** + * This package contains implementations of sliders for different number types + * <ul> + * <li>For doubles: {@link dev.isxander.yacl.gui.controllers.slider.DoubleSliderController}</li> + * <li>For floats: {@link dev.isxander.yacl.gui.controllers.slider.FloatSliderController}</li> + * <li>For integers: {@link dev.isxander.yacl.gui.controllers.slider.IntegerSliderController}</li> + * <li>For longs: {@link dev.isxander.yacl.gui.controllers.slider.LongSliderController}</li> + * </ul> + */ +package dev.isxander.yacl.gui.controllers.slider; |