From 7c4a40470df3ae569485676d04c84295fe494fdc Mon Sep 17 00:00:00 2001 From: jules Date: Mon, 16 Oct 2017 16:21:41 +0100 Subject: [PATCH] Added some better use of the Range class in NormalisableRange and Slider --- .../juce_core/maths/juce_NormalisableRange.h | 107 ++++------ .../juce_gui_basics/widgets/juce_Slider.cpp | 197 +++++++++--------- modules/juce_gui_basics/widgets/juce_Slider.h | 17 +- 3 files changed, 147 insertions(+), 174 deletions(-) diff --git a/modules/juce_core/maths/juce_NormalisableRange.h b/modules/juce_core/maths/juce_NormalisableRange.h index cf5b3e7a11..c25dc4bf47 100644 --- a/modules/juce_core/maths/juce_NormalisableRange.h +++ b/modules/juce_core/maths/juce_NormalisableRange.h @@ -38,39 +38,12 @@ class NormalisableRange { public: /** Creates a continuous range that performs a dummy mapping. */ - NormalisableRange() noexcept - : start(), end (1), interval(), - skew (static_cast (1)), symmetricSkew (false) - {} + NormalisableRange() noexcept = default; - /** Creates a copy of another range. */ - NormalisableRange (const NormalisableRange& other) noexcept - : start (other.start), end (other.end), - interval (other.interval), skew (other.skew), - symmetricSkew (other.symmetricSkew), - convertFrom0To1Function (other.convertFrom0To1Function), - convertTo0To1Function (other.convertTo0To1Function), - snapToLegalValueFunction (other.snapToLegalValueFunction) - { - checkInvariants(); - } - - /** Creates a copy of another range. */ - NormalisableRange& operator= (const NormalisableRange& other) noexcept - { - start = other.start; - end = other.end; - interval = other.interval; - skew = other.skew; - symmetricSkew = other.symmetricSkew; - convertFrom0To1Function = other.convertFrom0To1Function; - convertTo0To1Function = other.convertTo0To1Function; - snapToLegalValueFunction = other.snapToLegalValueFunction; - - checkInvariants(); - - return *this; - } + NormalisableRange (const NormalisableRange&) = default; + NormalisableRange (NormalisableRange&&) noexcept = default; + NormalisableRange& operator= (const NormalisableRange&) = default; + NormalisableRange& operator= (NormalisableRange&&) noexcept = default; /** Creates a NormalisableRange with a given range, interval and skew factor. */ NormalisableRange (ValueType rangeStart, @@ -84,23 +57,33 @@ public: checkInvariants(); } + /** Creates a NormalisableRange with a given range, continuous interval, but a dummy skew-factor. */ + NormalisableRange (ValueType rangeStart, + ValueType rangeEnd) noexcept + : start (rangeStart), end (rangeEnd) + { + checkInvariants(); + } + /** Creates a NormalisableRange with a given range and interval, but a dummy skew-factor. */ NormalisableRange (ValueType rangeStart, ValueType rangeEnd, ValueType intervalValue) noexcept - : start (rangeStart), end (rangeEnd), interval (intervalValue), - skew (static_cast (1)), symmetricSkew (false) + : start (rangeStart), end (rangeEnd), interval (intervalValue) { checkInvariants(); } /** Creates a NormalisableRange with a given range, continuous interval, but a dummy skew-factor. */ - NormalisableRange (ValueType rangeStart, - ValueType rangeEnd) noexcept - : start (rangeStart), end (rangeEnd), interval(), - skew (static_cast (1)), symmetricSkew (false) + NormalisableRange (Range range) noexcept + : NormalisableRange (range.getStart(), range.getEnd()) + { + } + + /** Creates a NormalisableRange with a given range and interval, but a dummy skew-factor. */ + NormalisableRange (Range range, ValueType intervalValue) noexcept + : NormalisableRange (range.getStart(), range.getEnd(), intervalValue) { - checkInvariants(); } /** Creates a NormalisableRange with a given range and an injective mapping function. @@ -121,9 +104,6 @@ public: std::function snapToLegalValueFunc = nullptr) noexcept : start (rangeStart), end (rangeEnd), - interval(), - skew (static_cast (1)), - symmetricSkew (false), convertFrom0To1Function (convertFrom0To1Func), convertTo0To1Function (convertTo0To1Func), snapToLegalValueFunction (snapToLegalValueFunc) @@ -139,7 +119,7 @@ public: if (convertTo0To1Function != nullptr) return convertTo0To1Function (start, end, v); - ValueType proportion = (v - start) / (end - start); + auto proportion = (v - start) / (end - start); if (skew == static_cast (1)) return proportion; @@ -147,11 +127,11 @@ public: if (! symmetricSkew) return std::pow (proportion, skew); - ValueType distanceFromMiddle = static_cast (2) * proportion - static_cast (1); + auto distanceFromMiddle = static_cast (2) * proportion - static_cast (1); return (static_cast (1) + std::pow (std::abs (distanceFromMiddle), skew) - * (distanceFromMiddle < static_cast (0) ? static_cast (-1) - : static_cast (1))) + * (distanceFromMiddle < ValueType() ? static_cast (-1) + : static_cast (1))) / static_cast (2); } @@ -171,12 +151,12 @@ public: return start + (end - start) * proportion; } - ValueType distanceFromMiddle = static_cast (2) * proportion - static_cast (1); + auto distanceFromMiddle = static_cast (2) * proportion - static_cast (1); if (skew != static_cast (1) && distanceFromMiddle != static_cast (0)) distanceFromMiddle = std::exp (std::log (std::abs (distanceFromMiddle)) / skew) - * (distanceFromMiddle < static_cast (0) ? static_cast (-1) - : static_cast (1)); + * (distanceFromMiddle < ValueType() ? static_cast (-1) + : static_cast (1)); return start + (end - start) / static_cast (2) * (static_cast (1) + distanceFromMiddle); } @@ -192,17 +172,11 @@ public: if (interval > ValueType()) v = start + interval * std::floor ((v - start) / interval + static_cast (0.5)); - if (v <= start || end <= start) - return start; - - if (v >= end) - return end; - - return v; + return (v <= start || end <= start) ? start : (v >= end ? end : v); } /** Returns the extent of the normalisable range. */ - Range getRange() const noexcept { return Range (start, end); } + Range getRange() const noexcept { return { start, end }; } /** Given a value which is between the start and end points, this sets the skew such that convertFrom0to1 (0.5) will return this value. @@ -218,16 +192,15 @@ public: jassert (centrePointValue < end); symmetricSkew = false; - skew = std::log (static_cast (0.5)) - / std::log ((centrePointValue - start) / (end - start)); + skew = std::log (static_cast (0.5)) / std::log ((centrePointValue - start) / (end - start)); checkInvariants(); } /** The minimum value of the non-normalised range. */ - ValueType start; + ValueType start = 0; /** The maximum value of the non-normalised range. */ - ValueType end; + ValueType end = 1; /** The snapping interval that should be used (for a non-normalised value). Use 0 for a continuous range. @@ -235,7 +208,7 @@ public: If you have used a lambda function for snapToLegalValueFunction in the constructor of this class then the interval is ignored. */ - ValueType interval; + ValueType interval = 0; /** An optional skew factor that alters the way values are distribute across the range. @@ -249,10 +222,10 @@ public: If you have used lambda functions for convertFrom0to1Func and convertFrom0to1Func in the constructor of this class then the skew value is ignored. */ - ValueType skew; + ValueType skew = 1; /** If true, the skew factor applies from the middle of the slider to each of its ends. */ - bool symmetricSkew; + bool symmetricSkew = false; private: void checkInvariants() const @@ -262,9 +235,9 @@ private: jassert (skew > ValueType()); } - std::function convertFrom0To1Function = nullptr, - convertTo0To1Function = nullptr, - snapToLegalValueFunction = nullptr; + std::function convertFrom0To1Function = {}, + convertTo0To1Function = {}, + snapToLegalValueFunction = {}; }; } // namespace juce diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index f057b69e05..a8f37bc608 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -96,7 +96,7 @@ public: || (incDecButtonMode == incDecButtonsDraggable_AutoDirection && incDecButtonsSideBySide); } - float getPositionOfValue (const double value) const + float getPositionOfValue (double value) const { if (isHorizontal() || isVertical()) return getLinearSliderPos (value); @@ -105,7 +105,7 @@ public: return 0.0f; } - void setRange (const double newMin, const double newMax, const double newInt) + void setRange (double newMin, double newMax, double newInt) { if (minimum != newMin || maximum != newMax || interval != newInt) { @@ -155,7 +155,7 @@ public: return currentValue.getValue(); } - void setValue (double newValue, const NotificationType notification) + void setValue (double newValue, NotificationType notification) { // for a two-value style slider, you should use the setMinValue() and setMaxValue() // methods to set the two values. @@ -192,8 +192,7 @@ public: } } - void setMinValue (double newValue, const NotificationType notification, - const bool allowNudgingOfOtherValues) + void setMinValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues) { // The minimum value only applies to sliders that are in two- or three-value mode. jassert (style == TwoValueHorizontal || style == TwoValueVertical @@ -227,8 +226,7 @@ public: } } - void setMaxValue (double newValue, const NotificationType notification, - const bool allowNudgingOfOtherValues) + void setMaxValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues) { // The maximum value only applies to sliders that are in two- or three-value mode. jassert (style == TwoValueHorizontal || style == TwoValueVertical @@ -262,7 +260,7 @@ public: } } - void setMinAndMaxValues (double newMinValue, double newMaxValue, const NotificationType notification) + void setMinAndMaxValues (double newMinValue, double newMaxValue, NotificationType notification) { // The maximum value only applies to sliders that are in two- or three-value mode. jassert (style == TwoValueHorizontal || style == TwoValueVertical @@ -304,7 +302,7 @@ public: return valueMax.getValue(); } - void triggerChangeMessage (const NotificationType notification) + void triggerChangeMessage (NotificationType notification) { if (notification != dontSendNotification) { @@ -360,7 +358,7 @@ public: { if (style == IncDecButtons) { - const double delta = (button == incButton) ? interval : -interval; + auto delta = (button == incButton) ? interval : -interval; auto newValue = owner.snapValue (getValue() + delta, notDragging); if (currentDrag != nullptr) @@ -390,7 +388,7 @@ public: void labelTextChanged (Label* label) override { - const double newValue = owner.snapValue (owner.getValueFromText (label->getText()), notDragging); + auto newValue = owner.snapValue (owner.getValueFromText (label->getText()), notDragging); if (newValue != static_cast (currentValue.getValue())) { @@ -405,7 +403,7 @@ public: { if (valueBox != nullptr) { - String newValue (owner.getTextFromValue (currentValue.getValue())); + auto newValue = owner.getTextFromValue (currentValue.getValue()); if (newValue != valueBox->getText()) valueBox->setText (newValue, dontSendNotification); @@ -425,7 +423,7 @@ public: return value; } - float getLinearSliderPos (const double value) const + float getLinearSliderPos (double value) const { double pos; @@ -445,7 +443,7 @@ public: return (float) (sliderRegionStart + pos * sliderRegionSize); } - void setSliderStyle (const SliderStyle newStyle) + void setSliderStyle (SliderStyle newStyle) { if (style != newStyle) { @@ -455,8 +453,8 @@ public: } } - void setVelocityModeParameters (const double sensitivity, const int threshold, - const double offset, const bool userCanPressKeyToSwapMode) + void setVelocityModeParameters (double sensitivity, int threshold, + double offset, bool userCanPressKeyToSwapMode) { velocityModeSensitivity = sensitivity; velocityModeOffset = offset; @@ -464,14 +462,14 @@ public: userKeyOverridesVelocity = userCanPressKeyToSwapMode; } - void setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint) + void setSkewFactorFromMidPoint (double sliderValueToShowAtMidPoint) { if (maximum > minimum) skewFactor = std::log (0.5) / std::log ((sliderValueToShowAtMidPoint - minimum) / (maximum - minimum)); } - void setIncDecButtonsMode (const IncDecButtonMode mode) + void setIncDecButtonsMode (IncDecButtonMode mode) { if (incDecButtonMode != mode) { @@ -480,10 +478,10 @@ public: } } - void setTextBoxStyle (const TextEntryBoxPosition newPosition, - const bool isReadOnly, - const int textEntryBoxWidth, - const int textEntryBoxHeight) + void setTextBoxStyle (TextEntryBoxPosition newPosition, + bool isReadOnly, + int textEntryBoxWidth, + int textEntryBoxHeight) { if (textBoxPos != newPosition || editableText != (! isReadOnly) @@ -500,7 +498,7 @@ public: } } - void setTextBoxIsEditable (const bool shouldBeEditable) + void setTextBoxIsEditable (bool shouldBeEditable) { editableText = shouldBeEditable; updateTextBoxEnablement(); @@ -514,7 +512,7 @@ public: valueBox->showEditor(); } - void hideTextBox (const bool discardCurrentEditorContents) + void hideTextBox (bool discardCurrentEditorContents) { if (valueBox != nullptr) { @@ -538,7 +536,7 @@ public: { if (valueBox != nullptr) { - const bool shouldBeEditable = editableText && owner.isEnabled(); + bool shouldBeEditable = editableText && owner.isEnabled(); if (valueBox->isEditable() != shouldBeEditable) // (to avoid changing the single/double click flags unless we need to) valueBox->setEditable (shouldBeEditable); @@ -549,8 +547,8 @@ public: { if (textBoxPos != NoTextBox) { - const String previousTextBoxContent (valueBox != nullptr ? valueBox->getText() - : owner.getTextFromValue (currentValue.getValue())); + auto previousTextBoxContent = (valueBox != nullptr ? valueBox->getText() + : owner.getTextFromValue (currentValue.getValue())); valueBox = nullptr; owner.addAndMakeVisible (valueBox = lf.createSliderTextBox (owner)); @@ -591,7 +589,7 @@ public: decButton->setRepeatSpeed (300, 100, 20); } - const String tooltip (owner.getTooltip()); + auto tooltip = owner.getTooltip(); incButton->setTooltip (tooltip); decButton->setTooltip (tooltip); } @@ -629,7 +627,7 @@ public: ModalCallbackFunction::forComponent (sliderMenuCallback, &owner)); } - static void sliderMenuCallback (const int result, Slider* slider) + static void sliderMenuCallback (int result, Slider* slider) { if (slider != nullptr) { @@ -647,16 +645,16 @@ public: int getThumbIndexAt (const MouseEvent& e) { - const bool isTwoValue = (style == TwoValueHorizontal || style == TwoValueVertical); - const bool isThreeValue = (style == ThreeValueHorizontal || style == ThreeValueVertical); + bool isTwoValue = (style == TwoValueHorizontal || style == TwoValueVertical); + bool isThreeValue = (style == ThreeValueHorizontal || style == ThreeValueVertical); if (isTwoValue || isThreeValue) { - const float mousePos = isVertical() ? e.position.y : e.position.x; + auto mousePos = isVertical() ? e.position.y : e.position.x; - const float normalPosDistance = std::abs (getLinearSliderPos (currentValue.getValue()) - mousePos); - const float minPosDistance = std::abs (getLinearSliderPos (valueMin.getValue()) + (isVertical() ? 0.1f : -0.1f) - mousePos); - const float maxPosDistance = std::abs (getLinearSliderPos (valueMax.getValue()) + (isVertical() ? -0.1f : 0.1f) - mousePos); + auto normalPosDistance = std::abs (getLinearSliderPos (currentValue.getValue()) - mousePos); + auto minPosDistance = std::abs (getLinearSliderPos (valueMin.getValue()) + (isVertical() ? 0.1f : -0.1f) - mousePos); + auto maxPosDistance = std::abs (getLinearSliderPos (valueMax.getValue()) + (isVertical() ? -0.1f : 0.1f) - mousePos); if (isTwoValue) return maxPosDistance <= minPosDistance ? 2 : 1; @@ -674,12 +672,12 @@ public: //============================================================================== void handleRotaryDrag (const MouseEvent& e) { - const float dx = e.position.x - sliderRect.getCentreX(); - const float dy = e.position.y - sliderRect.getCentreY(); + auto dx = e.position.x - sliderRect.getCentreX(); + auto dy = e.position.y - sliderRect.getCentreY(); if (dx * dx + dy * dy > 25.0f) { - double angle = std::atan2 ((double) dx, (double) -dy); + auto angle = std::atan2 ((double) dx, (double) -dy); while (angle < 0.0) angle += double_Pi * 2.0; @@ -714,7 +712,7 @@ public: } } - const double proportion = (angle - rotaryParams.startAngleRadians) / (rotaryParams.endAngleRadians - rotaryParams.startAngleRadians); + auto proportion = (angle - rotaryParams.startAngleRadians) / (rotaryParams.endAngleRadians - rotaryParams.startAngleRadians); valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, proportion)); lastAngle = angle; } @@ -722,7 +720,7 @@ public: void handleAbsoluteDrag (const MouseEvent& e) { - const float mousePos = (isHorizontal() || style == RotaryHorizontalDrag) ? e.position.x : e.position.y; + auto mousePos = (isHorizontal() || style == RotaryHorizontalDrag) ? e.position.x : e.position.y; double newPos = 0; if (style == RotaryHorizontalDrag @@ -768,19 +766,17 @@ public: void handleVelocityDrag (const MouseEvent& e) { - const bool hasHorizontalStyle = + bool hasHorizontalStyle = (isHorizontal() || style == RotaryHorizontalDrag || (style == IncDecButtons && incDecDragDirectionIsHorizontal())); - float mouseDiff; - if (style == RotaryHorizontalVerticalDrag) - mouseDiff = (e.position.x - mousePosWhenLastDragged.x) + (mousePosWhenLastDragged.y - e.position.y); - else - mouseDiff = (hasHorizontalStyle ? e.position.x - mousePosWhenLastDragged.x - : e.position.y - mousePosWhenLastDragged.y); + auto mouseDiff = style == RotaryHorizontalVerticalDrag + ? (e.position.x - mousePosWhenLastDragged.x) + (mousePosWhenLastDragged.y - e.position.y) + : (hasHorizontalStyle ? e.position.x - mousePosWhenLastDragged.x + : e.position.y - mousePosWhenLastDragged.y); - const double maxSpeed = jmax (200, sliderRegionSize); - double speed = jlimit (0.0, maxSpeed, (double) std::abs (mouseDiff)); + auto maxSpeed = jmax (200.0, (double) sliderRegionSize); + auto speed = jlimit (0.0, maxSpeed, (double) std::abs (mouseDiff)); if (speed != 0.0) { @@ -796,8 +792,7 @@ public: || (style == IncDecButtons && ! incDecDragDirectionIsHorizontal())) speed = -speed; - const double currentPos = owner.valueToProportionOfLength (valueWhenLastDragged); - + auto currentPos = owner.valueToProportionOfLength (valueWhenLastDragged); valueWhenLastDragged = owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + speed)); e.source.enableUnboundedMouseMovement (true, false); @@ -1033,8 +1028,8 @@ public: if (style == IncDecButtons) return interval * wheelAmount; - const double proportionDelta = wheelAmount * 0.15f; - const double currentPos = owner.valueToProportionOfLength (value); + auto proportionDelta = wheelAmount * 0.15; + auto currentPos = owner.valueToProportionOfLength (value); return owner.proportionOfLengthToValue (jlimit (0.0, 1.0, currentPos + proportionDelta)) - value; } @@ -1055,13 +1050,13 @@ public: if (valueBox != nullptr) valueBox->hideEditor (false); - const double value = static_cast (currentValue.getValue()); - const double delta = getMouseWheelDelta (value, (std::abs (wheel.deltaX) > std::abs (wheel.deltaY) - ? -wheel.deltaX : wheel.deltaY) - * (wheel.isReversed ? -1.0f : 1.0f)); + auto value = static_cast (currentValue.getValue()); + auto delta = getMouseWheelDelta (value, (std::abs (wheel.deltaX) > std::abs (wheel.deltaY) + ? -wheel.deltaX : wheel.deltaY) + * (wheel.isReversed ? -1.0f : 1.0f)); if (delta != 0.0) { - const double newValue = value + jmax (interval, std::abs (delta)) * (delta < 0 ? -1.0 : 1.0); + auto newValue = value + jmax (interval, std::abs (delta)) * (delta < 0 ? -1.0 : 1.0); DragInProgress drag (*this); setValue (owner.snapValue (newValue, notDragging), sendNotificationSync); @@ -1095,17 +1090,17 @@ public: { ms.enableUnboundedMouseMovement (false); - const double pos = sliderBeingDragged == 2 ? getMaxValue() - : (sliderBeingDragged == 1 ? getMinValue() - : static_cast (currentValue.getValue())); + auto pos = sliderBeingDragged == 2 ? getMaxValue() + : (sliderBeingDragged == 1 ? getMinValue() + : static_cast (currentValue.getValue())); Point mousePos; if (isRotary()) { mousePos = ms.getLastMouseDownPosition(); - const float delta = (float) (pixelsForFullDragExtent * (owner.valueToProportionOfLength (valueOnMouseDown) - - owner.valueToProportionOfLength (pos))); + auto delta = (float) (pixelsForFullDragExtent * (owner.valueToProportionOfLength (valueOnMouseDown) + - owner.valueToProportionOfLength (pos))); if (style == RotaryHorizontalDrag) mousePos += Point (-delta, 0.0f); else if (style == RotaryVerticalDrag) mousePos += Point (0.0f, delta); @@ -1117,7 +1112,7 @@ public: } else { - const float pixelPos = (float) getLinearSliderPos (pos); + auto pixelPos = (float) getLinearSliderPos (pos); mousePos = owner.localPointToGlobal (Point (isHorizontal() ? pixelPos : (owner.getWidth() / 2.0f), isVertical() ? pixelPos : (owner.getHeight() / 2.0f))); @@ -1135,7 +1130,7 @@ public: { if (isRotary()) { - const float sliderPos = (float) owner.valueToProportionOfLength (lastCurrentValue); + auto sliderPos = (float) owner.valueToProportionOfLength (lastCurrentValue); jassert (sliderPos >= 0 && sliderPos <= 1.0f); lf.drawRotarySlider (g, @@ -1166,8 +1161,7 @@ public: //============================================================================== void resized (LookAndFeel& lf) { - SliderLayout layout = lf.getSliderLayout (owner); - + auto layout = lf.getSliderLayout (owner); sliderRect = layout.sliderBounds; if (valueBox != nullptr) @@ -1193,7 +1187,7 @@ public: void resizeIncDecButtons() { - Rectangle buttonRect (sliderRect); + auto buttonRect = sliderRect; if (textBoxPos == TextBoxLeft || textBoxPos == TextBoxRight) buttonRect.expand (-2, 0); @@ -1360,7 +1354,7 @@ void Slider::removeListener (Listener* l) { pimpl->listeners.remove (l); } //============================================================================== Slider::SliderStyle Slider::getSliderStyle() const noexcept { return pimpl->style; } -void Slider::setSliderStyle (const SliderStyle newStyle) { pimpl->setSliderStyle (newStyle); } +void Slider::setSliderStyle (SliderStyle newStyle) { pimpl->setSliderStyle (newStyle); } void Slider::setRotaryParameters (RotaryParameters p) noexcept { @@ -1389,8 +1383,7 @@ int Slider::getVelocityThreshold() const noexcept { return pimpl->velo double Slider::getVelocitySensitivity() const noexcept { return pimpl->velocityModeSensitivity; } double Slider::getVelocityOffset() const noexcept { return pimpl->velocityModeOffset; } -void Slider::setVelocityModeParameters (const double sensitivity, const int threshold, - const double offset, const bool userCanPressKeyToSwapMode) +void Slider::setVelocityModeParameters (double sensitivity, int threshold, double offset, bool userCanPressKeyToSwapMode) { jassert (threshold >= 0); jassert (sensitivity > 0); @@ -1408,7 +1401,7 @@ void Slider::setSkewFactor (double factor, bool symmetricSkew) pimpl->symmetricSkew = symmetricSkew; } -void Slider::setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint) +void Slider::setSkewFactorFromMidPoint (double sliderValueToShowAtMidPoint) { pimpl->setSkewFactorFromMidPoint (sliderValueToShowAtMidPoint); pimpl->symmetricSkew = false; @@ -1416,21 +1409,20 @@ void Slider::setSkewFactorFromMidPoint (const double sliderValueToShowAtMidPoint int Slider::getMouseDragSensitivity() const noexcept { return pimpl->pixelsForFullDragExtent; } -void Slider::setMouseDragSensitivity (const int distanceForFullScaleDrag) +void Slider::setMouseDragSensitivity (int distanceForFullScaleDrag) { jassert (distanceForFullScaleDrag > 0); pimpl->pixelsForFullDragExtent = distanceForFullScaleDrag; } -void Slider::setIncDecButtonsMode (const IncDecButtonMode mode) { pimpl->setIncDecButtonsMode (mode); } +void Slider::setIncDecButtonsMode (IncDecButtonMode mode) { pimpl->setIncDecButtonsMode (mode); } Slider::TextEntryBoxPosition Slider::getTextBoxPosition() const noexcept { return pimpl->textBoxPos; } int Slider::getTextBoxWidth() const noexcept { return pimpl->textBoxWidth; } int Slider::getTextBoxHeight() const noexcept { return pimpl->textBoxHeight; } -void Slider::setTextBoxStyle (const TextEntryBoxPosition newPosition, const bool isReadOnly, - const int textEntryBoxWidth, const int textEntryBoxHeight) +void Slider::setTextBoxStyle (TextEntryBoxPosition newPosition, bool isReadOnly, int textEntryBoxWidth, int textEntryBoxHeight) { pimpl->setTextBoxStyle (newPosition, isReadOnly, textEntryBoxWidth, textEntryBoxHeight); } @@ -1438,15 +1430,15 @@ void Slider::setTextBoxStyle (const TextEntryBoxPosition newPosition, const bool bool Slider::isTextBoxEditable() const noexcept { return pimpl->editableText; } void Slider::setTextBoxIsEditable (const bool shouldBeEditable) { pimpl->setTextBoxIsEditable (shouldBeEditable); } void Slider::showTextBox() { pimpl->showTextBox(); } -void Slider::hideTextBox (const bool discardCurrentEditorContents) { pimpl->hideTextBox (discardCurrentEditorContents); } +void Slider::hideTextBox (bool discardCurrentEditorContents) { pimpl->hideTextBox (discardCurrentEditorContents); } void Slider::setChangeNotificationOnlyOnRelease (bool onlyNotifyOnRelease) { pimpl->sendChangeOnlyOnRelease = onlyNotifyOnRelease; } -bool Slider::getSliderSnapsToMousePosition() const noexcept { return pimpl->snapsToMousePos; } -void Slider::setSliderSnapsToMousePosition (const bool shouldSnapToMouse) { pimpl->snapsToMousePos = shouldSnapToMouse; } +bool Slider::getSliderSnapsToMousePosition() const noexcept { return pimpl->snapsToMousePos; } +void Slider::setSliderSnapsToMousePosition (bool shouldSnapToMouse) { pimpl->snapsToMousePos = shouldSnapToMouse; } void Slider::setPopupDisplayEnabled (bool showOnDrag, bool showOnHover, Component* parent, int hoverTimeout) { @@ -1464,22 +1456,20 @@ void Slider::lookAndFeelChanged() { pimpl->lookAndFeelChanged (getLookAndFeel( void Slider::enablementChanged() { repaint(); pimpl->updateTextBoxEnablement(); } //============================================================================== -double Slider::getMaximum() const noexcept { return pimpl->maximum; } -double Slider::getMinimum() const noexcept { return pimpl->minimum; } -double Slider::getInterval() const noexcept { return pimpl->interval; } +Range Slider::getRange() const noexcept { return { pimpl->minimum, pimpl->maximum }; } +double Slider::getMaximum() const noexcept { return pimpl->maximum; } +double Slider::getMinimum() const noexcept { return pimpl->minimum; } +double Slider::getInterval() const noexcept { return pimpl->interval; } -void Slider::setRange (double newMin, double newMax, double newInt) -{ - pimpl->setRange (newMin, newMax, newInt); -} +void Slider::setRange (double newMin, double newMax, double newInt) { pimpl->setRange (newMin, newMax, newInt); } +void Slider::setRange (Range newRange, double newInt) { pimpl->setRange (newRange.getStart(), newRange.getEnd(), newInt); } -Value& Slider::getValueObject() noexcept { return pimpl->currentValue; } -Value& Slider::getMinValueObject() noexcept { return pimpl->valueMin; } -Value& Slider::getMaxValueObject() noexcept { return pimpl->valueMax; } +double Slider::getValue() const { return pimpl->getValue(); } +Value& Slider::getValueObject() noexcept { return pimpl->currentValue; } +Value& Slider::getMinValueObject() noexcept { return pimpl->valueMin; } +Value& Slider::getMaxValueObject() noexcept { return pimpl->valueMax; } -double Slider::getValue() const { return pimpl->getValue(); } - -void Slider::setValue (double newValue, const NotificationType notification) +void Slider::setValue (double newValue, NotificationType notification) { pimpl->setValue (newValue, notification); } @@ -1487,17 +1477,17 @@ void Slider::setValue (double newValue, const NotificationType notification) double Slider::getMinValue() const { return pimpl->getMinValue(); } double Slider::getMaxValue() const { return pimpl->getMaxValue(); } -void Slider::setMinValue (double newValue, const NotificationType notification, bool allowNudgingOfOtherValues) +void Slider::setMinValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues) { pimpl->setMinValue (newValue, notification, allowNudgingOfOtherValues); } -void Slider::setMaxValue (double newValue, const NotificationType notification, bool allowNudgingOfOtherValues) +void Slider::setMaxValue (double newValue, NotificationType notification, bool allowNudgingOfOtherValues) { pimpl->setMaxValue (newValue, notification, allowNudgingOfOtherValues); } -void Slider::setMinAndMaxValues (double newMinValue, double newMaxValue, const NotificationType notification) +void Slider::setMinAndMaxValues (double newMinValue, double newMaxValue, NotificationType notification) { pimpl->setMinAndMaxValues (newMinValue, newMaxValue, notification); } @@ -1550,7 +1540,7 @@ double Slider::getValueFromText (const String& text) double Slider::proportionOfLengthToValue (double proportion) { - const double skew = getSkewFactor(); + auto skew = getSkewFactor(); if (! isSymmetricSkew()) { @@ -1571,8 +1561,8 @@ double Slider::proportionOfLengthToValue (double proportion) double Slider::valueToProportionOfLength (double value) { - const double n = (value - getMinimum()) / (getMaximum() - getMinimum()); - const double skew = getSkewFactor(); + auto n = (value - getMinimum()) / (getMaximum() - getMinimum()); + auto skew = getSkewFactor(); if (skew == 1.0) return n; @@ -1589,25 +1579,24 @@ double Slider::snapValue (double attemptedValue, DragMode) return attemptedValue; } -int Slider::getNumDecimalPlacesToDisplay() const noexcept { return pimpl->numDecimalPlaces; } +int Slider::getNumDecimalPlacesToDisplay() const noexcept { return pimpl->numDecimalPlaces; } //============================================================================== -int Slider::getThumbBeingDragged() const noexcept { return pimpl->sliderBeingDragged; } - +int Slider::getThumbBeingDragged() const noexcept { return pimpl->sliderBeingDragged; } void Slider::startedDragging() {} void Slider::stoppedDragging() {} void Slider::valueChanged() {} //============================================================================== -void Slider::setPopupMenuEnabled (const bool menuEnabled) { pimpl->menuEnabled = menuEnabled; } -void Slider::setScrollWheelEnabled (const bool enabled) { pimpl->scrollWheelEnabled = enabled; } +void Slider::setPopupMenuEnabled (bool menuEnabled) { pimpl->menuEnabled = menuEnabled; } +void Slider::setScrollWheelEnabled (bool enabled) { pimpl->scrollWheelEnabled = enabled; } bool Slider::isHorizontal() const noexcept { return pimpl->isHorizontal(); } bool Slider::isVertical() const noexcept { return pimpl->isVertical(); } bool Slider::isRotary() const noexcept { return pimpl->isRotary(); } bool Slider::isBar() const noexcept { return pimpl->isBar(); } -float Slider::getPositionOfValue (const double value) const { return pimpl->getPositionOfValue (value); } +float Slider::getPositionOfValue (double value) const { return pimpl->getPositionOfValue (value); } //============================================================================== void Slider::paint (Graphics& g) { pimpl->paint (g, getLookAndFeel()); } diff --git a/modules/juce_gui_basics/widgets/juce_Slider.h b/modules/juce_gui_basics/widgets/juce_Slider.h index fbec8e99e8..2ae9768839 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.h +++ b/modules/juce_gui_basics/widgets/juce_Slider.h @@ -404,18 +404,29 @@ public: double newMaximum, double newInterval = 0); + /** Sets the limits that the slider's value can take. + + @param newRange the range to allow + @param newInterval the steps in which the value is allowed to increase - if this + is not zero, the value will always be (newMinimum + (newInterval * an integer)). + */ + void setRange (Range newRange, double newInterval); + + /** Returns the slider's range. */ + Range getRange() const noexcept; + /** Returns the current maximum value. - @see setRange + @see setRange, getRange */ double getMaximum() const noexcept; /** Returns the current minimum value. - @see setRange + @see setRange, getRange */ double getMinimum() const noexcept; /** Returns the current step-size for values. - @see setRange + @see setRange, getRange */ double getInterval() const noexcept;