diff --git a/modules/juce_audio_processors/utilities/juce_AudioProcessorValueTreeState.cpp b/modules/juce_audio_processors/utilities/juce_AudioProcessorValueTreeState.cpp index 4c3c2a061a..a345f5ae47 100644 --- a/modules/juce_audio_processors/utilities/juce_AudioProcessorValueTreeState.cpp +++ b/modules/juce_audio_processors/utilities/juce_AudioProcessorValueTreeState.cpp @@ -421,7 +421,7 @@ struct AudioProcessorValueTreeState::SliderAttachment::Pimpl : private Attached Pimpl (AudioProcessorValueTreeState& s, const String& p, Slider& sl) : AttachedControlBase (s, p), slider (sl), ignoreCallbacks (false) { - NormalisableRange range (s.getParameterRange (paramID)); + NormalisableRange range (state.getParameterRange (paramID)); if (range.interval != 0 || range.skew != 0) { @@ -463,8 +463,13 @@ struct AudioProcessorValueTreeState::SliderAttachment::Pimpl : private Attached snapToLegalValueFunction }); } - if (auto* param = state.getParameter (paramID)) + if (auto* param = dynamic_cast (state.getParameter (paramID))) + { + slider.valueFromTextFunction = [param] (const String& text) { return (double) param->textToValueFunction (text); }; + slider.textFromValueFunction = [param] (double value) { return param->valueToTextFunction ((float) value); }; + slider.setDoubleClickReturnValue (true, range.convertFrom0to1 (param->getDefaultValue())); + } sendInitialUpdate(); slider.addListener (this); diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index 797a46f438..03f1ca15d9 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -1539,6 +1539,9 @@ String Slider::getTextValueSuffix() const String Slider::getTextFromValue (double v) { + if (textFromValueFunction != nullptr) + return textFromValueFunction (v); + if (getNumDecimalPlacesToDisplay() > 0) return String (v, getNumDecimalPlacesToDisplay()) + getTextValueSuffix(); @@ -1547,6 +1550,9 @@ String Slider::getTextFromValue (double v) double Slider::getValueFromText (const String& text) { + if (valueFromTextFunction != nullptr) + return valueFromTextFunction (text); + auto t = text.trimStart(); if (t.endsWith (getTextValueSuffix())) diff --git a/modules/juce_gui_basics/widgets/juce_Slider.h b/modules/juce_gui_basics/widgets/juce_Slider.h index 2bd32205f8..90c268ffb0 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.h +++ b/modules/juce_gui_basics/widgets/juce_Slider.h @@ -602,6 +602,12 @@ public: /** You can assign a lambda to this callback object to have it called when the slider's drag ends. */ std::function onDragEnd; + /** You can assign a lambda that will be used to convert textual values to the slider's normalised position. */ + std::function valueFromTextFunction; + + /** You can assign a lambda that will be used to convert the slider's normalised position to a textual value. */ + std::function textFromValueFunction; + //============================================================================== /** This lets you choose whether double-clicking moves the slider to a given position.