Animation.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 Package;
  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 an .animation file.
  39. */
  40. void createClips(const char* animationFile);
  41. /**
  42. * Creates an AnimationClip from the Animation.
  43. *
  44. * @param id The ID to the give the AnimationClip.
  45. * @param start The time (in milliseconds) that the AnimationClip will begin from.
  46. * @param end The time (in milliseconds) that the AnimationClip will end.
  47. *
  48. * @return The newly created AnimationClip; NULL if an AnimationClip already exists with the same ID.
  49. */
  50. AnimationClip* createClip(const char* id, unsigned long start, unsigned long end);
  51. /**
  52. * Finds the AnimationClip with the specified name. If NULL, gets the default clip.
  53. *
  54. * @param clipId The ID of the AnimationClip to get.
  55. *
  56. * @return The AnimationClip with the specified ID; NULL if an AnimationClip with the given ID is not found.
  57. */
  58. AnimationClip* getClip(const char* clipId = NULL);
  59. /**
  60. * Plays the AnimationClip with the specified name.
  61. *
  62. * @param clipId The ID of the AnimationClip to play. If NULL, plays the default clip.
  63. */
  64. void play(const char* clipId = NULL);
  65. /**
  66. * Stops the AnimationClip with the specified name.
  67. *
  68. * @param clipId The ID of the AnimationClip to stop. If NULL, stops the default clip.
  69. */
  70. void stop(const char* clipId = NULL);
  71. /**
  72. * Pauses the AnimationClip with the specified name.
  73. *
  74. * @param clipId The ID of the AnimationClip to pause. If NULL, pauses the default clip.
  75. */
  76. void pause(const char* clipId = NULL);
  77. /**
  78. * Returns true if this animation targets the given AnimationTarget.
  79. */
  80. bool targets(AnimationTarget* target) const;
  81. private:
  82. /**
  83. * Defines a channel which holds the target, target property, curve values, and duration.
  84. *
  85. * An animation can have 1 or more channels. All typical simple property animations
  86. * will have 1 channel. Skeletal animation will have 1 channel per joint to be animated.
  87. */
  88. class Channel
  89. {
  90. friend class AnimationClip;
  91. friend class Animation;
  92. friend class AnimationTarget;
  93. private:
  94. Channel(Animation* animation, AnimationTarget* target, int propertyId, Curve* curve, unsigned long duration);
  95. Channel(const Channel& copy, Animation* animation, AnimationTarget* target);
  96. Channel(const Channel&); // Hidden copy constructor.
  97. ~Channel();
  98. Channel& operator=(const Channel&); // Hidden copy assignment operator.
  99. Curve* getCurve() const;
  100. Animation* _animation; // Reference to the animation this channel belongs to.
  101. AnimationTarget* _target; // The target of this channel.
  102. int _propertyId; // The target property this channel targets.
  103. Curve* _curve; // The curve used to represent the animation data.
  104. unsigned long _duration; // The length of the animation (in milliseconds).
  105. };
  106. /**
  107. * Hidden copy constructor.
  108. */
  109. Animation(const Animation& copy);
  110. /**
  111. * Constructor.
  112. */
  113. Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type);
  114. /**
  115. * Constructor.
  116. */
  117. Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type);
  118. /**
  119. * Constructor.
  120. */
  121. Animation(const char* id);
  122. /**
  123. * Destructor.
  124. */
  125. ~Animation();
  126. /**
  127. * Hidden copy assignment operator.
  128. */
  129. Animation& operator=(const Animation&);
  130. /**
  131. * Creates the default clip.
  132. */
  133. void createDefaultClip();
  134. /**
  135. * Creates AnimationClip's for this Animation from the specified Property object.
  136. */
  137. void createClips(Properties* animationProperties, unsigned int frameCount);
  138. /**
  139. * Adds a clip to this Animation.
  140. */
  141. void addClip(AnimationClip* clip);
  142. /**
  143. * Finds the clip with the given ID.
  144. */
  145. AnimationClip* findClip(const char* id) const;
  146. /**
  147. * Creates a channel within this animation.
  148. */
  149. Channel* createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type);
  150. /**
  151. * Creates a channel within this animation.
  152. */
  153. Channel* createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type);
  154. /**
  155. * Adds a channel to the animation.
  156. */
  157. void addChannel(Channel* channel);
  158. /**
  159. * Removes a channel from the animation.
  160. */
  161. void removeChannel(Channel* channel);
  162. /**
  163. * Sets the rotation offset in a Curve representing a Transform's animation data.
  164. */
  165. void setTransformRotationOffset(Curve* curve, unsigned int propertyId);
  166. /**
  167. * Clones this animation.
  168. *
  169. * @return The newly created animation.
  170. */
  171. Animation* clone();
  172. AnimationController* _controller; // The AnimationController that this Animation will run on.
  173. std::string _id; // The Animation's ID.
  174. unsigned long _duration; // the length of the animation (in milliseconds).
  175. std::vector<Channel*> _channels; // The channels within this Animation.
  176. AnimationClip* _defaultClip; // The Animation's default clip.
  177. std::vector<AnimationClip*>* _clips; // All the clips created from this Animation.
  178. };
  179. }
  180. #endif