How to properly write an ofAddListener

I want to listen to a mouse release event inside an ofxPanels. I just spent hours trying to figure this out, but it still doesnt work. I have a panel:

ofxPanel m_mapPropertiesGui;

It is properly setup and working. I have my listening class, let’s call it “Aclass”. Inside, i created a method:

void onMouseRelease(int x, int y, int button);

(i also tried : onMouseRelease(ofMouseEventArgs & mousePosition))

here is my ofAddListener:

ofAddListener(m_mapPropertiesGui.mouseReleased, &aClassInstance, &Aclass::onMouseRelease);

i also tried:

ofAddListener(ofEvents.mouseReleased, &aClassInstance, &Aclass::onMouseRelease);

it would listen to all mouse release, but i could deal with it.

Hi, I dont think that ofxPanel has a mouse event dispatch.
the correct way is

void Aclass::onMouseRelease(ofMouseEventArgs & mousePosition){
if(ofxPanelInstance.getShape().inside(mousePosition.x, mousePosition.y)){
// do whatever here when the mouse is released inside the panel

ofAddListener(ofEvents().mouseReleased, &aClassInstance, &Aclass::onMouseRelease, OF_EVENT_ORDER_BEFORE_APP - 1);

Notice that there is an additional parameter passed to ofAddListener. This sets the priority of the listener. it is a number. A lower number means that the call back will be called first. ofxGui defaults to OF_EVENT_ORDER_BEFORE_APP, so passing ‘OF_EVENT_ORDER_BEFORE_APP - 1’ will call the callback before ofxPanel. This is necesary because ofxPanel will stop the mouse events propagation to go any further if the mouse is inside the panel.
Just in case you want to know. If the callback function returns a bool instead of void it will stop propagation of events when it returns true. Meaning, any event listener with lower priority will not get notified. This becomes useful in a lot of cases, in the case of the GUI it stops the mouse events getting passed to objects that are drawn behind the panel.

Hope this helps

1 Like

Tks! I also had a problem with my custom mouse cursor not triggering when over panels. This explained why :slight_smile:

1 Like