CCArmatureAnimation.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CCANIMATION_H__
  21. #define __CCANIMATION_H__
  22. #include "editor-support/cocostudio/CCProcessBase.h"
  23. #include "editor-support/cocostudio/CCTween.h"
  24. #include "editor-support/cocostudio/CocosStudioExport.h"
  25. #include <queue>
  26. namespace cocostudio {
  27. enum MovementEventType
  28. {
  29. START,
  30. COMPLETE,
  31. LOOP_COMPLETE
  32. };
  33. class Armature;
  34. class Bone;
  35. typedef void (cocos2d::Ref::*SEL_MovementEventCallFunc)(Armature *, MovementEventType, const std::string&);
  36. typedef void (cocos2d::Ref::*SEL_FrameEventCallFunc)(Bone *, const std::string&, int, int);
  37. #define movementEvent_selector(_SELECTOR) (cocostudio::SEL_MovementEventCallFunc)(&_SELECTOR)
  38. #define frameEvent_selector(_SELECTOR) (cocostudio::SEL_FrameEventCallFunc)(&_SELECTOR)
  39. struct FrameEvent
  40. {
  41. Bone *bone;
  42. std::string frameEventName;
  43. int originFrameIndex;
  44. int currentFrameIndex;
  45. };
  46. struct MovementEvent
  47. {
  48. Armature *armature;
  49. MovementEventType movementType;
  50. std::string movementID;
  51. };
  52. class CC_STUDIO_DLL ArmatureAnimation : public ProcessBase
  53. {
  54. public:
  55. /**
  56. * Create with a Armature
  57. * @param armature The Armature ArmatureAnimation will bind to
  58. */
  59. static ArmatureAnimation *create(Armature *armature);
  60. public:
  61. /**
  62. * @js ctor
  63. */
  64. ArmatureAnimation();
  65. /**
  66. * @js NA
  67. * @lua NA
  68. */
  69. virtual ~ArmatureAnimation(void);
  70. /**
  71. * Init with a Armature
  72. * @param armature The Armature ArmatureAnimation will bind to
  73. */
  74. virtual bool init(Armature *armature);
  75. /**
  76. * Scale animation play speed.
  77. * This method is deprecated, please use setSpeedScale.
  78. * @param animationScale Scale value
  79. */
  80. CC_DEPRECATED_ATTRIBUTE virtual void setAnimationScale(float animationScale);
  81. CC_DEPRECATED_ATTRIBUTE virtual float getAnimationScale() const;
  82. /**
  83. * Scale animation play speed.
  84. * @param animationScale Scale value
  85. */
  86. virtual void setSpeedScale(float speedScale);
  87. virtual float getSpeedScale() const;
  88. //! The animation update speed
  89. CC_DEPRECATED_ATTRIBUTE virtual void setAnimationInternal(float animationInternal) {}
  90. using ProcessBase::play;
  91. /**
  92. * Play animation by animation name.
  93. *
  94. * @param animationName The animation name you want to play
  95. * @param durationTo The frames between two animation changing-over.
  96. * It's meaning is changing to this animation need how many frames
  97. *
  98. * -1 : use the value from MovementData get from flash design panel
  99. * @param loop Whether the animation is loop
  100. *
  101. * loop < 0 : use the value from MovementData get from flash design panel
  102. * loop = 0 : this animation is not loop
  103. * loop > 0 : this animation is loop
  104. */
  105. virtual void play(const std::string& animationName, int durationTo = -1, int loop = -1);
  106. /**
  107. * Play animation by index, the other param is the same to play.
  108. * @deprecated, please use playWithIndex
  109. * @param animationIndex the animation index you want to play
  110. */
  111. CC_DEPRECATED_ATTRIBUTE virtual void playByIndex(int animationIndex, int durationTo = -1, int loop = -1);
  112. virtual void playWithIndex(int animationIndex, int durationTo = -1, int loop = -1);
  113. virtual void playWithNames(const std::vector<std::string>& movementNames, int durationTo = -1, bool loop = true);
  114. virtual void playWithIndexes(const std::vector<int>& movementIndexes, int durationTo = -1, bool loop = true);
  115. /**
  116. * Go to specified frame and play current movement.
  117. * You need first switch to the movement you want to play, then call this function.
  118. *
  119. * example : playByIndex(0);
  120. * gotoAndPlay(0);
  121. * playByIndex(1);
  122. * gotoAndPlay(0);
  123. * gotoAndPlay(15);
  124. */
  125. virtual void gotoAndPlay(int frameIndex);
  126. /**
  127. * Go to specified frame and pause current movement.
  128. */
  129. virtual void gotoAndPause(int frameIndex);
  130. /**
  131. * Pause the Process
  132. */
  133. virtual void pause() override;
  134. /**
  135. * Resume the Process
  136. */
  137. virtual void resume() override;
  138. /**
  139. * Stop the Process
  140. */
  141. virtual void stop() override;
  142. /**
  143. * Get movement count
  144. */
  145. ssize_t getMovementCount() const;
  146. virtual void update(float dt) override;
  147. /**
  148. * Get current movementID
  149. * @return The name of current movement
  150. */
  151. std::string getCurrentMovementID() const;
  152. /**
  153. * Set armature's movement event callback function
  154. * To disconnect this event, just setMovementEventCallFunc(nullptr, nullptr);
  155. */
  156. CC_DEPRECATED_ATTRIBUTE void setMovementEventCallFunc(cocos2d::Ref *target, SEL_MovementEventCallFunc callFunc);
  157. /**
  158. * Set armature's frame event callback function
  159. * To disconnect this event, just setFrameEventCallFunc(nullptr, nullptr);
  160. */
  161. CC_DEPRECATED_ATTRIBUTE void setFrameEventCallFunc(cocos2d::Ref *target, SEL_FrameEventCallFunc callFunc);
  162. void setMovementEventCallFunc(std::function<void(Armature *armature, MovementEventType movementType, const std::string& movementID)> listener);
  163. void setFrameEventCallFunc(std::function<void(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex)> listener);
  164. virtual void setAnimationData(AnimationData *data)
  165. {
  166. if (_animationData != data)
  167. {
  168. CC_SAFE_RETAIN(data);
  169. CC_SAFE_RELEASE(_animationData);
  170. _animationData = data;
  171. }
  172. }
  173. virtual AnimationData *getAnimationData() const { return _animationData; }
  174. /**
  175. * Returns a user assigned Object
  176. *
  177. * Similar to userData, but instead of holding a void* it holds an object
  178. *
  179. * @return A user assigned Object
  180. * @js NA
  181. * @lua NA
  182. */
  183. virtual Ref* getUserObject() { return _userObject; }
  184. /**
  185. * @js NA
  186. * @lua NA
  187. */
  188. virtual const Ref* getUserObject() const { return _userObject; }
  189. /**
  190. * Returns a user assigned Object
  191. *
  192. * Similar to UserData, but instead of holding a void* it holds an object.
  193. * The UserObject will be retained once in this method,
  194. * and the previous UserObject (if existed) will be release.
  195. * The UserObject will be released in Node's destructor.
  196. *
  197. * @param userObject A user assigned Object
  198. */
  199. virtual void setUserObject(Ref *userObject);
  200. protected:
  201. /**
  202. * Update(float dt) will call this handler, you can handle your logic here
  203. * @js NA
  204. * @lua NA
  205. */
  206. void updateHandler() override;
  207. /**
  208. * Update current key frame, and process auto stop, pause
  209. * @js NA
  210. * @lua NA
  211. */
  212. void updateFrameData(float currentPercent);
  213. /**
  214. * Emit a frame event
  215. * @js NA
  216. * @lua NA
  217. */
  218. void frameEvent(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex);
  219. /**
  220. * Emit a movement event
  221. */
  222. void movementEvent(Armature *armature, MovementEventType movementType, const std::string& movementID);
  223. void updateMovementList();
  224. bool isIgnoreFrameEvent() const { return _ignoreFrameEvent; }
  225. friend class Tween;
  226. protected:
  227. //! AnimationData save all MovementDatas this animation used.
  228. AnimationData *_animationData;
  229. //! Scale the animation speed
  230. float _speedScale;
  231. MovementData *_movementData; //! MovementData save all MovementFrameDatas this animation used.
  232. Armature *_armature; //! A weak reference of armature
  233. std::string _movementID; //! Current movement's name
  234. int _toIndex; //! The frame index in MovementData->m_pMovFrameDataArr, it's different from m_iFrameIndex.
  235. std::vector<Tween*> _tweenList;
  236. bool _ignoreFrameEvent;
  237. std::queue<FrameEvent*> _frameEventQueue;
  238. std::queue<MovementEvent*> _movementEventQueue;
  239. std::vector<std::string> _movementList;
  240. bool _onMovementList;
  241. bool _movementListLoop;
  242. unsigned int _movementIndex;
  243. int _movementListDurationTo;
  244. cocos2d::Ref *_userObject;
  245. protected:
  246. /**
  247. * MovementEvent CallFunc.
  248. * @param Armature* a Armature
  249. * @param MovementEventType, Event Type, like START, COMPLETE.
  250. * @param const char*, Movement ID, also called Movement Name
  251. */
  252. SEL_MovementEventCallFunc _movementEventCallFunc;
  253. /**
  254. * FrameEvent CallFunc.
  255. * @param Bone*, a Bone
  256. * @param const char*, the name of this frame event
  257. * @param int, origin frame index
  258. * @param int, current frame index, animation may be delayed
  259. */
  260. SEL_FrameEventCallFunc _frameEventCallFunc;
  261. cocos2d::Ref *_movementEventTarget;
  262. cocos2d::Ref *_frameEventTarget;
  263. std::function<void(Armature *armature, MovementEventType movementType, const std::string& movementID)> _movementEventListener;
  264. std::function<void(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex)> _frameEventListener;
  265. };
  266. }
  267. #endif /*__CCANIMATION_H__*/