Identifying source ofxGui instance from shared listeners?

I’d like to use a single listener for multiple instances of the ofxButton class, each of which performs the same function for a different on-screen object. Is there a way to identify the source ofxButton instance from within the listener / callback? Or to specify data in the call to addListener() that will be passed onto the listener (e.g. an id identifying the associated button)?

Currently, I’m creating an intermediate proxy class which stores the metadata and passes it onto the listener:

class ListenerProxy {
  TrainingSampleGuiListener(ofApp *app, int num) : app(app), num(num) {}
  void listener() { app->listener(num); }
  ofApp *app;
  int num;

ListenerProxy proxy(this, 0); // save metadata associated w/ this particular button
button->addListener(proxy, &ListenerProxy::listener);

but this seems like a clunky work-around.

you can declare the listener method to receive the sender to, as in:

void listener(const void * sender, bool & value){
    auto * button = (ofxButton*) sender;
    auto name = button->getName();
   }else if...

there’s a discrepancy in who is the sender for buttons and everything else, for buttons the sender is the button itself, for every other control the sender is the internal parameter so for an ofxFloatSlider you would need to cast to ofParameter instead. This will change in the future and the sender for a button will be an ofParameter but we only introduced coid parameters recently and they are not still working on the gui

all this listeners are checking any change to the related parameter, but can not differentiate if the change it’s related or not to the mouse click (or any other change of the value by code logic)

So, if I want to know only if it’s changed by mouse, how can I proceed?

you can’t directly listen to mouse events on a gui element but if you want to not listen to specific events when changing the parameter from other places you can use something like:

bool changingParam;

param = value;


void listener(float & newValue){
1 Like