Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. class CEventHandler
  2. {
  3. private:
  4.  
  5. // Prevent EventHandlers being created before event sys had been opened
  6. //
  7. static RwUInt32 refCountEventHandlersCreated;
  8.  
  9. public:
  10.  
  11. /**
  12. *
  13. * Construct an instance of the CEventHandler class. Clients of CEventHandler should not call the
  14. * constructor directly but by using the \a InitCEventHandler macro, this is because the parameters
  15. * to the constructor change when \a RWS_EVENTVISUALIZATION is defined, the \a InitCEventHandler takes
  16. * care of this.
  17. *
  18. * \param pCEventVisualization A pointer to the CEventVisualization class which defines an interface
  19. * which if supported provides the 3D position of the event handler which is used by the event
  20. * Visualization system.
  21. *
  22. * \note Event Visualization is only enabled when RWS_EVENTVISUALIZATION is defined.
  23. *
  24. * Example showing the difference between using CEventHandler, and InitCEventHandler
  25. *
  26. * \verbatim
  27.  
  28. class DisplayDebugTools : public CEventHandler
  29. {
  30. public:
  31.  
  32. virtual void HandleEvents(CMsg &pMsg);
  33.  
  34. DisplayDebugTools(void) : InitCEventHandler(m_pEntity) {}
  35.  
  36. // using the constructor directly would require RWS_EVENTVISUALIZATION to be handled i.e.
  37. //
  38. #ifdef RWS_EVENTVISUALIZATION
  39. DisplayDebugTools(void) : CEventHandler(m_pEntity) {}
  40. #else
  41. DisplayDebugTools(void) : CEventHandler() {}
  42. #endif
  43.  
  44. ~DisplayDebugTools(void);
  45.  
  46. protected:
  47.  
  48. C3DRwEntityPtr m_pEntity; // Note: C3DRwEntity is derived from CEventVisualization
  49. };
  50.  
  51. \endverbatim
  52. *
  53. * \note If the event handler doesn't provide the CEventVisualization interface initialize the
  54. * event handler with 0 as the parameter, i.e. InitCEventHandler(0).
  55. */
  56.  
  57. #ifdef RWS_EVENTVISUALIZATION
  58. CEventHandler(CEventVisualization *pCEventVisualization) :
  59. m_pCEventVisualization(pCEventVisualization), m_Active(true)
  60. {
  61. #ifndef NDEBUG
  62. refCountEventHandlersCreated++;
  63. #endif
  64. };
  65.  
  66. /**
  67. *
  68. */
  69.  
  70. #define InitCEventHandler(a) CEventHandler(a)
  71.  
  72. #else
  73. CEventHandler() : m_Active(true)
  74. {
  75. #ifndef NDEBUG
  76. refCountEventHandlersCreated++;
  77. #endif
  78. };
  79.  
  80. /**
  81. *
  82. */
  83.  
  84. #define InitCEventHandler(a) CEventHandler()
  85. #endif
  86.  
  87. virtual ~CEventHandler(void);
  88.  
  89. static void Open(RwUInt32 linkBlockSize = 0, RwUInt32 regBlockSize = 0);
  90. static void Close(void);
  91.  
  92. static void RegisterStreamChunkHandlers(void);
  93.  
  94. static void UnRegisterStreamChunkHandlers(void);
  95.  
  96. /**
  97. *
  98. * Purge - purges unused memory within the event system. Call can be slow.
  99. *
  100. */
  101.  
  102. static void Purge(void)
  103. {
  104. // Purges sub-objects which use 'RwFreeList'.
  105.  
  106. CLinkedMsg::PurgeMem();
  107. CRegisteredMsgs::PurgeMem();
  108. }
  109.  
  110.  
  111. /**
  112. *
  113. * Translate and process messages for this event handler \ref CEventHandler, this is how event
  114. * handlers receive an event,
  115. *
  116. * \param pMsg A reference to an event \ref CMsg, this is the event that caused HandleEvents
  117. * to be called.
  118. *
  119. * \note calling BaseClass::HandleEvents will pass on the event to the baseclass, this can
  120. * be used to extend the behavior of previously define event handler.
  121. *
  122. */
  123.  
  124. virtual void HandleEvents(CMsg &pMsg) = 0;
  125.  
  126. static void UnRegisterMsg(CEventId &Id);
  127.  
  128. static void RegisterMsg(CEventId &Id,const char *p_msgname,const char *p_formatstring);
  129.  
  130. static void ReplaceRegisteredMsg(CEventId &Id,const char *p_msgname,const char *p_formatstring);
  131.  
  132. void ReplaceLinkedMsg(CEventId &Id,const char *p_msgname,const char *p_formatstring);
  133.  
  134. static void ReplaceLinkedMsg(CEventHandler &rEventHand, CEventId &Id,const char *p_msgname,
  135. const char *p_formatstring, RwUInt16 priority = 0x8000);
  136.  
  137. static void SetLinkPriority(CEventHandler &rEventHand, CEventId &Id,
  138. const char *p_formatString, RwUInt16 priority = 0x8000);
  139.  
  140. static void DeleteEventHandlers(const CMsg *p_Msg);
  141.  
  142. static void DeleteEventHandlers(const CEventId &Id);
  143.  
  144. static void RegisterToDeleteEventHandlers(const CMsg *p_Msg);
  145.  
  146. static void RegisterToDeleteEventHandlers(const CEventId &Id);
  147.  
  148. static RwBool LinkMsgToEventHandler(
  149. CEventHandler *pCEventHandler,
  150. CEventId &Id,
  151. const char *p_formatstring = 0,
  152. RwUInt16 priority = 0x8000);
  153.  
  154. void LinkMsg(CEventId &Id,const char *p_formatstring = 0,RwUInt16 priority = 0x8000);
  155.  
  156. RwBool IsLinked(CEventId &Id);
  157.  
  158. RwInt32 GetLinkedPriority(CEventId &Id);
  159.  
  160. static void Delete(CEventHandler *pCEventHandler);
  161.  
  162. void Delete(void) {Delete(this);}
  163.  
  164. void UnLinkMsg(CEventId &Id);
  165.  
  166. /**
  167. *
  168. * \ref UnRegisterForAutoDelete Removes the CEventHandler from the list of
  169. * CEventHandlers to be deleted when
  170. * CEventHandler::DeleteEventHandlers() is called.
  171. *
  172. */
  173.  
  174. void UnRegisterForAutoDelete() { UnLinkMsg(iMsgDeleteEventHandler); }
  175.  
  176. /**
  177. *
  178. * \ref RegisterToAutoDelete
  179. *
  180. * Registers a CEventHandler with the auto delete event
  181. * \ref iMsgDeleteEventHandler. All of the event handlers
  182. * registered with this event can be destroyed at the end of a level
  183. * when CEventHandler::DeleteEventHandlers (iMsgDeleteEventHandler)
  184. * is called.
  185. *
  186. */
  187.  
  188. void RegisterForAutoDelete() { LinkMsg(iMsgDeleteEventHandler); }
  189.  
  190. static void ProcessPendingUnlinks(CLinkedMsg *pErm);
  191.  
  192. void SendMsgToEventHandler(CMsg &p_Msg,CEventHandler *pCEventHandler);
  193.  
  194. void SendMsgToEventHandler(const CEventId &Id,CEventHandler *pCEventHandler);
  195.  
  196. private:
  197.  
  198. CEventHandler(const CEventHandler& rhs); // Disable copy constructor
  199.  
  200. CEventHandler &operator = (const CEventHandler& rhs); // Disable copy operator,
  201.  
  202. // Following section is used to visualize events.
  203. //
  204. #ifdef RWS_EVENTVISUALIZATION
  205.  
  206. public:
  207.  
  208. CEventVisualization *Get_m_pCEventVisualization(void) {return m_pCEventVisualization;}
  209.  
  210. void Set_m_pCEventVisualization(CEventVisualization *pCEventVisualization)
  211. {m_pCEventVisualization = pCEventVisualization;}
  212.  
  213.  
  214. protected:
  215.  
  216. /**
  217. *
  218. * \ref m_pCEventVisualization Pointer to an event Visualization class, provides methods
  219. * for extracting the 3D position or the event handler.
  220. *
  221. * \see CEventHandler constructor
  222. *
  223. * \note m_pCEventVisualization is only available if RWS_EVENTVISUALIZATION is defined
  224. */
  225.  
  226. CEventVisualization *m_pCEventVisualization;
  227.  
  228. protected:
  229.  
  230. RwBool _SendMsg(CMsg &p_Msg);
  231.  
  232. /**
  233. *
  234. * Broadcast event plus data.
  235. *
  236. * \param p_Msg A reference to a CMsg class containing the Id of the event and any additional data.
  237. *
  238. * \return true if any event handlers are linked to the event, otherwise false.
  239. *
  240. */
  241. RwBool SendMsg(CMsg &p_Msg)
  242. {
  243. if (p_Msg.Id != ERROR_NOT_A_VALID_MESSAGE_ID && p_Msg.Id->Erm.p_next)
  244. {
  245. return _SendMsg(p_Msg);
  246. }
  247. else
  248. {
  249. return false;
  250. }
  251. }
  252.  
  253. /**
  254. *
  255. * Broadcast event. The data field of the sent message is set to 0.
  256. *
  257. * \param Id A reference to a CEventId class containing the Id of the event.
  258. *
  259. * \return true if any event handlers are linked to the event, otherwise false.
  260. *
  261. */
  262. RwBool SendMsg(const CEventId &Id)
  263. {
  264. if (Id != ERROR_NOT_A_VALID_MESSAGE_ID && Id->Erm.p_next)
  265. {
  266. CMsg iMsg(Id,0);
  267.  
  268. return _SendMsg (iMsg);
  269. }
  270. else
  271. {
  272. return false;
  273. }
  274. }
  275. #endif
  276.  
  277. public:
  278.  
  279. RwBool m_Active; /**< Used to block a CEventHandler from receiving any further
  280. events after Delete has been called. */
  281.  
  282. protected:
  283. } ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement