aboutsummaryrefslogtreecommitdiff
path: root/src/client/java/dev/isxander/yacl/gui/controllers/slider
diff options
context:
space:
mode:
authorXander <xander@isxander.dev>2023-04-25 16:28:41 +0100
committerGitHub <noreply@github.com>2023-04-25 16:28:41 +0100
commit13c7ba45ff201423eb8dba8a40cfb66ebb531439 (patch)
tree1e799aa9da11fbc0833bc6b7c6e6799633c2ec50 /src/client/java/dev/isxander/yacl/gui/controllers/slider
parent8ba7196ae990fe9aa98680aba1b387e385fff99c (diff)
downloadYetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.tar.gz
YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.tar.bz2
YetAnotherConfigLib-13c7ba45ff201423eb8dba8a40cfb66ebb531439.zip
Architectury! (#61)
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui/controllers/slider')
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java114
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java114
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java54
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java111
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java111
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java164
-rw-r--r--src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java10
7 files changed, 0 insertions, 678 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java
deleted file mode 100644
index 8e044b1..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java
+++ /dev/null
@@ -1,114 +0,0 @@
-package dev.isxander.yacl.gui.controllers.slider;
-
-import dev.isxander.yacl.api.Option;
-import net.minecraft.network.chat.Component;
-import org.apache.commons.lang3.Validate;
-
-import java.util.function.Function;
-
-/**
- * {@link ISliderController} for doubles.
- */
-public class DoubleSliderController implements ISliderController<Double> {
- /**
- * Formats doubles to two decimal places
- */
- public static final Function<Double, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,.2f", value).replaceAll("[\u00a0\u202F]", " "));
-
- private final Option<Double> option;
-
- private final double min, max, interval;
-
- private final Function<Double, Component> valueFormatter;
-
- /**
- * 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);
- }
-
- /**
- * 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 Component}
- */
- public DoubleSliderController(Option<Double> option, double min, double max, double interval, Function<Double, Component> valueFormatter) {
- Validate.isTrue(max > min, "`max` cannot be smaller than `min`");
- Validate.isTrue(interval > 0, "`interval` must be more than 0");
- Validate.notNull(valueFormatter, "`valueFormatter` must not be null");
-
- this.option = option;
- this.min = min;
- this.max = max;
- this.interval = interval;
- this.valueFormatter = valueFormatter;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Option<Double> option() {
- return option;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Component 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();
- }
-
-}
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java
deleted file mode 100644
index 25f2206..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java
+++ /dev/null
@@ -1,114 +0,0 @@
-package dev.isxander.yacl.gui.controllers.slider;
-
-import dev.isxander.yacl.api.Option;
-import net.minecraft.network.chat.Component;
-import org.apache.commons.lang3.Validate;
-
-import java.util.function.Function;
-
-/**
- * {@link ISliderController} for floats.
- */
-public class FloatSliderController implements ISliderController<Float> {
- /**
- * Formats floats to one decimal place
- */
- public static final Function<Float, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,.1f", value).replaceAll("[\u00a0\u202F]", " "));
-
- private final Option<Float> option;
-
- private final float min, max, interval;
-
- private final Function<Float, Component> valueFormatter;
-
- /**
- * 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);
- }
-
- /**
- * 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 Component}
- */
- public FloatSliderController(Option<Float> option, float min, float max, float interval, Function<Float, Component> valueFormatter) {
- Validate.isTrue(max > min, "`max` cannot be smaller than `min`");
- Validate.isTrue(interval > 0, "`interval` must be more than 0");
- Validate.notNull(valueFormatter, "`valueFormatter` must not be null");
-
- this.option = option;
- this.min = min;
- this.max = max;
- this.interval = interval;
- this.valueFormatter = valueFormatter;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Option<Float> option() {
- return option;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Component 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();
- }
-
-}
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java
deleted file mode 100644
index aa3c18f..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java
+++ /dev/null
@@ -1,54 +0,0 @@
-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.AbstractWidget;
-import dev.isxander.yacl.gui.YACLScreen;
-
-/**
- * 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 AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) {
- return new SliderControllerElement(this, screen, widgetDimension, min(), max(), interval());
- }
-}
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java
deleted file mode 100644
index 4a68497..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java
+++ /dev/null
@@ -1,111 +0,0 @@
-package dev.isxander.yacl.gui.controllers.slider;
-
-import dev.isxander.yacl.api.Option;
-import net.minecraft.network.chat.Component;
-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, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,d", value).replaceAll("[\u00a0\u202F]", " "));
-
- private final Option<Integer> option;
-
- private final int min, max, interval;
-
- private final Function<Integer, Component> 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 Component}
- */
- public IntegerSliderController(Option<Integer> option, int min, int max, int interval, Function<Integer, Component> valueFormatter) {
- Validate.isTrue(max > min, "`max` cannot be smaller than `min`");
- Validate.isTrue(interval > 0, "`interval` must be more than 0");
- Validate.notNull(valueFormatter, "`valueFormatter` must not be null");
-
- 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 Component 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/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java
deleted file mode 100644
index 681e7cf..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java
+++ /dev/null
@@ -1,111 +0,0 @@
-package dev.isxander.yacl.gui.controllers.slider;
-
-import dev.isxander.yacl.api.Option;
-import net.minecraft.network.chat.Component;
-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, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,d", value).replaceAll("[\u00a0\u202F]", " "));
-
- private final Option<Long> option;
-
- private final long min, max, interval;
-
- private final Function<Long, Component> 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 Component}
- */
- public LongSliderController(Option<Long> option, long min, long max, long interval, Function<Long, Component> valueFormatter) {
- Validate.isTrue(max > min, "`max` cannot be smaller than `min`");
- Validate.isTrue(interval > 0, "`interval` must be more than 0");
- Validate.notNull(valueFormatter, "`valueFormatter` must not be null");
-
- 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 Component 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/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java
deleted file mode 100644
index d00f3d7..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java
+++ /dev/null
@@ -1,164 +0,0 @@
-package dev.isxander.yacl.gui.controllers.slider;
-
-import com.mojang.blaze3d.platform.InputConstants;
-import com.mojang.blaze3d.vertex.PoseStack;
-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.GuiComponent;
-import net.minecraft.client.gui.screens.Screen;
-import net.minecraft.util.Mth;
-
-public class SliderControllerElement extends ControllerWidget<ISliderController<?>> {
- private final double min, max, interval;
-
- private float interpolation;
-
- private Dimension<Integer> sliderBounds;
-
- private boolean mouseDown = false;
-
- public SliderControllerElement(ISliderController<?> option, YACLScreen screen, Dimension<Integer> dim, double min, double max, double interval) {
- super(option, screen, dim);
- this.min = min;
- this.max = max;
- this.interval = interval;
- setDimension(dim);
- }
-
- @Override
- public void render(PoseStack matrices, int mouseX, int mouseY, float delta) {
- super.render(matrices, mouseX, mouseY, delta);
-
- calculateInterpolation();
- }
-
- @Override
- protected void drawHoveredControl(PoseStack matrices, int mouseX, int mouseY, float delta) {
- // track
- GuiComponent.fill(matrices, sliderBounds.x(), sliderBounds.centerY() - 1, sliderBounds.xLimit(), sliderBounds.centerY(), -1);
- // track shadow
- GuiComponent.fill(matrices, sliderBounds.x() + 1, sliderBounds.centerY(), sliderBounds.xLimit() + 1, sliderBounds.centerY() + 1, 0xFF404040);
-
- // thumb shadow
- GuiComponent.fill(matrices, getThumbX() - getThumbWidth() / 2 + 1, sliderBounds.y() + 1, getThumbX() + getThumbWidth() / 2 + 1, sliderBounds.yLimit() + 1, 0xFF404040);
- // thumb
- GuiComponent.fill(matrices, getThumbX() - getThumbWidth() / 2, sliderBounds.y(), getThumbX() + getThumbWidth() / 2, sliderBounds.yLimit(), -1);
- }
-
- @Override
- protected void drawValueText(PoseStack matrices, int mouseX, int mouseY, float delta) {
- matrices.pushPose();
- if (isHovered())
- matrices.translate(-(sliderBounds.width() + 6 + getThumbWidth() / 2f), 0, 0);
- super.drawValueText(matrices, mouseX, mouseY, delta);
- matrices.popPose();
- }
-
- @Override
- public boolean mouseClicked(double mouseX, double mouseY, int button) {
- if (!isAvailable() || button != 0 || !sliderBounds.isPointInside((int) mouseX, (int) mouseY))
- return false;
-
- mouseDown = true;
-
- setValueFromMouse(mouseX);
- return true;
- }
-
- @Override
- public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
- if (!isAvailable() || button != 0 || !mouseDown)
- return false;
-
- setValueFromMouse(mouseX);
- return true;
- }
-
- public void incrementValue(double amount) {
- control.setPendingValue(Mth.clamp(control.pendingValue() + interval * amount, min, max));
- calculateInterpolation();
- }
-
- @Override
- public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
- if (!isAvailable() || (!isMouseOver(mouseX, mouseY)) || (!Screen.hasShiftDown() && !Screen.hasControlDown()))
- return false;
-
- incrementValue(amount);
- return true;
- }
-
- @Override
- public boolean mouseReleased(double mouseX, double mouseY, int button) {
- if (isAvailable() && mouseDown)
- playDownSound();
- mouseDown = false;
-
- return super.mouseReleased(mouseX, mouseY, button);
- }
-
- @Override
- public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
- if (!focused)
- return false;
-
- switch (keyCode) {
- case InputConstants.KEY_LEFT -> incrementValue(-1);
- case InputConstants.KEY_RIGHT -> incrementValue(1);
- default -> {
- return false;
- }
- }
-
- return true;
- }
-
- @Override
- public boolean isMouseOver(double mouseX, double mouseY) {
- return super.isMouseOver(mouseX, mouseY) || mouseDown;
- }
-
- protected void setValueFromMouse(double mouseX) {
- double value = (mouseX - sliderBounds.x()) / sliderBounds.width() * control.range();
- control.setPendingValue(roundToInterval(value));
- calculateInterpolation();
- }
-
- protected double roundToInterval(double value) {
- return Mth.clamp(min + (interval * Math.round(value / interval)), min, max); // extremely imprecise, requires clamping
- }
-
- @Override
- protected int getHoveredControlWidth() {
- return sliderBounds.width() + getUnhoveredControlWidth() + 6 + getThumbWidth() / 2;
- }
-
- protected void calculateInterpolation() {
- interpolation = Mth.clamp((float) ((control.pendingValue() - control.min()) * 1 / control.range()), 0f, 1f);
- }
-
- @Override
- public void setDimension(Dimension<Integer> dim) {
- super.setDimension(dim);
- int trackWidth = dim.width() / 3;
- if (optionNameString.isEmpty())
- trackWidth = dim.width() / 2;
-
- sliderBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - getThumbWidth() / 2 - trackWidth, dim.centerY() - 5, trackWidth, 10);
- }
-
- protected int getThumbX() {
- return (int) (sliderBounds.x() + sliderBounds.width() * interpolation);
- }
-
- protected int getThumbWidth() {
- return 4;
- }
-
- @Override
- public void postRender(PoseStack matrices, int mouseX, int mouseY, float delta) {
- if (super.isMouseOver(mouseX, mouseY) || focused)
- super.postRender(matrices, mouseX, mouseY, delta);
- }
-}
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java
deleted file mode 100644
index bff0d57..0000000
--- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java
+++ /dev/null
@@ -1,10 +0,0 @@
-/**
- * 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;