Home | nevrax.com |
|
How to deal with events
Overall viewTo manage events, a class has to get or create a server. The server stores emitters, listeners, and events. When the server is required to pump events, it checks every emitters to get last events. All the listeners are stored in the server as a pair (type of event, listener). Thus a particular event can be handled by several listeners. It's the class which adds emitters and listeners it needs to the server. CEvent inherits CClassId. A predefined event is built by inheriting CEvent and using a CClassId, built whith a unique id. Existing events can be found in events.h. The interface provides a callback. A listener must implements this interface. Existing listeners can be found in event_listener.h. In the following example, the listener is supposed to have been added to an event server along with the event type EventCharId: CCallback cb; server.addListener (EventCharId, &cb); class CCallback : public IEventListener { virtual void operator ()(const CEvent& event) { CEventChar ec = (CEventChar &) event; // here we know that we receive a CEventChar event printf("%c", ec.Char); } }; It's the interface which gets low-level events and posts them to the server as NEL events. An emitter must implements this interface. Existing emitters can be found in emitters.h. A server is made of :
As for emitters, both server and class know the IListener. The listener callback is the operator() which takes an event in parameter. Thus, the user defines the listener/callback he needs and adds it to the server.
Here is an example of the use of the special listener CEventListenerAsync (defined in event_listener.cpp). Rq : This listener stores key states : if a key is pressed its value is on, else it's off.
// declaring a listener CEventListenerAsync asyncListener; // declaring the server CEventServer server; // adding an emitter to the server ... // here, a driver is initialized server.addEmitter(driver->getEventEmitter()); //in this ex the driver provides the emitter // adding the listener to the server asyncListener.addToServer(server); // events loop do { //pump for events server.pump(); // user main function mainproc(); } while(!asyncListener.isKeyPush(KeyESCAPE)); // removing listener from server asyncListener.removeFromServer(Server); Notes about the listener CEventListenerAsync : this listener adds two types of event to a server : EventKeyUpId and EventKeyDownId.
void CEventListenerAsync::addToServer (CEventServer& server) { server.addListener (EventKeyUpId, this); server.addListener (EventKeyDownId, this); } The remove method is similar to add method, we must precise both event id and listener to be removed.
void CEventListenerAsync::removeFromServer (CEventServer& server) { server.removeListener (EventKeyUpId, this); server.removeListener (EventKeyDownId, this); }
|