CCTween.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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/CCTween.h"
  21. #include "editor-support/cocostudio/CCArmatureAnimation.h"
  22. #include "editor-support/cocostudio/CCBone.h"
  23. #include "editor-support/cocostudio/CCArmature.h"
  24. #include "editor-support/cocostudio/CCUtilMath.h"
  25. #include "editor-support/cocostudio/CCTransformHelp.h"
  26. namespace cocostudio {
  27. using cocos2d::tweenfunc::Linear;
  28. Tween *Tween::create(Bone *bone)
  29. {
  30. Tween *pTween = new (std::nothrow) Tween();
  31. if (pTween && pTween->init(bone))
  32. {
  33. pTween->autorelease();
  34. return pTween;
  35. }
  36. CC_SAFE_DELETE(pTween);
  37. return nullptr;
  38. }
  39. Tween::Tween()
  40. : _movementBoneData(nullptr)
  41. , _tweenData(nullptr)
  42. , _from(nullptr)
  43. , _to(nullptr)
  44. , _between(nullptr)
  45. , _bone(nullptr)
  46. , _frameTweenEasing(Linear)
  47. , _fromIndex(0)
  48. , _toIndex(0)
  49. , _animation(nullptr)
  50. , _passLastFrame(false)
  51. {
  52. }
  53. Tween::~Tween(void)
  54. {
  55. CC_SAFE_DELETE( _from );
  56. CC_SAFE_DELETE( _between );
  57. }
  58. bool Tween::init(Bone *bone)
  59. {
  60. bool bRet = false;
  61. do
  62. {
  63. _from = new (std::nothrow) FrameData();
  64. _between = new (std::nothrow) FrameData();
  65. _bone = bone;
  66. _tweenData = _bone->getTweenData();
  67. _tweenData->displayIndex = -1;
  68. _animation = _bone->getArmature() != nullptr ? _bone->getArmature()->getAnimation() : nullptr;
  69. bRet = true;
  70. }
  71. while (0);
  72. return bRet;
  73. }
  74. void Tween::play(MovementBoneData *movementBoneData, int durationTo, int durationTween, int loop, int tweenEasing)
  75. {
  76. ProcessBase::play(durationTo, durationTween, loop, tweenEasing);
  77. if (loop)
  78. {
  79. _loopType = ANIMATION_TO_LOOP_FRONT;
  80. }
  81. else
  82. {
  83. _loopType = ANIMATION_NO_LOOP;
  84. }
  85. _totalDuration = 0;
  86. _betweenDuration = 0;
  87. _fromIndex = _toIndex = 0;
  88. bool difMovement = movementBoneData != _movementBoneData;
  89. setMovementBoneData(movementBoneData);
  90. _rawDuration = _movementBoneData->duration;
  91. FrameData *nextKeyFrame = _movementBoneData->getFrameData(0);
  92. _tweenData->displayIndex = nextKeyFrame->displayIndex;
  93. if (_bone->getArmature()->getArmatureData()->dataVersion >= VERSION_COMBINED)
  94. {
  95. TransformHelp::nodeSub(*_tweenData, *_bone->getBoneData());
  96. _tweenData->scaleX += 1;
  97. _tweenData->scaleY += 1;
  98. }
  99. if (_rawDuration == 0 )
  100. {
  101. _loopType = SINGLE_FRAME;
  102. if(durationTo == 0)
  103. {
  104. setBetween(nextKeyFrame, nextKeyFrame);
  105. }
  106. else
  107. {
  108. setBetween(_tweenData, nextKeyFrame);
  109. }
  110. _frameTweenEasing = Linear;
  111. }
  112. else if (_movementBoneData->frameList.size() > 1)
  113. {
  114. _durationTween = durationTween * _movementBoneData->scale;
  115. if (loop && _movementBoneData->delay != 0)
  116. {
  117. setBetween(_tweenData, tweenNodeTo(updateFrameData(1 - _movementBoneData->delay), _between));
  118. }
  119. else
  120. {
  121. if (!difMovement || durationTo == 0)
  122. {
  123. setBetween(nextKeyFrame, nextKeyFrame);
  124. }
  125. else
  126. {
  127. setBetween(_tweenData, nextKeyFrame);
  128. }
  129. }
  130. }
  131. tweenNodeTo(0);
  132. }
  133. void Tween::gotoAndPlay(int frameIndex)
  134. {
  135. ProcessBase::gotoFrame(frameIndex);
  136. _totalDuration = 0;
  137. _betweenDuration = 0;
  138. _fromIndex = _toIndex = 0;
  139. _isPlaying = true;
  140. _isComplete = _isPause = false;
  141. _currentPercent = (float)_curFrameIndex / ((float)_rawDuration-1);
  142. _currentFrame = _nextFrameIndex * _currentPercent;
  143. }
  144. void Tween::gotoAndPause(int frameIndex)
  145. {
  146. gotoAndPlay(frameIndex);
  147. pause();
  148. }
  149. void Tween::updateHandler()
  150. {
  151. if (_currentPercent >= 1)
  152. {
  153. switch(_loopType)
  154. {
  155. case SINGLE_FRAME:
  156. {
  157. _currentPercent = 1;
  158. _isComplete = true;
  159. _isPlaying = false;
  160. }
  161. break;
  162. case ANIMATION_NO_LOOP:
  163. {
  164. _loopType = ANIMATION_MAX;
  165. if (_durationTween <= 0)
  166. {
  167. _currentPercent = 1;
  168. }
  169. else
  170. {
  171. _currentPercent = (_currentPercent - 1) * _nextFrameIndex / _durationTween;
  172. }
  173. if (_currentPercent >= 1)
  174. {
  175. _currentPercent = 1;
  176. _isComplete = true;
  177. _isPlaying = false;
  178. break;
  179. }
  180. else
  181. {
  182. _nextFrameIndex = _durationTween;
  183. _currentFrame = _currentPercent * _nextFrameIndex;
  184. _totalDuration = 0;
  185. _betweenDuration = 0;
  186. _fromIndex = _toIndex = 0;
  187. break;
  188. }
  189. }
  190. break;
  191. case ANIMATION_TO_LOOP_FRONT:
  192. {
  193. _loopType = ANIMATION_LOOP_FRONT;
  194. _nextFrameIndex = _durationTween > 0 ? _durationTween : 1;
  195. if (_movementBoneData->delay != 0)
  196. {
  197. //
  198. _currentFrame = (1 - _movementBoneData->delay) * (float)_nextFrameIndex;
  199. _currentPercent = _currentFrame / _nextFrameIndex;
  200. }
  201. else
  202. {
  203. _currentPercent = 0;
  204. _currentFrame = 0;
  205. }
  206. _totalDuration = 0;
  207. _betweenDuration = 0;
  208. _fromIndex = _toIndex = 0;
  209. }
  210. break;
  211. case ANIMATION_MAX:
  212. {
  213. _currentPercent = 1;
  214. _isComplete = true;
  215. _isPlaying = false;
  216. }
  217. break;
  218. default:
  219. {
  220. _currentFrame = fmodf(_currentFrame, _nextFrameIndex);
  221. }
  222. break;
  223. }
  224. }
  225. if (_currentPercent < 1 && _loopType <= ANIMATION_TO_LOOP_BACK)
  226. {
  227. _currentPercent = sin(_currentPercent * CC_HALF_PI);
  228. }
  229. float percent = _currentPercent;
  230. if (_loopType > ANIMATION_TO_LOOP_BACK)
  231. {
  232. percent = updateFrameData(percent);
  233. }
  234. if(_frameTweenEasing != ::cocos2d::tweenfunc::TWEEN_EASING_MAX)
  235. {
  236. tweenNodeTo(percent);
  237. }
  238. }
  239. void Tween::setBetween(FrameData *from, FrameData *to, bool limit)
  240. {
  241. do
  242. {
  243. if(from->displayIndex < 0 && to->displayIndex >= 0)
  244. {
  245. _from->copy(to);
  246. _between->subtract(to, to, limit);
  247. break;
  248. }
  249. else if(to->displayIndex < 0 && from->displayIndex >= 0)
  250. {
  251. _from->copy(from);
  252. _between->subtract(to, to, limit);
  253. break;
  254. }
  255. _from->copy(from);
  256. _between->subtract(from, to, limit);
  257. }
  258. while (0);
  259. if (!from->isTween)
  260. {
  261. _tweenData->copy(from);
  262. _tweenData->isTween = true;
  263. }
  264. arriveKeyFrame(from);
  265. }
  266. void Tween::arriveKeyFrame(FrameData *keyFrameData)
  267. {
  268. if(keyFrameData)
  269. {
  270. DisplayManager *displayManager = _bone->getDisplayManager();
  271. //! Change bone's display
  272. int displayIndex = keyFrameData->displayIndex;
  273. if (!displayManager->isForceChangeDisplay())
  274. {
  275. displayManager->changeDisplayWithIndex(displayIndex, false);
  276. }
  277. //! Update bone zorder, bone's zorder is determined by frame zorder and bone zorder
  278. _tweenData->zOrder = keyFrameData->zOrder;
  279. _bone->updateZOrder();
  280. //! Update blend type
  281. _bone->setBlendFunc(keyFrameData->blendFunc);
  282. //! Update child armature's movement
  283. Armature *childAramture = _bone->getChildArmature();
  284. if(childAramture)
  285. {
  286. if(!keyFrameData->strMovement.empty())
  287. {
  288. childAramture->getAnimation()->play(keyFrameData->strMovement);
  289. }
  290. }
  291. }
  292. }
  293. FrameData *Tween::tweenNodeTo(float percent, FrameData *node)
  294. {
  295. node = node == nullptr ? _tweenData : node;
  296. if (!_from->isTween)
  297. {
  298. percent = 0;
  299. }
  300. node->x = _from->x + percent * _between->x;
  301. node->y = _from->y + percent * _between->y;
  302. node->scaleX = _from->scaleX + percent * _between->scaleX;
  303. node->scaleY = _from->scaleY + percent * _between->scaleY;
  304. node->skewX = _from->skewX + percent * _between->skewX;
  305. node->skewY = _from->skewY + percent * _between->skewY;
  306. _bone->setTransformDirty(true);
  307. if (node && _between->isUseColorInfo)
  308. {
  309. tweenColorTo(percent, node);
  310. }
  311. return node;
  312. }
  313. void Tween::tweenColorTo(float percent, FrameData *node)
  314. {
  315. node->a = _from->a + percent * _between->a;
  316. node->r = _from->r + percent * _between->r;
  317. node->g = _from->g + percent * _between->g;
  318. node->b = _from->b + percent * _between->b;
  319. _bone->updateColor();
  320. }
  321. float Tween::updateFrameData(float currentPercent)
  322. {
  323. if (currentPercent > 1 && _movementBoneData->delay != 0)
  324. {
  325. currentPercent = fmodf(currentPercent, 1);
  326. }
  327. float playedTime = ((float)_rawDuration-1) * currentPercent;
  328. //! If play to current frame's front or back, then find current frame again
  329. if (playedTime < _totalDuration || playedTime >= _totalDuration + _betweenDuration)
  330. {
  331. /*
  332. * Get frame length, if _toIndex >= _length, then set _toIndex to 0, start anew.
  333. * _toIndex is next index will play
  334. */
  335. long length = _movementBoneData->frameList.size();
  336. cocos2d::Vector<FrameData *> &frames = _movementBoneData->frameList;
  337. FrameData *from = nullptr;
  338. FrameData *to = nullptr;
  339. if (playedTime < frames.at(0)->frameID)
  340. {
  341. from = to = frames.at(0);
  342. setBetween(from, to);
  343. return _currentPercent;
  344. }
  345. if(playedTime >= frames.at(length - 1)->frameID)
  346. {
  347. // If _passLastFrame is true and playedTime >= frames[length - 1]->frameID, then do not need to go on.
  348. if (_passLastFrame)
  349. {
  350. from = to = frames.at(length - 1);
  351. setBetween(from, to);
  352. return _currentPercent;
  353. }
  354. _passLastFrame = true;
  355. }
  356. else
  357. {
  358. _passLastFrame = false;
  359. }
  360. do
  361. {
  362. _fromIndex = _toIndex;
  363. from = frames.at(_fromIndex);
  364. _totalDuration = from->frameID;
  365. _toIndex = _fromIndex + 1;
  366. if (_toIndex >= length)
  367. {
  368. _toIndex = 0;
  369. }
  370. to = frames.at(_toIndex);
  371. //! Guaranteed to trigger frame event
  372. if(!from->strEvent.empty() && !_animation->isIgnoreFrameEvent())
  373. {
  374. _animation->frameEvent(_bone, from->strEvent, from->frameID, playedTime);
  375. }
  376. if (playedTime == from->frameID || (_passLastFrame && _fromIndex == length-1))
  377. {
  378. break;
  379. }
  380. }
  381. while (playedTime < from->frameID || playedTime >= to->frameID);
  382. _betweenDuration = to->frameID - from->frameID;
  383. _frameTweenEasing = from->tweenEasing;
  384. setBetween(from, to, false);
  385. }
  386. currentPercent = _betweenDuration == 0 ? 0 : (playedTime - _totalDuration) / (float)_betweenDuration;
  387. /*
  388. * If frame tween easing equal to TWEEN_EASING_MAX, then it will not do tween.
  389. */
  390. TweenType tweenType = (_frameTweenEasing != Linear) ? _frameTweenEasing : _tweenEasing;
  391. if (tweenType != cocos2d::tweenfunc::TWEEN_EASING_MAX && tweenType != Linear && !_passLastFrame)
  392. {
  393. currentPercent = cocos2d::tweenfunc::tweenTo(currentPercent, tweenType, _from->easingParams);
  394. }
  395. return currentPercent;
  396. }
  397. }