CCAnimation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 __CC_ANIMATION_H__
  25. #define __CC_ANIMATION_H__
  26. #include "platform/CCPlatformConfig.h"
  27. #include "base/CCRef.h"
  28. #include "base/CCValue.h"
  29. #include "base/CCVector.h"
  30. #include "2d/CCSpriteFrame.h"
  31. #include <string>
  32. NS_CC_BEGIN
  33. class Texture2D;
  34. class SpriteFrame;
  35. /**
  36. * @addtogroup _2d
  37. * @{
  38. */
  39. /** @class AnimationFrame
  40. *
  41. * A frame of the animation. It contains information like:
  42. * - sprite frame name.
  43. * - # of delay units.
  44. * - offset
  45. @since v2.0
  46. */
  47. class CC_DLL AnimationFrame : public Ref, public Clonable
  48. {
  49. public:
  50. /** @struct DisplayedEventInfo
  51. * When the animation display,Dispatches the event of UserData.
  52. */
  53. struct DisplayedEventInfo
  54. {
  55. Node* target;
  56. const ValueMap* userInfo;
  57. };
  58. /**
  59. * Creates the animation frame with a spriteframe, number of delay units and a notification user info.
  60. *
  61. * @param spriteFrame The animation frame with a spriteframe.
  62. * @param delayUnits Number of delay units.
  63. * @param userInfo A notification user info.
  64. * @since 3.0
  65. */
  66. static AnimationFrame* create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo);
  67. /** Return a SpriteFrameName to be used.
  68. *
  69. * @return a SpriteFrameName to be used.
  70. */
  71. SpriteFrame* getSpriteFrame() const { return _spriteFrame; };
  72. /** Set the SpriteFrame.
  73. *
  74. * @param frame A SpriteFrame will be used.
  75. */
  76. void setSpriteFrame(SpriteFrame* frame)
  77. {
  78. CC_SAFE_RETAIN(frame);
  79. CC_SAFE_RELEASE(_spriteFrame);
  80. _spriteFrame = frame;
  81. }
  82. /** Gets the units of time the frame takes.
  83. *
  84. * @return The units of time the frame takes.
  85. */
  86. float getDelayUnits() const { return _delayUnits; };
  87. /** Sets the units of time the frame takes.
  88. *
  89. * @param delayUnits The units of time the frame takes.
  90. */
  91. void setDelayUnits(float delayUnits) { _delayUnits = delayUnits; };
  92. /** @brief Gets user information
  93. * A AnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this dictionary as UserInfo.
  94. * If UserInfo is nil, then no notification will be broadcast.
  95. *
  96. * @return A dictionary as UserInfo
  97. */
  98. const ValueMap& getUserInfo() const { return _userInfo; };
  99. ValueMap& getUserInfo() { return _userInfo; };
  100. /** Sets user information.
  101. * @param userInfo A dictionary as UserInfo.
  102. */
  103. void setUserInfo(const ValueMap& userInfo)
  104. {
  105. _userInfo = userInfo;
  106. }
  107. // Overrides
  108. virtual AnimationFrame *clone() const override;
  109. CC_CONSTRUCTOR_ACCESS:
  110. /**
  111. * @js ctor
  112. */
  113. AnimationFrame();
  114. /**
  115. * @js NA
  116. * @lua NA
  117. */
  118. virtual ~AnimationFrame();
  119. /** initializes the animation frame with a spriteframe, number of delay units and a notification user info */
  120. bool initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo);
  121. protected:
  122. /** SpriteFrameName to be used */
  123. SpriteFrame* _spriteFrame;
  124. /** how many units of time the frame takes */
  125. float _delayUnits;
  126. /** A AnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this dictionary as UserInfo. If UserInfo is nil, then no notification will be broadcast. */
  127. ValueMap _userInfo;
  128. private:
  129. CC_DISALLOW_COPY_AND_ASSIGN(AnimationFrame);
  130. };
  131. /** @class Animation
  132. * A Animation object is used to perform animations on the Sprite objects.
  133. * The Animation object contains AnimationFrame objects, and a possible delay between the frames.
  134. * You can animate a Animation object by using the Animate action. Example:
  135. * @code
  136. * sprite->runAction(Animate::create(animation));
  137. * @endcode
  138. */
  139. class CC_DLL Animation : public Ref, public Clonable
  140. {
  141. public:
  142. /** Creates an animation.
  143. * @since v0.99.5
  144. */
  145. static Animation* create(void);
  146. /* Creates an animation with an array of SpriteFrame and a delay between frames in seconds.
  147. * The frames will be added with one "delay unit".
  148. * @since v0.99.5
  149. * @param arrayOfSpriteFrameNames An array of SpriteFrame.
  150. * @param delay A delay between frames in seconds.
  151. * @param loops The times the animation is going to loop.
  152. */
  153. static Animation* createWithSpriteFrames(const Vector<SpriteFrame*>& arrayOfSpriteFrameNames, float delay = 0.0f, unsigned int loops = 1);
  154. /* Creates an animation with an array of AnimationFrame, the delay per units in seconds and how many times it should be executed.
  155. * @since v2.0
  156. * @param arrayOfAnimationFrameNames An animation with an array of AnimationFrame.
  157. * @param delayPerUnit The delay per units in seconds and how many times it should be executed.
  158. * @param loops The times the animation is going to loop.
  159. */
  160. static Animation* create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops = 1);
  161. /** Adds a SpriteFrame to a Animation.
  162. *
  163. * @param frame The frame will be added with one "delay unit".
  164. */
  165. void addSpriteFrame(SpriteFrame *frame);
  166. /** Adds a frame with an image filename. Internally it will create a SpriteFrame and it will add it.
  167. * The frame will be added with one "delay unit".
  168. * Added to facilitate the migration from v0.8 to v0.9.
  169. * @param filename The path of SpriteFrame.
  170. */
  171. void addSpriteFrameWithFile(const std::string& filename);
  172. /**
  173. * @deprecated. Use addSpriteFrameWithFile() instead.
  174. @js NA
  175. */
  176. CC_DEPRECATED_ATTRIBUTE void addSpriteFrameWithFileName(const std::string& filename){ addSpriteFrameWithFile(filename);}
  177. /** Adds a frame with a texture and a rect. Internally it will create a SpriteFrame and it will add it.
  178. * The frame will be added with one "delay unit".
  179. * Added to facilitate the migration from v0.8 to v0.9.
  180. * @param pobTexture A frame with a texture.
  181. * @param rect The Texture of rect.
  182. */
  183. void addSpriteFrameWithTexture(Texture2D* pobTexture, const Rect& rect);
  184. /** Gets the total Delay units of the Animation.
  185. *
  186. * @return The total Delay units of the Animation.
  187. */
  188. float getTotalDelayUnits() const { return _totalDelayUnits; };
  189. /** Sets the delay in seconds of the "delay unit".
  190. *
  191. * @param delayPerUnit The delay in seconds of the "delay unit".
  192. */
  193. void setDelayPerUnit(float delayPerUnit) { _delayPerUnit = delayPerUnit; };
  194. /** Gets the delay in seconds of the "delay unit".
  195. *
  196. * @return The delay in seconds of the "delay unit".
  197. */
  198. float getDelayPerUnit() const { return _delayPerUnit; };
  199. /** Gets the duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit.
  200. *
  201. * @return Result of totalDelayUnits * delayPerUnit.
  202. */
  203. float getDuration() const;
  204. /** Gets the array of AnimationFrames.
  205. *
  206. * @return The array of AnimationFrames.
  207. */
  208. const Vector<AnimationFrame*>& getFrames() const { return _frames; };
  209. /** Sets the array of AnimationFrames.
  210. *
  211. * @param frames The array of AnimationFrames.
  212. */
  213. void setFrames(const Vector<AnimationFrame*>& frames)
  214. {
  215. _frames = frames;
  216. }
  217. /** Checks whether to restore the original frame when animation finishes.
  218. *
  219. * @return Restore the original frame when animation finishes.
  220. */
  221. bool getRestoreOriginalFrame() const { return _restoreOriginalFrame; };
  222. /** Sets whether to restore the original frame when animation finishes.
  223. *
  224. * @param restoreOriginalFrame Whether to restore the original frame when animation finishes.
  225. */
  226. void setRestoreOriginalFrame(bool restoreOriginalFrame) { _restoreOriginalFrame = restoreOriginalFrame; };
  227. /** Gets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ...
  228. *
  229. * @return The times the animation is going to loop.
  230. */
  231. unsigned int getLoops() const { return _loops; };
  232. /** Sets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ...
  233. *
  234. * @param loops The times the animation is going to loop.
  235. */
  236. void setLoops(unsigned int loops) { _loops = loops; };
  237. // overrides
  238. virtual Animation *clone() const override;
  239. CC_CONSTRUCTOR_ACCESS:
  240. Animation();
  241. virtual ~Animation(void);
  242. /** Initializes a Animation. */
  243. bool init();
  244. /** Initializes a Animation with frames and a delay between frames.
  245. * @since v0.99.5
  246. */
  247. bool initWithSpriteFrames(const Vector<SpriteFrame*>& arrayOfSpriteFrameNames, float delay = 0.0f, unsigned int loops = 1);
  248. /** Initializes a Animation with AnimationFrame.
  249. * @since v2.0
  250. */
  251. bool initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops);
  252. protected:
  253. /** total Delay units of the Animation. */
  254. float _totalDelayUnits;
  255. /** Delay in seconds of the "delay unit". */
  256. float _delayPerUnit;
  257. /** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit. */
  258. float _duration;
  259. /** array of AnimationFrames. */
  260. Vector<AnimationFrame*> _frames;
  261. /** whether or not it shall restore the original frame when the animation finishes. */
  262. bool _restoreOriginalFrame;
  263. /** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */
  264. unsigned int _loops;
  265. private:
  266. CC_DISALLOW_COPY_AND_ASSIGN(Animation);
  267. };
  268. // end of sprite_nodes group
  269. /// @}
  270. NS_CC_END
  271. #endif // __CC_ANIMATION_H__