CCAnimation.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CC_ANIMATION_H__
  24. #define __CC_ANIMATION_H__
  25. #include "platform/CCPlatformConfig.h"
  26. #include "base/CCRef.h"
  27. #include "base/CCValue.h"
  28. #include "base/CCVector.h"
  29. #include "2d/CCSpriteFrame.h"
  30. #include <string>
  31. NS_CC_BEGIN
  32. class Texture2D;
  33. class SpriteFrame;
  34. /**
  35. * @addtogroup _2d
  36. * @{
  37. */
  38. /** @class AnimationFrame
  39. *
  40. * A frame of the animation. It contains information like:
  41. * - sprite frame name.
  42. * - # of delay units.
  43. * - offset
  44. @since v2.0
  45. */
  46. class CC_DLL AnimationFrame : public Ref, public Clonable
  47. {
  48. public:
  49. /** @struct DisplayedEventInfo
  50. * When the animation display,Dispatches the event of UserData.
  51. */
  52. struct DisplayedEventInfo
  53. {
  54. Node* target;
  55. const ValueMap* userInfo;
  56. };
  57. /**
  58. * Creates the animation frame with a spriteframe, number of delay units and a notification user info.
  59. *
  60. * @param spriteFrame The animation frame with a spriteframe.
  61. * @param delayUnits Number of delay units.
  62. * @param userInfo A notification user info.
  63. * @since 3.0
  64. */
  65. static AnimationFrame* create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo);
  66. /** Return a SpriteFrameName to be used.
  67. *
  68. * @return a SpriteFrameName to be used.
  69. */
  70. SpriteFrame* getSpriteFrame() const { return _spriteFrame; };
  71. /** Set the SpriteFrame.
  72. *
  73. * @param frame A SpriteFrame will be used.
  74. */
  75. void setSpriteFrame(SpriteFrame* frame)
  76. {
  77. CC_SAFE_RETAIN(frame);
  78. CC_SAFE_RELEASE(_spriteFrame);
  79. _spriteFrame = frame;
  80. }
  81. /** Gets the units of time the frame takes.
  82. *
  83. * @return The units of time the frame takes.
  84. */
  85. float getDelayUnits() const { return _delayUnits; };
  86. /** Sets the units of time the frame takes.
  87. *
  88. * @param delayUnits The units of time the frame takes.
  89. */
  90. void setDelayUnits(float delayUnits) { _delayUnits = delayUnits; };
  91. /** @brief Gets user information
  92. * A AnimationFrameDisplayedNotification notification will be broadcast when the frame is displayed with this dictionary as UserInfo.
  93. * If UserInfo is nil, then no notification will be broadcast.
  94. *
  95. * @return A dictionary as UserInfo
  96. */
  97. const ValueMap& getUserInfo() const { return _userInfo; };
  98. ValueMap& getUserInfo() { return _userInfo; };
  99. /** Sets user information.
  100. * @param userInfo A dictionary as UserInfo.
  101. */
  102. void setUserInfo(const ValueMap& userInfo)
  103. {
  104. _userInfo = userInfo;
  105. }
  106. // Overrides
  107. virtual AnimationFrame *clone() const override;
  108. CC_CONSTRUCTOR_ACCESS:
  109. /**
  110. * @js ctor
  111. */
  112. AnimationFrame();
  113. /**
  114. * @js NA
  115. * @lua NA
  116. */
  117. virtual ~AnimationFrame();
  118. /** initializes the animation frame with a spriteframe, number of delay units and a notification user info */
  119. bool initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo);
  120. protected:
  121. /** SpriteFrameName to be used */
  122. SpriteFrame* _spriteFrame;
  123. /** how many units of time the frame takes */
  124. float _delayUnits;
  125. /** 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. */
  126. ValueMap _userInfo;
  127. private:
  128. CC_DISALLOW_COPY_AND_ASSIGN(AnimationFrame);
  129. };
  130. /** @class Animation
  131. * A Animation object is used to perform animations on the Sprite objects.
  132. * The Animation object contains AnimationFrame objects, and a possible delay between the frames.
  133. * You can animate a Animation object by using the Animate action. Example:
  134. * @code
  135. * sprite->runAction(Animate::create(animation));
  136. * @endcode
  137. */
  138. class CC_DLL Animation : public Ref, public Clonable
  139. {
  140. public:
  141. /** Creates an animation.
  142. * @since v0.99.5
  143. */
  144. static Animation* create(void);
  145. /* Creates an animation with an array of SpriteFrame and a delay between frames in seconds.
  146. * The frames will be added with one "delay unit".
  147. * @since v0.99.5
  148. * @param arrayOfSpriteFrameNames An array of SpriteFrame.
  149. * @param delay A delay between frames in seconds.
  150. * @param loops The times the animation is going to loop.
  151. */
  152. static Animation* createWithSpriteFrames(const Vector<SpriteFrame*>& arrayOfSpriteFrameNames, float delay = 0.0f, unsigned int loops = 1);
  153. /* Creates an animation with an array of AnimationFrame, the delay per units in seconds and how many times it should be executed.
  154. * @since v2.0
  155. * @param arrayOfAnimationFrameNames An animation with an array of AnimationFrame.
  156. * @param delayPerUnit The delay per units in seconds and how many times it should be executed.
  157. * @param loops The times the animation is going to loop.
  158. */
  159. static Animation* create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops = 1);
  160. /** Adds a SpriteFrame to a Animation.
  161. *
  162. * @param frame The frame will be added with one "delay unit".
  163. */
  164. void addSpriteFrame(SpriteFrame *frame);
  165. /** Adds a frame with an image filename. Internally it will create a SpriteFrame and it will add it.
  166. * The frame will be added with one "delay unit".
  167. * Added to facilitate the migration from v0.8 to v0.9.
  168. * @param filename The path of SpriteFrame.
  169. */
  170. void addSpriteFrameWithFile(const std::string& filename);
  171. /**
  172. * @deprecated. Use addSpriteFrameWithFile() instead.
  173. @js NA
  174. */
  175. CC_DEPRECATED_ATTRIBUTE void addSpriteFrameWithFileName(const std::string& filename){ addSpriteFrameWithFile(filename);}
  176. /** Adds a frame with a texture and a rect. Internally it will create a SpriteFrame and it will add it.
  177. * The frame will be added with one "delay unit".
  178. * Added to facilitate the migration from v0.8 to v0.9.
  179. * @param pobTexture A frame with a texture.
  180. * @param rect The Texture of rect.
  181. */
  182. void addSpriteFrameWithTexture(Texture2D* pobTexture, const Rect& rect);
  183. /** Gets the total Delay units of the Animation.
  184. *
  185. * @return The total Delay units of the Animation.
  186. */
  187. float getTotalDelayUnits() const { return _totalDelayUnits; };
  188. /** Sets the delay in seconds of the "delay unit".
  189. *
  190. * @param delayPerUnit The delay in seconds of the "delay unit".
  191. */
  192. void setDelayPerUnit(float delayPerUnit) { _delayPerUnit = delayPerUnit; };
  193. /** Gets the delay in seconds of the "delay unit".
  194. *
  195. * @return The delay in seconds of the "delay unit".
  196. */
  197. float getDelayPerUnit() const { return _delayPerUnit; };
  198. /** Gets the duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit.
  199. *
  200. * @return Result of totalDelayUnits * delayPerUnit.
  201. */
  202. float getDuration() const;
  203. /** Gets the array of AnimationFrames.
  204. *
  205. * @return The array of AnimationFrames.
  206. */
  207. const Vector<AnimationFrame*>& getFrames() const { return _frames; };
  208. /** Sets the array of AnimationFrames.
  209. *
  210. * @param frames The array of AnimationFrames.
  211. */
  212. void setFrames(const Vector<AnimationFrame*>& frames)
  213. {
  214. _frames = frames;
  215. }
  216. /** Checks whether to restore the original frame when animation finishes.
  217. *
  218. * @return Restore the original frame when animation finishes.
  219. */
  220. bool getRestoreOriginalFrame() const { return _restoreOriginalFrame; };
  221. /** Sets whether to restore the original frame when animation finishes.
  222. *
  223. * @param restoreOriginalFrame Whether to restore the original frame when animation finishes.
  224. */
  225. void setRestoreOriginalFrame(bool restoreOriginalFrame) { _restoreOriginalFrame = restoreOriginalFrame; };
  226. /** Gets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ...
  227. *
  228. * @return The times the animation is going to loop.
  229. */
  230. unsigned int getLoops() const { return _loops; };
  231. /** Sets the times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ...
  232. *
  233. * @param loops The times the animation is going to loop.
  234. */
  235. void setLoops(unsigned int loops) { _loops = loops; };
  236. // overrides
  237. virtual Animation *clone() const override;
  238. CC_CONSTRUCTOR_ACCESS:
  239. Animation();
  240. virtual ~Animation(void);
  241. /** Initializes a Animation. */
  242. bool init();
  243. /** Initializes a Animation with frames and a delay between frames.
  244. * @since v0.99.5
  245. */
  246. bool initWithSpriteFrames(const Vector<SpriteFrame*>& arrayOfSpriteFrameNames, float delay = 0.0f, unsigned int loops = 1);
  247. /** Initializes a Animation with AnimationFrame.
  248. * @since v2.0
  249. */
  250. bool initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops);
  251. protected:
  252. /** total Delay units of the Animation. */
  253. float _totalDelayUnits;
  254. /** Delay in seconds of the "delay unit". */
  255. float _delayPerUnit;
  256. /** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit. */
  257. float _duration;
  258. /** array of AnimationFrames. */
  259. Vector<AnimationFrame*> _frames;
  260. /** whether or not it shall restore the original frame when the animation finishes. */
  261. bool _restoreOriginalFrame;
  262. /** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */
  263. unsigned int _loops;
  264. private:
  265. CC_DISALLOW_COPY_AND_ASSIGN(Animation);
  266. };
  267. // end of sprite_nodes group
  268. /// @}
  269. NS_CC_END
  270. #endif // __CC_ANIMATION_H__