From 4d977cc9764ecf0073650f126700f6ff638fa06b Mon Sep 17 00:00:00 2001 From: xander Date: Thu, 1 Sep 2022 11:58:49 +0100 Subject: javadoc! added LongSliderController renamed Control -> Controller add minecraft simple option binding constructor --- .../controllers/slider/DoubleSliderControl.java | 72 ------------ .../controllers/slider/DoubleSliderController.java | 113 +++++++++++++++++++ .../gui/controllers/slider/FloatSliderControl.java | 72 ------------ .../controllers/slider/FloatSliderController.java | 113 +++++++++++++++++++ .../gui/controllers/slider/ISliderControl.java | 29 ----- .../gui/controllers/slider/ISliderController.java | 54 +++++++++ .../controllers/slider/IntegerSliderControl.java | 72 ------------ .../slider/IntegerSliderController.java | 111 ++++++++++++++++++ .../controllers/slider/LongSliderController.java | 111 ++++++++++++++++++ .../controllers/slider/SliderControlElement.java | 125 --------------------- .../slider/SliderControllerElement.java | 121 ++++++++++++++++++++ .../yacl/gui/controllers/slider/package-info.java | 10 ++ 12 files changed, 633 insertions(+), 370 deletions(-) delete mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java delete mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java delete mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java delete mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java delete mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java create mode 100644 src/main/java/dev/isxander/yacl/gui/controllers/slider/package-info.java (limited to 'src/main/java/dev/isxander/yacl/gui/controllers/slider') diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java deleted file mode 100644 index 5aaa6c4..0000000 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.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 DoubleSliderControl implements ISliderControl { - public static final Function DEFAULT_FORMATTER = value -> Text.of(String.format("%.2f", value)); - - private final Option option; - - private final double min, max, interval; - - private final Function valueFormatter; - - public DoubleSliderControl(Option option, double min, double max, double interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - public DoubleSliderControl(Option option, double min, double max, double interval, Function 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 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(value); - } - - @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/DoubleSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java new file mode 100644 index 0000000..8d74ceb --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java @@ -0,0 +1,113 @@ +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; + +/** + * {@link ISliderController} for doubles. + */ +public class DoubleSliderController implements ISliderController { + /** + * Formats doubles to two decimal places + */ + public static final Function DEFAULT_FORMATTER = value -> Text.of(String.format("%.2f", value)); + + private final Option option; + + private final double min, max, interval; + + private final Function 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 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 Text} + */ + public DoubleSliderController(Option option, double min, double max, double interval, Function 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 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(); + } + +} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java deleted file mode 100644 index d74ec33..0000000 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.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 FloatSliderControl implements ISliderControl { - public static final Function DEFAULT_FORMATTER = value -> Text.of(String.format("%.1f", value)); - - private final Option option; - - private final float min, max, interval; - - private final Function valueFormatter; - - public FloatSliderControl(Option option, float min, float max, float interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - public FloatSliderControl(Option option, float min, float max, float interval, Function 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 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((float) value); - } - - @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/FloatSliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java new file mode 100644 index 0000000..6f4192b --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java @@ -0,0 +1,113 @@ +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; + +/** + * {@link ISliderController} for floats. + */ +public class FloatSliderController implements ISliderController { + /** + * Formats floats to one decimal place + */ + public static final Function DEFAULT_FORMATTER = value -> Text.of(String.format("%.1f", value)); + + private final Option option; + + private final float min, max, interval; + + private final Function 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 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 Text} + */ + public FloatSliderController(Option option, float min, float max, float interval, Function 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 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(); + } + +} 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 extends Control { - 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 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. + *

+ * 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 extends Controller { + /** + * 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 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 { - public static final Function DEFAULT_FORMATTER = value -> Text.of(String.valueOf(value)); - - private final Option option; - - private final int min, max, interval; - - private final Function valueFormatter; - - public IntegerSliderControl(Option option, int min, int max, int interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - public IntegerSliderControl(Option option, int min, int max, int interval, Function 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 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 { + public static final Function DEFAULT_FORMATTER = value -> Text.of(NumberFormatHelper.formatSmaller(value)); + + private final Option option; + + private final int min, max, interval; + + private final Function 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 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 option, int min, int max, int interval, Function 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 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 { + public static final Function DEFAULT_FORMATTER = value -> Text.of(NumberFormatHelper.formatSmaller(value)); + + private final Option option; + + private final long min, max, interval; + + private final Function 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 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 option, long min, long max, long interval, Function 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 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/SliderControlElement.java deleted file mode 100644 index b8dab71..0000000 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java +++ /dev/null @@ -1,125 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.controllers.ControlWidget; -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; - -public class SliderControlElement extends ControlWidget> { - private final double min, max, interval; - - private float interpolation; - - private Dimension sliderBounds; - - private boolean mouseDown = false; - - public SliderControlElement(ISliderControl option, Screen screen, Dimension dim, double min, double max, double interval) { - super(option, screen, dim); - this.min = min; - this.max = max; - this.interval = interval; - } - - @Override - public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { - super.render(matrices, mouseX, mouseY, delta); - - if (sliderBounds == null) - sliderBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - getThumbWidth() / 2 - dim.width() / 3, dim.centerY() - 4, dim.width() / 3, 8); - calculateInterpolation(); - } - - @Override - protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { - // track - DrawableHelper.fill(matrices, sliderBounds.x(), sliderBounds.centerY() - 1, sliderBounds.xLimit(), sliderBounds.centerY(), -1); - // track shadow - DrawableHelper.fill(matrices, sliderBounds.x() + 1, sliderBounds.centerY(), sliderBounds.xLimit() + 1, sliderBounds.centerY() + 1, 0xFF404040); - - // thumb shadow - DrawableHelper.fill(matrices, getThumbX() - getThumbWidth() / 2 + 1, sliderBounds.y() + 1, getThumbX() + getThumbWidth() / 2 + 1, sliderBounds.yLimit() + 1, 0xFF404040); - // thumb - DrawableHelper.fill(matrices, getThumbX() - getThumbWidth() / 2, sliderBounds.y(), getThumbX() + getThumbWidth() / 2, sliderBounds.yLimit(), -1); - } - - @Override - protected void drawValueText(MatrixStack matrices) { - matrices.push(); - if (hovered) - matrices.translate(-(sliderBounds.width() + 6 + getThumbWidth() / 2f), 0, 0); - super.drawValueText(matrices); - matrices.pop(); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (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 (button != 0 || !sliderBounds.isPointInside((int) mouseX, (int) mouseY)) - return false; - - setValueFromMouse(mouseX); - return true; - } - - @Override - public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - if (!isMouseOver(mouseX, mouseY) || !Screen.hasShiftDown()) - return false; - - control.setPendingValue(MathHelper.clamp(control.pendingValue() + interval * amount, min, max)); - calculateInterpolation(); - return true; - } - - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - if (mouseDown) - playDownSound(); - mouseDown = false; - - return super.mouseReleased(mouseX, mouseY, button); - } - - private void setValueFromMouse(double mouseX) { - double value = (mouseX - sliderBounds.x()) / sliderBounds.width() * control.range(); - double roundedValue = min + (interval * Math.round(value / interval)); - control.setPendingValue(roundedValue); - calculateInterpolation(); - } - - @Override - protected int getHoveredControlWidth() { - int textWidth = textRenderer.getWidth(getValueText()); - return hovered ? sliderBounds.width() + textWidth + 6 + getThumbWidth() / 2 : textWidth ; - } - - 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); - } - - private int getThumbWidth() { - return 4; - } -} diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java new file mode 100644 index 0000000..139ef39 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java @@ -0,0 +1,121 @@ +package dev.isxander.yacl.gui.controllers.slider; + +import dev.isxander.yacl.api.utils.Dimension; +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.util.math.MathHelper; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +public class SliderControllerElement extends ControllerWidget> { + private final double min, max, interval; + + private float interpolation; + + private Dimension sliderBounds; + + private boolean mouseDown = false; + + public SliderControllerElement(ISliderController option, Screen screen, Dimension dim, double min, double max, double interval) { + super(option, screen, dim); + this.min = min; + this.max = max; + this.interval = interval; + } + + @Override + public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { + super.render(matrices, mouseX, mouseY, delta); + + if (sliderBounds == null) + sliderBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - getThumbWidth() / 2 - dim.width() / 3, dim.centerY() - 4, dim.width() / 3, 8); + calculateInterpolation(); + } + + @Override + protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { + // track + DrawableHelper.fill(matrices, sliderBounds.x(), sliderBounds.centerY() - 1, sliderBounds.xLimit(), sliderBounds.centerY(), -1); + // track shadow + DrawableHelper.fill(matrices, sliderBounds.x() + 1, sliderBounds.centerY(), sliderBounds.xLimit() + 1, sliderBounds.centerY() + 1, 0xFF404040); + + // thumb shadow + DrawableHelper.fill(matrices, getThumbX() - getThumbWidth() / 2 + 1, sliderBounds.y() + 1, getThumbX() + getThumbWidth() / 2 + 1, sliderBounds.yLimit() + 1, 0xFF404040); + // thumb + DrawableHelper.fill(matrices, getThumbX() - getThumbWidth() / 2, sliderBounds.y(), getThumbX() + getThumbWidth() / 2, sliderBounds.yLimit(), -1); + } + + @Override + protected void drawValueText(MatrixStack matrices) { + matrices.push(); + if (hovered) + matrices.translate(-(sliderBounds.width() + 6 + getThumbWidth() / 2f), 0, 0); + super.drawValueText(matrices); + matrices.pop(); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (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 (button != 0 || !sliderBounds.isPointInside((int) mouseX, (int) mouseY)) + return false; + + setValueFromMouse(mouseX); + return true; + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + if (!isMouseOver(mouseX, mouseY) || !Screen.hasShiftDown()) + return false; + + control.setPendingValue(MathHelper.clamp(control.pendingValue() + interval * amount, min, max)); + calculateInterpolation(); + return true; + } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + if (mouseDown) + playDownSound(); + mouseDown = false; + + return super.mouseReleased(mouseX, mouseY, button); + } + + private void setValueFromMouse(double mouseX) { + double value = (mouseX - sliderBounds.x()) / sliderBounds.width() * control.range(); + double roundedValue = min + (interval * Math.round(value / interval)); + control.setPendingValue(roundedValue); + calculateInterpolation(); + } + + @Override + protected int getHoveredControlWidth() { + return sliderBounds.width() + getUnhoveredControlWidth() + 6 + getThumbWidth() / 2; + } + + private void calculateInterpolation() { + interpolation = (float) ((control.pendingValue() - control.min()) * 1 / control.range()); + } + + private int getThumbX() { + return (int) (sliderBounds.x() + sliderBounds.width() * interpolation); + } + + private int getThumbWidth() { + return 4; + } +} 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 + *

    + *
  • For doubles: {@link dev.isxander.yacl.gui.controllers.slider.DoubleSliderController}
  • + *
  • For floats: {@link dev.isxander.yacl.gui.controllers.slider.FloatSliderController}
  • + *
  • For integers: {@link dev.isxander.yacl.gui.controllers.slider.IntegerSliderController}
  • + *
  • For longs: {@link dev.isxander.yacl.gui.controllers.slider.LongSliderController}
  • + *
+ */ +package dev.isxander.yacl.gui.controllers.slider; -- cgit