Animation.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef ANIMATION_H_
  2. #define ANIMATION_H_
  3. #include "Ref.h"
  4. #include "Properties.h"
  5. #include "Curve.h"
  6. namespace gameplay
  7. {
  8. class AnimationTarget;
  9. class AnimationController;
  10. class AnimationClip;
  11. /**
  12. * Defines a generic property animation.
  13. *
  14. * To run an animation you must play an AnimationClip.
  15. * Every Animation has the default clip which will run from begin-end time.
  16. * You can create additional clips to run only parts of an animation and control
  17. * various runtime characteristics, such as repeat count, etc.
  18. */
  19. class Animation : public Ref
  20. {
  21. friend class AnimationClip;
  22. friend class AnimationTarget;
  23. friend class Bundle;
  24. public:
  25. /**
  26. * Gets the Animation's ID.
  27. *
  28. * @return The Animation's ID.
  29. */
  30. const char* getId() const;
  31. /**
  32. * Gets the Animation's duration.
  33. *
  34. * @return The Animation's duration (in milliseconds).
  35. */
  36. unsigned long getDuration() const;
  37. /**
  38. * Creates an AnimationClip from the Properties object defined at the specified URL,
  39. * where the URL is of the format "<file-path>.<extension>#<namespace-id>/<namespace-id>/.../<namespace-id>"
  40. * (and "#<namespace-id>/<namespace-id>/.../<namespace-id>" is optional).
  41. *
  42. * @param url The URL pointing to the Properties object containing the clip definitions.
  43. */
  44. void createClips(const char* url);
  45. /**
  46. * Creates an AnimationClip from the Animation.
  47. *
  48. * @param id The ID to the give the AnimationClip.
  49. * @param start The time (in milliseconds) that the AnimationClip will begin from.
  50. * @param end The time (in milliseconds) that the AnimationClip will end.
  51. *
  52. * @return The newly created AnimationClip; NULL if an AnimationClip already exists with the same ID.
  53. */
  54. AnimationClip* createClip(const char* id, unsigned long start, unsigned long end);
  55. /**
  56. * Finds the AnimationClip with the specified name. If NULL, gets the default clip.
  57. *
  58. * @param clipId The ID of the AnimationClip to get.
  59. *
  60. * @return The AnimationClip with the specified ID; NULL if an AnimationClip with the given ID is not found.
  61. */
  62. AnimationClip* getClip(const char* clipId = NULL);
  63. /**
  64. * Returns the AnimationClip at the given index.
  65. *
  66. * @param index Index of the clip to return.
  67. */
  68. AnimationClip* getClip(unsigned int index) const;
  69. /**
  70. * Returns the number of animation clips in this animation.
  71. */
  72. unsigned int getClipCount() const;
  73. /**
  74. * Plays the AnimationClip with the specified name.
  75. *
  76. * @param clipId The ID of the AnimationClip to play. If NULL, plays the default clip.
  77. */
  78. void play(const char* clipId = NULL);
  79. /**
  80. * Stops the AnimationClip with the specified name.
  81. *
  82. * @param clipId The ID of the AnimationClip to stop. If NULL, stops the default clip.
  83. */
  84. void stop(const char* clipId = NULL);
  85. /**
  86. * Pauses the AnimationClip with the specified name.
  87. *
  88. * @param clipId The ID of the AnimationClip to pause. If NULL, pauses the default clip.
  89. */
  90. void pause(const char* clipId = NULL);
  91. /**
  92. * Returns true if this animation targets the given AnimationTarget.
  93. */
  94. bool targets(AnimationTarget* target) const;
  95. private:
  96. /**
  97. * Defines a channel which holds the target, target property, curve values, and duration.
  98. *
  99. * An animation can have 1 or more channels. All typical simple property animations
  100. * will have 1 channel. Skeletal animation will have 1 channel per joint to be animated.
  101. */
  102. class Channel
  103. {
  104. friend class AnimationClip;
  105. friend class Animation;
  106. friend class AnimationTarget;
  107. private:
  108. Channel(Animation* animation, AnimationTarget* target, int propertyId, Curve* curve, unsigned long duration);
  109. Channel(const Channel& copy, Animation* animation, AnimationTarget* target);
  110. Channel(const Channel&); // Hidden copy constructor.
  111. ~Channel();
  112. Channel& operator=(const Channel&); // Hidden copy assignment operator.
  113. Curve* getCurve() const;
  114. Animation* _animation; // Reference to the animation this channel belongs to.
  115. AnimationTarget* _target; // The target of this channel.
  116. int _propertyId; // The target property this channel targets.
  117. Curve* _curve; // The curve used to represent the animation data.
  118. unsigned long _duration; // The length of the animation (in milliseconds).
  119. };
  120. /**
  121. * Hidden copy constructor.
  122. */
  123. Animation(const Animation& copy);
  124. /**
  125. * Constructor.
  126. */
  127. Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type);
  128. /**
  129. * Constructor.
  130. */
  131. Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type);
  132. /**
  133. * Constructor.
  134. */
  135. Animation(const char* id);
  136. /**
  137. * Destructor.
  138. */
  139. ~Animation();
  140. /**
  141. * Hidden copy assignment operator.
  142. */
  143. Animation& operator=(const Animation&);
  144. /**
  145. * Creates the default clip.
  146. */
  147. void createDefaultClip();
  148. /**
  149. * Creates AnimationClip's for this Animation from the specified Property object.
  150. */
  151. void createClips(Properties* animationProperties, unsigned int frameCount);
  152. /**
  153. * Adds a clip to this Animation.
  154. */
  155. void addClip(AnimationClip* clip);
  156. /**
  157. * Finds the clip with the given ID.
  158. */
  159. AnimationClip* findClip(const char* id) const;
  160. /**
  161. * Creates a channel within this animation.
  162. */
  163. Channel* createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type);
  164. /**
  165. * Creates a channel within this animation.
  166. */
  167. Channel* createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type);
  168. /**
  169. * Adds a channel to the animation.
  170. */
  171. void addChannel(Channel* channel);
  172. /**
  173. * Removes a channel from the animation.
  174. */
  175. void removeChannel(Channel* channel);
  176. /**
  177. * Sets the rotation offset in a Curve representing a Transform's animation data.
  178. */
  179. void setTransformRotationOffset(Curve* curve, unsigned int propertyId);
  180. /**
  181. * Clones this animation.
  182. *
  183. * @param channel The channel to clone and add to the animation.
  184. * @param target The target of the animation.
  185. *
  186. * @return The newly created animation.
  187. */
  188. Animation* clone(Channel* channel, AnimationTarget* target);
  189. AnimationController* _controller; // The AnimationController that this Animation will run on.
  190. std::string _id; // The Animation's ID.
  191. unsigned long _duration; // the length of the animation (in milliseconds).
  192. std::vector<Channel*> _channels; // The channels within this Animation.
  193. AnimationClip* _defaultClip; // The Animation's default clip.
  194. std::vector<AnimationClip*>* _clips; // All the clips created from this Animation.
  195. };
  196. }
  197. #endif