diff options
author | xander <xander@isxander.dev> | 2022-09-01 08:57:59 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-01 08:57:59 +0100 |
commit | 6f8ef7daaafd71090b2c334c10eadc8dedc738d9 (patch) | |
tree | d4054a65d99070c944132be83d25e109750dc5f9 /src/main/java/dev/isxander/yacl/gui/controllers/slider | |
parent | 9d0a5e937f97c1c17d034393e01636d5241f376a (diff) | |
download | YetAnotherConfigLib-6f8ef7daaafd71090b2c334c10eadc8dedc738d9.tar.gz YetAnotherConfigLib-6f8ef7daaafd71090b2c334c10eadc8dedc738d9.tar.bz2 YetAnotherConfigLib-6f8ef7daaafd71090b2c334c10eadc8dedc738d9.zip |
GUI Implementation
Added groups
Added button "option"
Added test mod
Diffstat (limited to 'src/main/java/dev/isxander/yacl/gui/controllers/slider')
5 files changed, 370 insertions, 0 deletions
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 new file mode 100644 index 0000000..5aaa6c4 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderControl.java @@ -0,0 +1,72 @@ +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<Double> { + public static final Function<Double, Text> DEFAULT_FORMATTER = value -> Text.of(String.format("%.2f", value)); + + private final Option<Double> option; + + private final double min, max, interval; + + private final Function<Double, Text> valueFormatter; + + public DoubleSliderControl(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) { + 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<Double> 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/FloatSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java new file mode 100644 index 0000000..d74ec33 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderControl.java @@ -0,0 +1,72 @@ +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<Float> { + public static final Function<Float, Text> DEFAULT_FORMATTER = value -> Text.of(String.format("%.1f", value)); + + private final Option<Float> option; + + private final float min, max, interval; + + private final Function<Float, Text> valueFormatter; + + public FloatSliderControl(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) { + 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<Float> 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/ISliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java new file mode 100644 index 0000000..8c1cdf8 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderControl.java @@ -0,0 +1,29 @@ +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/IntegerSliderControl.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java new file mode 100644 index 0000000..99c8137 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderControl.java @@ -0,0 +1,72 @@ +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/SliderControlElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java new file mode 100644 index 0000000..b8dab71 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControlElement.java @@ -0,0 +1,125 @@ +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<ISliderControl<?>> { + private final double min, max, interval; + + private float interpolation; + + private Dimension<Integer> sliderBounds; + + private boolean mouseDown = false; + + public SliderControlElement(ISliderControl<?> option, Screen screen, Dimension<Integer> 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; + } +} |