CCActionTimeline.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  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 __CCTIMELINE_ACTION_H__
  21. #define __CCTIMELINE_ACTION_H__
  22. #include "editor-support/cocostudio/ActionTimeline/CCTimeLine.h"
  23. #include "base/CCProtocols.h"
  24. #include "editor-support/cocostudio/CocosStudioExport.h"
  25. #include "2d/CCAction.h"
  26. NS_TIMELINE_BEGIN
  27. typedef struct AnimationInfo
  28. {
  29. AnimationInfo()
  30. :startIndex(0)
  31. ,endIndex(0)
  32. {
  33. }
  34. AnimationInfo(const std::string& otherName, int otherStartIndex, int otherEndIndex)
  35. :name(otherName)
  36. ,startIndex(otherStartIndex)
  37. ,endIndex(otherEndIndex)
  38. {
  39. }
  40. std::string name;
  41. int startIndex;
  42. int endIndex;
  43. //need set call back before clip added to ActionTimeline
  44. // or see @ActionTimeline::setAnimationEndCallBack
  45. std::function<void()> clipEndCallBack;
  46. } AnimationClip;
  47. class CC_STUDIO_DLL ActionTimelineData : public cocos2d::Ref
  48. {
  49. public:
  50. static ActionTimelineData* create(int actionTag);
  51. virtual void setActionTag(int actionTag) { _actionTag = actionTag; }
  52. virtual int getActionTag() const { return _actionTag; }
  53. CC_CONSTRUCTOR_ACCESS:
  54. ActionTimelineData();
  55. virtual bool init(int actionTag);
  56. protected:
  57. int _actionTag;
  58. };
  59. class CC_STUDIO_DLL ActionTimeline : public cocos2d::Action, public cocos2d::PlayableProtocol
  60. {
  61. public:
  62. friend class Frame;
  63. static ActionTimeline* create();
  64. ActionTimeline();
  65. virtual ~ActionTimeline();
  66. virtual void play(std::string animationName, bool loop);
  67. virtual bool init();
  68. /** Goto the specified frame index, and start playing from this index.
  69. * @param startIndex The animation will play from this index.
  70. */
  71. virtual void gotoFrameAndPlay(int startIndex);
  72. /** Goto the specified frame index, and start playing from this index.
  73. * @param startIndex The animation will play from this index.
  74. * @param loop Whether or not the animation need loop.
  75. */
  76. virtual void gotoFrameAndPlay(int startIndex, bool loop);
  77. /** Goto the specified frame index, and start playing from start index, end at end index.
  78. * @param startIndex The animation will play from this index.
  79. * @param endIndex The animation will end at this index.
  80. * @param loop Whether or not the animation need loop.
  81. */
  82. virtual void gotoFrameAndPlay(int startIndex, int endIndex, bool loop);
  83. /** Goto the specified frame index, and start playing from start index, end at end index.
  84. * @param startIndex The animation will play from this index.
  85. * @param endIndex The animation will end at this index.
  86. * @param currentFrameIndex set current frame index.
  87. * @param loop Whether or not the animation need loop.
  88. */
  89. virtual void gotoFrameAndPlay(int startIndex, int endIndex, int currentFrameIndex, bool loop);
  90. /** Goto the specified frame index, and pause at this index.
  91. * @param startIndex The animation will pause at this index.
  92. */
  93. virtual void gotoFrameAndPause(int startIndex);
  94. /** Pause the animation. */
  95. virtual void pause();
  96. /** Resume the animation. */
  97. virtual void resume();
  98. /** Whether or not Action is playing. */
  99. virtual bool isPlaying() const;
  100. /** Set the animation speed, this will speed up or slow down the speed. */
  101. virtual void setTimeSpeed(float speed) { _timeSpeed = speed; }
  102. /** Get current animation speed. */
  103. virtual float getTimeSpeed() const { return _timeSpeed; }
  104. /** duration of the whole action*/
  105. virtual void setDuration(int duration) { _duration = duration; }
  106. virtual int getDuration() const { return _duration; }
  107. /** Start frame index of this action*/
  108. virtual int getStartFrame() const { return _startFrame; }
  109. /** End frame of this action.
  110. * When action play to this frame, if action is not loop, then it will stop,
  111. * or it will play from start frame again. */
  112. virtual int getEndFrame() const { return _endFrame; }
  113. /** Set current frame index, this will cause action plays to this frame. */
  114. virtual void setCurrentFrame(int frameIndex);
  115. /** Get current frame. */
  116. virtual int getCurrentFrame() const { return _currentFrame; }
  117. /** add Timeline to ActionTimeline */
  118. virtual void addTimeline(Timeline* timeline);
  119. virtual void removeTimeline(Timeline* timeline);
  120. virtual const cocos2d::Vector<Timeline*>& getTimelines() const { return _timelineList; }
  121. /** AnimationInfo*/
  122. virtual void addAnimationInfo(const AnimationInfo& animationInfo);
  123. virtual void removeAnimationInfo(std::string animationName);
  124. virtual bool IsAnimationInfoExists(const std::string& animationName);
  125. virtual const AnimationInfo& getAnimationInfo(const std::string& animationName);
  126. /**add a frame end call back to animation's end frame
  127. * @param animationName @addFrameEndCallFunc, make the animationName as funcKey
  128. * @param func the callback function
  129. */
  130. virtual void setAnimationEndCallFunc(const std::string animationName, std::function<void()> func);
  131. /** Set ActionTimeline's frame event callback function */
  132. void setFrameEventCallFunc(std::function<void(Frame *)> listener);
  133. void clearFrameEventCallFunc();
  134. /** Last frame callback will call when arriving last frame */
  135. void setLastFrameCallFunc(std::function<void()> listener);
  136. void clearLastFrameCallFunc();
  137. /** add a callback function after played frameIndex
  138. * @param frameIndex the frame index call back after
  139. * @param funcKey for identity the callback function
  140. * @param func the callback function
  141. */
  142. virtual void addFrameEndCallFunc(int frameIndex, const std::string& funcKey, std::function<void()> func);
  143. // remove callback function after frameIndex which identified with funcKey
  144. virtual void removeFrameEndCallFunc(int frameIndex, const std::string& funcKey);
  145. // clear callback functions after frameIndex
  146. virtual void removeFrameEndCallFuncs(int frameIndex);
  147. // clear all the callback functions after frameIndexs in this actiontimeline
  148. virtual void clearFrameEndCallFuncs();
  149. /** Inherit from Action. */
  150. /** Returns a clone of ActionTimeline */
  151. virtual ActionTimeline* clone() const override;
  152. /** Returns a reverse of ActionTimeline.
  153. * Not implement yet.
  154. */
  155. virtual ActionTimeline* reverse() const override { return nullptr; }
  156. virtual void step(float delta) override;
  157. virtual void startWithTarget(cocos2d::Node *target) override;
  158. virtual bool isDone() const override { return false; }
  159. /// @{
  160. /// @name implement Playable Protocol
  161. virtual void start() override;
  162. virtual void stop() override;
  163. /// @} end of PlayableProtocol
  164. protected:
  165. virtual void gotoFrame(int frameIndex);
  166. virtual void stepToFrame(int frameIndex);
  167. // emit call back after frameIndex played
  168. virtual void emitFrameEndCallFuncs(int frameIndex);
  169. /** emit frame event, call it when enter a frame*/
  170. virtual void emitFrameEvent(Frame* frame);
  171. std::map<int, cocos2d::Vector<Timeline*>> _timelineMap;
  172. cocos2d::Vector<Timeline*> _timelineList;
  173. int _duration;
  174. double _time;
  175. float _timeSpeed;
  176. float _frameInternal;
  177. bool _playing;
  178. int _currentFrame;
  179. int _startFrame;
  180. int _endFrame;
  181. bool _loop;
  182. std::function<void(Frame*)> _frameEventListener;
  183. std::function<void()> _lastFrameListener;
  184. std::map<int, std::map<std::string, std::function<void()> > > _frameEndCallFuncs;
  185. std::map<std::string, AnimationInfo> _animationInfos;
  186. };
  187. NS_TIMELINE_END
  188. #endif /*__CCTIMELINE_ACTION_H__*/