CCActionManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2009 Valentin Milea
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2017 Chukong Technologies Inc.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "2d/CCActionManager.h"
  25. #include "2d/CCNode.h"
  26. #include "2d/CCAction.h"
  27. #include "base/CCScheduler.h"
  28. #include "base/ccMacros.h"
  29. #include "base/ccCArray.h"
  30. #include "base/uthash.h"
  31. NS_CC_BEGIN
  32. //
  33. // singleton stuff
  34. //
  35. typedef struct _hashElement
  36. {
  37. struct _ccArray *actions;
  38. Node *target;
  39. int actionIndex;
  40. Action *currentAction;
  41. bool currentActionSalvaged;
  42. bool paused;
  43. UT_hash_handle hh;
  44. } tHashElement;
  45. ActionManager::ActionManager()
  46. : _targets(nullptr),
  47. _currentTarget(nullptr),
  48. _currentTargetSalvaged(false)
  49. {
  50. }
  51. ActionManager::~ActionManager()
  52. {
  53. CCLOGINFO("deallocing ActionManager: %p", this);
  54. removeAllActions();
  55. }
  56. // private
  57. void ActionManager::deleteHashElement(tHashElement *element)
  58. {
  59. ccArrayFree(element->actions);
  60. HASH_DEL(_targets, element);
  61. element->target->release();
  62. free(element);
  63. }
  64. void ActionManager::actionAllocWithHashElement(tHashElement *element)
  65. {
  66. // 4 actions per Node by default
  67. if (element->actions == nullptr)
  68. {
  69. element->actions = ccArrayNew(4);
  70. }else
  71. if (element->actions->num == element->actions->max)
  72. {
  73. ccArrayDoubleCapacity(element->actions);
  74. }
  75. }
  76. void ActionManager::removeActionAtIndex(ssize_t index, tHashElement *element)
  77. {
  78. Action *action = static_cast<Action*>(element->actions->arr[index]);
  79. if (action == element->currentAction && (! element->currentActionSalvaged))
  80. {
  81. element->currentAction->retain();
  82. element->currentActionSalvaged = true;
  83. }
  84. ccArrayRemoveObjectAtIndex(element->actions, index, true);
  85. // update actionIndex in case we are in tick. looping over the actions
  86. if (element->actionIndex >= index)
  87. {
  88. element->actionIndex--;
  89. }
  90. if (element->actions->num == 0)
  91. {
  92. if (_currentTarget == element)
  93. {
  94. _currentTargetSalvaged = true;
  95. }
  96. else
  97. {
  98. deleteHashElement(element);
  99. }
  100. }
  101. }
  102. // pause / resume
  103. void ActionManager::pauseTarget(Node *target)
  104. {
  105. tHashElement *element = nullptr;
  106. HASH_FIND_PTR(_targets, &target, element);
  107. if (element)
  108. {
  109. element->paused = true;
  110. }
  111. }
  112. void ActionManager::resumeTarget(Node *target)
  113. {
  114. tHashElement *element = nullptr;
  115. HASH_FIND_PTR(_targets, &target, element);
  116. if (element)
  117. {
  118. element->paused = false;
  119. }
  120. }
  121. Vector<Node*> ActionManager::pauseAllRunningActions()
  122. {
  123. Vector<Node*> idsWithActions;
  124. for (tHashElement *element=_targets; element != nullptr; element = (tHashElement *)element->hh.next)
  125. {
  126. if (! element->paused)
  127. {
  128. element->paused = true;
  129. idsWithActions.pushBack(element->target);
  130. }
  131. }
  132. return idsWithActions;
  133. }
  134. void ActionManager::resumeTargets(const Vector<Node*>& targetsToResume)
  135. {
  136. for(const auto &node : targetsToResume)
  137. {
  138. this->resumeTarget(node);
  139. }
  140. }
  141. // run
  142. void ActionManager::addAction(Action *action, Node *target, bool paused)
  143. {
  144. CCASSERT(action != nullptr, "action can't be nullptr!");
  145. CCASSERT(target != nullptr, "target can't be nullptr!");
  146. if(action == nullptr || target == nullptr)
  147. return;
  148. tHashElement *element = nullptr;
  149. // we should convert it to Ref*, because we save it as Ref*
  150. Ref *tmp = target;
  151. HASH_FIND_PTR(_targets, &tmp, element);
  152. if (! element)
  153. {
  154. element = (tHashElement*)calloc(sizeof(*element), 1);
  155. element->paused = paused;
  156. target->retain();
  157. element->target = target;
  158. HASH_ADD_PTR(_targets, target, element);
  159. }
  160. actionAllocWithHashElement(element);
  161. CCASSERT(! ccArrayContainsObject(element->actions, action), "action already be added!");
  162. ccArrayAppendObject(element->actions, action);
  163. action->startWithTarget(target);
  164. }
  165. // remove
  166. void ActionManager::removeAllActions()
  167. {
  168. for (tHashElement *element = _targets; element != nullptr; )
  169. {
  170. auto target = element->target;
  171. element = (tHashElement*)element->hh.next;
  172. removeAllActionsFromTarget(target);
  173. }
  174. }
  175. void ActionManager::removeAllActionsFromTarget(Node *target)
  176. {
  177. // explicit null handling
  178. if (target == nullptr)
  179. {
  180. return;
  181. }
  182. tHashElement *element = nullptr;
  183. HASH_FIND_PTR(_targets, &target, element);
  184. if (element)
  185. {
  186. if (ccArrayContainsObject(element->actions, element->currentAction) && (! element->currentActionSalvaged))
  187. {
  188. element->currentAction->retain();
  189. element->currentActionSalvaged = true;
  190. }
  191. ccArrayRemoveAllObjects(element->actions);
  192. if (_currentTarget == element)
  193. {
  194. _currentTargetSalvaged = true;
  195. }
  196. else
  197. {
  198. deleteHashElement(element);
  199. }
  200. }
  201. }
  202. void ActionManager::removeAction(Action *action)
  203. {
  204. // explicit null handling
  205. if (action == nullptr)
  206. {
  207. return;
  208. }
  209. tHashElement *element = nullptr;
  210. Ref *target = action->getOriginalTarget();
  211. HASH_FIND_PTR(_targets, &target, element);
  212. if (element)
  213. {
  214. auto i = ccArrayGetIndexOfObject(element->actions, action);
  215. if (i != CC_INVALID_INDEX)
  216. {
  217. removeActionAtIndex(i, element);
  218. }
  219. }
  220. }
  221. void ActionManager::removeActionByTag(int tag, Node *target)
  222. {
  223. CCASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
  224. CCASSERT(target != nullptr, "target can't be nullptr!");
  225. if (target == nullptr)
  226. {
  227. return;
  228. }
  229. tHashElement *element = nullptr;
  230. HASH_FIND_PTR(_targets, &target, element);
  231. if (element)
  232. {
  233. auto limit = element->actions->num;
  234. for (int i = 0; i < limit; ++i)
  235. {
  236. Action *action = static_cast<Action*>(element->actions->arr[i]);
  237. if (action->getTag() == (int)tag && action->getOriginalTarget() == target)
  238. {
  239. removeActionAtIndex(i, element);
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. void ActionManager::removeAllActionsByTag(int tag, Node *target)
  246. {
  247. CCASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
  248. CCASSERT(target != nullptr, "target can't be nullptr!");
  249. if (target == nullptr)
  250. {
  251. return;
  252. }
  253. tHashElement *element = nullptr;
  254. HASH_FIND_PTR(_targets, &target, element);
  255. if (element)
  256. {
  257. auto limit = element->actions->num;
  258. for (int i = 0; i < limit;)
  259. {
  260. Action *action = static_cast<Action*>(element->actions->arr[i]);
  261. if (action->getTag() == (int)tag && action->getOriginalTarget() == target)
  262. {
  263. removeActionAtIndex(i, element);
  264. --limit;
  265. }
  266. else
  267. {
  268. ++i;
  269. }
  270. }
  271. }
  272. }
  273. void ActionManager::removeActionsByFlags(unsigned int flags, Node *target)
  274. {
  275. if (flags == 0)
  276. {
  277. return;
  278. }
  279. CCASSERT(target != nullptr, "target can't be nullptr!");
  280. if (target == nullptr)
  281. {
  282. return;
  283. }
  284. tHashElement *element = nullptr;
  285. HASH_FIND_PTR(_targets, &target, element);
  286. if (element)
  287. {
  288. auto limit = element->actions->num;
  289. for (int i = 0; i < limit;)
  290. {
  291. Action *action = static_cast<Action*>(element->actions->arr[i]);
  292. if ((action->getFlags() & flags) != 0 && action->getOriginalTarget() == target)
  293. {
  294. removeActionAtIndex(i, element);
  295. --limit;
  296. }
  297. else
  298. {
  299. ++i;
  300. }
  301. }
  302. }
  303. }
  304. // get
  305. // FIXME: Passing "const O *" instead of "const O&" because HASH_FIND_IT requires the address of a pointer
  306. // and, it is not possible to get the address of a reference
  307. Action* ActionManager::getActionByTag(int tag, const Node *target) const
  308. {
  309. CCASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
  310. tHashElement *element = nullptr;
  311. HASH_FIND_PTR(_targets, &target, element);
  312. if (element)
  313. {
  314. if (element->actions != nullptr)
  315. {
  316. auto limit = element->actions->num;
  317. for (int i = 0; i < limit; ++i)
  318. {
  319. Action *action = static_cast<Action*>(element->actions->arr[i]);
  320. if (action->getTag() == (int)tag)
  321. {
  322. return action;
  323. }
  324. }
  325. }
  326. }
  327. return nullptr;
  328. }
  329. // FIXME: Passing "const O *" instead of "const O&" because HASH_FIND_IT requires the address of a pointer
  330. // and, it is not possible to get the address of a reference
  331. ssize_t ActionManager::getNumberOfRunningActionsInTarget(const Node *target) const
  332. {
  333. tHashElement *element = nullptr;
  334. HASH_FIND_PTR(_targets, &target, element);
  335. if (element)
  336. {
  337. return element->actions ? element->actions->num : 0;
  338. }
  339. return 0;
  340. }
  341. // FIXME: Passing "const O *" instead of "const O&" because HASH_FIND_IT requires the address of a pointer
  342. // and, it is not possible to get the address of a reference
  343. size_t ActionManager::getNumberOfRunningActionsInTargetByTag(const Node *target,
  344. int tag)
  345. {
  346. CCASSERT(tag != Action::INVALID_TAG, "Invalid tag value!");
  347. tHashElement *element = nullptr;
  348. HASH_FIND_PTR(_targets, &target, element);
  349. if(!element || !element->actions)
  350. return 0;
  351. int count = 0;
  352. auto limit = element->actions->num;
  353. for(int i = 0; i < limit; ++i)
  354. {
  355. auto action = static_cast<Action*>(element->actions->arr[i]);
  356. if(action->getTag() == tag)
  357. ++count;
  358. }
  359. return count;
  360. }
  361. ssize_t ActionManager::getNumberOfRunningActions() const
  362. {
  363. ssize_t count = 0;
  364. struct _hashElement* element = nullptr;
  365. struct _hashElement* tmp = nullptr;
  366. HASH_ITER(hh, _targets, element, tmp)
  367. {
  368. count += (element->actions ? element->actions->num : 0);
  369. }
  370. return count;
  371. }
  372. // main loop
  373. void ActionManager::update(float dt)
  374. {
  375. for (tHashElement *elt = _targets; elt != nullptr; )
  376. {
  377. _currentTarget = elt;
  378. _currentTargetSalvaged = false;
  379. if (! _currentTarget->paused)
  380. {
  381. // The 'actions' MutableArray may change while inside this loop.
  382. for (_currentTarget->actionIndex = 0; _currentTarget->actionIndex < _currentTarget->actions->num;
  383. _currentTarget->actionIndex++)
  384. {
  385. _currentTarget->currentAction = static_cast<Action*>(_currentTarget->actions->arr[_currentTarget->actionIndex]);
  386. if (_currentTarget->currentAction == nullptr)
  387. {
  388. continue;
  389. }
  390. _currentTarget->currentActionSalvaged = false;
  391. _currentTarget->currentAction->step(dt);
  392. if (_currentTarget->currentActionSalvaged)
  393. {
  394. // The currentAction told the node to remove it. To prevent the action from
  395. // accidentally deallocating itself before finishing its step, we retained
  396. // it. Now that step is done, it's safe to release it.
  397. _currentTarget->currentAction->release();
  398. } else
  399. if (_currentTarget->currentAction->isDone())
  400. {
  401. _currentTarget->currentAction->stop();
  402. Action *action = _currentTarget->currentAction;
  403. // Make currentAction nil to prevent removeAction from salvaging it.
  404. _currentTarget->currentAction = nullptr;
  405. removeAction(action);
  406. }
  407. _currentTarget->currentAction = nullptr;
  408. }
  409. }
  410. // elt, at this moment, is still valid
  411. // so it is safe to ask this here (issue #490)
  412. elt = (tHashElement*)(elt->hh.next);
  413. // only delete currentTarget if no actions were scheduled during the cycle (issue #481)
  414. if (_currentTargetSalvaged && _currentTarget->actions->num == 0)
  415. {
  416. deleteHashElement(_currentTarget);
  417. }
  418. //if some node reference 'target', it's reference count >= 2 (issues #14050)
  419. else if (_currentTarget->target->getReferenceCount() == 1)
  420. {
  421. deleteHashElement(_currentTarget);
  422. }
  423. }
  424. // issue #635
  425. _currentTarget = nullptr;
  426. }
  427. NS_CC_END