From: NathanSweet Date: Thu, 30 May 2013 16:15:32 +0000 (+0200) Subject: Added clamp() to allow a min/max range outside the slider's range. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=898d802c98c50675284e16a51be302c9697c8a6f;p=mikumikustudio%2Flibgdx-mikumikustudio.git Added clamp() to allow a min/max range outside the slider's range. --- 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 40b8adcf1..34c98e001 100644 --- a/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java +++ b/gdx/src/com/badlogic/gdx/scenes/scene2d/ui/Slider.java @@ -192,16 +192,16 @@ public class Slider extends Widget { float height = getHeight() - bg.getTopHeight() - bg.getBottomHeight(); float knobHeight = knob == null ? 0 : knob.getMinHeight(); sliderPos = y - bg.getBottomHeight() - knobHeight * 0.5f; + value = min + (max - min) * (sliderPos / (height - knobHeight)); sliderPos = Math.max(0, sliderPos); sliderPos = Math.min(height - knobHeight, sliderPos); - value = min + (max - min) * (sliderPos / (height - knobHeight)); } else { float width = getWidth() - bg.getLeftWidth() - bg.getRightWidth(); float knobWidth = knob == null ? 0 : knob.getMinWidth(); sliderPos = x - bg.getLeftWidth() - knobWidth * 0.5f; + value = min + (max - min) * (sliderPos / (width - knobWidth)); sliderPos = Math.max(0, sliderPos); sliderPos = Math.min(width - knobWidth, sliderPos); - value = min + (max - min) * (sliderPos / (width - knobWidth)); } float oldValue = value; @@ -226,9 +226,10 @@ public class Slider extends Widget { } /** Sets the slider position, rounded to the nearest step size and clamped to the minumum and maximim values. + * {@link #clamp(float)} can be overidden to allow values outside of the sliders min/max range. * @return false if the value was not changed because the slider already had the value or it was canceled by a listener. */ public boolean setValue (float value) { - value = snap(MathUtils.clamp(Math.round(value / stepSize) * stepSize, min, max)); + value = snap(clamp(Math.round(value / stepSize) * stepSize)); float oldValue = this.value; if (value == oldValue) return false; float oldVisualValue = getVisualValue(); @@ -245,6 +246,12 @@ public class Slider extends Widget { return !cancelled; } + /** Clamps the value to the sliders min/max range. This can be overidden to allow a range different from the slider knob's + * range. */ + protected float clamp (float value) { + return MathUtils.clamp(value, min, max); + } + /** Sets the range of this slider. The slider's current value is reset to min. */ public void setRange (float min, float max) { if (min > max) throw new IllegalArgumentException("min must be <= max");