CCActionManager.h 7.4 KB

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