/cocos/2d/CCNotificationCenter.cpp

https://bitbucket.org/nesdavid/cocos2dx · C++ · 301 lines · 219 code · 46 blank · 36 comment · 43 complexity · a71706fdde8bed6e38877e3bdf2ee3c9 MD5 · raw file

  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2011 Erawppa
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCNotificationCenter.h"
  22. #include "CCArray.h"
  23. #include "CCScriptSupport.h"
  24. #include <string>
  25. using namespace std;
  26. NS_CC_BEGIN
  27. static NotificationCenter *s_sharedNotifCenter = NULL;
  28. NotificationCenter::NotificationCenter()
  29. : _scriptHandler(0)
  30. {
  31. _observers = __Array::createWithCapacity(3);
  32. _observers->retain();
  33. }
  34. NotificationCenter::~NotificationCenter()
  35. {
  36. _observers->release();
  37. }
  38. NotificationCenter *NotificationCenter::getInstance()
  39. {
  40. if (!s_sharedNotifCenter)
  41. {
  42. s_sharedNotifCenter = new NotificationCenter;
  43. }
  44. return s_sharedNotifCenter;
  45. }
  46. void NotificationCenter::destroyInstance()
  47. {
  48. CC_SAFE_RELEASE_NULL(s_sharedNotifCenter);
  49. }
  50. // XXX: deprecated
  51. NotificationCenter *NotificationCenter::sharedNotificationCenter(void)
  52. {
  53. return NotificationCenter::getInstance();
  54. }
  55. // XXX: deprecated
  56. void NotificationCenter::purgeNotificationCenter(void)
  57. {
  58. NotificationCenter::destroyInstance();
  59. }
  60. //
  61. // internal functions
  62. //
  63. bool NotificationCenter::observerExisted(Object *target,const char *name, Object *sender)
  64. {
  65. Object* obj = NULL;
  66. CCARRAY_FOREACH(_observers, obj)
  67. {
  68. NotificationObserver* observer = (NotificationObserver*) obj;
  69. if (!observer)
  70. continue;
  71. if (!strcmp(observer->getName(),name) && observer->getTarget() == target && observer->getSender() == sender)
  72. return true;
  73. }
  74. return false;
  75. }
  76. //
  77. // observer functions
  78. //
  79. void NotificationCenter::addObserver(Object *target,
  80. SEL_CallFuncO selector,
  81. const char *name,
  82. Object *sender)
  83. {
  84. if (this->observerExisted(target, name, sender))
  85. return;
  86. NotificationObserver *observer = new NotificationObserver(target, selector, name, sender);
  87. if (!observer)
  88. return;
  89. observer->autorelease();
  90. _observers->addObject(observer);
  91. }
  92. void NotificationCenter::removeObserver(Object *target,const char *name)
  93. {
  94. Object* obj = NULL;
  95. CCARRAY_FOREACH(_observers, obj)
  96. {
  97. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  98. if (!observer)
  99. continue;
  100. if (!strcmp(observer->getName(),name) && observer->getTarget() == target)
  101. {
  102. _observers->removeObject(observer);
  103. return;
  104. }
  105. }
  106. }
  107. int NotificationCenter::removeAllObservers(Object *target)
  108. {
  109. Object *obj = NULL;
  110. __Array *toRemove = __Array::create();
  111. CCARRAY_FOREACH(_observers, obj)
  112. {
  113. NotificationObserver *observer = static_cast<NotificationObserver *>(obj);
  114. if (!observer)
  115. continue;
  116. if (observer->getTarget() == target)
  117. {
  118. toRemove->addObject(observer);
  119. }
  120. }
  121. _observers->removeObjectsInArray(toRemove);
  122. return static_cast<int>(toRemove->count());
  123. }
  124. void NotificationCenter::registerScriptObserver( Object *target, int handler,const char* name)
  125. {
  126. if (this->observerExisted(target, name, NULL))
  127. return;
  128. NotificationObserver *observer = new NotificationObserver(target, NULL, name, NULL);
  129. if (!observer)
  130. return;
  131. observer->setHandler(handler);
  132. observer->autorelease();
  133. _observers->addObject(observer);
  134. }
  135. void NotificationCenter::unregisterScriptObserver(Object *target,const char* name)
  136. {
  137. Object* obj = NULL;
  138. CCARRAY_FOREACH(_observers, obj)
  139. {
  140. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  141. if (!observer)
  142. continue;
  143. if ( !strcmp(observer->getName(),name) && observer->getTarget() == target)
  144. {
  145. _observers->removeObject(observer);
  146. }
  147. }
  148. }
  149. void NotificationCenter::postNotification(const char *name, Object *sender)
  150. {
  151. __Array* ObserversCopy = __Array::createWithCapacity(_observers->count());
  152. ObserversCopy->addObjectsFromArray(_observers);
  153. Object* obj = NULL;
  154. CCARRAY_FOREACH(ObserversCopy, obj)
  155. {
  156. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  157. if (!observer)
  158. continue;
  159. if (!strcmp(name,observer->getName()) && (observer->getSender() == sender || observer->getSender() == NULL || sender == NULL))
  160. {
  161. if (0 != observer->getHandler())
  162. {
  163. BasicScriptData data(this, (void*)name);
  164. ScriptEvent scriptEvent(kNotificationEvent,(void*)&data);
  165. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
  166. }
  167. else
  168. {
  169. observer->performSelector(sender);
  170. }
  171. }
  172. }
  173. }
  174. void NotificationCenter::postNotification(const char *name)
  175. {
  176. this->postNotification(name,NULL);
  177. }
  178. int NotificationCenter::getObserverHandlerByName(const char* name)
  179. {
  180. if (NULL == name || strlen(name) == 0)
  181. {
  182. return 0;
  183. }
  184. Object* obj = NULL;
  185. CCARRAY_FOREACH(_observers, obj)
  186. {
  187. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  188. if (NULL == observer)
  189. continue;
  190. if ( 0 == strcmp(observer->getName(),name) )
  191. {
  192. return observer->getHandler();
  193. break;
  194. }
  195. }
  196. return 0;
  197. }
  198. ////////////////////////////////////////////////////////////////////////////////
  199. ///
  200. /// NotificationObserver
  201. ///
  202. ////////////////////////////////////////////////////////////////////////////////
  203. NotificationObserver::NotificationObserver(Object *target,
  204. SEL_CallFuncO selector,
  205. const char *name,
  206. Object *sender)
  207. {
  208. _target = target;
  209. _selector = selector;
  210. _sender = sender;
  211. _name = name;
  212. _handler = 0;
  213. }
  214. NotificationObserver::~NotificationObserver()
  215. {
  216. }
  217. void NotificationObserver::performSelector(Object *sender)
  218. {
  219. if (_target)
  220. {
  221. if (sender) {
  222. (_target->*_selector)(sender);
  223. } else {
  224. (_target->*_selector)(_sender);
  225. }
  226. }
  227. }
  228. Object *NotificationObserver::getTarget() const
  229. {
  230. return _target;
  231. }
  232. SEL_CallFuncO NotificationObserver::getSelector() const
  233. {
  234. return _selector;
  235. }
  236. const char* NotificationObserver::getName() const
  237. {
  238. return _name.c_str();
  239. }
  240. Object* NotificationObserver::getSender() const
  241. {
  242. return _sender;
  243. }
  244. int NotificationObserver::getHandler() const
  245. {
  246. return _handler;
  247. }
  248. void NotificationObserver::setHandler(int var)
  249. {
  250. _handler = var;
  251. }
  252. NS_CC_END