AnimationController.h 6.2 KB

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