CCNotificationCenter.h 6.6 KB

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