AnimationController.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #ifndef ANIMATIONCONTROLLER_H_
  2. #define ANIMATIONCONTROLLER_H_
  3. #include "AnimationClip.h"
  4. #include "Animation.h"
  5. #include "AnimationTarget.h"
  6. #include "Properties.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * Defines a class for controlling game animation.
  11. */
  12. class AnimationController
  13. {
  14. friend class Game;
  15. friend class Animation;
  16. friend class AnimationClip;
  17. friend class SceneLoader;
  18. public:
  19. /**
  20. * Creates an animation on this target from a set of key value and key time pairs.
  21. * Cannot use Curve::BEZIER or CURVE::HERMITE as the interpolation type since they require tangents/control points.
  22. *
  23. * @param id The ID of the animation.
  24. * @param target The animation target.
  25. * @param propertyId The property on this target to animate.
  26. * @param keyCount The number of keyframes in the animation. Must be greater than one.
  27. * @param keyTimes The list of key times for the animation (in milliseconds).
  28. * @param keyValues The list of key values for the animation.
  29. * @param type The curve interpolation type.
  30. *
  31. * @return The newly created animation.
  32. */
  33. Animation* createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, Curve::InterpolationType type);
  34. /**
  35. * Creates an animation on this target from a set of key value and key time pairs.
  36. *
  37. * @param id The ID of the animation.
  38. * @param target The animation target.
  39. * @param propertyId The property on this target to animate.
  40. * @param keyCount The number of keyframes in the animation. Must be greater than one.
  41. * @param keyTimes The list of key times for the animation (in milliseconds).
  42. * @param keyValues The list of key values for the animation.
  43. * @param keyInValue The list of key in values for the animation.
  44. * @param keyOutValue The list of key out values for the animation.
  45. * @param type The curve interpolation type.
  46. *
  47. * @return The newly created animation.
  48. */
  49. Animation* createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, Curve::InterpolationType type);
  50. /**
  51. * Creates an animation on this target using the data from the given properties object.
  52. *
  53. * @param id The ID of the animation.
  54. * @param target The animation target.
  55. * @param animationFile The animation file defining the animation data.
  56. *
  57. * @return The newly created animation.
  58. */
  59. Animation* createAnimation(const char* id, AnimationTarget* target, const char* animationFile);
  60. /**
  61. * Creates a simple two keyframe from-to animation.
  62. * Cannot use Curve::BEZIER or CURVE::HERMITE as the interpolation type since they require tangents/control points.
  63. *
  64. * @param id The ID of the animation.
  65. * @param target The animation target.
  66. * @param propertyId The property on this target to animate.
  67. * @param from The values to animate from.
  68. * @param to The values to animate to.
  69. * @param type The curve interpolation type.
  70. * @param duration The duration of the animation (in milliseconds).
  71. *
  72. * @return The newly created animation.
  73. */
  74. Animation* createAnimationFromTo(const char* id, AnimationTarget* target, int propertyId, float* from, float* to, Curve::InterpolationType type, unsigned long duration);
  75. /**
  76. * Creates a simple two keyframe from-by animation.
  77. * Cannot use Curve::BEZIER or CURVE::HERMITE as the interpolation type since they require tangents/control points.
  78. *
  79. * @param id The ID of the animation.
  80. * @param target The animation target.
  81. * @param propertyId The property on this target to animate.
  82. * @param from The values to animate from.
  83. * @param by The values to animate by.
  84. * @param type The curve interpolation type.
  85. * @param duration The duration of the animation (in milliseconds).
  86. *
  87. * @return The newly created animation.
  88. */
  89. Animation* createAnimationFromBy(const char* id, AnimationTarget* target, int propertyId, float* from, float* by, Curve::InterpolationType type, unsigned long duration);
  90. /**
  91. * Finds the animation with the given ID.
  92. *
  93. * @param id The ID of the animation to get. NULL if the Animation is not found.
  94. *
  95. * @return The animation, or NULL if not found.
  96. */
  97. Animation* getAnimation(const char* id) const;
  98. /**
  99. * Returns the first animation that targets the given AnimationTarget.
  100. */
  101. Animation* getAnimation(AnimationTarget* target) const;
  102. /**
  103. * Stops all AnimationClips currently playing on the AnimationController.
  104. */
  105. void stopAllAnimations();
  106. /**
  107. * Removes the given animation from this AnimationTarget.
  108. */
  109. void destroyAnimation(Animation* animation);
  110. /**
  111. * Removes all animations from the AnimationTarget.
  112. */
  113. void destroyAllAnimations();
  114. private:
  115. enum State
  116. {
  117. RUNNING,
  118. IDLE,
  119. PAUSED,
  120. STOPPED
  121. };
  122. /**
  123. * Constructor.
  124. */
  125. AnimationController();
  126. /**
  127. * Constructor.
  128. */
  129. AnimationController(const AnimationController& copy);
  130. /**
  131. * Destructor.
  132. */
  133. ~AnimationController();
  134. /**
  135. * Creates an animation on this target using the data from the given properties object.
  136. *
  137. * @param id The ID of the animation.
  138. * @param target The animation target.
  139. * @param properties The properties object defining the animation data.
  140. *
  141. * @return The newly created animation.
  142. */
  143. Animation* createAnimation(const char* id, AnimationTarget* target, Properties* animationProperties);
  144. /**
  145. * Gets the controller's state.
  146. *
  147. * @return The current state.
  148. */
  149. State getState() const;
  150. /**
  151. * Callback for when the controller is initialized.
  152. */
  153. void initialize();
  154. /*
  155. * Callback for when the controller is finalized.
  156. */
  157. void finalize();
  158. /**
  159. * Resumes the AnimationController.
  160. */
  161. void resume();
  162. /**
  163. * Pauses the AnimationController.
  164. */
  165. void pause();
  166. /**
  167. * Schedules an AnimationClip to run.
  168. */
  169. void schedule(AnimationClip* clip);
  170. /**
  171. * Unschedules an AnimationClip.
  172. */
  173. void unschedule(AnimationClip* clip);
  174. /**
  175. * Callback for when the controller receives a frame update event.
  176. */
  177. void update(long elapsedTime);
  178. /**
  179. * Adds an animation on this AnimationTarget.
  180. */
  181. void addAnimation(Animation* animation);
  182. State _state; // The current state of the AnimationController.
  183. std::list<AnimationClip*> _runningClips; // A list of running AnimationClips.
  184. std::list<AnimationTarget*> _activeTargets; // A list of animating AnimationTargets.
  185. std::vector<Animation*> _animations; // A list of animations registered with the AnimationController
  186. };
  187. }
  188. #endif