CCArmatureAnimation.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCArmatureAnimation.h"
  21. #include "editor-support/cocostudio/CCArmature.h"
  22. #include "editor-support/cocostudio/CCBone.h"
  23. #include "editor-support/cocostudio/CCArmatureDefine.h"
  24. #include "editor-support/cocostudio/CCUtilMath.h"
  25. #include "editor-support/cocostudio/CCDatas.h"
  26. using namespace cocos2d;
  27. namespace cocostudio {
  28. ArmatureAnimation *ArmatureAnimation::create(Armature *armature)
  29. {
  30. ArmatureAnimation *pArmatureAnimation = new (std::nothrow) ArmatureAnimation();
  31. if (pArmatureAnimation && pArmatureAnimation->init(armature))
  32. {
  33. pArmatureAnimation->autorelease();
  34. return pArmatureAnimation;
  35. }
  36. CC_SAFE_DELETE(pArmatureAnimation);
  37. return nullptr;
  38. }
  39. ArmatureAnimation::ArmatureAnimation()
  40. : _animationData(nullptr)
  41. , _speedScale(1)
  42. , _movementData(nullptr)
  43. , _armature(nullptr)
  44. , _movementID("")
  45. , _toIndex(0)
  46. , _ignoreFrameEvent(false)
  47. , _onMovementList(false)
  48. , _movementListLoop(false)
  49. , _movementListDurationTo(-1)
  50. , _userObject(nullptr)
  51. , _movementEventCallFunc(nullptr)
  52. , _frameEventCallFunc(nullptr)
  53. , _movementEventTarget(nullptr)
  54. , _frameEventTarget(nullptr)
  55. , _movementEventListener(nullptr)
  56. , _frameEventListener(nullptr)
  57. {
  58. }
  59. ArmatureAnimation::~ArmatureAnimation(void)
  60. {
  61. CC_SAFE_RELEASE_NULL(_animationData);
  62. CC_SAFE_RELEASE_NULL(_userObject);
  63. }
  64. bool ArmatureAnimation::init(Armature *armature)
  65. {
  66. bool bRet = false;
  67. do
  68. {
  69. _armature = armature;
  70. _tweenList.clear();
  71. bRet = true;
  72. }
  73. while (0);
  74. return bRet;
  75. }
  76. void ArmatureAnimation::pause()
  77. {
  78. for (const auto& tween : _tweenList)
  79. {
  80. tween->pause();
  81. }
  82. ProcessBase::pause();
  83. }
  84. void ArmatureAnimation::resume()
  85. {
  86. for (const auto& tween : _tweenList)
  87. {
  88. tween->resume();
  89. }
  90. ProcessBase::resume();
  91. }
  92. void ArmatureAnimation::stop()
  93. {
  94. for (const auto& tween : _tweenList)
  95. {
  96. tween->stop();
  97. }
  98. _tweenList.clear();
  99. ProcessBase::stop();
  100. }
  101. void ArmatureAnimation::setAnimationScale(float animationScale )
  102. {
  103. setSpeedScale(animationScale);
  104. }
  105. float ArmatureAnimation::getAnimationScale() const
  106. {
  107. return getSpeedScale();
  108. }
  109. void ArmatureAnimation::setSpeedScale(float speedScale)
  110. {
  111. if(speedScale == _speedScale)
  112. {
  113. return;
  114. }
  115. _speedScale = speedScale;
  116. _processScale = !_movementData ? _speedScale : _speedScale * _movementData->scale;
  117. const Map<std::string, Bone*>& map = _armature->getBoneDic();
  118. for(auto& element : map)
  119. {
  120. Bone *bone = element.second;
  121. bone->getTween()->setProcessScale(_processScale);
  122. if (bone->getChildArmature())
  123. {
  124. bone->getChildArmature()->getAnimation()->setSpeedScale(_processScale);
  125. }
  126. }
  127. }
  128. float ArmatureAnimation::getSpeedScale() const
  129. {
  130. return _speedScale;
  131. }
  132. void ArmatureAnimation::play(const std::string& animationName, int durationTo, int loop)
  133. {
  134. if (animationName.empty())
  135. {
  136. CCLOG("_animationData can not be null");
  137. return;
  138. }
  139. // CCASSERT(_animationData, "_animationData can not be null");
  140. _movementData = _animationData->getMovement(animationName);
  141. if (nullptr == _movementData)
  142. {
  143. CCLOG("_movementData can not be null");
  144. return;
  145. }
  146. // CCASSERT(_movementData, "_movementData can not be null");
  147. //! Get key frame count
  148. _rawDuration = _movementData->duration;
  149. _movementID = animationName;
  150. _processScale = _speedScale * _movementData->scale;
  151. //! Further processing parameters
  152. durationTo = (durationTo == -1) ? _movementData->durationTo : durationTo;
  153. int durationTween = _movementData->durationTween == 0 ? _rawDuration : _movementData->durationTween;
  154. cocos2d::tweenfunc::TweenType tweenEasing = _movementData->tweenEasing;
  155. loop = (loop < 0) ? _movementData->loop : loop;
  156. _onMovementList = false;
  157. ProcessBase::play(durationTo, durationTween, loop, tweenEasing);
  158. if (_rawDuration == 0)
  159. {
  160. _loopType = SINGLE_FRAME;
  161. }
  162. else
  163. {
  164. if (loop)
  165. {
  166. _loopType = ANIMATION_TO_LOOP_FRONT;
  167. }
  168. else
  169. {
  170. _loopType = ANIMATION_NO_LOOP;
  171. }
  172. _durationTween = durationTween;
  173. }
  174. MovementBoneData *movementBoneData = nullptr;
  175. _tweenList.clear();
  176. const Map<std::string, Bone*>& map = _armature->getBoneDic();
  177. for(auto& element : map)
  178. {
  179. Bone *bone = element.second;
  180. movementBoneData = static_cast<MovementBoneData *>(_movementData->movBoneDataDic.at(bone->getName()));
  181. Tween *tween = bone->getTween();
  182. if(movementBoneData && movementBoneData->frameList.size() > 0)
  183. {
  184. _tweenList.push_back(tween);
  185. movementBoneData->duration = _movementData->duration;
  186. tween->play(movementBoneData, durationTo, durationTween, loop, tweenEasing);
  187. tween->setProcessScale(_processScale);
  188. if (bone->getChildArmature())
  189. {
  190. bone->getChildArmature()->getAnimation()->setSpeedScale(_processScale);
  191. }
  192. }
  193. else
  194. {
  195. if(!bone->isIgnoreMovementBoneData())
  196. {
  197. //! this bone is not include in this movement, so hide it
  198. bone->getDisplayManager()->changeDisplayWithIndex(-1, false);
  199. tween->stop();
  200. }
  201. }
  202. }
  203. _armature->update(0);
  204. }
  205. void ArmatureAnimation::playByIndex(int animationIndex, int durationTo, int loop)
  206. {
  207. playWithIndex(animationIndex, durationTo, loop);
  208. }
  209. void ArmatureAnimation::playWithIndex(int animationIndex, int durationTo, int loop)
  210. {
  211. std::vector<std::string> &movName = _animationData->movementNames;
  212. CC_ASSERT((animationIndex > -1) && ((unsigned int)animationIndex < movName.size()));
  213. std::string animationName = movName.at(animationIndex);
  214. play(animationName, durationTo, loop);
  215. }
  216. void ArmatureAnimation::playWithNames(const std::vector<std::string>& movementNames, int durationTo, bool loop)
  217. {
  218. _movementList.clear();
  219. _movementListLoop = loop;
  220. _movementListDurationTo = durationTo;
  221. _onMovementList = true;
  222. _movementIndex = 0;
  223. _movementList = movementNames;
  224. updateMovementList();
  225. }
  226. void ArmatureAnimation::playWithIndexes(const std::vector<int>& movementIndexes, int durationTo, bool loop)
  227. {
  228. _movementList.clear();
  229. _movementListLoop = loop;
  230. _movementListDurationTo = durationTo;
  231. _onMovementList = true;
  232. _movementIndex = 0;
  233. std::vector<std::string> &movName = _animationData->movementNames;
  234. for(auto& index : movementIndexes)
  235. {
  236. std::string name = movName.at(index);
  237. _movementList.push_back(name);
  238. }
  239. updateMovementList();
  240. }
  241. void ArmatureAnimation::gotoAndPlay(int frameIndex)
  242. {
  243. if (!_movementData || frameIndex < 0 || frameIndex >= _movementData->duration)
  244. {
  245. CCLOG("Please ensure you have played a movement, and the frameIndex is in the range.");
  246. return;
  247. }
  248. bool ignoreFrameEvent = _ignoreFrameEvent;
  249. _ignoreFrameEvent = true;
  250. _isPlaying = true;
  251. _isComplete = _isPause = false;
  252. ProcessBase::gotoFrame(frameIndex);
  253. _currentPercent = (float)_curFrameIndex / ((float)_movementData->duration-1);
  254. _currentFrame = _nextFrameIndex * _currentPercent;
  255. for (const auto &tween : _tweenList)
  256. {
  257. tween->gotoAndPlay(frameIndex);
  258. }
  259. _armature->update(0);
  260. _ignoreFrameEvent = ignoreFrameEvent;
  261. }
  262. void ArmatureAnimation::gotoAndPause(int frameIndex)
  263. {
  264. gotoAndPlay(frameIndex);
  265. pause();
  266. }
  267. ssize_t ArmatureAnimation::getMovementCount() const
  268. {
  269. return _animationData->getMovementCount();
  270. }
  271. void ArmatureAnimation::update(float dt)
  272. {
  273. ProcessBase::update(dt);
  274. for (const auto &tween : _tweenList)
  275. {
  276. tween->update(dt);
  277. }
  278. if(_frameEventQueue.size() > 0 || _movementEventQueue.size() > 0)
  279. {
  280. _armature->retain();
  281. _armature->autorelease();
  282. }
  283. while (_frameEventQueue.size() > 0)
  284. {
  285. FrameEvent *event = _frameEventQueue.front();
  286. _frameEventQueue.pop();
  287. _ignoreFrameEvent = true;
  288. if(_frameEventTarget)
  289. {
  290. (_frameEventTarget->*_frameEventCallFunc)(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex);
  291. }
  292. if (_frameEventListener)
  293. {
  294. _frameEventListener(event->bone, event->frameEventName, event->originFrameIndex, event->currentFrameIndex);
  295. }
  296. _ignoreFrameEvent = false;
  297. CC_SAFE_DELETE(event);
  298. }
  299. while (_movementEventQueue.size() > 0)
  300. {
  301. MovementEvent *event = _movementEventQueue.front();
  302. _movementEventQueue.pop();
  303. if(_movementEventTarget)
  304. {
  305. (_movementEventTarget->*_movementEventCallFunc)(event->armature, event->movementType, event->movementID);
  306. }
  307. if (_movementEventListener)
  308. {
  309. _movementEventListener(event->armature, event->movementType, event->movementID);
  310. }
  311. CC_SAFE_DELETE(event);
  312. }
  313. }
  314. void ArmatureAnimation::updateHandler()
  315. {
  316. if (_currentPercent >= 1)
  317. {
  318. switch(_loopType)
  319. {
  320. case ANIMATION_NO_LOOP:
  321. {
  322. _loopType = ANIMATION_MAX;
  323. _currentFrame = (_currentPercent - 1) * _nextFrameIndex;
  324. _currentPercent = _currentFrame / _durationTween;
  325. if (_currentPercent >= 1.0f)
  326. {
  327. }
  328. else
  329. {
  330. _nextFrameIndex = _durationTween;
  331. movementEvent(_armature, START, _movementID);
  332. break;
  333. }
  334. }
  335. break;
  336. case ANIMATION_MAX:
  337. case SINGLE_FRAME:
  338. {
  339. _currentPercent = 1;
  340. _isComplete = true;
  341. _isPlaying = false;
  342. movementEvent(_armature, COMPLETE, _movementID);
  343. updateMovementList();
  344. }
  345. break;
  346. case ANIMATION_TO_LOOP_FRONT:
  347. {
  348. _loopType = ANIMATION_LOOP_FRONT;
  349. _currentPercent = fmodf(_currentPercent, 1);
  350. _currentFrame = _nextFrameIndex == 0 ? 0 : fmodf(_currentFrame, _nextFrameIndex);
  351. _nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
  352. movementEvent(_armature, START, _movementID);
  353. }
  354. break;
  355. default:
  356. {
  357. //_currentPercent = fmodf(_currentPercent, 1);
  358. _currentFrame = fmodf(_currentFrame, _nextFrameIndex);
  359. _toIndex = 0;
  360. movementEvent(_armature, LOOP_COMPLETE, _movementID);
  361. }
  362. break;
  363. }
  364. }
  365. }
  366. std::string ArmatureAnimation::getCurrentMovementID() const
  367. {
  368. if (_isComplete)
  369. {
  370. return "";
  371. }
  372. return _movementID;
  373. }
  374. void ArmatureAnimation::setMovementEventCallFunc(Ref *target, SEL_MovementEventCallFunc callFunc)
  375. {
  376. _movementEventTarget = target;
  377. _movementEventCallFunc = callFunc;
  378. }
  379. void ArmatureAnimation::setFrameEventCallFunc(Ref *target, SEL_FrameEventCallFunc callFunc)
  380. {
  381. _frameEventTarget = target;
  382. _frameEventCallFunc = callFunc;
  383. }
  384. void ArmatureAnimation::setMovementEventCallFunc(std::function<void(Armature *armature, MovementEventType movementType, const std::string& movementID)> listener)
  385. {
  386. _movementEventListener = listener;
  387. }
  388. void ArmatureAnimation::setFrameEventCallFunc(std::function<void(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex)> listener)
  389. {
  390. _frameEventListener = listener;
  391. }
  392. void ArmatureAnimation::setUserObject(Ref *pUserObject)
  393. {
  394. CC_SAFE_RETAIN(pUserObject);
  395. CC_SAFE_RELEASE(_userObject);
  396. _userObject = pUserObject;
  397. }
  398. void ArmatureAnimation::frameEvent(Bone *bone, const std::string& frameEventName, int originFrameIndex, int currentFrameIndex)
  399. {
  400. if ((_frameEventTarget && _frameEventCallFunc) || _frameEventListener)
  401. {
  402. FrameEvent *frameEvent = new (std::nothrow) FrameEvent();
  403. frameEvent->bone = bone;
  404. frameEvent->frameEventName = frameEventName;
  405. frameEvent->originFrameIndex = originFrameIndex;
  406. frameEvent->currentFrameIndex = currentFrameIndex;
  407. _frameEventQueue.push(frameEvent);
  408. }
  409. }
  410. void ArmatureAnimation::movementEvent(Armature *armature, MovementEventType movementType, const std::string& movementID)
  411. {
  412. if ((_movementEventTarget && _movementEventCallFunc) || _movementEventListener)
  413. {
  414. MovementEvent *movementEvent = new (std::nothrow) MovementEvent();
  415. movementEvent->armature = armature;
  416. movementEvent->movementType = movementType;
  417. movementEvent->movementID = movementID;
  418. _movementEventQueue.push(movementEvent);
  419. }
  420. }
  421. void ArmatureAnimation::updateMovementList()
  422. {
  423. if (_onMovementList)
  424. {
  425. if (_movementListLoop)
  426. {
  427. play(_movementList.at(_movementIndex), _movementListDurationTo, 0);
  428. _movementIndex++;
  429. if (_movementIndex >= _movementList.size())
  430. {
  431. _movementIndex = 0;
  432. }
  433. }
  434. else
  435. {
  436. if (_movementIndex < _movementList.size())
  437. {
  438. play(_movementList.at(_movementIndex), _movementListDurationTo, 0);
  439. _movementIndex++;
  440. }
  441. else
  442. {
  443. _onMovementList = false;
  444. }
  445. }
  446. _onMovementList = true;
  447. }
  448. }
  449. }