How to pass events to the GUI from a mouse event handler?

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
ssexton
Posts: 54
Joined: Wed Oct 25, 2006 7:46 am
Location: South Florida
Contact:

How to pass events to the GUI from a mouse event handler?

Post by ssexton »

I have some code to do node picking and some other actions in the EMIE_LMOUSE_PRESSED_DOWN portion of my main event handler. Awhile back, I discovered that I needed to test whether the GUI wanted the event before I did anything with it, else if someone clicked on GUI controls (such as in a dialog) the clicks would "pass through" to the scene, because both the GUI and my custom code would process it.

In 1.3.1, I had code like this to check if the GUI wanted the event or not:

Code: Select all

if (disableEvents) return false;  // guard against infloops
// ... switch() etc for other event types

bool wasDisabled = disableEvents;
disableEvents = true;  // prevent nasty recursion from GUI
bool guiEvent = ctx->guienv->postEventFromUser(event);
disableEvents = wasDisabled;
if (guiEvent) return true; // gui wanted it

// .. do my scene node picking stuff
ie., I just used postEventFromUser on the GUI to see if it wanted it, if it said yes, then I did nothing with it. Of course if it says no, then it would turn around and call me again which is why I have the "disableEvents" boolean, else you get a stack fault from infinite recursion.

This code worked in 1.3.1. In 1.4beta, I find that EGET_MENU_ITEM_SELECTED is being handled (not just generated, but handled = dispatched to my event handler) from within the postEventFromUser() call, and thus was getting dropped by my recursive event guard (in 1.3.1, postEventFromUser would return after generating the event, but before dispatching it, thus I could re-enable events and the menu select event would get processed). Fixing it was no big deal, I just special cased the menu select event (I didn't want to move the disableEvent code because then I could end up with multiple "guards" like that all over the place) but figuring out that was what was happening did take some time. And I don't like having the special case there, since who knows what else might be getting dispatched from within postEventFromUser() that I drop by accident.

If you followed this discussion so far, and the issues, my question is this: what is the "right" way to delegate events to the GUI?
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

the infinite loop thing was a bug, it's now fixed in svn.

Code: Select all

if (ctx->guienv->postEventFromUser(event)) 
  return true; // gui wanted it

// .. do my scene node picking stuff

// possibly post event to scene manager

return true; // remember to return true so gui/smgr don't get the event again
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
Post Reply