From 2ac6f00b489b0615be44fa69463331a47de5b649 Mon Sep 17 00:00:00 2001 From: NathanSweet Date: Sun, 12 Jun 2016 04:10:07 +0200 Subject: [PATCH] Moved snapping from ProgressBar to Slider to prevent snapping when setting the value programmatically. --- CHANGES | 3 +++ .../gdx/scenes/scene2d/ui/ProgressBar.java | 21 --------------- .../gdx/scenes/scene2d/ui/Slider.java | 27 ++++++++++++++++--- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/CHANGES b/CHANGES index 6baa06959a7..fdfe5e7bf97 100755 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +[1.9.4] +- Moved snapping from ProgressBar to Slider to prevent snapping when setting the value programmatically. + [1.9.3] - Switched to MobiDevelop's RoboVM fork (http://robovm.mobidevelop.com) - Addition of Intel Multi-OS Engine backend for deploying to iOS diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ProgressBar.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ProgressBar.java index ea229f8cf7f..bac9be20b73 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ProgressBar.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ProgressBar.java @@ -49,10 +49,7 @@ public class ProgressBar extends Widget implements Disableable { final boolean vertical; private float animateDuration, animateTime; private Interpolation animateInterpolation = Interpolation.linear; - private float[] snapValues; - private float threshold; boolean disabled; - boolean shiftIgnoresSnap; private Interpolation visualInterpolation = Interpolation.linear; public ProgressBar (float min, float max, float stepSize, boolean vertical, Skin skin) { @@ -233,8 +230,6 @@ protected float getKnobPosition () { * listener. */ public boolean setValue (float value) { value = clamp(Math.round(value / stepSize) * stepSize); - if (!shiftIgnoresSnap || (!Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) && !Gdx.input.isKeyPressed(Keys.SHIFT_RIGHT))) - value = snap(value); float oldValue = this.value; if (value == oldValue) return false; float oldVisualValue = getVisualValue(); @@ -319,22 +314,6 @@ public void setVisualInterpolation (Interpolation interpolation) { this.visualInterpolation = interpolation; } - /** Will make this progress bar snap to the specified values, if the knob is within the threshold. - * @param values May be null. */ - public void setSnapToValues (float[] values, float threshold) { - this.snapValues = values; - this.threshold = threshold; - } - - /** Returns a snapped value. */ - private float snap (float value) { - if (snapValues == null) return value; - for (int i = 0; i < snapValues.length; i++) { - if (Math.abs(value - snapValues[i]) <= threshold) return snapValues[i]; - } - return value; - } - public void setDisabled (boolean disabled) { this.disabled = disabled; } diff --git a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java index f1baa8b303f..b8b6b519c36 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java @@ -16,6 +16,8 @@ package com.badlogic.gdx.scenes.scene2d.ui; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.g2d.NinePatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.Interpolation; @@ -39,6 +41,8 @@ public class Slider extends ProgressBar { int draggingPointer = -1; boolean mouseOver; private Interpolation visualInterpolationInverse = Interpolation.linear; + private float[] snapValues; + private float threshold; public Slider (float min, float max, float stepSize, boolean vertical, Skin skin) { this(min, max, stepSize, vertical, skin.get("default-" + (vertical ? "vertical" : "horizontal"), SliderStyle.class)); @@ -59,8 +63,6 @@ public Slider (float min, float max, float stepSize, boolean vertical, Skin skin public Slider (float min, float max, float stepSize, boolean vertical, SliderStyle style) { super(min, max, stepSize, vertical, style); - shiftIgnoresSnap = true; - addListener(new InputListener() { public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { if (disabled) return false; @@ -112,8 +114,8 @@ public SliderStyle getStyle () { protected Drawable getKnobDrawable () { SliderStyle style = getStyle(); return (disabled && style.disabledKnob != null) ? style.disabledKnob - : (isDragging() && style.knobDown != null) ? style.knobDown : ((mouseOver && style.knobOver != null) ? style.knobOver - : style.knob); + : (isDragging() && style.knobDown != null) ? style.knobDown + : ((mouseOver && style.knobOver != null) ? style.knobOver : style.knob); } boolean calculatePositionAndValue (float x, float y) { @@ -144,11 +146,28 @@ boolean calculatePositionAndValue (float x, float y) { } float oldValue = value; + if (!Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) && !Gdx.input.isKeyPressed(Keys.SHIFT_RIGHT)) value = snap(value); boolean valueSet = setValue(value); if (value == oldValue) position = oldPosition; return valueSet; } + /** Returns a snapped value. */ + private float snap (float value) { + if (snapValues == null) return value; + for (int i = 0; i < snapValues.length; i++) { + if (Math.abs(value - snapValues[i]) <= threshold) return snapValues[i]; + } + return value; + } + + /** Will make this progress bar snap to the specified values, if the knob is within the threshold. + * @param values May be null. */ + public void setSnapToValues (float[] values, float threshold) { + this.snapValues = values; + this.threshold = threshold; + } + /** Returns true if the slider is being dragged. */ public boolean isDragging () { return draggingPointer != -1;