eventManager.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _EVENTMANAGER_H_
  23. #define _EVENTMANAGER_H_
  24. #ifndef _CONSOLE_H_
  25. #include "console/console.h"
  26. #endif
  27. #ifndef _DISPATCHER_H_
  28. #include "util/messaging/dispatcher.h"
  29. #endif
  30. #ifndef _TSIMPLEHASHTABLE_H
  31. #include "core/tSimpleHashTable.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. /// Listener class used by the EventManager to dispatch messages to specific
  35. /// callbacks.
  36. //-----------------------------------------------------------------------------
  37. class EventManagerListener : public Dispatcher::IMessageListener
  38. {
  39. friend class EventManager;
  40. /// Stores subscription information for a subscriber.
  41. struct Subscriber
  42. {
  43. SimObjectPtr< SimObject > listener; ///< The listener object.
  44. StringTableEntry callback; ///< The callback to execute when the event is triggered.
  45. StringTableEntry event; ///< The event being listened for.
  46. U32 callDepth;
  47. bool removeFlag;
  48. };
  49. /// Subscriber table hashed by event name.
  50. SimpleHashTable< Vector<Subscriber> > mSubscribers;
  51. public:
  52. /// Called by the EventManager queue when an event is triggered. Calls all listeners subscribed to the triggered event.
  53. virtual bool onMessageReceived( StringTableEntry queue, const char* event, const char* data );
  54. virtual bool onMessageObjectReceived( StringTableEntry queue, Message *msg ) { return true; };
  55. };
  56. /// @addtogroup msgsys Message System
  57. // @{
  58. //-----------------------------------------------------------------------------
  59. /// The EventManager class is a wrapper for the standard messaging system. It
  60. /// provides functionality for management of event queues, events, and
  61. /// subscriptions.
  62. ///
  63. /// Creating an EventManager is as simple as calling <tt>new EventManager</tt>
  64. /// and specifying a queue name.
  65. ///
  66. /// Example Usage:
  67. ///
  68. /// @code
  69. /// // Create the EventManager.
  70. /// $MyEventManager = new EventManager() { queue = "MyEventManager"; };
  71. ///
  72. /// // Create an event.
  73. /// $MyEventManager.registerEvent( "SomeCoolEvent" );
  74. ///
  75. /// // Create a listener and subscribe.
  76. /// $MyListener = new ScriptMsgListener() { class = MyListener; };
  77. /// $MyEventManager.subscribe( $MyListener, "SomeCoolEvent" );
  78. ///
  79. /// function MyListener::onSomeCoolEvent( %this, %data )
  80. /// {
  81. /// echo( "onSomeCoolEvent Triggered" );
  82. /// }
  83. ///
  84. /// // Trigger the event.
  85. /// $MyEventManager.postEvent( "SomeCoolEvent", "Data" );
  86. /// @endcode
  87. //-----------------------------------------------------------------------------
  88. class EventManager : public SimObject
  89. {
  90. typedef SimObject Parent;
  91. private:
  92. /// The name of the message queue.
  93. StringTableEntry mQueue;
  94. /// Registered events.
  95. Vector<StringTableEntry> mEvents;
  96. /// The event listener. Listens for all events and dispatches them to the appropriate subscribers.
  97. EventManagerListener mListener;
  98. /// List of all EventManagers.
  99. static Vector<EventManager*> smEventManagers;
  100. /// Sets the message queue.
  101. static bool _setMessageQueue( void *obj, const char *index, const char *data )
  102. {
  103. static_cast<EventManager*>( obj )->setMessageQueue( data );
  104. return false;
  105. };
  106. public:
  107. DECLARE_CONOBJECT( EventManager );
  108. EventManager();
  109. virtual ~EventManager();
  110. static void initPersistFields();
  111. /// @name Properties
  112. /// @{
  113. StringTableEntry getMessageQueue() const { return mQueue; }
  114. void setMessageQueue( const char* queue );
  115. /// @}
  116. /// @name Event Management
  117. /// @{
  118. /// Checks if an event is registered.
  119. bool isRegisteredEvent( const char* eventName );
  120. /// Registers an event.
  121. bool registerEvent( const char* eventName );
  122. /// Removes an event.
  123. void unregisterEvent( const char* eventName );
  124. /// Removes all events.
  125. void unregisterAllEvents();
  126. /// Triggers an event.
  127. bool postEvent( const char* eventName, const char* data );
  128. /// Adds a subscription to an event.
  129. bool subscribe( SimObject *callbackObj, const char* event, const char* callback = NULL );
  130. /// Remove a subscriber from an event.
  131. void remove( SimObject *cbObj, const char* event );
  132. void removeAll( SimObject *cbObj );
  133. /// @}
  134. /// @name Debug Output
  135. /// @{
  136. /// Prints all registered events to the console.
  137. void dumpEvents();
  138. /// Prints all subscribers to the console.
  139. void dumpSubscribers();
  140. /// Prints subscribers to a specific event to the console.
  141. void dumpSubscribers( const char* event );
  142. /// @}
  143. /// @name Event Manager Tracking
  144. /// @{
  145. /// Adds an EventManager.
  146. static void addEventManager( EventManager* em ) { smEventManagers.push_back( em ); };
  147. /// Removes an EventManager.
  148. static void removeEventManager( EventManager* em )
  149. {
  150. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  151. {
  152. if( *iter == em )
  153. {
  154. smEventManagers.erase_fast( iter );
  155. break;
  156. }
  157. }
  158. };
  159. /// Retrieves an EventManager.
  160. static EventManager* getEventManager( const char* name )
  161. {
  162. StringTableEntry queue = StringTable->insert( name );
  163. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  164. {
  165. if( ( *iter )->mQueue == queue )
  166. return *iter;
  167. }
  168. return NULL;
  169. };
  170. /// Prints all the EventManagers to the console.
  171. static void printEventManagers()
  172. {
  173. for( Vector<EventManager*>::iterator iter = smEventManagers.begin(); iter != smEventManagers.end(); iter++ )
  174. ( *iter )->dumpSubscribers();
  175. }
  176. /// @}
  177. };
  178. // @}
  179. #endif // _EVENTMANAGER_H_