Changing colour of button in plugin editor when button is pressed


#1

The follow code is in my editor, however when I click the button the colour doesn’t change but the breakpoint is reached:

// Callback for submit button
void AudioProcessorEditor::buttonClicked (Button *button)
{
    if (button == &submitBtn_)
    {
	    submitBtn_.setColour(0xff008000, Colours::green);
	    submitBtn_.colourChanged();
    }
}

#2

I think the problem is the ColourId enum that you’re using. I couldn’t find 0xff008000 referenced in the docs, but not sure what type of button you’re using. You should do something like:

// Callback for submit button
void AudioProcessorEditor::buttonClicked (Button* button)
{
    if (button == &submitBtn_)
    {
         submitBtn_.setColour (TextButton::buttonColourId, Colours::green);
    }
}

…using an appropriate enumerator (by its fully qualified name) for the type of button you’re using. I tried the above for a TextButton and the button turned green on click.

You don’t need to call colourChanged(). It gets called automatically whenever a Component’s colour(s) are changed using the setColour. (So in your example it would be called twice).


#3

Ahh, I see that 0xff008000 is Colours::green. But in the first argument of Component::setColour you need to tell the button which of the Button’s colourIds you want to change.

See the ColourIds for TextButton for some possible options, and keep in mind that different types of Button - TextButton, DrawableButton, ToggleButton - have their own, unique sets of ColourIds.


#4

P.S. If you’re wanting to set the colour of a button for it’s ‘on’ state, you might be better off calling…

submitButton.setColour (TextButton::buttonOnColourId, Colours::green);

…in your Editor’s constructor (substituting TextButton for the appropriate Button subtype), rather than changing the colours ‘on the fly’ in the ButtonClicked callback. Then you can manage the Button’s state in your buttonClicked callback e.g.

void AudioProcessorEditor::buttonClicked (Button* button)
{
    if (button == &submitButton)
    {
        submitButton.setToggleState (! submitButton.getToggleState(),
                                     NotificationType::dontSendNotification);
    }
}

Also, using _ suffixes is frowned upon.