Animation.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Animation.h
  3. */
  4. #ifndef ANIMATION_H_
  5. #define ANIMATION_H_
  6. #include "Ref.h"
  7. namespace gameplay
  8. {
  9. class AnimationTarget;
  10. class AnimationController;
  11. class AnimationClip;
  12. class Curve;
  13. /**
  14. * Defines a generic property animation.
  15. *
  16. * To run an animation you must play an AnimationClip.
  17. * Every Animation has the default clip which will run from begin-end time.
  18. * You can create additional clips to run only parts of an animation and control
  19. * various runtime characteristics, such as repeat count, etc.
  20. */
  21. class Animation : public Ref
  22. {
  23. friend class AnimationController;
  24. friend class AnimationClip;
  25. friend class AnimationTarget;
  26. friend class Package;
  27. public:
  28. /**
  29. * Gets the Animation's ID.
  30. *
  31. * @return The Animation's ID.
  32. */
  33. const char* getId() const;
  34. /**
  35. * Gets the Animation's duration.
  36. *
  37. * @return The Animation's duration (in milliseconds).
  38. */
  39. unsigned long getDuration() const;
  40. /**
  41. * Creates an AnimationClip from an .animation file.
  42. */
  43. void createClips(const char* animationFile);
  44. /**
  45. * Creates an AnimationClip from the Animation.
  46. *
  47. * @param id The ID to the give the AnimationClip.
  48. * @param start The time (in milliseconds) that the AnimationClip will begin from.
  49. * @param end The time (in milliseconds) that the AnimationClip will end.
  50. *
  51. * @return The newly created AnimationClip; NULL if an AnimationClip already exists with the same ID.
  52. */
  53. AnimationClip* createClip(const char* id, unsigned long start, unsigned long end);
  54. /**
  55. * Finds the AnimationClip with the specified name. If NULL, gets the default clip.
  56. *
  57. * @param clipId The ID of the AnimationClip to get.
  58. *
  59. * @return The AnimationClip with the specified ID; NULL if an AnimationClip with the given ID is not found.
  60. */
  61. AnimationClip* getClip(const char* clipId = NULL);
  62. /**
  63. * Plays the AnimationClip with the specified name.
  64. *
  65. * @param clipId The ID of the AnimationClip to play. If NULL, plays the default clip.
  66. */
  67. void play(const char* clipId = NULL);
  68. /**
  69. * Stops the AnimationClip with the specified name.
  70. *
  71. * @param clipId The ID of the AnimationClip to stop. If NULL, stops the default clip.
  72. */
  73. void stop(const char* clipId = NULL);
  74. private:
  75. /**
  76. * Defines a channel which holds the target, target property, curve values, and duration.
  77. *
  78. * An animation can have 1 or more channels. All typical simple property animations
  79. * will have 1 channel. Skeletal animation will have 1 channel per joint to be animated.
  80. */
  81. class Channel
  82. {
  83. friend class AnimationClip;
  84. friend class Animation;
  85. private:
  86. Channel(AnimationTarget* target, int propertyId, Curve* curve, unsigned long duration);
  87. Channel(const Channel& copy);
  88. ~Channel();
  89. AnimationTarget* _target; // The target of this channel.
  90. int _propertyId; // The target property this channel targets.
  91. Curve* _curve; // The curve used to represent the animation data.
  92. unsigned long _duration; // The length of the animation (in milliseconds).
  93. bool _isRelative; // Whether the data should be treated relatively or not.
  94. };
  95. /**
  96. * Constructor.
  97. */
  98. Animation();
  99. /**
  100. * Constructor.
  101. */
  102. Animation(const Animation& copy);
  103. /**
  104. * Constructor.
  105. */
  106. Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type);
  107. /**
  108. * Constructor.
  109. */
  110. Animation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type);
  111. /**
  112. * Destructor.
  113. */
  114. ~Animation();
  115. /**
  116. * Adds a clip to this Animation.
  117. */
  118. void addClip(AnimationClip* clip);
  119. /**
  120. * Finds the clip with the given ID.
  121. */
  122. AnimationClip* findClip(const char* id) const;
  123. /**
  124. * Creates the default clip.
  125. */
  126. void createDefaultClip();
  127. /**
  128. * Creates a channel within this animation.
  129. */
  130. Channel* createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, unsigned int type);
  131. /**
  132. * Creates a channel within this animation.
  133. */
  134. Channel* createChannel(AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, unsigned int type);
  135. /**
  136. * Adds a channel to the animation.
  137. */
  138. void addChannel(Channel* channel);
  139. AnimationController* _controller; // The AnimationController that this Animation will run on.
  140. std::string _id; // The Animation's ID.
  141. unsigned long _duration; // the length of the animation (in milliseconds).
  142. std::vector<Channel*> _channels; // The channels within this Animation.
  143. std::vector<AnimationClip*> _clips; // All the clips created from this Animation.
  144. };
  145. }
  146. #endif