CCNotificationCenter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /****************************************************************************
  2. Copyright (c) 2011 Erawppa
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CCNOTIFICATIONCENTER_H__
  23. #define __CCNOTIFICATIONCENTER_H__
  24. /// @cond DO_NOT_SHOW
  25. #include "base/CCRef.h"
  26. #include "base/ccTypes.h"
  27. NS_CC_BEGIN
  28. class __Array;
  29. class ScriptHandlerMgr;
  30. /**
  31. * @cond DO_NOT_SHOW
  32. * @{
  33. */
  34. class CC_DLL __NotificationCenter : public Ref
  35. {
  36. friend class ScriptHandlerMgr;
  37. public:
  38. /** __NotificationCenter constructor
  39. * @js ctor
  40. */
  41. __NotificationCenter();
  42. /** __NotificationCenter destructor
  43. * @js NA
  44. * @lua NA
  45. */
  46. ~__NotificationCenter();
  47. /** Gets the single instance of __NotificationCenter. */
  48. static __NotificationCenter *getInstance();
  49. /** Destroys the single instance of __NotificationCenter. */
  50. static void destroyInstance();
  51. /** @deprecated use getInstance() instead */
  52. CC_DEPRECATED_ATTRIBUTE static __NotificationCenter *sharedNotificationCenter(void);
  53. /** @deprecated use destroyInstance() instead */
  54. CC_DEPRECATED_ATTRIBUTE static void purgeNotificationCenter(void);
  55. /** @brief Adds an observer for the specified target.
  56. * @param target The target which wants to observe notification events.
  57. * @param selector The callback function which will be invoked when the specified notification event was posted.
  58. * @param name The name of this notification.
  59. * @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. nullptr means that the sender is not used to decide whether to deliver the notification to target.
  60. */
  61. void addObserver(Ref *target,
  62. SEL_CallFuncO selector,
  63. const std::string& name,
  64. Ref *sender);
  65. /** @brief Removes the observer by the specified target and name.
  66. * @param target The target of this notification.
  67. * @param name The name of this notification.
  68. */
  69. void removeObserver(Ref *target,const std::string& name);
  70. /** @brief Removes all notifications registered by this target
  71. * @param target The target of this notification.
  72. * @returns the number of observers removed
  73. */
  74. int removeAllObservers(Ref *target);
  75. /** @brief Registers one hander for script binding.
  76. * @note Only supports Lua Binding now.
  77. * @param handler The lua handler.
  78. */
  79. void registerScriptObserver(Ref *target,int handler,const std::string& name);
  80. /** Unregisters script observer */
  81. void unregisterScriptObserver(Ref *target,const std::string& name);
  82. /** @brief Posts one notification event by name.
  83. * @param name The name of this notification.
  84. */
  85. void postNotification(const std::string& name);
  86. /** @brief Posts one notification event by name.
  87. * @param name The name of this notification.
  88. * @param sender The object posting the notification. Can be nullptr
  89. */
  90. void postNotification(const std::string& name, Ref *sender);
  91. /** @brief Gets script handler.
  92. * @note Only supports Lua Binding now.
  93. * @return The script handle.
  94. */
  95. int getScriptHandler() const { return _scriptHandler; }
  96. /** @brief Gets observer script handler.
  97. * @param name The name of this notification.
  98. * @return The observer script handle.
  99. */
  100. int getObserverHandlerByName(const std::string& name);
  101. private:
  102. // internal functions
  103. // Check whether the observer exists by the specified target and name.
  104. bool observerExisted(Ref *target,const std::string& name, Ref *sender);
  105. // variables
  106. //
  107. __Array *_observers;
  108. int _scriptHandler;
  109. };
  110. class CC_DLL NotificationObserver : public Ref
  111. {
  112. public:
  113. /** @brief NotificationObserver constructor
  114. * @param target The target which wants to observer notification events.
  115. * @param selector The callback function which will be invoked when the specified notification event was posted.
  116. * @param name The name of this notification.
  117. * @param sender The object whose notifications the target wants to receive. Only notifications sent by this sender are delivered to the target. nullptr means that the sender is not used to decide whether to deliver the notification to target.
  118. * @js NA
  119. * @lua NA
  120. */
  121. NotificationObserver(Ref *target,
  122. SEL_CallFuncO selector,
  123. const std::string& name,
  124. Ref *sender);
  125. /** NotificationObserver destructor function
  126. * @js NA
  127. * @lua NA
  128. */
  129. ~NotificationObserver();
  130. /** Invokes the callback function of this observer
  131. * @js NA
  132. * @lua NA
  133. */
  134. void performSelector(Ref *sender);
  135. // Getters / Setters
  136. /**
  137. * @js NA
  138. * @lua NA
  139. */
  140. Ref* getTarget() const;
  141. /**
  142. * @js NA
  143. * @lua NA
  144. */
  145. SEL_CallFuncO getSelector() const;
  146. /**
  147. * @js NA
  148. * @lua NA
  149. */
  150. const std::string& getName() const;
  151. /**
  152. * @js NA
  153. * @lua NA
  154. */
  155. Ref* getSender() const;
  156. /**
  157. * @js NA
  158. * @lua NA
  159. */
  160. int getHandler() const;
  161. /**
  162. * @js NA
  163. * @lua NA
  164. */
  165. void setHandler(int handler);
  166. private:
  167. Ref* _target;
  168. SEL_CallFuncO _selector;
  169. std::string _name;
  170. Ref* _sender;
  171. int _handler;
  172. };
  173. /**
  174. * @}
  175. */
  176. NS_CC_END
  177. /// @endcond
  178. #endif//__CCNOTIFICATIONCENTER_H__