AnimationClip.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. #include "Base.h"
  2. #include "AnimationClip.h"
  3. #include "Animation.h"
  4. #include "AnimationTarget.h"
  5. #include "Game.h"
  6. #include "Quaternion.h"
  7. #include "ScriptController.h"
  8. namespace gameplay
  9. {
  10. AnimationClip::AnimationClip(const char* id, Animation* animation, unsigned long startTime, unsigned long endTime)
  11. : _id(id), _animation(animation), _startTime(startTime), _endTime(endTime), _duration(_endTime - _startTime),
  12. _stateBits(0x00), _repeatCount(1.0f), _activeDuration(_duration * _repeatCount), _speed(1.0f), _timeStarted(0),
  13. _elapsedTime(0), _crossFadeToClip(NULL), _crossFadeOutElapsed(0), _crossFadeOutDuration(0), _blendWeight(1.0f),
  14. _beginListeners(NULL), _endListeners(NULL), _listeners(NULL), _listenerItr(NULL), _scriptListeners(NULL)
  15. {
  16. GP_ASSERT(_animation);
  17. GP_ASSERT(0 <= startTime && startTime <= _animation->_duration && 0 <= endTime && endTime <= _animation->_duration);
  18. for (size_t i = 0, count = _animation->_channels.size(); i < count; i++)
  19. {
  20. GP_ASSERT(_animation->_channels[i]);
  21. GP_ASSERT(_animation->_channels[i]->getCurve());
  22. _values.push_back(new AnimationValue(_animation->_channels[i]->getCurve()->getComponentCount()));
  23. }
  24. }
  25. AnimationClip::~AnimationClip()
  26. {
  27. std::vector<AnimationValue*>::iterator valueIter = _values.begin();
  28. while (valueIter != _values.end())
  29. {
  30. SAFE_DELETE(*valueIter);
  31. valueIter++;
  32. }
  33. _values.clear();
  34. SAFE_RELEASE(_crossFadeToClip);
  35. SAFE_DELETE(_beginListeners);
  36. SAFE_DELETE(_endListeners);
  37. if (_scriptListeners)
  38. {
  39. for (size_t i = 0; i < _scriptListeners->size(); i++)
  40. {
  41. SAFE_DELETE((*_scriptListeners)[i]);
  42. }
  43. SAFE_DELETE(_scriptListeners);
  44. }
  45. if (_listeners)
  46. {
  47. *_listenerItr = _listeners->begin();
  48. while (*_listenerItr != _listeners->end())
  49. {
  50. ListenerEvent* lEvt = **_listenerItr;
  51. SAFE_DELETE(lEvt);
  52. ++*_listenerItr;
  53. }
  54. SAFE_DELETE(_listeners);
  55. }
  56. SAFE_DELETE(_listenerItr);
  57. }
  58. AnimationClip::ListenerEvent::ListenerEvent(Listener* listener, unsigned long eventTime)
  59. {
  60. _listener = listener;
  61. _eventTime = eventTime;
  62. }
  63. AnimationClip::ListenerEvent::~ListenerEvent()
  64. {
  65. }
  66. const char* AnimationClip::getId() const
  67. {
  68. return _id.c_str();
  69. }
  70. Animation* AnimationClip::getAnimation() const
  71. {
  72. return _animation;
  73. }
  74. unsigned long AnimationClip::getStartTime() const
  75. {
  76. return _startTime;
  77. }
  78. unsigned long AnimationClip::getEndTime() const
  79. {
  80. return _endTime;
  81. }
  82. float AnimationClip::getElaspedTime() const
  83. {
  84. return _elapsedTime;
  85. }
  86. void AnimationClip::setRepeatCount(float repeatCount)
  87. {
  88. GP_ASSERT(repeatCount == REPEAT_INDEFINITE || repeatCount > 0.0f);
  89. _repeatCount = repeatCount;
  90. if (repeatCount == REPEAT_INDEFINITE)
  91. {
  92. _activeDuration = _duration;
  93. }
  94. else
  95. {
  96. _activeDuration = _repeatCount * _duration;
  97. }
  98. }
  99. float AnimationClip::getRepeatCount() const
  100. {
  101. return _repeatCount;
  102. }
  103. void AnimationClip::setActiveDuration(unsigned long duration)
  104. {
  105. if (duration == REPEAT_INDEFINITE)
  106. {
  107. _repeatCount = REPEAT_INDEFINITE;
  108. _activeDuration = _duration;
  109. }
  110. else
  111. {
  112. _activeDuration = _duration;
  113. _repeatCount = (float)_activeDuration / (float)_duration;
  114. }
  115. }
  116. unsigned long AnimationClip::getActiveDuration() const
  117. {
  118. if (_repeatCount == REPEAT_INDEFINITE)
  119. return REPEAT_INDEFINITE;
  120. return _activeDuration;
  121. }
  122. unsigned long AnimationClip::getDuration() const
  123. {
  124. return _duration;
  125. }
  126. void AnimationClip::setSpeed(float speed)
  127. {
  128. _speed = speed;
  129. }
  130. float AnimationClip::getSpeed() const
  131. {
  132. return _speed;
  133. }
  134. void AnimationClip::setBlendWeight(float blendWeight)
  135. {
  136. _blendWeight = blendWeight;
  137. }
  138. float AnimationClip::getBlendWeight() const
  139. {
  140. return _blendWeight;
  141. }
  142. bool AnimationClip::isPlaying() const
  143. {
  144. return (isClipStateBitSet(CLIP_IS_PLAYING_BIT) && !isClipStateBitSet(CLIP_IS_PAUSED_BIT));
  145. }
  146. void AnimationClip::play()
  147. {
  148. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  149. {
  150. // If paused, reset the bit and return.
  151. if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
  152. {
  153. resetClipStateBit(CLIP_IS_PAUSED_BIT);
  154. return;
  155. }
  156. // If the clip is set to be removed, reset the flag.
  157. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  158. resetClipStateBit(CLIP_IS_MARKED_FOR_REMOVAL_BIT);
  159. // Set the state bit to restart.
  160. setClipStateBit(CLIP_IS_RESTARTED_BIT);
  161. }
  162. else
  163. {
  164. setClipStateBit(CLIP_IS_PLAYING_BIT);
  165. GP_ASSERT(_animation);
  166. GP_ASSERT(_animation->_controller);
  167. _animation->_controller->schedule(this);
  168. }
  169. _timeStarted = Game::getGameTime();
  170. }
  171. void AnimationClip::stop()
  172. {
  173. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  174. {
  175. // Reset the restarted and paused bits.
  176. resetClipStateBit(CLIP_IS_RESTARTED_BIT);
  177. resetClipStateBit(CLIP_IS_PAUSED_BIT);
  178. // Mark the clip to removed from the AnimationController.
  179. setClipStateBit(CLIP_IS_MARKED_FOR_REMOVAL_BIT);
  180. }
  181. }
  182. void AnimationClip::pause()
  183. {
  184. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT) && !isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  185. {
  186. setClipStateBit(CLIP_IS_PAUSED_BIT);
  187. }
  188. }
  189. void AnimationClip::crossFade(AnimationClip* clip, unsigned long duration)
  190. {
  191. GP_ASSERT(clip);
  192. // Check if the given clip is fading into this clip.
  193. // We should reset the clip from fading out, and this one from fading in
  194. // in order to start the crossfade back the other way.
  195. if (clip->isClipStateBitSet(CLIP_IS_FADING_OUT_BIT) && clip->_crossFadeToClip == this)
  196. {
  197. clip->resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  198. clip->_crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  199. SAFE_RELEASE(clip->_crossFadeToClip);
  200. }
  201. // If I already have a clip I'm fading to and it's not the same as the given clip release it.
  202. // Assign the new clip and increase it's ref count.
  203. if (_crossFadeToClip)
  204. {
  205. SAFE_RELEASE(_crossFadeToClip);
  206. }
  207. // Set and initialize the crossfade clip
  208. _crossFadeToClip = clip;
  209. _crossFadeToClip->addRef();
  210. _crossFadeToClip->setClipStateBit(CLIP_IS_FADING_IN_BIT);
  211. _crossFadeToClip->_blendWeight = 0.0f;
  212. // Set and initialize this clip to fade out
  213. setClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  214. setClipStateBit(CLIP_IS_FADING_OUT_BIT);
  215. _crossFadeOutElapsed = 0.0f;
  216. _crossFadeOutDuration = duration;
  217. // If this clip is currently not playing, we should start playing it.
  218. if (!isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  219. play();
  220. // Start playing the cross fade clip.
  221. _crossFadeToClip->play();
  222. }
  223. void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long eventTime)
  224. {
  225. GP_ASSERT(listener);
  226. GP_ASSERT(eventTime < _activeDuration);
  227. ListenerEvent* listenerEvent = new ListenerEvent(listener, eventTime);
  228. if (!_listeners)
  229. {
  230. _listeners = new std::list<ListenerEvent*>;
  231. _listeners->push_front(listenerEvent);
  232. _listenerItr = new std::list<ListenerEvent*>::iterator;
  233. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  234. *_listenerItr = _listeners->begin();
  235. }
  236. else
  237. {
  238. for (std::list<ListenerEvent*>::iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
  239. {
  240. GP_ASSERT(*itr);
  241. if (eventTime < (*itr)->_eventTime)
  242. {
  243. itr = _listeners->insert(itr, listenerEvent);
  244. // If playing, update the iterator if we need to.
  245. // otherwise, it will just be set the next time the clip gets played.
  246. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  247. {
  248. float currentTime = fmodf(_elapsedTime, (float)_duration);
  249. GP_ASSERT(**_listenerItr || *_listenerItr == _listeners->end());
  250. if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_eventTime)) ||
  251. (_speed <= 0 && currentTime > eventTime && (*_listenerItr == _listeners->begin() || eventTime > (**_listenerItr)->_eventTime)))
  252. *_listenerItr = itr;
  253. }
  254. return;
  255. }
  256. }
  257. _listeners->push_back(listenerEvent);
  258. }
  259. }
  260. void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
  261. {
  262. if (!_beginListeners)
  263. _beginListeners = new std::vector<Listener*>;
  264. GP_ASSERT(listener);
  265. _beginListeners->push_back(listener);
  266. }
  267. void AnimationClip::addEndListener(AnimationClip::Listener* listener)
  268. {
  269. if (!_endListeners)
  270. _endListeners = new std::vector<Listener*>;
  271. GP_ASSERT(listener);
  272. _endListeners->push_back(listener);
  273. }
  274. void AnimationClip::addBeginListener(const char* function)
  275. {
  276. if (!_scriptListeners)
  277. _scriptListeners = new std::vector<ScriptListener*>;
  278. ScriptListener* listener = new ScriptListener(function);
  279. _scriptListeners->push_back(listener);
  280. addBeginListener(listener);
  281. }
  282. void AnimationClip::addEndListener(const char* function)
  283. {
  284. if (!_scriptListeners)
  285. _scriptListeners = new std::vector<ScriptListener*>;
  286. ScriptListener* listener = new ScriptListener(function);
  287. _scriptListeners->push_back(listener);
  288. addEndListener(listener);
  289. }
  290. void AnimationClip::addListener(const char* function, unsigned long eventTime)
  291. {
  292. if (!_scriptListeners)
  293. _scriptListeners = new std::vector<ScriptListener*>;
  294. ScriptListener* listener = new ScriptListener(function);
  295. _scriptListeners->push_back(listener);
  296. addListener(listener, eventTime);
  297. }
  298. bool AnimationClip::update(float elapsedTime)
  299. {
  300. if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
  301. {
  302. return false;
  303. }
  304. else if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  305. { // If the marked for removal bit is set, it means stop() was called on the AnimationClip at some point
  306. // after the last update call. Reset the flag, and return true so the AnimationClip is removed from the
  307. // running clips on the AnimationController.
  308. onEnd();
  309. return true;
  310. }
  311. else if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  312. {
  313. onBegin();
  314. }
  315. else
  316. {
  317. _elapsedTime += elapsedTime * _speed;
  318. if (_repeatCount == REPEAT_INDEFINITE && _elapsedTime <= 0)
  319. _elapsedTime = _activeDuration + _elapsedTime;
  320. }
  321. float currentTime = 0.0f;
  322. // Check to see if clip is complete.
  323. if (_repeatCount != REPEAT_INDEFINITE && ((_speed >= 0.0f && _elapsedTime >= _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0.0f)))
  324. {
  325. resetClipStateBit(CLIP_IS_STARTED_BIT);
  326. if (_speed >= 0.0f)
  327. {
  328. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  329. if (_duration == 0)
  330. {
  331. currentTime = 0.0f;
  332. }
  333. else
  334. {
  335. currentTime = (float)(_activeDuration % _duration); // Get's the fractional part of the final repeat.
  336. if (currentTime == 0.0f)
  337. currentTime = _duration;
  338. }
  339. }
  340. else
  341. {
  342. currentTime = 0.0f; // If we are negative speed, the end value should be 0.
  343. }
  344. }
  345. else
  346. {
  347. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  348. if (_duration == 0)
  349. currentTime = 0.0f;
  350. else // Gets portion/fraction of the repeat.
  351. currentTime = fmodf(_elapsedTime, _duration);
  352. }
  353. // Notify any listeners of Animation events.
  354. if (_listeners)
  355. {
  356. GP_ASSERT(_listenerItr);
  357. if (_speed >= 0.0f)
  358. {
  359. while (*_listenerItr != _listeners->end() && _elapsedTime >= (long) (**_listenerItr)->_eventTime)
  360. {
  361. GP_ASSERT(_listenerItr);
  362. GP_ASSERT(**_listenerItr);
  363. GP_ASSERT((**_listenerItr)->_listener);
  364. (**_listenerItr)->_listener->animationEvent(this, Listener::TIME);
  365. ++*_listenerItr;
  366. }
  367. }
  368. else
  369. {
  370. while (*_listenerItr != _listeners->begin() && _elapsedTime <= (long) (**_listenerItr)->_eventTime)
  371. {
  372. GP_ASSERT(_listenerItr);
  373. GP_ASSERT(**_listenerItr);
  374. GP_ASSERT((**_listenerItr)->_listener);
  375. (**_listenerItr)->_listener->animationEvent(this, Listener::TIME);
  376. --*_listenerItr;
  377. }
  378. }
  379. }
  380. // Add back in start time, and divide by the total animation's duration to get the actual percentage complete
  381. GP_ASSERT(_animation);
  382. // If the animation duration is zero (start time == end time, such as when there is only a single keyframe),
  383. // then prevent a divide by zero and set percentComplete = 1.
  384. float percentComplete = _animation->_duration == 0 ? 1 : ((float)_startTime + currentTime) / (float)_animation->_duration;
  385. percentComplete = MATH_CLAMP(percentComplete, 0.0f, 1.0f);
  386. if (isClipStateBitSet(CLIP_IS_FADING_OUT_BIT))
  387. {
  388. GP_ASSERT(_crossFadeToClip);
  389. GP_ASSERT(_crossFadeOutDuration > 0);
  390. if (isClipStateBitSet(CLIP_IS_FADING_OUT_STARTED_BIT)) // Calculate elapsed time since the fade out begin.
  391. {
  392. GP_ASSERT(_crossFadeToClip);
  393. _crossFadeOutElapsed = (Game::getGameTime() - _crossFadeToClip->_timeStarted) * fabs(_speed);
  394. resetClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  395. }
  396. else
  397. {
  398. // continue tracking elapsed time.
  399. _crossFadeOutElapsed += elapsedTime * fabs(_speed);
  400. }
  401. if (_crossFadeOutElapsed < _crossFadeOutDuration)
  402. {
  403. // Calculate this clip's blend weight.
  404. float tempBlendWeight = ((float)_crossFadeOutDuration - _crossFadeOutElapsed) / (float)_crossFadeOutDuration;
  405. // If this clip is fading in, adjust the crossfade clip's weight to be a percentage of your current blend weight
  406. if (isClipStateBitSet(CLIP_IS_FADING_IN_BIT))
  407. {
  408. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight) * _blendWeight;
  409. _blendWeight -= _crossFadeToClip->_blendWeight;
  410. }
  411. else
  412. {
  413. // Just set the blend weight.
  414. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight);
  415. _blendWeight = tempBlendWeight;
  416. }
  417. }
  418. else
  419. {
  420. // Fade is done.
  421. _crossFadeToClip->_blendWeight = 1.0f;
  422. _blendWeight = 0.0f;
  423. resetClipStateBit(CLIP_IS_STARTED_BIT);
  424. resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  425. _crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  426. SAFE_RELEASE(_crossFadeToClip);
  427. }
  428. }
  429. // Evaluate this clip.
  430. Animation::Channel* channel = NULL;
  431. AnimationValue* value = NULL;
  432. AnimationTarget* target = NULL;
  433. size_t channelCount = _animation->_channels.size();
  434. for (size_t i = 0; i < channelCount; i++)
  435. {
  436. channel = _animation->_channels[i];
  437. GP_ASSERT(channel);
  438. target = channel->_target;
  439. GP_ASSERT(target);
  440. value = _values[i];
  441. GP_ASSERT(value);
  442. // Evaluate the point on Curve
  443. GP_ASSERT(channel->getCurve());
  444. channel->getCurve()->evaluate(percentComplete, value->_value);
  445. // Set the animation value on the target property.
  446. target->setAnimationPropertyValue(channel->_propertyId, value, _blendWeight);
  447. }
  448. // When ended. Probably should move to it's own method so we can call it when the clip is ended early.
  449. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT) || !isClipStateBitSet(CLIP_IS_STARTED_BIT))
  450. {
  451. onEnd();
  452. return true;
  453. }
  454. return false;
  455. }
  456. void AnimationClip::onBegin()
  457. {
  458. // Initialize animation to play.
  459. setClipStateBit(CLIP_IS_STARTED_BIT);
  460. if (_speed >= 0)
  461. {
  462. _elapsedTime = (Game::getGameTime() - _timeStarted) * _speed;
  463. if (_listeners)
  464. *_listenerItr = _listeners->begin();
  465. }
  466. else
  467. {
  468. _elapsedTime = _activeDuration + (Game::getGameTime() - _timeStarted) * _speed;
  469. if (_listeners)
  470. *_listenerItr = _listeners->end();
  471. }
  472. // Notify begin listeners if any.
  473. if (_beginListeners)
  474. {
  475. std::vector<Listener*>::iterator listener = _beginListeners->begin();
  476. while (listener != _beginListeners->end())
  477. {
  478. GP_ASSERT(*listener);
  479. (*listener)->animationEvent(this, Listener::BEGIN);
  480. listener++;
  481. }
  482. }
  483. }
  484. void AnimationClip::onEnd()
  485. {
  486. _blendWeight = 1.0f;
  487. resetClipStateBit(CLIP_ALL_BITS);
  488. // Notify end listeners if any.
  489. if (_endListeners)
  490. {
  491. std::vector<Listener*>::iterator listener = _endListeners->begin();
  492. while (listener != _endListeners->end())
  493. {
  494. GP_ASSERT(*listener);
  495. (*listener)->animationEvent(this, Listener::END);
  496. listener++;
  497. }
  498. }
  499. }
  500. bool AnimationClip::isClipStateBitSet(unsigned char bit) const
  501. {
  502. return (_stateBits & bit) == bit;
  503. }
  504. void AnimationClip::setClipStateBit(unsigned char bit)
  505. {
  506. _stateBits |= bit;
  507. }
  508. void AnimationClip::resetClipStateBit(unsigned char bit)
  509. {
  510. _stateBits &= ~bit;
  511. }
  512. AnimationClip* AnimationClip::clone(Animation* animation) const
  513. {
  514. // Don't clone the elapsed time, listeners or crossfade information.
  515. AnimationClip* newClip = new AnimationClip(getId(), animation, getStartTime(), getEndTime());
  516. newClip->setRepeatCount(getRepeatCount());
  517. newClip->setSpeed(getSpeed());
  518. newClip->setRepeatCount(getRepeatCount());
  519. newClip->setBlendWeight(getBlendWeight());
  520. size_t size = _values.size();
  521. newClip->_values.resize(size, NULL);
  522. for (size_t i = 0; i < size; ++i)
  523. {
  524. if (newClip->_values[i] == NULL)
  525. {
  526. newClip->_values[i] = new AnimationValue(*_values[i]);
  527. }
  528. else
  529. {
  530. *newClip->_values[i] = *_values[i];
  531. }
  532. }
  533. return newClip;
  534. }
  535. AnimationClip::ScriptListener::ScriptListener(const std::string& function)
  536. {
  537. // Store the function name.
  538. this->function = Game::getInstance()->getScriptController()->loadUrl(function.c_str());
  539. }
  540. void AnimationClip::ScriptListener::animationEvent(AnimationClip* clip, EventType type)
  541. {
  542. Game::getInstance()->getScriptController()->executeFunction<void>(function.c_str(), "<AnimationClip>[AnimationClip::Listener::EventType]", clip, type);
  543. }
  544. }