From ecfa5d10409f9146ffe2750614f61b2d02dff8c4 Mon Sep 17 00:00:00 2001 From: jules Date: Thu, 4 Feb 2016 09:38:20 +0000 Subject: [PATCH] Tidied up some rotary parameter handling code in Slider --- .../juce_gui_basics/widgets/juce_Slider.cpp | 71 ++++++++++--------- modules/juce_gui_basics/widgets/juce_Slider.h | 41 +++++++---- 2 files changed, 64 insertions(+), 48 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index 8fa7589367..8214833ae3 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -35,8 +35,6 @@ public: minimum (0), maximum (10), interval (0), doubleClickReturnValue (0), skewFactor (1.0), velocityModeSensitivity (1.0), velocityModeOffset (0.0), velocityModeThreshold (1), - rotaryStart (float_Pi * 1.2f), - rotaryEnd (float_Pi * 2.8f), sliderRegionStart (0), sliderRegionSize (1), sliderBeingDragged (-1), pixelsForFullDragExtent (250), textBoxPos (textBoxPosition), @@ -47,7 +45,6 @@ public: doubleClickToValue (false), isVelocityBased (false), userKeyOverridesVelocity (true), - rotaryStop (true), incDecButtonsSideBySide (false), sendChangeOnlyOnRelease (false), popupDisplayEnabled (false), @@ -57,6 +54,9 @@ public: snapsToMousePos (true), parentForPopupDisplay (nullptr) { + rotaryParams.startAngleRadians = float_Pi * 1.2f; + rotaryParams.endAngleRadians = float_Pi * 2.8f; + rotaryParams.stopAtEnd = true; } ~Pimpl() @@ -465,20 +465,6 @@ public: } } - void setRotaryParameters (const float startAngleRadians, - const float endAngleRadians, - const bool stopAtEnd) - { - // make sure the values are sensible.. - jassert (startAngleRadians >= 0 && endAngleRadians >= 0); - jassert (startAngleRadians < float_Pi * 4.0f && endAngleRadians < float_Pi * 4.0f); - jassert (startAngleRadians < endAngleRadians); - - rotaryStart = startAngleRadians; - rotaryEnd = endAngleRadians; - rotaryStop = stopAtEnd; - } - void setVelocityModeParameters (const double sensitivity, const int threshold, const double offset, const bool userCanPressKeyToSwapMode) { @@ -707,7 +693,7 @@ public: while (angle < 0.0) angle += double_Pi * 2.0; - if (rotaryStop && ! e.mouseWasClicked()) + if (rotaryParams.stopAtEnd && ! e.mouseWasClicked()) { if (std::abs (angle - lastAngle) > double_Pi) { @@ -718,26 +704,26 @@ public: } if (angle >= lastAngle) - angle = jmin (angle, (double) jmax (rotaryStart, rotaryEnd)); + angle = jmin (angle, (double) jmax (rotaryParams.startAngleRadians, rotaryParams.endAngleRadians)); else - angle = jmax (angle, (double) jmin (rotaryStart, rotaryEnd)); + angle = jmax (angle, (double) jmin (rotaryParams.startAngleRadians, rotaryParams.endAngleRadians)); } else { - while (angle < rotaryStart) + while (angle < rotaryParams.startAngleRadians) angle += double_Pi * 2.0; - if (angle > rotaryEnd) + if (angle > rotaryParams.endAngleRadians) { - if (smallestAngleBetween (angle, rotaryStart) - <= smallestAngleBetween (angle, rotaryEnd)) - angle = rotaryStart; + if (smallestAngleBetween (angle, rotaryParams.startAngleRadians) + <= smallestAngleBetween (angle, rotaryParams.endAngleRadians)) + angle = rotaryParams.startAngleRadians; else - angle = rotaryEnd; + angle = rotaryParams.endAngleRadians; } } - const double proportion = (angle - rotaryStart) / (rotaryEnd - rotaryStart); + const double proportion = (angle - rotaryParams.startAngleRadians) / (rotaryParams.endAngleRadians - rotaryParams.startAngleRadians); valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, proportion)); lastAngle = angle; } @@ -853,8 +839,9 @@ public: minMaxDiff = (double) valueMax.getValue() - (double) valueMin.getValue(); - lastAngle = rotaryStart + (rotaryEnd - rotaryStart) - * owner.valueToProportionOfLength (currentValue.getValue()); + lastAngle = rotaryParams.startAngleRadians + + (rotaryParams.endAngleRadians - rotaryParams.startAngleRadians) + * owner.valueToProportionOfLength (currentValue.getValue()); valueWhenLastDragged = (sliderBeingDragged == 2 ? valueMax : (sliderBeingDragged == 1 ? valueMin @@ -1108,7 +1095,8 @@ public: lf.drawRotarySlider (g, sliderRect.getX(), sliderRect.getY(), sliderRect.getWidth(), sliderRect.getHeight(), - sliderPos, rotaryStart, rotaryEnd, owner); + sliderPos, rotaryParams.startAngleRadians, + rotaryParams.endAngleRadians, owner); } else { @@ -1195,7 +1183,7 @@ public: double valueWhenLastDragged, valueOnMouseDown, skewFactor, lastAngle; double velocityModeSensitivity, velocityModeOffset, minMaxDiff; int velocityModeThreshold; - float rotaryStart, rotaryEnd; + RotaryParameters rotaryParams; Point mouseDragStartPos, mousePosWhenLastDragged; int sliderRegionStart, sliderRegionSize; int sliderBeingDragged; @@ -1214,7 +1202,6 @@ public: bool doubleClickToValue; bool isVelocityBased; bool userKeyOverridesVelocity; - bool rotaryStop; bool incDecButtonsSideBySide; bool sendChangeOnlyOnRelease; bool popupDisplayEnabled; @@ -1326,9 +1313,25 @@ void Slider::removeListener (SliderListener* const listener) { pimpl->listene Slider::SliderStyle Slider::getSliderStyle() const noexcept { return pimpl->style; } void Slider::setSliderStyle (const SliderStyle newStyle) { pimpl->setSliderStyle (newStyle); } -void Slider::setRotaryParameters (const float startAngleRadians, const float endAngleRadians, const bool stopAtEnd) +void Slider::setRotaryParameters (RotaryParameters p) noexcept +{ + // make sure the values are sensible.. + jassert (p.startAngleRadians >= 0 && p.endAngleRadians >= 0); + jassert (p.startAngleRadians < float_Pi * 4.0f && p.endAngleRadians < float_Pi * 4.0f); + jassert (p.startAngleRadians < p.endAngleRadians); + + pimpl->rotaryParams = p; +} + +void Slider::setRotaryParameters (float startAngleRadians, float endAngleRadians, bool stopAtEnd) noexcept +{ + RotaryParameters p = { startAngleRadians, endAngleRadians, stopAtEnd }; + setRotaryParameters (p); +} + +Slider::RotaryParameters Slider::getRotaryParameters() const noexcept { - pimpl->setRotaryParameters (startAngleRadians, endAngleRadians, stopAtEnd); + return pimpl->rotaryParams; } void Slider::setVelocityBasedMode (bool vb) { pimpl->isVelocityBased = vb; } diff --git a/modules/juce_gui_basics/widgets/juce_Slider.h b/modules/juce_gui_basics/widgets/juce_Slider.h index 2e29348d51..29c89de8e1 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.h +++ b/modules/juce_gui_basics/widgets/juce_Slider.h @@ -138,22 +138,35 @@ public: SliderStyle getSliderStyle() const noexcept; //============================================================================== - /** Changes the properties of a rotary slider. - - @param startAngleRadians the angle (in radians, clockwise from the top) at which - the slider's minimum value is represented - @param endAngleRadians the angle (in radians, clockwise from the top) at which - the slider's maximum value is represented. This must be - greater than startAngleRadians - @param stopAtEnd determines what happens when a circular drag action rotates beyond - the minimum or maximum angle. If true, the value will stop changing - until the mouse moves back the way it came; if false, the value - will snap back to the value nearest to the mouse. Note that this has - no effect if the drag mode is vertical or horizontal. - */ + struct RotaryParameters + { + /** The angle (in radians, clockwise from the top) at which + the slider's minimum value is represented. */ + float startAngleRadians; + + /** The angle (in radians, clockwise from the top) at which + the slider's maximum value is represented. This must be + greater than startAngleRadians. */ + float endAngleRadians; + + /** Determines what happens when a circular drag action rotates beyond + the minimum or maximum angle. If true, the value will stop changing + until the mouse moves back the way it came; if false, the value + will snap back to the value nearest to the mouse. Note that this has + no effect if the drag mode is vertical or horizontal.*/ + bool stopAtEnd; + }; + + /** Changes the properties of a rotary slider. */ + void setRotaryParameters (RotaryParameters newParameters) noexcept; + + /** Changes the properties of a rotary slider. */ void setRotaryParameters (float startAngleRadians, float endAngleRadians, - bool stopAtEnd); + bool stopAtEnd) noexcept; + + /** Changes the properties of a rotary slider. */ + RotaryParameters getRotaryParameters() const noexcept; /** Sets the distance the mouse has to move to drag the slider across the full extent of its range.