Browse Source

Passed the valueToTextFunction and textToValueFunction lambdas from an AudioProcessorValueTreeState parameter to an attached slider

tags/2021-05-28
Tom Poole 8 years ago
parent
commit
c2a877cac8
3 changed files with 19 additions and 2 deletions
  1. +7
    -2
      modules/juce_audio_processors/utilities/juce_AudioProcessorValueTreeState.cpp
  2. +6
    -0
      modules/juce_gui_basics/widgets/juce_Slider.cpp
  3. +6
    -0
      modules/juce_gui_basics/widgets/juce_Slider.h

+ 7
- 2
modules/juce_audio_processors/utilities/juce_AudioProcessorValueTreeState.cpp View File

@@ -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<float> range (s.getParameterRange (paramID));
NormalisableRange<float> 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<AudioProcessorValueTreeState::Parameter*> (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);


+ 6
- 0
modules/juce_gui_basics/widgets/juce_Slider.cpp View File

@@ -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()))


+ 6
- 0
modules/juce_gui_basics/widgets/juce_Slider.h View File

@@ -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<void()> onDragEnd;
/** You can assign a lambda that will be used to convert textual values to the slider's normalised position. */
std::function<double (const String&)> valueFromTextFunction;
/** You can assign a lambda that will be used to convert the slider's normalised position to a textual value. */
std::function<String (double)> textFromValueFunction;
//==============================================================================
/** This lets you choose whether double-clicking moves the slider to a given position.


Loading…
Cancel
Save