CCActionManager.cpp 14 KB

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