CCBAnimationManager.cpp 35 KB

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