AnimationClip.cpp 19 KB

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