CCActionManager.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifndef __ACTION_CCACTION_MANAGER_H__
  25. #define __ACTION_CCACTION_MANAGER_H__
  26. #include "2d/CCAction.h"
  27. #include "base/CCVector.h"
  28. #include "base/CCRef.h"
  29. NS_CC_BEGIN
  30. class Action;
  31. struct _hashElement;
  32. /**
  33. * @addtogroup actions
  34. * @{
  35. */
  36. /** @class ActionManager
  37. @brief ActionManager is a singleton that manages all the actions.
  38. Normally you won't need to use this singleton directly. 99% of the cases you will use the Node interface,
  39. which uses this singleton.
  40. But there are some cases where you might need to use this singleton.
  41. Examples:
  42. - When you want to run an action where the target is different from a Node.
  43. - When you want to pause / resume the actions.
  44. @since v0.8
  45. */
  46. class CC_DLL ActionManager : public Ref
  47. {
  48. public:
  49. /**
  50. * @js ctor
  51. */
  52. ActionManager(void);
  53. /**
  54. * @js NA
  55. * @lua NA
  56. */
  57. ~ActionManager(void);
  58. // actions
  59. /** Adds an action with a target.
  60. If the target is already present, then the action will be added to the existing target.
  61. If the target is not present, a new instance of this target will be created either paused or not, and the action will be added to the newly created target.
  62. When the target is paused, the queued actions won't be 'ticked'.
  63. *
  64. * @param action A certain action.
  65. * @param target The target which need to be added an action.
  66. * @param paused Is the target paused or not.
  67. */
  68. void addAction(Action *action, Node *target, bool paused);
  69. /** Removes all actions from all the targets.
  70. */
  71. void removeAllActions();
  72. /** Removes all actions from a certain target.
  73. All the actions that belongs to the target will be removed.
  74. *
  75. * @param target A certain target.
  76. */
  77. void removeAllActionsFromTarget(Node *target);
  78. /** Removes an action given an action reference.
  79. *
  80. * @param action A certain target.
  81. */
  82. void removeAction(Action *action);
  83. /** Removes an action given its tag and the target.
  84. *
  85. * @param tag The action's tag.
  86. * @param target A certain target.
  87. */
  88. void removeActionByTag(int tag, Node *target);
  89. /** Removes all actions given its tag and the target.
  90. *
  91. * @param tag The actions' tag.
  92. * @param target A certain target.
  93. * @js NA
  94. */
  95. void removeAllActionsByTag(int tag, Node *target);
  96. /** Removes all actions matching at least one bit in flags and the target.
  97. *
  98. * @param flags The flag field to match the actions' flags based on bitwise AND.
  99. * @param target A certain target.
  100. * @js NA
  101. */
  102. void removeActionsByFlags(unsigned int flags, Node *target);
  103. /** Gets an action given its tag an a target.
  104. *
  105. * @param tag The action's tag.
  106. * @param target A certain target.
  107. * @return The Action the with the given tag.
  108. */
  109. Action* getActionByTag(int tag, const Node *target) const;
  110. /** Returns the numbers of actions that are running in a certain target.
  111. * Composable actions are counted as 1 action. Example:
  112. * - If you are running 1 Sequence of 7 actions, it will return 1.
  113. * - If you are running 7 Sequences of 2 actions, it will return 7.
  114. *
  115. * @param target A certain target.
  116. * @return The numbers of actions that are running in a certain target.
  117. * @js NA
  118. */
  119. ssize_t getNumberOfRunningActionsInTarget(const Node *target) const;
  120. /** Returns the numbers of actions that are running in all targets.
  121. * Composable actions are counted as 1 action. Example:
  122. * - If you are running 1 Sequence of 7 actions, it will return 1.
  123. * - If you are running 7 Sequences of 2 actions, it will return 7.
  124. *
  125. * @return The numbers of actions that are running in a certain target.
  126. * @js NA
  127. */
  128. ssize_t getNumberOfRunningActions() const;
  129. /** @deprecated Use getNumberOfRunningActionsInTarget() instead.
  130. */
  131. CC_DEPRECATED_ATTRIBUTE ssize_t numberOfRunningActionsInTarget(Node *target) const { return getNumberOfRunningActionsInTarget(target); }
  132. /** Returns the numbers of actions that are running in a
  133. * certain target with a specific tag.
  134. * Like getNumberOfRunningActionsInTarget Composable actions
  135. * are counted as 1 action. Example:
  136. * - If you are running 1 Sequence of 7 actions, it will return 1.
  137. * - If you are running 7 Sequences of 2 actions, it will return 7.
  138. *
  139. * @param target A certain target.
  140. * @param tag Tag that will be searched.
  141. * @return The numbers of actions that are running in a certain target
  142. * with a specific tag.
  143. * @see getNumberOfRunningActionsInTarget
  144. * @js NA
  145. */
  146. size_t getNumberOfRunningActionsInTargetByTag(const Node *target, int tag);
  147. /** Pauses the target: all running actions and newly added actions will be paused.
  148. *
  149. * @param target A certain target.
  150. */
  151. void pauseTarget(Node *target);
  152. /** Resumes the target. All queued actions will be resumed.
  153. *
  154. * @param target A certain target.
  155. */
  156. void resumeTarget(Node *target);
  157. /** Pauses all running actions, returning a list of targets whose actions were paused.
  158. *
  159. * @return A list of targets whose actions were paused.
  160. */
  161. Vector<Node*> pauseAllRunningActions();
  162. /** Resume a set of targets (convenience function to reverse a pauseAllRunningActions call).
  163. *
  164. * @param targetsToResume A set of targets need to be resumed.
  165. */
  166. void resumeTargets(const Vector<Node*>& targetsToResume);
  167. /** Main loop of ActionManager.
  168. * @param dt In seconds.
  169. */
  170. void update(float dt);
  171. protected:
  172. // declared in ActionManager.m
  173. void removeActionAtIndex(ssize_t index, struct _hashElement *element);
  174. void deleteHashElement(struct _hashElement *element);
  175. void actionAllocWithHashElement(struct _hashElement *element);
  176. protected:
  177. struct _hashElement *_targets;
  178. struct _hashElement *_currentTarget;
  179. bool _currentTargetSalvaged;
  180. };
  181. // end of actions group
  182. /// @}
  183. NS_CC_END
  184. #endif // __ACTION_CCACTION_MANAGER_H__