AnimationController.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "Base.h"
  2. #include "AnimationController.h"
  3. #include "Game.h"
  4. #include "Curve.h"
  5. namespace gameplay
  6. {
  7. AnimationController::AnimationController()
  8. : _state(IDLE), _animations(NULL)
  9. {
  10. }
  11. AnimationController::~AnimationController()
  12. {
  13. destroyAllAnimations();
  14. }
  15. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, Curve::InterpolationType type)
  16. {
  17. assert(type != Curve::BEZIER && type != Curve::HERMITE);
  18. assert(id && keyCount >= 2 && keyTimes && keyValues);
  19. Animation* animation = getAnimation(id);
  20. if (animation != NULL)
  21. return NULL;
  22. animation = new Animation(id, target, propertyId, keyCount, keyTimes, keyValues, type);
  23. addAnimation(animation);
  24. target->addAnimation(animation);
  25. return animation;
  26. }
  27. Animation* AnimationController::createAnimation(const char* id, AnimationTarget* target, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, Curve::InterpolationType type)
  28. {
  29. assert(id && keyCount >= 2 && keyTimes && keyValues && keyInValue && keyOutValue);
  30. Animation* animation = getAnimation(id);
  31. if (animation != NULL)
  32. return NULL;
  33. animation = new Animation(id, target, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
  34. addAnimation(animation);
  35. target->addAnimation(animation);
  36. return animation;
  37. }
  38. Animation* AnimationController::createAnimationFromTo(const char* id, AnimationTarget* target, int propertyId, float* from, float* to, Curve::InterpolationType type, unsigned long duration)
  39. {
  40. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  41. float* keyValues = new float[2 * propertyComponentCount];
  42. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  43. memcpy(keyValues + propertyComponentCount, to, sizeof(float) * propertyComponentCount);
  44. unsigned long* keyTimes = new unsigned long[2];
  45. keyTimes[0] = 0;
  46. keyTimes[1] = duration;
  47. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  48. SAFE_DELETE_ARRAY(keyTimes);
  49. return animation;
  50. }
  51. Animation* AnimationController::createAnimationFromBy(const char* id, AnimationTarget* target, int propertyId, float* from, float* by, Curve::InterpolationType type, unsigned long duration)
  52. {
  53. const unsigned int propertyComponentCount = target->getAnimationPropertyComponentCount(propertyId);
  54. float* keyValues = new float[2 * propertyComponentCount];
  55. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  56. memcpy(keyValues + propertyComponentCount, by, sizeof(float) * propertyComponentCount);
  57. unsigned long* keyTimes = new unsigned long[2];
  58. keyTimes[0] = 0;
  59. keyTimes[1] = duration;
  60. Animation* animation = createAnimation(id, target, propertyId, 2, keyTimes, keyValues, type);
  61. SAFE_DELETE_ARRAY(keyTimes);
  62. return animation;
  63. }
  64. Animation* AnimationController::getAnimation(const char* id) const
  65. {
  66. unsigned int animationCount = _animations.size();
  67. for (unsigned int i = 0; i < animationCount; i++)
  68. {
  69. if (_animations.at(i)->_id.compare(id) == 0)
  70. {
  71. return _animations.at(i);
  72. }
  73. }
  74. return NULL;
  75. }
  76. void AnimationController::stopAllAnimations()
  77. {
  78. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  79. while (clipIter != _runningClips.end())
  80. {
  81. AnimationClip* clip = *clipIter;
  82. clip->_isPlaying = false;
  83. clip->onEnd();
  84. clipIter = _runningClips.erase(clipIter);
  85. SAFE_RELEASE(clip);
  86. }
  87. _runningClips.clear();
  88. _state = IDLE;
  89. }
  90. AnimationController::State AnimationController::getState() const
  91. {
  92. return _state;
  93. }
  94. void AnimationController::initialize()
  95. {
  96. _state = IDLE;
  97. }
  98. void AnimationController::finalize()
  99. {
  100. _state = PAUSED;
  101. }
  102. void AnimationController::resume()
  103. {
  104. if (_runningClips.empty())
  105. _state = IDLE;
  106. else
  107. _state = RUNNING;
  108. }
  109. void AnimationController::pause()
  110. {
  111. _state = PAUSED;
  112. }
  113. void AnimationController::schedule(AnimationClip* clip)
  114. {
  115. if (_runningClips.empty())
  116. {
  117. _state = RUNNING;
  118. }
  119. if (clip->_isPlaying)
  120. {
  121. _runningClips.remove(clip);
  122. clip->_isPlaying = false;
  123. clip->onEnd();
  124. }
  125. else
  126. {
  127. clip->addRef();
  128. }
  129. _runningClips.push_back(clip);
  130. }
  131. void AnimationController::unschedule(AnimationClip* clip)
  132. {
  133. if (clip->_isPlaying)
  134. {
  135. _runningClips.remove(clip);
  136. SAFE_RELEASE(clip);
  137. }
  138. if (_runningClips.empty())
  139. _state = IDLE;
  140. }
  141. void AnimationController::update(long elapsedTime)
  142. {
  143. if (_state != RUNNING)
  144. return;
  145. std::list<AnimationClip*>::iterator clipIter = _runningClips.begin();
  146. unsigned int clipCount = 0;
  147. while (clipIter != _runningClips.end())
  148. {
  149. AnimationClip* clip = (*clipIter);
  150. if (clip->update(elapsedTime))
  151. {
  152. clipIter = _runningClips.erase(clipIter);
  153. SAFE_RELEASE(clip);
  154. }
  155. else
  156. {
  157. clipIter++;
  158. }
  159. clipCount++;
  160. }
  161. if (_runningClips.empty())
  162. _state = IDLE;
  163. }
  164. void AnimationController::addAnimation(Animation* animation)
  165. {
  166. _animations.push_back(animation);
  167. }
  168. void AnimationController::destroyAnimation(Animation* animation)
  169. {
  170. std::vector<Animation*>::iterator itr = _animations.begin();
  171. while (itr != _animations.end())
  172. {
  173. if (animation == *itr)
  174. {
  175. _animations.erase(itr);
  176. return;
  177. }
  178. itr++;
  179. }
  180. }
  181. void AnimationController::destroyAllAnimations()
  182. {
  183. stopAllAnimations();
  184. std::vector<Animation*>::iterator itr = _animations.begin();
  185. while (itr != _animations.end())
  186. {
  187. Animation* animation = *itr;
  188. SAFE_RELEASE(animation);
  189. itr++;
  190. }
  191. _animations.clear();
  192. }
  193. }