Way to pass parameter to InterfaceGl button callback?

Hi all,

Is there a way to pass a parameter to the callback function for a button in InterfaceGl?

Here’s my code (without a param)

void MyApp::setup(){
    mParams = ci::params::InterfaceGl::create( ci::app::getWindow(), "App parameters", ci::app::toPixels( ci::ivec2( 200, 400 ) ) );
    mParams->addButton( "Click me", bind( &MyApp::button, this ) );
}

void MyApp::button(){
    // Can this method receive an int or string parameter ie. the label of the button
    // or is there a way to getOptions?
}

Unsure of how to approach this problem…

Any help very appreciated!

Hi,

Would you like to handle all buttons in one callback function? As far as I know, this is not really the way how Params is designed, but you can work around it. For example, with something like this:

void MyApp::setup{
    ...
    mParams->addButton( "Click me 0", [ & ]() { button( 0 ); } );
    mParams->addButton( "Click me 1", [ & ]() { button( 1 ); } );
}

void MyApp::button( int buttonId ){
    ...
}

-Gabor

1 Like

Hi Gabor,

Thanks for the suggestion. This solves it for the moment.

Ideally, I’d like to have a single callback with some parameter indication what the originating button is, without having to duplicate that in the addButton. I just feel that it’d be the most succinct way of creating a lot of buttons with a simple switch/case statement that ideally referenced the labels of the buttons to limit the mystery between the visual display and the code.

Thanks for the recommendation, it’s working perfectly…

You can pass a variable into a lambda, maybe some variation on this would be useful:

for (int i=0; i<numButtons; i++) {
  mParams->addButton( "Click me " + toString(i), [ &, i ]() { button( i ); } );
}

In practice, I usually find that using separate callback handlers for each button is easier to follow, especially if you weren’t the original person writing the code. That, and not having to pass an extra argument through the lambda saves a bit of typing in the most common case.