Skip to content

Commit

Permalink
Moved snapping from ProgressBar to Slider to prevent snapping when se…
Browse files Browse the repository at this point in the history
…tting the value programmatically.
  • Loading branch information
NathanSweet committed Jun 12, 2016
1 parent 7b00689 commit 2ac6f00
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
3 changes: 3 additions & 0 deletions 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
Expand Down
21 changes: 0 additions & 21 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/ProgressBar.java
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
27 changes: 23 additions & 4 deletions gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2ac6f00

Please sign in to comment.