getControlParameterIndex - an elegant implementation


#1

Hi,
I thought this might be helpful simple (imho) way to implement getControlParameterIndex

I’ve made a utility class to simplify linking betwen parameters and components.
It uses JUCEs own powerful ValueTree based elements. It encapsulates attachments from AudioProcessorValueTreeState to add property for each component.

Feel free if you got any feedback. (The nullptr boilerplate was added after initial tests showed it’s possible to get there with no valid pointer).


#2

Interesting! For people, who doesn’t use the AudioProcessorValueTree, here is something more simple, less elegant, but effective:

    int getControlParameterIndex (Component& c) override
    {
        Component* cc= &c;
        while (cc!=nullptr)
        {
            PluginParameterAssociated* pa=dynamic_cast<PluginParameterAssociated*>(cc);
            if (pa!=nullptr)
            {
                return pa->getParameterNumber();
            };
            cc=cc->getParentComponent();
        };
        
        return -1;
    };

And Components which are assigned to a control-index, simply inherit from this class:
(You can also attach this class to parent-components, and all childs will associated with the index)

class PluginParameterAssociated
{
public:
    PluginParameterAssociated(int parameterNumber_)
    : parameterNumber(parameterNumber_)
    {};

    virtual ~PluginParameterAssociated() 
    {};

    int getParameterNumber()
    {
        return parameterNumber;
    };
 private:
    int parameterNumber;
};