Streamlining GUI object creation


#1

I have a parent component patch containing a whole lot of GUI buttons.

So the header file looks like:

private:
    std::unique_ptr<TextButton> button_a, button_b,..... button_xyz;

and then in the .cpp file, i have a function which takes care of some button creation tasks:

void TransportComponent::makeTextButton (std::unique_ptr<TextButton>& button, std::string label, bool toggle)
{
    button = std::make_unique<TextButton> (label);
    addAndMakeVisible (*button);
    button->setTriggeredOnMouseDown (true);
    button->addListener (this);
    button->setClickingTogglesState (toggle);
}

and then, the click functions:

void TransportComponent::buttonClicked (Button* buttonThatWasClicked)
{
    if (buttonThatWasClicked == button_zoomIn.get())
    {
        waveformComponent.zoom (2);
    }
    else if (buttonThatWasClicked == button_zoomOut.get())
    {
        waveformComponent.zoom (0.5);
    }
    ...
}

and finally the resize scaling:

void TransportComponent::resized()
{
    double x = 1.0 / 20.0;
    double height = 1.0 / 2.0;
    double y = height * 0;
    
    button_play->setBoundsRelative (x * 0, y, x, height);
    button_pause->setBoundsRelative (x * 1, y, x, height);
    button_zoomAll->setBoundsRelative (x * 2, y, x, height);
    button_zoomOut->setBoundsRelative (x * 3, y, x, height);
    ....
}

basically, what i’d like to do, is to have a single method that creates a button from a bunch of arguments (identifier, position, clickingTogglesState value, and then even perhaps a lambda function for its click response).

I’m thinking perhaps something like std::map may be a good start?


#2

Have you seen the new lambda methods available? That’s half your issue solved already I think :slight_smile: