AnimationClip.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #ifndef ANIMATIONCLIP_H_
  2. #define ANIMATIONCLIP_H_
  3. #include "Base.h"
  4. #include "AnimationValue.h"
  5. #include "Curve.h"
  6. #include "Animation.h"
  7. namespace gameplay
  8. {
  9. class Animation;
  10. class AnimationValue;
  11. class ScriptListener;
  12. /**
  13. * Defines the runtime session of an Animation to be played.
  14. */
  15. class AnimationClip : public Ref
  16. {
  17. friend class AnimationController;
  18. friend class Animation;
  19. public:
  20. /**
  21. * Defines a constant for indefinitely repeating an AnimationClip.
  22. */
  23. static const unsigned int REPEAT_INDEFINITE = 0;
  24. /**
  25. * Defines an animation event listener.
  26. */
  27. class Listener
  28. {
  29. friend class AnimationClip;
  30. public:
  31. /*
  32. * Constructor.
  33. */
  34. Listener()
  35. {
  36. }
  37. /**
  38. * The type of animation event.
  39. */
  40. enum EventType
  41. {
  42. /**
  43. * Event fired when the clip begins.
  44. */
  45. BEGIN,
  46. /**
  47. * Event fired when the clip ends.
  48. */
  49. END,
  50. /**
  51. * Event fired at a specified time during a clip update.
  52. */
  53. TIME
  54. };
  55. /*
  56. * Destructor.
  57. */
  58. virtual ~Listener() { }
  59. /**
  60. * Handles when animation event occurs.
  61. */
  62. virtual void animationEvent(AnimationClip* clip, EventType type) = 0;
  63. };
  64. /**
  65. * Gets the AnimationClip's ID.
  66. *
  67. * @return The AnimationClip's ID.
  68. */
  69. const char* getId() const;
  70. /**
  71. * Gets the Animation that this AnimationClip was created from.
  72. *
  73. * @return The Animation that this clip was created from.
  74. */
  75. Animation* getAnimation() const;
  76. /**
  77. * Gets the AnimationClip's start time.
  78. *
  79. * @return The time (in milliseconds) that the AnimationClip starts playing from.
  80. */
  81. unsigned long getStartTime() const;
  82. /**
  83. * Gets the AnimationClip's end time.
  84. *
  85. * @return The time (in milliseconds) that the AnimationClip will end.
  86. */
  87. unsigned long getEndTime() const;
  88. /**
  89. * Gets the AnimationClip's elapsed time.
  90. *
  91. * @return The elapsed time of the AnimationClip (in milliseconds).
  92. */
  93. float getElaspedTime() const;
  94. /**
  95. * Sets the AnimationClip's repeat count. Overrides repeat duration.
  96. *
  97. * Use REPEAT_INDEFINITE to play the AnimationClip indefinitely.
  98. *
  99. * @param repeatCount The repeat count to set on the AnimationClip.
  100. */
  101. void setRepeatCount(float repeatCount);
  102. /**
  103. * Gets the AnimationClip's repeat count.
  104. *
  105. * @return The repeat count that is set on the AnimationClip.
  106. */
  107. float getRepeatCount() const;
  108. /**
  109. * Sets the AnimationClip's active duration. Overrides repeat count.
  110. *
  111. * Use REPEAT_INDEFINITE to play the AnimationClip indefinitely.
  112. *
  113. * @param duration The active duration that is set on the AnimationClip, in milliseconds.
  114. */
  115. void setActiveDuration(unsigned long duration);
  116. /**
  117. * Gets the AnimationClip's active duration.
  118. *
  119. * @return the AnimationClip's active duration.
  120. */
  121. unsigned long getActiveDuration() const;
  122. /**
  123. * Gets the AnimationClip's duration.
  124. *
  125. * @return the AnimationClip's duration, in milliseconds.
  126. */
  127. unsigned long getDuration() const;
  128. /**
  129. * Set the AnimationClip's running speed.
  130. *
  131. * @param speed The clips running speed.
  132. */
  133. void setSpeed(float speed);
  134. /**
  135. * Gets the AninimationClip's running speed.
  136. *
  137. * @return The AninimationClip's running speed.
  138. */
  139. float getSpeed() const;
  140. /**
  141. * Sets the blend weight of the AnimationClip.
  142. *
  143. * @param blendWeight The blend weight to apply to the clip.
  144. */
  145. void setBlendWeight(float blendWeight);
  146. /**
  147. * Gets the blend weight of the AnimationClip.
  148. *
  149. * @return The blendweight of the AnimationClip.
  150. */
  151. float getBlendWeight() const;
  152. /**
  153. * Checks if the AnimationClip is playing.
  154. *
  155. * @return true if the AnimationClip is playing; false if the AnimationClip is not playing.
  156. */
  157. bool isPlaying() const;
  158. /**
  159. * Plays the AnimationClip.
  160. */
  161. void play();
  162. /**
  163. * Stops the AnimationClip.
  164. */
  165. void stop();
  166. /**
  167. * Pauses the AnimationClip.
  168. */
  169. void pause();
  170. /**
  171. * Fades this clip out, and the specified clip in over the given duration.
  172. *
  173. * @param clip The clip to fade into.
  174. * @param duration The duration of the fade.
  175. */
  176. void crossFade(AnimationClip* clip, unsigned long duration);
  177. /**
  178. * Adds an animation begin listener.
  179. *
  180. * @param listener The listener to be called when an AnimationClip begins.
  181. */
  182. void addBeginListener(AnimationClip::Listener* listener);
  183. /**
  184. * Adds an animation end listener.
  185. *
  186. * @param listener The listener to be called when an AnimationClip ends.
  187. */
  188. void addEndListener(AnimationClip::Listener* listener);
  189. /**
  190. * Adds an animation listener to be called back at the specified eventTime during the playback
  191. * of the AnimationClip.
  192. *
  193. * @param listener The listener to be called when the AnimationClip reaches the
  194. * specified time in its playback.
  195. * @param eventTime The time the listener will be called during the playback of the AnimationClip.
  196. * Must be between 0 and the duration of the AnimationClip.
  197. */
  198. void addListener(AnimationClip::Listener* listener, unsigned long eventTime);
  199. /**
  200. * Adds an animation begin listener.
  201. *
  202. * Note: the given Lua function must have the same function signature as AnimationClip::Listener::animationEvent.
  203. *
  204. * @param function The Lua script function to be called when an AnimationClip begins.
  205. */
  206. void addBeginListener(const char* function);
  207. /**
  208. * Adds an animation end listener.
  209. *
  210. * Note: the given Lua function must have the same function signature as AnimationClip::Listener::animationEvent.
  211. *
  212. * @param function The Lua script function to be called when an AnimationClip ends.
  213. */
  214. void addEndListener(const char* function);
  215. /**
  216. * Adds an animation listener to be called back at the specified eventTime during the playback
  217. * of the AnimationClip.
  218. *
  219. * Note: the given Lua function must have the same function signature as AnimationClip::Listener::animationEvent.
  220. *
  221. * @param function The Lua script function to be called when an AnimationClip reaches the
  222. * specified time in its playback.
  223. * @param eventTime The time the listener will be called during the playback of the AnimationClip.
  224. * Must be between 0 and the duration of the AnimationClip.
  225. */
  226. void addListener(const char* function, unsigned long eventTime);
  227. private:
  228. static const unsigned char CLIP_IS_PLAYING_BIT = 0x01; // Bit representing whether AnimationClip is a running clip in AnimationController
  229. static const unsigned char CLIP_IS_STARTED_BIT = 0x02; // Bit representing whether the AnimationClip has actually been started (ie: received first call to update())
  230. static const unsigned char CLIP_IS_FADING_OUT_STARTED_BIT = 0x04; // Bit representing that a cross fade has started.
  231. static const unsigned char CLIP_IS_FADING_OUT_BIT = 0x08; // Bit representing whether the clip is fading out.
  232. static const unsigned char CLIP_IS_FADING_IN_BIT = 0x10; // Bit representing whether the clip is fading out.
  233. static const unsigned char CLIP_IS_MARKED_FOR_REMOVAL_BIT = 0x20; // Bit representing whether the clip has ended and should be removed from the AnimationController.
  234. static const unsigned char CLIP_IS_RESTARTED_BIT = 0x40; // Bit representing if the clip should be restarted by the AnimationController.
  235. static const unsigned char CLIP_IS_PAUSED_BIT = 0x80; // Bit representing if the clip is currently paused.
  236. static const unsigned char CLIP_ALL_BITS = 0xFF; // Bit mask for all the state bits.
  237. /**
  238. * ListenerEvent.
  239. *
  240. * Internal structure used for storing the event time at which an AnimationClip::Listener should be called back.
  241. */
  242. struct ListenerEvent
  243. {
  244. /**
  245. * Constructor.
  246. */
  247. ListenerEvent(Listener* listener, unsigned long eventTime);
  248. /**
  249. * Destructor.
  250. */
  251. ~ListenerEvent();
  252. /**
  253. * Hidden copy assignment operator.
  254. */
  255. ListenerEvent& operator=(const ListenerEvent&);
  256. Listener* _listener; // This listener to call back when this event is triggered.
  257. unsigned long _eventTime; // The time at which the listener will be called back at during the playback of the AnimationClip.
  258. };
  259. /**
  260. * Listener implementation for script callbacks.
  261. */
  262. struct ScriptListener : public AnimationClip::Listener
  263. {
  264. /**
  265. * Constructor.
  266. */
  267. ScriptListener(const std::string& function);
  268. /**
  269. * @see AnimationClip::Listener::animationEvent
  270. */
  271. void animationEvent(AnimationClip* clip, EventType type);
  272. /** The function to call back when an animation event occurs. */
  273. std::string function;
  274. };
  275. /**
  276. * Constructor.
  277. */
  278. AnimationClip(const char* id, Animation* animation, unsigned long startTime, unsigned long endTime);
  279. /**
  280. * Constructor.
  281. */
  282. AnimationClip();
  283. /**
  284. * Constructor.
  285. */
  286. AnimationClip(const AnimationClip& copy);
  287. /**
  288. * Destructor.
  289. */
  290. ~AnimationClip();
  291. /**
  292. * Hidden copy assignment operator.
  293. */
  294. AnimationClip& operator=(const AnimationClip&);
  295. /**
  296. * Updates the animation with the elapsed time.
  297. */
  298. bool update(float elapsedTime);
  299. /**
  300. * Handles when the AnimationClip begins.
  301. */
  302. void onBegin();
  303. /**
  304. * Handles when the AnimationClip ends.
  305. */
  306. void onEnd();
  307. /**
  308. * Determines whether the given bit is set in the AnimationClip's state.
  309. */
  310. bool isClipStateBitSet(unsigned char bit) const;
  311. /**
  312. * Sets the given bit in the AnimationClip's state.
  313. */
  314. void setClipStateBit(unsigned char bit);
  315. /**
  316. * Resets the given bit in the AnimationClip's state.
  317. */
  318. void resetClipStateBit(unsigned char bit);
  319. /**
  320. * Clones the animation clip.
  321. *
  322. * @param animation The animation that the new clip belongs to.
  323. *
  324. * @return The newly created animation clip.
  325. */
  326. AnimationClip* clone(Animation* animation) const;
  327. std::string _id; // AnimationClip ID.
  328. Animation* _animation; // The Animation this clip is created from.
  329. unsigned long _startTime; // Start time of the clip.
  330. unsigned long _endTime; // End time of the clip.
  331. unsigned long _duration; // The total duration.
  332. unsigned char _stateBits; // Bit flag used to keep track of the clip's current state.
  333. float _repeatCount; // The clip's repeat count.
  334. unsigned long _activeDuration; // The active duration of the clip.
  335. float _speed; // The speed that the clip is playing. Default is 1.0. Negative goes in reverse.
  336. double _timeStarted; // The game time when this clip was actually started.
  337. float _elapsedTime; // Time elapsed while the clip is running.
  338. AnimationClip* _crossFadeToClip; // The clip to cross fade to.
  339. float _crossFadeOutElapsed; // The amount of time that has elapsed for the crossfade.
  340. unsigned long _crossFadeOutDuration; // The duration of the cross fade.
  341. float _blendWeight; // The clip's blendweight.
  342. std::vector<AnimationValue*> _values; // AnimationValue holder.
  343. std::vector<Listener*>* _beginListeners; // Collection of begin listeners on the clip.
  344. std::vector<Listener*>* _endListeners; // Collection of end listeners on the clip.
  345. std::list<ListenerEvent*>* _listeners; // Ordered collection of listeners on the clip.
  346. std::list<ListenerEvent*>::iterator* _listenerItr; // Iterator that points to the next listener event to be triggered.
  347. std::vector<ScriptListener*>* _scriptListeners; // Collection of listeners that are bound to Lua script functions.
  348. };
  349. }
  350. #endif