diff options
author | Juuxel <kasperi.kauppi@gmail.com> | 2019-08-28 19:33:43 +0300 |
---|---|---|
committer | Juuxel <kasperi.kauppi@gmail.com> | 2019-08-28 19:33:43 +0300 |
commit | d7cbe76961b9927ced3398761ba448a898cbd782 (patch) | |
tree | 2ea5a7751722700f6944c0a675879271ec3d7bf4 | |
parent | 01092d934ade473813b02825f4ea42749b5ed324 (diff) | |
download | LibGui-d7cbe76961b9927ced3398761ba448a898cbd782.tar.gz LibGui-d7cbe76961b9927ced3398761ba448a898cbd782.tar.bz2 LibGui-d7cbe76961b9927ced3398761ba448a898cbd782.zip |
Fix issues with small value ranges, only fire needed value change events
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java | 4 | ||||
-rw-r--r-- | src/main/java/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); } } |