Browse Source

Tidied up some rotary parameter handling code in Slider

tags/2021-05-28
jules 9 years ago
parent
commit
ecfa5d1040
2 changed files with 64 additions and 48 deletions
  1. +37
    -34
      modules/juce_gui_basics/widgets/juce_Slider.cpp
  2. +27
    -14
      modules/juce_gui_basics/widgets/juce_Slider.h

+ 37
- 34
modules/juce_gui_basics/widgets/juce_Slider.cpp View File

@@ -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<float> 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; }


+ 27
- 14
modules/juce_gui_basics/widgets/juce_Slider.h View File

@@ -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.


Loading…
Cancel
Save