AnimationClip.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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), _loopBlendTime(0), _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::getElapsedTime() 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 + _loopBlendTime;
  93. }
  94. else
  95. {
  96. _activeDuration = _duration * _repeatCount;
  97. if (repeatCount > 1.0f && _loopBlendTime > 0.0f)
  98. _activeDuration += std::ceil(repeatCount - 1.0f) * _loopBlendTime;
  99. }
  100. }
  101. float AnimationClip::getRepeatCount() const
  102. {
  103. return _repeatCount;
  104. }
  105. void AnimationClip::setActiveDuration(unsigned long duration)
  106. {
  107. GP_ASSERT(duration >= 0);
  108. if (duration == REPEAT_INDEFINITE)
  109. {
  110. _activeDuration = _duration + _loopBlendTime;
  111. }
  112. else
  113. {
  114. _activeDuration = duration;
  115. _repeatCount = (float)_activeDuration / (float)_duration;
  116. }
  117. }
  118. unsigned long AnimationClip::getActiveDuration() const
  119. {
  120. if (_repeatCount == REPEAT_INDEFINITE)
  121. return REPEAT_INDEFINITE;
  122. return _activeDuration;
  123. }
  124. unsigned long AnimationClip::getDuration() const
  125. {
  126. return _duration;
  127. }
  128. void AnimationClip::setSpeed(float speed)
  129. {
  130. _speed = speed;
  131. }
  132. float AnimationClip::getSpeed() const
  133. {
  134. return _speed;
  135. }
  136. void AnimationClip::setBlendWeight(float blendWeight)
  137. {
  138. _blendWeight = blendWeight;
  139. }
  140. float AnimationClip::getBlendWeight() const
  141. {
  142. return _blendWeight;
  143. }
  144. void AnimationClip::setLoopBlendTime(float loopBlendTime)
  145. {
  146. _loopBlendTime = loopBlendTime;
  147. if (_loopBlendTime < 0.0f)
  148. _loopBlendTime = 0.0f;
  149. }
  150. float AnimationClip::getLoopBlendTime() const
  151. {
  152. return _loopBlendTime;
  153. }
  154. bool AnimationClip::isPlaying() const
  155. {
  156. return (isClipStateBitSet(CLIP_IS_PLAYING_BIT) && !isClipStateBitSet(CLIP_IS_PAUSED_BIT));
  157. }
  158. void AnimationClip::play()
  159. {
  160. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  161. {
  162. // If paused, reset the bit and return.
  163. if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
  164. {
  165. resetClipStateBit(CLIP_IS_PAUSED_BIT);
  166. return;
  167. }
  168. // If the clip is set to be removed, reset the flag.
  169. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  170. resetClipStateBit(CLIP_IS_MARKED_FOR_REMOVAL_BIT);
  171. // Set the state bit to restart.
  172. setClipStateBit(CLIP_IS_RESTARTED_BIT);
  173. }
  174. else
  175. {
  176. setClipStateBit(CLIP_IS_PLAYING_BIT);
  177. GP_ASSERT(_animation);
  178. GP_ASSERT(_animation->_controller);
  179. _animation->_controller->schedule(this);
  180. }
  181. _timeStarted = Game::getGameTime();
  182. }
  183. void AnimationClip::stop()
  184. {
  185. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  186. {
  187. // Reset the restarted and paused bits.
  188. resetClipStateBit(CLIP_IS_RESTARTED_BIT);
  189. resetClipStateBit(CLIP_IS_PAUSED_BIT);
  190. // Mark the clip to removed from the AnimationController.
  191. setClipStateBit(CLIP_IS_MARKED_FOR_REMOVAL_BIT);
  192. }
  193. }
  194. void AnimationClip::pause()
  195. {
  196. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT) && !isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  197. {
  198. setClipStateBit(CLIP_IS_PAUSED_BIT);
  199. }
  200. }
  201. void AnimationClip::crossFade(AnimationClip* clip, unsigned long duration)
  202. {
  203. GP_ASSERT(clip);
  204. // Check if the given clip is fading into this clip.
  205. // We should reset the clip from fading out, and this one from fading in
  206. // in order to start the crossfade back the other way.
  207. if (clip->isClipStateBitSet(CLIP_IS_FADING_OUT_BIT) && clip->_crossFadeToClip == this)
  208. {
  209. clip->resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  210. clip->_crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  211. SAFE_RELEASE(clip->_crossFadeToClip);
  212. }
  213. // If I already have a clip I'm fading to and it's not the same as the given clip release it.
  214. // Assign the new clip and increase it's ref count.
  215. if (_crossFadeToClip)
  216. {
  217. SAFE_RELEASE(_crossFadeToClip);
  218. }
  219. // Set and initialize the crossfade clip
  220. _crossFadeToClip = clip;
  221. _crossFadeToClip->addRef();
  222. _crossFadeToClip->setClipStateBit(CLIP_IS_FADING_IN_BIT);
  223. _crossFadeToClip->_blendWeight = 0.0f;
  224. // Set and initialize this clip to fade out
  225. setClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  226. setClipStateBit(CLIP_IS_FADING_OUT_BIT);
  227. _crossFadeOutElapsed = 0.0f;
  228. _crossFadeOutDuration = duration;
  229. // If this clip is currently not playing, we should start playing it.
  230. if (!isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  231. play();
  232. // Start playing the cross fade clip.
  233. _crossFadeToClip->play();
  234. }
  235. void AnimationClip::addListener(AnimationClip::Listener* listener, unsigned long eventTime)
  236. {
  237. GP_ASSERT(listener);
  238. GP_ASSERT(eventTime < _activeDuration);
  239. ListenerEvent* listenerEvent = new ListenerEvent(listener, eventTime);
  240. if (!_listeners)
  241. {
  242. _listeners = new std::list<ListenerEvent*>;
  243. _listeners->push_front(listenerEvent);
  244. _listenerItr = new std::list<ListenerEvent*>::iterator;
  245. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  246. *_listenerItr = _listeners->begin();
  247. }
  248. else
  249. {
  250. for (std::list<ListenerEvent*>::iterator itr = _listeners->begin(); itr != _listeners->end(); itr++)
  251. {
  252. GP_ASSERT(*itr);
  253. if (eventTime < (*itr)->_eventTime)
  254. {
  255. itr = _listeners->insert(itr, listenerEvent);
  256. // If playing, update the iterator if we need to.
  257. // otherwise, it will just be set the next time the clip gets played.
  258. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  259. {
  260. float currentTime = fmodf(_elapsedTime, (float)_duration);
  261. GP_ASSERT(**_listenerItr || *_listenerItr == _listeners->end());
  262. if ((_speed >= 0.0f && currentTime < eventTime && (*_listenerItr == _listeners->end() || eventTime < (**_listenerItr)->_eventTime)) ||
  263. (_speed <= 0 && currentTime > eventTime && (*_listenerItr == _listeners->begin() || eventTime > (**_listenerItr)->_eventTime)))
  264. {
  265. *_listenerItr = itr;
  266. }
  267. }
  268. return;
  269. }
  270. }
  271. _listeners->push_back(listenerEvent);
  272. }
  273. }
  274. void AnimationClip::removeListener(AnimationClip::Listener* listener, unsigned long eventTime)
  275. {
  276. if (_listeners)
  277. {
  278. GP_ASSERT(listener);
  279. std::list<ListenerEvent*>::iterator iter = std::find_if(_listeners->begin(), _listeners->end(), [&](ListenerEvent* lst){ return lst->_eventTime == eventTime && lst->_listener == listener; });
  280. if (iter != _listeners->end())
  281. {
  282. if (isClipStateBitSet(CLIP_IS_PLAYING_BIT))
  283. {
  284. float currentTime = fmodf(_elapsedTime, (float)_duration);
  285. GP_ASSERT(**_listenerItr || *_listenerItr == _listeners->end());
  286. // We the listener has not been triggered yet, then check if it is next to be triggered, remove it, and update the iterator
  287. if (((_speed >= 0.0f && currentTime < eventTime) || (_speed <= 0 && currentTime > eventTime)) &&
  288. *iter == **_listenerItr)
  289. {
  290. *_listenerItr = _listeners->erase(iter);
  291. return;
  292. }
  293. }
  294. _listeners->erase(iter);
  295. }
  296. }
  297. }
  298. void AnimationClip::addBeginListener(AnimationClip::Listener* listener)
  299. {
  300. if (!_beginListeners)
  301. _beginListeners = new std::vector<Listener*>;
  302. GP_ASSERT(listener);
  303. _beginListeners->push_back(listener);
  304. }
  305. void AnimationClip::removeBeginListener(AnimationClip::Listener* listener)
  306. {
  307. if (_beginListeners)
  308. {
  309. GP_ASSERT(listener);
  310. std::vector<Listener*>::iterator iter = std::find(_beginListeners->begin(), _beginListeners->end(), listener);
  311. if (iter != _beginListeners->end())
  312. {
  313. _beginListeners->erase(iter);
  314. }
  315. }
  316. }
  317. void AnimationClip::addEndListener(AnimationClip::Listener* listener)
  318. {
  319. if (!_endListeners)
  320. _endListeners = new std::vector<Listener*>;
  321. GP_ASSERT(listener);
  322. _endListeners->push_back(listener);
  323. }
  324. void AnimationClip::removeEndListener(AnimationClip::Listener* listener)
  325. {
  326. if (_endListeners)
  327. {
  328. GP_ASSERT(listener);
  329. std::vector<Listener*>::iterator iter = std::find(_endListeners->begin(), _endListeners->end(), listener);
  330. if (iter != _endListeners->end())
  331. {
  332. _endListeners->erase(iter);
  333. }
  334. }
  335. }
  336. void AnimationClip::addBeginListener(const char* function)
  337. {
  338. if (!_scriptListeners)
  339. _scriptListeners = new std::vector<ScriptListener*>;
  340. ScriptListener* listener = new ScriptListener(function);
  341. _scriptListeners->push_back(listener);
  342. addBeginListener(listener);
  343. }
  344. void AnimationClip::removeBeginListener(const char* function)
  345. {
  346. if (_scriptListeners)
  347. {
  348. std::string functionRef = Game::getInstance()->getScriptController()->loadUrl(function);
  349. std::vector<ScriptListener*>::iterator iter = std::find_if(_scriptListeners->begin(), _scriptListeners->end(), [&](ScriptListener* listener){ return listener->function == functionRef; });
  350. if (iter != _scriptListeners->end())
  351. {
  352. ScriptListener* listener = *iter;
  353. removeBeginListener(listener);
  354. _scriptListeners->erase(iter);
  355. SAFE_DELETE(listener);
  356. }
  357. }
  358. }
  359. void AnimationClip::addEndListener(const char* function)
  360. {
  361. if (!_scriptListeners)
  362. _scriptListeners = new std::vector<ScriptListener*>;
  363. ScriptListener* listener = new ScriptListener(function);
  364. _scriptListeners->push_back(listener);
  365. addEndListener(listener);
  366. }
  367. void AnimationClip::removeEndListener(const char* function)
  368. {
  369. if (_scriptListeners)
  370. {
  371. std::string functionRef = Game::getInstance()->getScriptController()->loadUrl(function);
  372. std::vector<ScriptListener*>::iterator iter = std::find_if(_scriptListeners->begin(), _scriptListeners->end(), [&](ScriptListener* listener){ return listener->function == functionRef; });
  373. if (iter != _scriptListeners->end())
  374. {
  375. ScriptListener* listener = *iter;
  376. removeEndListener(listener);
  377. _scriptListeners->erase(iter);
  378. SAFE_DELETE(listener);
  379. }
  380. }
  381. }
  382. void AnimationClip::addListener(const char* function, unsigned long eventTime)
  383. {
  384. if (!_scriptListeners)
  385. _scriptListeners = new std::vector<ScriptListener*>;
  386. ScriptListener* listener = new ScriptListener(function);
  387. _scriptListeners->push_back(listener);
  388. addListener(listener, eventTime);
  389. }
  390. void AnimationClip::removeListener(const char* function, unsigned long eventTime)
  391. {
  392. if (_scriptListeners)
  393. {
  394. GP_ASSERT(eventTime < _activeDuration); // Do this check here, before we modify any state
  395. std::string functionRef = Game::getInstance()->getScriptController()->loadUrl(function);
  396. std::vector<ScriptListener*>::iterator iter = std::find_if(_scriptListeners->begin(), _scriptListeners->end(), [&](ScriptListener* listener){ return listener->function == functionRef; });
  397. if (iter != _scriptListeners->end())
  398. {
  399. ScriptListener* listener = *iter;
  400. removeListener(listener, eventTime);
  401. _scriptListeners->erase(iter);
  402. SAFE_DELETE(listener);
  403. }
  404. }
  405. }
  406. bool AnimationClip::update(float elapsedTime)
  407. {
  408. if (isClipStateBitSet(CLIP_IS_PAUSED_BIT))
  409. {
  410. return false;
  411. }
  412. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT))
  413. {
  414. // If the marked for removal bit is set, it means stop() was called on the AnimationClip at some point
  415. // after the last update call. Reset the flag, and return true so the AnimationClip is removed from the
  416. // running clips on the AnimationController.
  417. onEnd();
  418. return true;
  419. }
  420. if (!isClipStateBitSet(CLIP_IS_STARTED_BIT))
  421. {
  422. // Clip is just starting
  423. onBegin();
  424. }
  425. else
  426. {
  427. // Clip was already running
  428. _elapsedTime += elapsedTime * _speed;
  429. if (_repeatCount == REPEAT_INDEFINITE && _elapsedTime <= 0)
  430. {
  431. // Elapsed time is moving backwards, so wrap it back around the end when it falls below zero
  432. _elapsedTime = _activeDuration + _elapsedTime;
  433. // TODO: account for _loopBlendTime
  434. }
  435. }
  436. // Current time within a loop of the clip
  437. float currentTime = 0.0f;
  438. // Check to see if clip is complete.
  439. if (_repeatCount != REPEAT_INDEFINITE && ((_speed >= 0.0f && _elapsedTime >= _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0.0f)))
  440. {
  441. // We finished our active duration (including repeats), so clamp to our end value.
  442. resetClipStateBit(CLIP_IS_STARTED_BIT);
  443. // Ensure we end off at the endpoints of our clip (-speed==0, +speed==_duration)
  444. currentTime = _speed < 0.0f ? 0.0f : _duration;
  445. }
  446. else
  447. {
  448. // If _duration == 0, we have a "pose". Just set currentTime to 0.
  449. if (_duration == 0)
  450. {
  451. currentTime = 0.0f;
  452. }
  453. else
  454. {
  455. // Animation is running normally.
  456. currentTime = fmodf(_elapsedTime, _duration + _loopBlendTime);
  457. }
  458. }
  459. // Notify any listeners of Animation events.
  460. if (_listeners)
  461. {
  462. GP_ASSERT(_listenerItr);
  463. if (_speed >= 0.0f)
  464. {
  465. while (*_listenerItr != _listeners->end() && _elapsedTime >= (long) (**_listenerItr)->_eventTime)
  466. {
  467. GP_ASSERT(_listenerItr);
  468. GP_ASSERT(**_listenerItr);
  469. GP_ASSERT((**_listenerItr)->_listener);
  470. (**_listenerItr)->_listener->animationEvent(this, Listener::TIME);
  471. ++(*_listenerItr);
  472. }
  473. }
  474. else
  475. {
  476. while (*_listenerItr != _listeners->begin() && _elapsedTime <= (long) (**_listenerItr)->_eventTime)
  477. {
  478. GP_ASSERT(_listenerItr);
  479. GP_ASSERT(**_listenerItr);
  480. GP_ASSERT((**_listenerItr)->_listener);
  481. (**_listenerItr)->_listener->animationEvent(this, Listener::TIME);
  482. --(*_listenerItr);
  483. }
  484. }
  485. }
  486. // Add back in start time, and divide by the total animation's duration to get the actual percentage complete
  487. GP_ASSERT(_animation);
  488. // Compute percentage complete for the current loop (prevent a divide by zero if _duration==0).
  489. // Note that we don't use (currentTime/(_duration+_loopBlendTime)). That's because we want a
  490. // % value that is outside the 0-1 range for loop smoothing/blending purposes.
  491. float percentComplete = _duration == 0 ? 1 : currentTime / (float)_duration;
  492. if (_loopBlendTime == 0.0f)
  493. percentComplete = MATH_CLAMP(percentComplete, 0.0f, 1.0f);
  494. // If we're cross fading, compute blend weights
  495. if (isClipStateBitSet(CLIP_IS_FADING_OUT_BIT))
  496. {
  497. GP_ASSERT(_crossFadeToClip);
  498. GP_ASSERT(_crossFadeOutDuration > 0);
  499. if (isClipStateBitSet(CLIP_IS_FADING_OUT_STARTED_BIT)) // Calculate elapsed time since the fade out begin.
  500. {
  501. GP_ASSERT(_crossFadeToClip);
  502. _crossFadeOutElapsed = (Game::getGameTime() - _crossFadeToClip->_timeStarted) * fabs(_speed);
  503. resetClipStateBit(CLIP_IS_FADING_OUT_STARTED_BIT);
  504. }
  505. else
  506. {
  507. // continue tracking elapsed time.
  508. _crossFadeOutElapsed += elapsedTime * fabs(_speed);
  509. }
  510. if (_crossFadeOutElapsed < _crossFadeOutDuration)
  511. {
  512. // Calculate this clip's blend weight.
  513. float tempBlendWeight = ((float)_crossFadeOutDuration - _crossFadeOutElapsed) / (float)_crossFadeOutDuration;
  514. // If this clip is fading in, adjust the crossfade clip's weight to be a percentage of your current blend weight
  515. if (isClipStateBitSet(CLIP_IS_FADING_IN_BIT))
  516. {
  517. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight) * _blendWeight;
  518. _blendWeight -= _crossFadeToClip->_blendWeight;
  519. }
  520. else
  521. {
  522. // Just set the blend weight.
  523. _crossFadeToClip->_blendWeight = (1.0f - tempBlendWeight);
  524. _blendWeight = tempBlendWeight;
  525. }
  526. }
  527. else
  528. {
  529. // Fade is done.
  530. _crossFadeToClip->_blendWeight = 1.0f;
  531. _blendWeight = 0.0f;
  532. resetClipStateBit(CLIP_IS_STARTED_BIT);
  533. resetClipStateBit(CLIP_IS_FADING_OUT_BIT);
  534. _crossFadeToClip->resetClipStateBit(CLIP_IS_FADING_IN_BIT);
  535. SAFE_RELEASE(_crossFadeToClip);
  536. }
  537. }
  538. // Evaluate this clip.
  539. Animation::Channel* channel = NULL;
  540. AnimationValue* value = NULL;
  541. AnimationTarget* target = NULL;
  542. size_t channelCount = _animation->_channels.size();
  543. float percentageStart = (float)_startTime / (float)_animation->_duration;
  544. float percentageEnd = (float)_endTime / (float)_animation->_duration;
  545. float percentageBlend = (float)_loopBlendTime / (float)_animation->_duration;
  546. for (size_t i = 0; i < channelCount; i++)
  547. {
  548. channel = _animation->_channels[i];
  549. GP_ASSERT(channel);
  550. target = channel->_target;
  551. GP_ASSERT(target);
  552. value = _values[i];
  553. GP_ASSERT(value);
  554. // Evaluate the point on Curve
  555. GP_ASSERT(channel->getCurve());
  556. channel->getCurve()->evaluate(percentComplete, percentageStart, percentageEnd, percentageBlend, value->_value);
  557. // Set the animation value on the target property.
  558. target->setAnimationPropertyValue(channel->_propertyId, value, _blendWeight);
  559. }
  560. // When ended. Probably should move to it's own method so we can call it when the clip is ended early.
  561. if (isClipStateBitSet(CLIP_IS_MARKED_FOR_REMOVAL_BIT) || !isClipStateBitSet(CLIP_IS_STARTED_BIT))
  562. {
  563. onEnd();
  564. return true;
  565. }
  566. return false;
  567. }
  568. void AnimationClip::onBegin()
  569. {
  570. addRef();
  571. // Initialize animation to play.
  572. setClipStateBit(CLIP_IS_STARTED_BIT);
  573. if (_speed >= 0)
  574. {
  575. _elapsedTime = (Game::getGameTime() - _timeStarted) * _speed;
  576. if (_listeners)
  577. *_listenerItr = _listeners->begin();
  578. }
  579. else
  580. {
  581. _elapsedTime = _activeDuration + (Game::getGameTime() - _timeStarted) * _speed;
  582. if (_listeners)
  583. *_listenerItr = _listeners->end();
  584. }
  585. // Notify begin listeners if any.
  586. if (_beginListeners)
  587. {
  588. std::vector<Listener*>::iterator listener = _beginListeners->begin();
  589. while (listener != _beginListeners->end())
  590. {
  591. GP_ASSERT(*listener);
  592. (*listener)->animationEvent(this, Listener::BEGIN);
  593. listener++;
  594. }
  595. }
  596. release();
  597. }
  598. void AnimationClip::onEnd()
  599. {
  600. addRef();
  601. _blendWeight = 1.0f;
  602. resetClipStateBit(CLIP_ALL_BITS);
  603. // Notify end listeners if any.
  604. if (_endListeners)
  605. {
  606. std::vector<Listener*>::iterator listener = _endListeners->begin();
  607. while (listener != _endListeners->end())
  608. {
  609. GP_ASSERT(*listener);
  610. (*listener)->animationEvent(this, Listener::END);
  611. listener++;
  612. }
  613. }
  614. release();
  615. }
  616. bool AnimationClip::isClipStateBitSet(unsigned char bit) const
  617. {
  618. return (_stateBits & bit) == bit;
  619. }
  620. void AnimationClip::setClipStateBit(unsigned char bit)
  621. {
  622. _stateBits |= bit;
  623. }
  624. void AnimationClip::resetClipStateBit(unsigned char bit)
  625. {
  626. _stateBits &= ~bit;
  627. }
  628. AnimationClip* AnimationClip::clone(Animation* animation) const
  629. {
  630. // Don't clone the elapsed time, listeners or crossfade information.
  631. AnimationClip* newClip = new AnimationClip(getId(), animation, getStartTime(), getEndTime());
  632. newClip->setSpeed(getSpeed());
  633. newClip->setRepeatCount(getRepeatCount());
  634. newClip->setBlendWeight(getBlendWeight());
  635. size_t size = _values.size();
  636. newClip->_values.resize(size, NULL);
  637. for (size_t i = 0; i < size; ++i)
  638. {
  639. if (newClip->_values[i] == NULL)
  640. {
  641. newClip->_values[i] = new AnimationValue(*_values[i]);
  642. }
  643. else
  644. {
  645. *newClip->_values[i] = *_values[i];
  646. }
  647. }
  648. return newClip;
  649. }
  650. AnimationClip::ScriptListener::ScriptListener(const std::string& function)
  651. {
  652. // Store the function name.
  653. this->function = Game::getInstance()->getScriptController()->loadUrl(function.c_str());
  654. }
  655. void AnimationClip::ScriptListener::animationEvent(AnimationClip* clip, EventType type)
  656. {
  657. Game::getInstance()->getScriptController()->executeFunction<void>(function.c_str(), "<AnimationClip>[AnimationClip::Listener::EventType]", clip, type);
  658. }
  659. }