ofxGui + parameters + Listener options

@roymacdonald gave me some tips which I am recording here.

I am always unsure how to best trigger specific actions based on a ofParameter change, with the least amount of code.

Options:

Add a lambda function right away to the listener.
Notice that the float must be changed to the parameter type of the ofParameter

listeners.push(all_cyan.newListener( [&](float&) 
    {for(size_t i = 0; i < numHeads; i++){
        heads[i].cyan = all_cyan;
    }}));

Call the same function for different parameters

void ofApp::setup(){
  listeners.push(cyan.newListener(  this , &ofApp::updateConlors));
  listeners.push(magenta.newListener(  this , &ofApp::updateConlors));
  listeners.push(yellow.newListener(  this , &ofApp::updateConlors));
}
void ofApp::updateConlors(float &){
// change float to the correct type of the ofParameter that it is being listened
    //don't know which variable caused the listener to call this function
}
ofParameterGroup paramGroup;// declared elsewhere

void ofApp::setup(){
  paramGroup.add(cyan);
  paramGroup.add(magenta);
  paramGroup.add(yellow);
  listeners.push(paramGroup.parameterChangedE().newListener(this, &ofApp::paramsChanged));
}

ofApp::paramsChanged(ofAbstractParameter & param){
//param is the one that changed but it is passed as an ofAbstractParameter (the base class of ofParameter)
you can either cast it if you know before hand what was its parameter type or you can use its name directly.
if(param.getName() == "cyan"){}
}
2 Likes