CCBAnimationManager.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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/cocosbuilder/CCBAnimationManager.h"
  21. #include "editor-support/cocosbuilder/CCBReader.h"
  22. #include "editor-support/cocosbuilder/CCNode+CCBRelativePositioning.h"
  23. #include "audio/include/SimpleAudioEngine.h"
  24. #include "editor-support/cocosbuilder/CCBSelectorResolver.h"
  25. #include <string>
  26. #include <sstream>
  27. #include <set>
  28. using namespace cocos2d;
  29. using namespace std;
  30. using namespace cocos2d::extension;
  31. namespace cocosbuilder {
  32. // Implementation of CCBAinmationManager
  33. CCBAnimationManager::CCBAnimationManager()
  34. : _jsControlled(false)
  35. , _owner(nullptr)
  36. , _autoPlaySequenceId(0)
  37. , _rootNode(nullptr)
  38. , _rootContainerSize(Size::ZERO)
  39. , _delegate(nullptr)
  40. , _runningSequence(nullptr)
  41. {
  42. init();
  43. }
  44. bool CCBAnimationManager::init()
  45. {
  46. _target = nullptr;
  47. _animationCompleteCallbackFunc = nullptr;
  48. return true;
  49. }
  50. CCBAnimationManager::~CCBAnimationManager()
  51. {
  52. // DictElement *pElement = nullptr;
  53. // CCDICT_FOREACH(_nodeSequences, pElement)
  54. // {
  55. // Node *node = (Node*)pElement->getIntKey();
  56. // node->release();
  57. // }
  58. //
  59. // CCDICT_FOREACH(_baseValues, pElement)
  60. // {
  61. // Node *node = (Node*)pElement->getIntKey();
  62. // node->release();
  63. // }
  64. if (_rootNode)
  65. {
  66. _rootNode->stopAllActions();
  67. }
  68. setRootNode(nullptr);
  69. setDelegate(nullptr);
  70. for (auto iter = _objects.begin(); iter != _objects.end(); ++iter)
  71. {
  72. for (auto iter2 = iter->second.begin(); iter2 != iter->second.end(); ++iter2)
  73. {
  74. iter2->second->release();
  75. }
  76. }
  77. CC_SAFE_RELEASE(_target);
  78. }
  79. Vector<CCBSequence*>& CCBAnimationManager::getSequences()
  80. {
  81. return _sequences;
  82. }
  83. void CCBAnimationManager::setSequences(const Vector<CCBSequence*>& seq)
  84. {
  85. _sequences = seq;
  86. }
  87. int CCBAnimationManager::getAutoPlaySequenceId()
  88. {
  89. return _autoPlaySequenceId;
  90. }
  91. void CCBAnimationManager::setAutoPlaySequenceId(int autoPlaySequenceId)
  92. {
  93. _autoPlaySequenceId = autoPlaySequenceId;
  94. }
  95. Node* CCBAnimationManager::getRootNode()
  96. {
  97. return _rootNode;
  98. }
  99. void CCBAnimationManager::setRootNode(Node *pRootNode)
  100. {
  101. _rootNode = pRootNode;
  102. }
  103. void CCBAnimationManager::setDocumentControllerName(const std::string &name)
  104. {
  105. _documentControllerName = name;
  106. }
  107. std::string CCBAnimationManager::getDocumentControllerName()
  108. {
  109. return _documentControllerName;
  110. }
  111. void CCBAnimationManager::addDocumentCallbackNode(Node *node)
  112. {
  113. _documentCallbackNodes.pushBack(node);
  114. }
  115. void CCBAnimationManager::addDocumentCallbackName(std::string name)
  116. {
  117. _documentCallbackNames.push_back(Value(name));
  118. }
  119. void CCBAnimationManager::addDocumentCallbackControlEvents(Control::EventType eventType)
  120. {
  121. _documentCallbackControlEvents.push_back(Value(static_cast<int>(eventType)));
  122. }
  123. ValueVector& CCBAnimationManager::getDocumentCallbackNames()
  124. {
  125. return _documentCallbackNames;
  126. }
  127. Vector<Node*>& CCBAnimationManager::getDocumentCallbackNodes()
  128. {
  129. return _documentCallbackNodes;
  130. }
  131. ValueVector& CCBAnimationManager::getDocumentCallbackControlEvents()
  132. {
  133. return _documentCallbackControlEvents;
  134. }
  135. void CCBAnimationManager::addDocumentOutletNode(Node *node)
  136. {
  137. _documentOutletNodes.pushBack(node);
  138. }
  139. void CCBAnimationManager::addDocumentOutletName(std::string name)
  140. {
  141. _documentOutletNames.push_back(Value(name));
  142. }
  143. ValueVector& CCBAnimationManager::getDocumentOutletNames()
  144. {
  145. return _documentOutletNames;
  146. }
  147. Vector<Node*>& CCBAnimationManager::getDocumentOutletNodes()
  148. {
  149. return _documentOutletNodes;
  150. }
  151. std::string CCBAnimationManager::getLastCompletedSequenceName()
  152. {
  153. return _lastCompletedSequenceName;
  154. }
  155. ValueVector& CCBAnimationManager::getKeyframeCallbacks()
  156. {
  157. return _keyframeCallbacks;
  158. }
  159. const Size& CCBAnimationManager::getRootContainerSize()
  160. {
  161. return _rootContainerSize;
  162. }
  163. void CCBAnimationManager::setRootContainerSize(const Size &rootContainerSize)
  164. {
  165. _rootContainerSize.setSize(rootContainerSize.width, rootContainerSize.height);
  166. }
  167. CCBAnimationManagerDelegate* CCBAnimationManager::getDelegate()
  168. {
  169. return _delegate;
  170. }
  171. void CCBAnimationManager::setDelegate(CCBAnimationManagerDelegate *pDelegate)
  172. {
  173. CC_SAFE_RELEASE(dynamic_cast<Ref*>(_delegate));
  174. _delegate = pDelegate;
  175. CC_SAFE_RETAIN(dynamic_cast<Ref*>(_delegate));
  176. }
  177. const char* CCBAnimationManager::getRunningSequenceName()
  178. {
  179. if (_runningSequence)
  180. {
  181. return _runningSequence->getName();
  182. }
  183. return nullptr;
  184. }
  185. const Size& CCBAnimationManager::getContainerSize(Node *pNode)
  186. {
  187. if (pNode)
  188. {
  189. return pNode->getContentSize();
  190. }
  191. else
  192. {
  193. return _rootContainerSize;
  194. }
  195. }
  196. // refer to CCBReader::readNodeGraph() for data structure of pSeq
  197. void CCBAnimationManager::addNode(Node *pNode, const std::unordered_map<int, Map<std::string, CCBSequenceProperty*>>& seq)
  198. {
  199. // pNode->retain();
  200. _nodeSequences[pNode] = seq;
  201. }
  202. void CCBAnimationManager::setBaseValue(const Value& value, Node *pNode, const std::string& propName)
  203. {
  204. auto& props = _baseValues[pNode];
  205. props[propName] = value;
  206. }
  207. const Value& CCBAnimationManager::getBaseValue(Node *pNode, const std::string& propName)
  208. {
  209. auto& props = _baseValues[pNode];
  210. return props[propName];
  211. }
  212. void CCBAnimationManager::setObject(Ref* obj, Node *pNode, const std::string& propName)
  213. {
  214. auto& props = _objects[pNode];
  215. auto iter = props.find(propName);
  216. if (iter != props.end())
  217. iter->second->release();
  218. props[propName] = obj;
  219. obj->retain();
  220. }
  221. Ref* CCBAnimationManager::getObject(Node *pNode, const std::string& propName)
  222. {
  223. auto& props = _objects[pNode];
  224. auto iter = props.find(propName);
  225. if (iter != props.end())
  226. return iter->second;
  227. return nullptr;
  228. }
  229. int CCBAnimationManager::getSequenceId(const char* pSequenceName)
  230. {
  231. string seqName(pSequenceName);
  232. for (auto& seq : _sequences)
  233. {
  234. if (seqName.compare(seq->getName()) == 0)
  235. {
  236. return seq->getSequenceId();
  237. }
  238. }
  239. return -1;
  240. }
  241. CCBSequence* CCBAnimationManager::getSequence(int nSequenceId)
  242. {
  243. for (auto& seq : _sequences)
  244. {
  245. if (seq->getSequenceId() == nSequenceId)
  246. {
  247. return seq;
  248. }
  249. }
  250. return nullptr;
  251. }
  252. float CCBAnimationManager::getSequenceDuration(const char *pSequenceName)
  253. {
  254. int id = getSequenceId(pSequenceName);
  255. if (id != -1)
  256. return getSequence(id)->getDuration();
  257. return 0;
  258. }
  259. void CCBAnimationManager::moveAnimationsFromNode(Node* fromNode, Node* toNode)
  260. {
  261. // Move base values
  262. auto baseValueIter = _baseValues.find(fromNode);
  263. if(baseValueIter != _baseValues.end())
  264. {
  265. _baseValues[toNode] = baseValueIter->second;
  266. _baseValues.erase(baseValueIter);
  267. // fromNode->release();
  268. // toNode->retain();
  269. }
  270. auto objIter = _objects.find(fromNode);
  271. if (objIter != _objects.end())
  272. {
  273. _objects[toNode] = objIter->second;
  274. _objects.erase(objIter);
  275. }
  276. // Move seqs
  277. auto seqsIter = _nodeSequences.find(fromNode);
  278. if (seqsIter != _nodeSequences.end())
  279. {
  280. _nodeSequences[toNode] = seqsIter->second;
  281. _nodeSequences.erase(seqsIter);
  282. // fromNode->release();
  283. // toNode->retain();
  284. }
  285. }
  286. // Refer to CCBReader::readKeyframe() for the real type of value
  287. ActionInterval* CCBAnimationManager::getAction(CCBKeyframe *pKeyframe0, CCBKeyframe *pKeyframe1, const std::string& propName, Node *pNode)
  288. {
  289. float duration = pKeyframe1->getTime() - (pKeyframe0 ? pKeyframe0->getTime() : 0);
  290. if (propName == "rotationX")
  291. {
  292. return CCBRotateXTo::create(duration, pKeyframe1->getValue().asFloat());
  293. }
  294. else if (propName == "rotationY")
  295. {
  296. return CCBRotateYTo::create(duration, pKeyframe1->getValue().asFloat());
  297. }
  298. else if (propName == "rotation")
  299. {
  300. return CCBRotateTo::create(duration, pKeyframe1->getValue().asFloat());
  301. }
  302. else if (propName == "opacity")
  303. {
  304. return FadeTo::create(duration, pKeyframe1->getValue().asByte());
  305. }
  306. else if (propName == "color")
  307. {
  308. auto c = pKeyframe1->getValue().asValueMap();
  309. unsigned char r = c["r"].asByte();
  310. unsigned char g = c["g"].asByte();
  311. unsigned char b = c["b"].asByte();
  312. return TintTo::create(duration, r, g, b);
  313. }
  314. else if (propName == "visible")
  315. {
  316. if (pKeyframe1->getValue().asBool())
  317. {
  318. return Sequence::createWithTwoActions(DelayTime::create(duration), Show::create());
  319. }
  320. else
  321. {
  322. return Sequence::createWithTwoActions(DelayTime::create(duration), Hide::create());
  323. }
  324. }
  325. else if (propName == "displayFrame")
  326. {
  327. return Sequence::createWithTwoActions(DelayTime::create(duration),
  328. CCBSetSpriteFrame::create(static_cast<SpriteFrame*>(pKeyframe1->getObject())));
  329. }
  330. else if (propName == "position")
  331. {
  332. // Get position type
  333. auto& array = getBaseValue(pNode, propName).asValueVector();
  334. CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
  335. // Get relative position
  336. auto value = pKeyframe1->getValue().asValueVector();
  337. float x = value[0].asFloat();
  338. float y = value[1].asFloat();
  339. Size containerSize = getContainerSize(pNode->getParent());
  340. Vec2 absPos = getAbsolutePosition(Vec2(x,y), type, containerSize, propName);
  341. return MoveTo::create(duration, absPos);
  342. }
  343. else if (propName == "scale")
  344. {
  345. // Get position type
  346. auto& array = getBaseValue(pNode, propName).asValueVector();
  347. CCBReader::ScaleType type = (CCBReader::ScaleType)array[2].asInt();
  348. // Get relative scale
  349. auto value = pKeyframe1->getValue().asValueVector();
  350. float x = value[0].asFloat();
  351. float y = value[1].asFloat();
  352. if (type == CCBReader::ScaleType::MULTIPLY_RESOLUTION)
  353. {
  354. float resolutionScale = CCBReader::getResolutionScale();
  355. x *= resolutionScale;
  356. y *= resolutionScale;
  357. }
  358. return ScaleTo::create(duration, x, y);
  359. }
  360. else if (propName == "skew")
  361. {
  362. // Get relative skew
  363. auto& value = pKeyframe1->getValue().asValueVector();
  364. float x = value[0].asFloat();
  365. float y = value[1].asFloat();
  366. return SkewTo::create(duration, x, y);
  367. }
  368. else
  369. {
  370. log("CCBReader: Failed to create animation for property: %s", propName.c_str());
  371. }
  372. return nullptr;
  373. }
  374. void CCBAnimationManager::setAnimatedProperty(const std::string& propName, Node *pNode, const Value& value, Ref* obj, float fTweenDuration)
  375. {
  376. if (fTweenDuration > 0)
  377. {
  378. // Create a fake keyframe to generate the action from
  379. CCBKeyframe *kf1 = new (std::nothrow) CCBKeyframe();
  380. kf1->autorelease();
  381. kf1->setObject(obj);
  382. kf1->setValue(value);
  383. kf1->setTime(fTweenDuration);
  384. kf1->setEasingType(CCBKeyframe::EasingType::LINEAR);
  385. // Animate
  386. ActionInterval *tweenAction = getAction(nullptr, kf1, propName, pNode);
  387. pNode->runAction(tweenAction);
  388. }
  389. else
  390. {
  391. // Just set the value
  392. if (propName == "position")
  393. {
  394. // Get position type
  395. auto& array = getBaseValue(pNode, propName).asValueVector();
  396. CCBReader::PositionType type = (CCBReader::PositionType)array[2].asInt();
  397. // Get relative position
  398. auto& valueVector = value.asValueVector();
  399. float x = valueVector[0].asFloat();
  400. float y = valueVector[1].asFloat();
  401. pNode->setPosition(getAbsolutePosition(Vec2(x,y), type, getContainerSize(pNode->getParent()), propName));
  402. }
  403. else if (propName == "scale")
  404. {
  405. // Get scale type
  406. auto& array = getBaseValue(pNode, propName).asValueVector();
  407. CCBReader::ScaleType type = (CCBReader::ScaleType)array[2].asInt();
  408. // Get relative scale
  409. auto& valueVector = value.asValueVector();
  410. float x = valueVector[0].asFloat();
  411. float y = valueVector[1].asFloat();
  412. setRelativeScale(pNode, x, y, type, propName);
  413. }
  414. else if(propName == "skew")
  415. {
  416. // Get relative scale
  417. auto& valueVector = value.asValueVector();
  418. float x = valueVector[0].asFloat();
  419. float y = valueVector[1].asFloat();
  420. pNode->setSkewX(x);
  421. pNode->setSkewY(y);
  422. }
  423. else
  424. {
  425. // [node setValue:value forKey:name];
  426. // TODO: only handle rotation, opacity, displayFrame, color
  427. if (propName == "rotation")
  428. {
  429. float rotate = value.asFloat();
  430. pNode->setRotation(rotate);
  431. } else if(propName == "rotationX")
  432. {
  433. float rotate = value.asFloat();
  434. pNode->setRotationSkewX(rotate);
  435. }else if(propName == "rotationY")
  436. {
  437. float rotate = value.asFloat();
  438. pNode->setRotationSkewY(rotate);
  439. }
  440. else if (propName == "opacity")
  441. {
  442. unsigned char opacity = value.asByte();
  443. pNode->setOpacity(opacity);
  444. }
  445. else if (propName == "displayFrame")
  446. {
  447. static_cast<Sprite*>(pNode)->setSpriteFrame(static_cast<SpriteFrame*>(obj));
  448. }
  449. else if (propName == "color")
  450. {
  451. auto c = value.asValueMap();
  452. unsigned char r = c["r"].asByte();
  453. unsigned char g = c["g"].asByte();
  454. unsigned char b = c["b"].asByte();
  455. pNode->setColor(Color3B(r, g, b));
  456. }
  457. else if (propName == "visible")
  458. {
  459. bool visible = value.asBool();
  460. pNode->setVisible(visible);
  461. }
  462. else
  463. {
  464. log("unsupported property name is %s", propName.c_str());
  465. CCASSERT(false, "unsupported property now");
  466. }
  467. }
  468. }
  469. }
  470. void CCBAnimationManager::setFirstFrame(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
  471. {
  472. auto& keyframes = pSeqProp->getKeyframes();
  473. if (keyframes.empty())
  474. {
  475. // Use base value (no animation)
  476. auto& baseValue = getBaseValue(pNode, pSeqProp->getName());
  477. auto obj = getObject(pNode, pSeqProp->getName());
  478. CCASSERT(!baseValue.isNull(), "No baseValue found for property");
  479. setAnimatedProperty(pSeqProp->getName(), pNode, baseValue, obj, fTweenDuration);
  480. }
  481. else
  482. {
  483. // Use first keyframe
  484. CCBKeyframe *keyframe = keyframes.at(0);
  485. setAnimatedProperty(pSeqProp->getName(), pNode, keyframe->getValue(), keyframe->getObject(), fTweenDuration);
  486. }
  487. }
  488. ActionInterval* CCBAnimationManager::getEaseAction(ActionInterval *pAction, CCBKeyframe::EasingType easingType, float fEasingOpt)
  489. {
  490. if (dynamic_cast<Sequence*>(pAction))
  491. {
  492. return pAction;
  493. }
  494. if (easingType == CCBKeyframe::EasingType::LINEAR)
  495. {
  496. return pAction;
  497. }
  498. else if (easingType == CCBKeyframe::EasingType::INSTANT)
  499. {
  500. return CCBEaseInstant::create(pAction);
  501. }
  502. else if (easingType == CCBKeyframe::EasingType::CUBIC_IN)
  503. {
  504. return EaseIn::create(pAction, fEasingOpt);
  505. }
  506. else if (easingType == CCBKeyframe::EasingType::CUBIC_OUT)
  507. {
  508. return EaseOut::create(pAction, fEasingOpt);
  509. }
  510. else if (easingType == CCBKeyframe::EasingType::CUBIC_INOUT)
  511. {
  512. return EaseInOut::create(pAction, fEasingOpt);
  513. }
  514. else if (easingType == CCBKeyframe::EasingType::BACK_IN)
  515. {
  516. return EaseBackIn::create(pAction);
  517. }
  518. else if (easingType == CCBKeyframe::EasingType::BACK_OUT)
  519. {
  520. return EaseBackOut::create(pAction);
  521. }
  522. else if (easingType == CCBKeyframe::EasingType::BACK_INOUT)
  523. {
  524. return EaseBackInOut::create(pAction);
  525. }
  526. else if (easingType == CCBKeyframe::EasingType::BOUNCE_IN)
  527. {
  528. return EaseBounceIn::create(pAction);
  529. }
  530. else if (easingType == CCBKeyframe::EasingType::BOUNCE_OUT)
  531. {
  532. return EaseBounceOut::create(pAction);
  533. }
  534. else if (easingType == CCBKeyframe::EasingType::BOUNCE_INOUT)
  535. {
  536. return EaseBounceInOut::create(pAction);
  537. }
  538. else if (easingType == CCBKeyframe::EasingType::ELASTIC_IN)
  539. {
  540. return EaseElasticIn::create(pAction, fEasingOpt);
  541. }
  542. else if (easingType == CCBKeyframe::EasingType::ELASTIC_OUT)
  543. {
  544. return EaseElasticOut::create(pAction, fEasingOpt);
  545. }
  546. else if (easingType == CCBKeyframe::EasingType::ELASTIC_INOUT)
  547. {
  548. return EaseElasticInOut::create(pAction, fEasingOpt);
  549. }
  550. else
  551. {
  552. log("CCBReader: Unknown easing type %d", static_cast<int>(easingType));
  553. return pAction;
  554. }
  555. }
  556. Sequence* CCBAnimationManager::actionForCallbackChannel(CCBSequenceProperty* channel) {
  557. float lastKeyframeTime = 0;
  558. Vector<FiniteTimeAction*> actions;
  559. auto& keyframes = channel->getKeyframes();
  560. ssize_t numKeyframes = keyframes.size();
  561. for (long i = 0; i < numKeyframes; ++i)
  562. {
  563. CCBKeyframe *keyframe = keyframes.at(i);
  564. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  565. lastKeyframeTime = keyframe->getTime();
  566. if(timeSinceLastKeyframe > 0) {
  567. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  568. }
  569. auto& keyVal = keyframe->getValue().asValueVector();
  570. std::string selectorName = keyVal[0].asString();
  571. CCBReader::TargetType selectorTarget = (CCBReader::TargetType)keyVal[1].asInt();
  572. if(_jsControlled) {
  573. std::stringstream callbackName;
  574. callbackName << static_cast<int>(selectorTarget);
  575. callbackName << ":" + selectorName;
  576. auto callback = _keyframeCallFuncs.at(callbackName.str());
  577. if (nullptr != callback)
  578. {
  579. CallFunc* callbackClone = callback->clone();
  580. if (callbackClone != nullptr)
  581. {
  582. actions.pushBack(callbackClone);
  583. }
  584. }
  585. }
  586. else
  587. {
  588. Ref* target = nullptr;
  589. if(selectorTarget == CCBReader::TargetType::DOCUMENT_ROOT)
  590. target = _rootNode;
  591. else if (selectorTarget == CCBReader::TargetType::OWNER)
  592. target = _owner;
  593. if(target != nullptr)
  594. {
  595. if(!selectorName.empty())
  596. {
  597. SEL_CallFuncN selCallFunc = 0;
  598. CCBSelectorResolver* targetAsCCBSelectorResolver = dynamic_cast<CCBSelectorResolver *>(target);
  599. if(targetAsCCBSelectorResolver != nullptr)
  600. {
  601. selCallFunc = targetAsCCBSelectorResolver->onResolveCCBCCCallFuncSelector(target, selectorName.c_str ());
  602. }
  603. if(selCallFunc == 0)
  604. {
  605. CCLOG("Skipping selector '%s' since no CCBSelectorResolver is present.", selectorName.c_str());
  606. }
  607. else
  608. {
  609. auto savedTarget = std::make_shared<Vector<Ref*>>();
  610. savedTarget->pushBack(target);
  611. auto callback = CallFuncN::create([savedTarget, selCallFunc](Node* sender){
  612. auto t = savedTarget->at(0);
  613. (t->*selCallFunc)(sender);
  614. });
  615. actions.pushBack(callback);
  616. }
  617. }
  618. else
  619. {
  620. CCLOG("Unexpected empty selector.");
  621. }
  622. }
  623. }
  624. }
  625. if(actions.size() < 1) return nullptr;
  626. return Sequence::create(actions);
  627. }
  628. Sequence* CCBAnimationManager::actionForSoundChannel(CCBSequenceProperty* channel) {
  629. float lastKeyframeTime = 0;
  630. Vector<FiniteTimeAction*> actions;
  631. auto& keyframes = channel->getKeyframes();
  632. ssize_t numKeyframes = keyframes.size();
  633. for (int i = 0; i < numKeyframes; ++i)
  634. {
  635. CCBKeyframe *keyframe = keyframes.at(i);
  636. float timeSinceLastKeyframe = keyframe->getTime() - lastKeyframeTime;
  637. lastKeyframeTime = keyframe->getTime();
  638. if(timeSinceLastKeyframe > 0) {
  639. actions.pushBack(DelayTime::create(timeSinceLastKeyframe));
  640. }
  641. stringstream ss (stringstream::in | stringstream::out);
  642. auto& keyVal = keyframe->getValue().asValueVector();
  643. std::string soundFile = keyVal[0].asString();
  644. float pitch = 0.0f, pan = 0.0f, gain = 0.0f;
  645. ss << keyVal[1].asString();
  646. ss >> pitch;
  647. ss.flush();
  648. ss << keyVal[2].asString();
  649. ss >> pan;
  650. ss.flush();
  651. ss << keyVal[3].asString();
  652. ss >> gain;
  653. ss.flush();
  654. actions.pushBack(CCBSoundEffect::actionWithSoundFile(soundFile, pitch, pan, gain));
  655. }
  656. if(actions.size() < 1) return nullptr;
  657. return Sequence::create(actions);
  658. }
  659. void CCBAnimationManager::runAction(Node *pNode, CCBSequenceProperty *pSeqProp, float fTweenDuration)
  660. {
  661. auto& keyframes = pSeqProp->getKeyframes();
  662. ssize_t numKeyframes = keyframes.size();
  663. if (numKeyframes > 1)
  664. {
  665. // Make an animation!
  666. Vector<FiniteTimeAction*> actions;
  667. CCBKeyframe *keyframeFirst = keyframes.at(0);
  668. float timeFirst = keyframeFirst->getTime() + fTweenDuration;
  669. if (timeFirst > 0)
  670. {
  671. actions.pushBack(DelayTime::create(timeFirst));
  672. }
  673. for (ssize_t i = 0; i < numKeyframes - 1; ++i)
  674. {
  675. CCBKeyframe *kf0 = keyframes.at(i);
  676. CCBKeyframe *kf1 = keyframes.at(i+1);
  677. ActionInterval *action = getAction(kf0, kf1, pSeqProp->getName(), pNode);
  678. if (action)
  679. {
  680. // Apply easing
  681. action = getEaseAction(action, kf0->getEasingType(), kf0->getEasingOpt());
  682. actions.pushBack(action);
  683. }
  684. }
  685. auto seq = Sequence::create(actions);
  686. pNode->runAction(seq);
  687. }
  688. }
  689. void CCBAnimationManager::runAnimations(const char *pName, float fTweenDuration)
  690. {
  691. runAnimationsForSequenceNamedTweenDuration(pName, fTweenDuration);
  692. }
  693. void CCBAnimationManager::runAnimations(const char *pName)
  694. {
  695. runAnimationsForSequenceNamed(pName);
  696. }
  697. void CCBAnimationManager::runAnimations(int nSeqId, float fTweenDuraiton)
  698. {
  699. runAnimationsForSequenceIdTweenDuration(nSeqId, fTweenDuraiton);
  700. }
  701. void CCBAnimationManager::runAnimationsForSequenceIdTweenDuration(int nSeqId, float fTweenDuration)
  702. {
  703. CCASSERT(nSeqId != -1, "Sequence id couldn't be found");
  704. _rootNode->stopAllActions();
  705. for (auto nodeSeqIter = _nodeSequences.begin(); nodeSeqIter != _nodeSequences.end(); ++nodeSeqIter)
  706. {
  707. Node *node = nodeSeqIter->first;
  708. node->stopAllActions();
  709. // Refer to CCBReader::readKeyframe() for the real type of value
  710. auto seqs = nodeSeqIter->second;
  711. auto seqNodeProps = seqs[nSeqId];
  712. std::set<std::string> seqNodePropNames;
  713. if (!seqNodeProps.empty())
  714. {
  715. // Reset nodes that have sequence node properties, and run actions on them
  716. for (auto iter = seqNodeProps.begin(); iter != seqNodeProps.end(); ++iter)
  717. {
  718. const std::string propName = iter->first;
  719. CCBSequenceProperty *seqProp = iter->second;
  720. seqNodePropNames.insert(propName);
  721. setFirstFrame(node, seqProp, fTweenDuration);
  722. runAction(node, seqProp, fTweenDuration);
  723. }
  724. }
  725. // Reset the nodes that may have been changed by other timelines
  726. auto& nodeBaseValues = _baseValues[node];
  727. if (!nodeBaseValues.empty())
  728. {
  729. for (auto iter = nodeBaseValues.begin(); iter != nodeBaseValues.end(); ++iter)
  730. {
  731. if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
  732. {
  733. setAnimatedProperty(iter->first, node, iter->second, nullptr, fTweenDuration);
  734. }
  735. }
  736. }
  737. auto& nodeObject = _objects[node];
  738. if (!nodeObject.empty())
  739. {
  740. for (auto iter = nodeObject.begin(); iter != nodeObject.end(); ++iter)
  741. {
  742. if (seqNodePropNames.find(iter->first) == seqNodePropNames.end())
  743. {
  744. setAnimatedProperty(iter->first, node, Value(), iter->second, fTweenDuration);
  745. }
  746. }
  747. }
  748. }
  749. // Make callback at end of sequence
  750. CCBSequence *seq = getSequence(nSeqId);
  751. Action *completeAction = Sequence::createWithTwoActions(DelayTime::create(seq->getDuration() + fTweenDuration),
  752. CallFunc::create( CC_CALLBACK_0(CCBAnimationManager::sequenceCompleted,this)));
  753. _rootNode->runAction(completeAction);
  754. // Set the running scene
  755. if(seq->getCallbackChannel() != nullptr) {
  756. Action* action = (Action *)actionForCallbackChannel(seq->getCallbackChannel());
  757. if(action != nullptr) {
  758. _rootNode->runAction(action);
  759. }
  760. }
  761. if(seq->getSoundChannel() != nullptr) {
  762. Action* action = (Action *)actionForSoundChannel(seq->getSoundChannel());
  763. if(action != nullptr) {
  764. _rootNode->runAction(action);
  765. }
  766. }
  767. _runningSequence = getSequence(nSeqId);
  768. }
  769. void CCBAnimationManager::runAnimationsForSequenceNamedTweenDuration(const char *pName, float fTweenDuration)
  770. {
  771. int seqId = getSequenceId(pName);
  772. runAnimationsForSequenceIdTweenDuration(seqId, fTweenDuration);
  773. }
  774. void CCBAnimationManager::runAnimationsForSequenceNamed(const char *pName)
  775. {
  776. runAnimationsForSequenceNamedTweenDuration(pName, 0);
  777. }
  778. void CCBAnimationManager::debug()
  779. {
  780. }
  781. void CCBAnimationManager::setAnimationCompletedCallback(Ref *target, SEL_CallFunc callbackFunc) {
  782. if (target)
  783. {
  784. target->retain();
  785. }
  786. if (_target)
  787. {
  788. _target->release();
  789. }
  790. _target = target;
  791. _animationCompleteCallbackFunc = callbackFunc;
  792. }
  793. void CCBAnimationManager::setCallFunc(CallFunc* callFunc, const std::string &callbackNamed)
  794. {
  795. _keyframeCallFuncs.insert(callbackNamed, callFunc);
  796. }
  797. void CCBAnimationManager::sequenceCompleted()
  798. {
  799. const char *runningSequenceName = _runningSequence->getName();
  800. int nextSeqId = _runningSequence->getChainedSequenceId();
  801. _runningSequence = nullptr;
  802. if(_lastCompletedSequenceName != runningSequenceName) {
  803. _lastCompletedSequenceName = runningSequenceName;
  804. }
  805. if (nextSeqId != -1)
  806. {
  807. runAnimationsForSequenceIdTweenDuration(nextSeqId, 0);
  808. }
  809. if (_delegate)
  810. {
  811. // There may be another runAnimation() call in this delegate method
  812. // which will assign _runningSequence
  813. _delegate->completedAnimationSequenceNamed(runningSequenceName);
  814. }
  815. if (_target && _animationCompleteCallbackFunc) {
  816. (_target->*_animationCompleteCallbackFunc)();
  817. }
  818. }
  819. // Custom actions
  820. /************************************************************
  821. CCBSetSpriteFrame
  822. ************************************************************/
  823. CCBSetSpriteFrame* CCBSetSpriteFrame::create(SpriteFrame *pSpriteFrame)
  824. {
  825. CCBSetSpriteFrame *ret = new (std::nothrow) CCBSetSpriteFrame();
  826. if (ret)
  827. {
  828. if (ret->initWithSpriteFrame(pSpriteFrame))
  829. {
  830. ret->autorelease();
  831. }
  832. else
  833. {
  834. CC_SAFE_DELETE(ret);
  835. }
  836. }
  837. return ret;
  838. }
  839. bool CCBSetSpriteFrame::initWithSpriteFrame(SpriteFrame *pSpriteFrame)
  840. {
  841. _spriteFrame = pSpriteFrame;
  842. CC_SAFE_RETAIN(_spriteFrame);
  843. return true;
  844. }
  845. CCBSetSpriteFrame::~CCBSetSpriteFrame()
  846. {
  847. CC_SAFE_RELEASE_NULL(_spriteFrame);
  848. }
  849. CCBSetSpriteFrame* CCBSetSpriteFrame::clone() const
  850. {
  851. // no copy constructor
  852. auto a = new (std::nothrow) CCBSetSpriteFrame();
  853. a->initWithSpriteFrame(_spriteFrame);
  854. a->autorelease();
  855. return a;
  856. }
  857. CCBSetSpriteFrame* CCBSetSpriteFrame::reverse() const
  858. {
  859. // returns a copy of itself
  860. return this->clone();
  861. }
  862. void CCBSetSpriteFrame::update(float /*time*/)
  863. {
  864. static_cast<Sprite*>(_target)->setSpriteFrame(_spriteFrame);
  865. }
  866. /************************************************************
  867. CCBSoundEffect
  868. ************************************************************/
  869. CCBSoundEffect* CCBSoundEffect::actionWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
  870. CCBSoundEffect* pRet = new (std::nothrow) CCBSoundEffect();
  871. if (pRet != nullptr && pRet->initWithSoundFile(filename, pitch, pan, gain))
  872. {
  873. pRet->autorelease();
  874. }
  875. else
  876. {
  877. CC_SAFE_DELETE(pRet);
  878. }
  879. return pRet;
  880. }
  881. CCBSoundEffect::~CCBSoundEffect()
  882. {
  883. }
  884. bool CCBSoundEffect::initWithSoundFile(const std::string &filename, float pitch, float pan, float gain) {
  885. _soundFile = filename;
  886. _pitch = pitch;
  887. _pan = pan;
  888. _gain = gain;
  889. return true;
  890. }
  891. CCBSoundEffect* CCBSoundEffect::clone() const
  892. {
  893. // no copy constructor
  894. auto a = new (std::nothrow) CCBSoundEffect();
  895. a->initWithSoundFile(_soundFile, _pitch, _pan, _gain);
  896. a->autorelease();
  897. return a;
  898. }
  899. CCBSoundEffect* CCBSoundEffect::reverse() const
  900. {
  901. // returns a copy of itself
  902. return this->clone();
  903. }
  904. void CCBSoundEffect::update(float /*time*/)
  905. {
  906. CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(_soundFile.c_str());
  907. }
  908. /************************************************************
  909. CCBRotateTo
  910. ************************************************************/
  911. CCBRotateTo* CCBRotateTo::create(float fDuration, float fAngle)
  912. {
  913. CCBRotateTo *ret = new (std::nothrow) CCBRotateTo();
  914. if (ret)
  915. {
  916. if (ret->initWithDuration(fDuration, fAngle))
  917. {
  918. ret->autorelease();
  919. }
  920. else
  921. {
  922. CC_SAFE_DELETE(ret);
  923. }
  924. }
  925. return ret;
  926. }
  927. bool CCBRotateTo::initWithDuration(float fDuration, float fAngle)
  928. {
  929. if (ActionInterval::initWithDuration(fDuration))
  930. {
  931. _dstAngle = fAngle;
  932. return true;
  933. }
  934. else
  935. {
  936. return false;
  937. }
  938. }
  939. CCBRotateTo* CCBRotateTo::clone() const
  940. {
  941. // no copy constructor
  942. auto a = new (std::nothrow) CCBRotateTo();
  943. a->initWithDuration(_duration, _dstAngle);
  944. a->autorelease();
  945. return a;
  946. }
  947. CCBRotateTo* CCBRotateTo::reverse() const
  948. {
  949. CCASSERT(false, "reverse() is not supported in CCBRotateTo");
  950. return nullptr;
  951. }
  952. void CCBRotateTo::startWithTarget(Node *pNode)
  953. {
  954. ActionInterval::startWithTarget(pNode);
  955. _startAngle = _target->getRotation();
  956. _diffAngle = _dstAngle - _startAngle;
  957. }
  958. void CCBRotateTo::update(float time)
  959. {
  960. _target->setRotation(_startAngle + (_diffAngle * time))
  961. ;
  962. }
  963. /************************************************************
  964. CCBRotateXTO
  965. ************************************************************/
  966. CCBRotateXTo* CCBRotateXTo::create(float fDuration, float fAngle)
  967. {
  968. CCBRotateXTo *ret = new (std::nothrow) CCBRotateXTo();
  969. if (ret)
  970. {
  971. if (ret->initWithDuration(fDuration, fAngle))
  972. {
  973. ret->autorelease();
  974. }
  975. else
  976. {
  977. CC_SAFE_DELETE(ret);
  978. }
  979. }
  980. return ret;
  981. }
  982. bool CCBRotateXTo::initWithDuration(float fDuration, float fAngle)
  983. {
  984. if (ActionInterval::initWithDuration(fDuration))
  985. {
  986. _dstAngle = fAngle;
  987. return true;
  988. }
  989. else
  990. {
  991. return false;
  992. }
  993. }
  994. void CCBRotateXTo::startWithTarget(Node *pNode)
  995. {
  996. //CCActionInterval::startWithTarget(pNode);
  997. _originalTarget = pNode;
  998. _target = pNode;
  999. _elapsed = 0.0f;
  1000. _firstTick = true;
  1001. _startAngle = _target->getRotationSkewX();
  1002. _diffAngle = _dstAngle - _startAngle;
  1003. }
  1004. CCBRotateXTo* CCBRotateXTo::clone() const
  1005. {
  1006. // no copy constructor
  1007. auto a = new (std::nothrow) CCBRotateXTo();
  1008. a->initWithDuration(_duration, _dstAngle);
  1009. a->autorelease();
  1010. return a;
  1011. }
  1012. CCBRotateXTo* CCBRotateXTo::reverse() const
  1013. {
  1014. CCASSERT(false, "reverse() is not supported in CCBRotateXTo");
  1015. return nullptr;
  1016. }
  1017. void CCBRotateXTo::update(float time)
  1018. {
  1019. _target->setRotationSkewX(_startAngle + (_diffAngle * time));
  1020. }
  1021. /************************************************************
  1022. CCBRotateYTO
  1023. ************************************************************/
  1024. CCBRotateYTo* CCBRotateYTo::create(float fDuration, float fAngle)
  1025. {
  1026. CCBRotateYTo *ret = new (std::nothrow) CCBRotateYTo();
  1027. if (ret)
  1028. {
  1029. if (ret->initWithDuration(fDuration, fAngle))
  1030. {
  1031. ret->autorelease();
  1032. }
  1033. else
  1034. {
  1035. CC_SAFE_DELETE(ret);
  1036. }
  1037. }
  1038. return ret;
  1039. }
  1040. bool CCBRotateYTo::initWithDuration(float fDuration, float fAngle)
  1041. {
  1042. if (ActionInterval::initWithDuration(fDuration))
  1043. {
  1044. _dstAngle = fAngle;
  1045. return true;
  1046. }
  1047. else
  1048. {
  1049. return false;
  1050. }
  1051. }
  1052. CCBRotateYTo* CCBRotateYTo::clone() const
  1053. {
  1054. // no copy constructor
  1055. auto a = new (std::nothrow) CCBRotateYTo();
  1056. a->initWithDuration(_duration, _dstAngle);
  1057. a->autorelease();
  1058. return a;
  1059. }
  1060. CCBRotateYTo* CCBRotateYTo::reverse() const
  1061. {
  1062. CCASSERT(false, "reverse() is not supported in CCBRotateXTo");
  1063. return nullptr;
  1064. }
  1065. void CCBRotateYTo::startWithTarget(Node *pNode)
  1066. {
  1067. // ActionInterval::startWithTarget(pNode);
  1068. _originalTarget = pNode;
  1069. _target = pNode;
  1070. _elapsed = 0.0f;
  1071. _firstTick = true;
  1072. _startAngle = _target->getRotationSkewY();
  1073. _diffAngle = _dstAngle - _startAngle;
  1074. }
  1075. void CCBRotateYTo::update(float time)
  1076. {
  1077. _target->setRotationSkewY(_startAngle + (_diffAngle * time));
  1078. }
  1079. /************************************************************
  1080. CCBEaseInstant
  1081. ************************************************************/
  1082. CCBEaseInstant* CCBEaseInstant::create(ActionInterval *pAction)
  1083. {
  1084. CCBEaseInstant *pRet = new (std::nothrow) CCBEaseInstant();
  1085. if (pRet && pRet->initWithAction(pAction))
  1086. {
  1087. pRet->autorelease();
  1088. }
  1089. else
  1090. {
  1091. CC_SAFE_RELEASE_NULL(pRet);
  1092. }
  1093. return pRet;
  1094. }
  1095. CCBEaseInstant* CCBEaseInstant::clone() const
  1096. {
  1097. // no copy constructor
  1098. auto a = new (std::nothrow) CCBEaseInstant();
  1099. a->initWithAction(_inner);
  1100. a->autorelease();
  1101. return a;
  1102. }
  1103. CCBEaseInstant* CCBEaseInstant::reverse() const
  1104. {
  1105. return CCBEaseInstant::create(_inner->reverse());
  1106. }
  1107. void CCBEaseInstant::update(float dt)
  1108. {
  1109. if (dt < 0)
  1110. {
  1111. _inner->update(0);
  1112. }
  1113. else
  1114. {
  1115. _inner->update(1);
  1116. }
  1117. }
  1118. }