From d7cbe76961b9927ced3398761ba448a898cbd782 Mon Sep 17 00:00:00 2001 From: Juuxel Date: Wed, 28 Aug 2019 19:33:43 +0300 Subject: Fix issues with small value ranges, only fire needed value change events --- .../cottonmc/cotton/gui/client/modmenu/ConfigGui.java | 4 ++-- .../io/github/cottonmc/cotton/gui/widget/WSlider.java | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java index 674cb32..2bca040 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java @@ -32,8 +32,8 @@ public class ConfigGui extends LightweightGuiDescription { WTextField testField = new WTextField(); testField.setSuggestion("test"); root.add(testField, 0, 3, 4, 1); - root.add(new WSlider(-150, -123, Axis.VERTICAL).setValueChangeListener(System.out::println), 6, 0, 1, 3); - root.add(new WSlider(-150, -123, Axis.HORIZONTAL).setValueChangeListener(System.out::println), 1, 4, 4, 1); + root.add(new WSlider(-1, 1, Axis.VERTICAL).setValueChangeListener(System.out::println), 6, 0, 1, 3); + root.add(new WSlider(-1, 0, Axis.HORIZONTAL).setValueChangeListener(System.out::println), 1, 4, 4, 1); root.add(new WKirbSprite(), 5, 4); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index d253cea..26574dd 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -33,7 +33,17 @@ public class WSlider extends WWidget { private final Axis axis; private int value; - private float valueToCoordRatio, coordToValueRatio; + + /** + * A value:coordinate ratio. Used for converting user input into values. + */ + private float valueToCoordRatio; + + /** + * A coordinate:value ratio. Used for rendering the thumb. + */ + private float coordToValueRatio; + @Nullable private IntConsumer valueChangeListener = null; @Nullable private Runnable focusReleaseListener = null; @@ -47,7 +57,7 @@ public class WSlider extends WWidget { this.min = min; this.max = max; - this.valueRange = max - min + 1; + this.valueRange = max - min; this.axis = axis; this.value = min; } @@ -92,8 +102,9 @@ public class WSlider extends WWidget { if (isFocused()) { int pos = (axis == Axis.VERTICAL ? (height - y) : x) - THUMB_SIZE / 2; int rawValue = min + (int) (valueToCoordRatio * pos); + int previousValue = value; value = MathHelper.clamp(rawValue, min, max); - if (valueChangeListener != null) valueChangeListener.accept(value); + if (value != previousValue && valueChangeListener != null) valueChangeListener.accept(value); } } -- cgit