Changes between Initial Version and Version 1 of EventManagement


Ignore:
Timestamp:
10/06/07 19:12:37 (17 years ago)
Author:
art
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • EventManagement

    v1 v1  
     1= Manual = 
     2== Event System == 
     3 
     4Event management presents a system providing your application with message basaed communication of your software modules. Instead of using callback functions it is often more reliable to use an event based messaging system. Also events helps you to decrease dependency of different software modules. 
     5 
     6So how event system works. Similar to the Qt signal/slot implementation the engine offers you an actor/channel implementation. Here one register new event channels in the engine, by calling 
     7{{{ 
     8#!cpp 
     9Engine::sEventManager()->createChannel("new_channel"); 
     10}}} 
     11 
     12Now any event actor connected to the channel can receive and send messages (Note: to send messages you not have to be an actor, only if you also like to receive them you have to be one). To create an event actor you simple derive a new class from engine's EventActor class: 
     13{{{ 
     14#!cpp 
     15class Actor : public nrEngine::EventActor 
     16{ 
     17 
     18   // Initialize actor 
     19   Actor() 
     20   { 
     21       // connect Actor to the new_channel event channel 
     22       connect("new_channel"); 
     23   } 
     24 
     25 
     26   // React on events 
     27   void OnEvent(const nrEngine::EventChannel& channel, SharedPtr<nrEngine::Event> event) 
     28   { 
     29      // Check whenever the received event is of the type... 
     30      if (event->same_as<OnGUIEvent>()) 
     31      { 
     32          // cast the event into the type we want to have         
     33          SharedPtr<OnGUIEvent> ev = event_shared_cast<OnGUIEvent>(event); 
     34 
     35          // use new received event 
     36          reactOnGuiEvent(ev); 
     37 
     38          // just for example purpose we send an event to the messaging system 
     39          emit(new OnReceivedGUIEvent()); 
     40      } 
     41   } 
     42 
     43}; 
     44 
     45}}} 
     46   
     47In this example we assumed you have already the classes OnGUIEvent and OnReceivedGUIEvent declared in your application. An example declaration of such a classes could look like this: 
     48{{{ 
     49#!cpp 
     50 
     51// Class derived from Event can be send through the event system 
     52class OnGUIEvent : public nrEngine::Event { 
     53 
     54    // need to declare this here to create meta information about this event             
     55    META_Event(OnGUIEvent) 
     56         
     57    public: 
     58 
     59        // just get data from the event 
     60        const std::string& getGUIName() { return _guiname; } 
     61        void* getData() { return _data; } 
     62 
     63        // create event and fill already with some data.                         
     64        OnGUIEvent(const std::string& u, nrEngine::Priority prior = nrEngine::Priority::IMMEDIATE) :  
     65                        nrEngine::Event(prior), _guiname(u) {} 
     66                 
     67    private: 
     68        // data stored in the event 
     69        std::string _guiname; 
     70        void* _data; 
     71}; 
     72 
     73}}} 
     74As you can see it is pretty simple to create new event types. Even more through the META_Event macro each event type can be also checked by a simple string comparison, i.e.  
     75{{{ 
     76#!cpp  
     77if (event->getEventType() == "OnGUIEvent") reactOnGuiEvent(event);  
     78}}} 
     79 
     80 
     81== Future work == 
     82Currently there is no remote event support. Hence sending events through some physical communication channel (e.g. network) does not work. In that case a plugin must be implemented which can deliver event messages on both sides of the channel. In fact the implementation shouldn't be a big problem and can be expanded completely by a plugin ;-) 
     83