Overloading text from value for slider


#1

Hi,

I have one rotary slider which I want to show custom value (changing units based on the value). I’m not sure the correct way to overload textFromValueFunction(double value).

I tried making a new slider class that inhereits from Slider:

class DelaySlider : public Slider
{
public:
	DelaySlider()
	{

	}

	juce::String getTextFromValue(double value)
	{
		if (value >= 1000) {
			return juce::String(value/1000.0) + "s";
		}
		else
			return juce::String(value) + "ms";
	};


};

And then using a DelaySlider instead of Slider for the appropriate one, but it doesn’t work. Debugging shows that textFromValueFunction isn’t called.

Am I approaching this the correct way?

Thanks

EDIT: I got the function name wrong. I’m an idiot. Still interested if there’s a better way to do the overload than creating a new class that is a child of Slider, though?


#2

The Slider class has 5 public std::function<> members that you can assign to respond to:

  • onValueChange
  • onDragStart
  • onDragEnd
  • valueFromTextFunction
  • textFromValueFunction

Here’s an example I have laying around that lets a slider control a 32-bit unsigned int:

  controllerDataSlider->textFromValueFunction = [] (double val) {
       auto scaled = static_cast<unsigned int>(((0xffffffff) * val)  + 0.5f);
       return String(scaled);
   };
   controllerDataSlider->valueFromTextFunction = [] (const String& text) {
        int64 textValue = text.getLargeIntValue();
        return textValue / static_cast<double>(0xFFFFFFFF);
   };

#3

If you want to override a method, add the override keyword in your declaration (header), like:

juce::String getTextFromValue(double value) override //...

In this case the compiler will tell you, if your method doesn’t override because of spelling errors or the original is not virtual.
Since the override keyword was added later, code without it is still legit, but as soon as you used it once in a class, you get warnings for all methods, that override but don’t have the keyword…

But for this particular case, @bgporter’s answer is the more modern approach, since the lambdas are even propagated to a slider, if you use the AudioProcessorValueTreeState::SliderAttachment.