CCBReader.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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 <ctype.h>
  21. #include <algorithm>
  22. #include "base/CCDirector.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "2d/CCScene.h"
  25. #include "2d/CCSpriteFrameCache.h"
  26. #include "renderer/CCTextureCache.h"
  27. #include "editor-support/cocosbuilder/CCBReader.h"
  28. #include "editor-support/cocosbuilder/CCNodeLoader.h"
  29. #include "editor-support/cocosbuilder/CCNodeLoaderLibrary.h"
  30. #include "editor-support/cocosbuilder/CCNodeLoaderListener.h"
  31. #include "editor-support/cocosbuilder/CCBMemberVariableAssigner.h"
  32. #include "editor-support/cocosbuilder/CCBSelectorResolver.h"
  33. #include "editor-support/cocosbuilder/CCBAnimationManager.h"
  34. #include "editor-support/cocosbuilder/CCBSequenceProperty.h"
  35. #include "editor-support/cocosbuilder/CCBKeyframe.h"
  36. #include <sstream>
  37. using namespace cocos2d;
  38. using namespace cocos2d::extension;
  39. namespace cocosbuilder {
  40. /*************************************************************************
  41. Implementation of CCBFile
  42. *************************************************************************/
  43. CCBFile::CCBFile():_CCBFileNode(nullptr) {}
  44. CCBFile* CCBFile::create()
  45. {
  46. CCBFile *ret = new (std::nothrow) CCBFile();
  47. if (ret)
  48. {
  49. ret->autorelease();
  50. }
  51. return ret;
  52. }
  53. Node* CCBFile::getCCBFileNode()
  54. {
  55. return _CCBFileNode;
  56. }
  57. void CCBFile::setCCBFileNode(Node *pNode)
  58. {
  59. CC_SAFE_RELEASE(_CCBFileNode);
  60. _CCBFileNode = pNode;
  61. CC_SAFE_RETAIN(_CCBFileNode);
  62. }
  63. /*************************************************************************
  64. Implementation of CCBReader
  65. *************************************************************************/
  66. CCBReader::CCBReader(NodeLoaderLibrary * pNodeLoaderLibrary, CCBMemberVariableAssigner * pCCBMemberVariableAssigner, CCBSelectorResolver * pCCBSelectorResolver, NodeLoaderListener * pNodeLoaderListener)
  67. : _data(nullptr)
  68. , _bytes(nullptr)
  69. , _currentByte(-1)
  70. , _currentBit(-1)
  71. , _owner(nullptr)
  72. , _animationManager(nullptr)
  73. , _animatedProps(nullptr)
  74. {
  75. this->_nodeLoaderLibrary = pNodeLoaderLibrary;
  76. this->_nodeLoaderLibrary->retain();
  77. this->_CCBMemberVariableAssigner = pCCBMemberVariableAssigner;
  78. this->_CCBSelectorResolver = pCCBSelectorResolver;
  79. this->_nodeLoaderListener = pNodeLoaderListener;
  80. init();
  81. }
  82. CCBReader::CCBReader(CCBReader * ccbReader)
  83. : _data(nullptr)
  84. , _bytes(nullptr)
  85. , _currentByte(-1)
  86. , _currentBit(-1)
  87. , _owner(nullptr)
  88. , _animationManager(nullptr)
  89. , _animatedProps(nullptr)
  90. {
  91. this->_loadedSpriteSheets = ccbReader->_loadedSpriteSheets;
  92. this->_nodeLoaderLibrary = ccbReader->_nodeLoaderLibrary;
  93. this->_nodeLoaderLibrary->retain();
  94. this->_CCBMemberVariableAssigner = ccbReader->_CCBMemberVariableAssigner;
  95. this->_CCBSelectorResolver = ccbReader->_CCBSelectorResolver;
  96. this->_nodeLoaderListener = ccbReader->_nodeLoaderListener;
  97. this->_CCBRootPath = ccbReader->getCCBRootPath();
  98. init();
  99. }
  100. CCBReader::CCBReader()
  101. : _data(nullptr)
  102. , _bytes(nullptr)
  103. , _currentByte(-1)
  104. , _currentBit(-1)
  105. , _owner(nullptr)
  106. , _animationManager(nullptr)
  107. , _nodeLoaderLibrary(nullptr)
  108. , _nodeLoaderListener(nullptr)
  109. , _CCBMemberVariableAssigner(nullptr)
  110. , _CCBSelectorResolver(nullptr)
  111. {
  112. init();
  113. }
  114. CCBReader::~CCBReader()
  115. {
  116. CC_SAFE_RELEASE_NULL(_owner);
  117. this->_nodeLoaderLibrary->release();
  118. _ownerOutletNames.clear();
  119. _ownerCallbackNames.clear();
  120. // Clear string cache.
  121. this->_stringCache.clear();
  122. setAnimationManager(nullptr);
  123. }
  124. void CCBReader::setCCBRootPath(const char* ccbRootPath)
  125. {
  126. CCASSERT(ccbRootPath != nullptr, "ccbRootPath can't be nullptr!");
  127. _CCBRootPath = ccbRootPath;
  128. }
  129. const std::string& CCBReader::getCCBRootPath() const
  130. {
  131. return _CCBRootPath;
  132. }
  133. bool CCBReader::init()
  134. {
  135. // Setup action manager
  136. CCBAnimationManager *pActionManager = new (std::nothrow) CCBAnimationManager();
  137. setAnimationManager(pActionManager);
  138. pActionManager->release();
  139. // Setup resolution scale and container size
  140. _animationManager->setRootContainerSize(Director::getInstance()->getWinSize());
  141. return true;
  142. }
  143. CCBAnimationManager* CCBReader::getAnimationManager()
  144. {
  145. return _animationManager;
  146. }
  147. void CCBReader::setAnimationManager(CCBAnimationManager *pAnimationManager)
  148. {
  149. CC_SAFE_RELEASE(_animationManager);
  150. _animationManager = pAnimationManager;
  151. CC_SAFE_RETAIN(_animationManager);
  152. }
  153. CCBReader::CCBAnimationManagerMapPtr CCBReader::getAnimationManagers()
  154. {
  155. return _animationManagers;
  156. }
  157. void CCBReader::setAnimationManagers(CCBAnimationManagerMapPtr x)
  158. {
  159. _animationManagers = x;
  160. }
  161. CCBMemberVariableAssigner * CCBReader::getCCBMemberVariableAssigner() {
  162. return this->_CCBMemberVariableAssigner;
  163. }
  164. CCBSelectorResolver * CCBReader::getCCBSelectorResolver() {
  165. return this->_CCBSelectorResolver;
  166. }
  167. std::set<std::string>* CCBReader::getAnimatedProperties()
  168. {
  169. return _animatedProps;
  170. }
  171. std::set<std::string>& CCBReader::getLoadedSpriteSheet()
  172. {
  173. return _loadedSpriteSheets;
  174. }
  175. Ref* CCBReader::getOwner()
  176. {
  177. return _owner;
  178. }
  179. Node* CCBReader::readNodeGraphFromFile(const char *pCCBFileName)
  180. {
  181. return this->readNodeGraphFromFile(pCCBFileName, nullptr);
  182. }
  183. Node* CCBReader::readNodeGraphFromFile(const char* pCCBFileName, Ref* pOwner)
  184. {
  185. return this->readNodeGraphFromFile(pCCBFileName, pOwner, Director::getInstance()->getWinSize());
  186. }
  187. Node* CCBReader::readNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner, const Size &parentSize)
  188. {
  189. if (nullptr == pCCBFileName || strlen(pCCBFileName) == 0)
  190. {
  191. return nullptr;
  192. }
  193. std::string strCCBFileName(pCCBFileName);
  194. std::string strSuffix(".ccbi");
  195. // Add ccbi suffix
  196. if (!CCBReader::endsWith(strCCBFileName.c_str(), strSuffix.c_str()))
  197. {
  198. strCCBFileName += strSuffix;
  199. }
  200. std::string strPath = FileUtils::getInstance()->fullPathForFilename(strCCBFileName);
  201. auto dataPtr = std::make_shared<Data>(FileUtils::getInstance()->getDataFromFile(strPath));
  202. Node *ret = this->readNodeGraphFromData(dataPtr, pOwner, parentSize);
  203. return ret;
  204. }
  205. Node* CCBReader::readNodeGraphFromData(std::shared_ptr<cocos2d::Data> data, Ref *pOwner, const Size &parentSize)
  206. {
  207. _data = data;
  208. _bytes =_data->getBytes();
  209. _currentByte = 0;
  210. _currentBit = 0;
  211. _owner = pOwner;
  212. CC_SAFE_RETAIN(_owner);
  213. _animationManager->setRootContainerSize(parentSize);
  214. _animationManager->_owner = _owner;
  215. Node *pNodeGraph = readFileWithCleanUp(true, std::make_shared<CCBAnimationManagerMap>());
  216. if (pNodeGraph && _animationManager->getAutoPlaySequenceId() != -1)
  217. {
  218. // Auto play animations
  219. _animationManager->runAnimationsForSequenceIdTweenDuration(_animationManager->getAutoPlaySequenceId(), 0);
  220. }
  221. // Assign actionManagers to userObject
  222. for (auto iter = _animationManagers->begin(); iter != _animationManagers->end(); ++iter)
  223. {
  224. Node* pNode = iter->first;
  225. CCBAnimationManager* manager = iter->second;
  226. pNode->setUserObject(manager);
  227. if (_jsControlled)
  228. {
  229. _nodesWithAnimationManagers.pushBack(pNode);
  230. _animationManagersForNodes.pushBack(manager);
  231. }
  232. }
  233. return pNodeGraph;
  234. }
  235. Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName)
  236. {
  237. return createSceneWithNodeGraphFromFile(pCCBFileName, nullptr);
  238. }
  239. Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner)
  240. {
  241. return createSceneWithNodeGraphFromFile(pCCBFileName, pOwner, Director::getInstance()->getWinSize());
  242. }
  243. Scene* CCBReader::createSceneWithNodeGraphFromFile(const char *pCCBFileName, Ref *pOwner, const Size &parentSize)
  244. {
  245. Node *pNode = readNodeGraphFromFile(pCCBFileName, pOwner, parentSize);
  246. Scene *pScene = Scene::create();
  247. pScene->addChild(pNode);
  248. return pScene;
  249. }
  250. void CCBReader::cleanUpNodeGraph(Node *node)
  251. {
  252. node->setUserObject(nullptr);
  253. auto& children = node->getChildren();
  254. for(const auto &obj : children) {
  255. cleanUpNodeGraph(obj);
  256. }
  257. }
  258. Node* CCBReader::readFileWithCleanUp(bool bCleanUp, CCBAnimationManagerMapPtr am)
  259. {
  260. if (! readHeader())
  261. {
  262. return nullptr;
  263. }
  264. if (! readStringCache())
  265. {
  266. return nullptr;
  267. }
  268. if (! readSequences())
  269. {
  270. return nullptr;
  271. }
  272. setAnimationManagers(am);
  273. Node *pNode = readNodeGraph(nullptr);
  274. _animationManagers->insert(pNode, _animationManager);
  275. if (bCleanUp)
  276. {
  277. cleanUpNodeGraph(pNode);
  278. }
  279. return pNode;
  280. }
  281. bool CCBReader::readStringCache() {
  282. int numStrings = this->readInt(false);
  283. for(int i = 0; i < numStrings; i++) {
  284. this->_stringCache.push_back(this->readUTF8());
  285. }
  286. return true;
  287. }
  288. bool CCBReader::readHeader()
  289. {
  290. /* If no bytes loaded, don't crash about it. */
  291. if(this->_bytes == nullptr) {
  292. return false;
  293. }
  294. /* Read magic bytes */
  295. int magicBytes = *((int*)(this->_bytes + this->_currentByte));
  296. this->_currentByte += 4;
  297. if(CC_SWAP_INT32_BIG_TO_HOST(magicBytes) != (*reinterpret_cast<const int*>("ccbi"))) {
  298. return false;
  299. }
  300. /* Read version. */
  301. int version = this->readInt(false);
  302. if(version != CCB_VERSION) {
  303. log("WARNING! Incompatible ccbi file version (file: %d reader: %d)", version, CCB_VERSION);
  304. return false;
  305. }
  306. // Read JS check
  307. _jsControlled = this->readBool();
  308. _animationManager->_jsControlled = _jsControlled;
  309. return true;
  310. }
  311. unsigned char CCBReader::readByte()
  312. {
  313. unsigned char byte = this->_bytes[this->_currentByte];
  314. this->_currentByte++;
  315. return byte;
  316. }
  317. bool CCBReader::readBool()
  318. {
  319. return 0 == this->readByte() ? false : true;
  320. }
  321. std::string CCBReader::readUTF8()
  322. {
  323. std::string ret;
  324. int b0 = this->readByte();
  325. int b1 = this->readByte();
  326. int numBytes = b0 << 8 | b1;
  327. char* pStr = (char*)malloc(numBytes+1);
  328. memcpy(pStr, _bytes+_currentByte, numBytes);
  329. pStr[numBytes] = '\0';
  330. ret = pStr;
  331. free(pStr);
  332. _currentByte += numBytes;
  333. return ret;
  334. }
  335. bool CCBReader::getBit() {
  336. bool bit;
  337. unsigned char byte = *(this->_bytes + this->_currentByte);
  338. if(byte & (1 << this->_currentBit)) {
  339. bit = true;
  340. } else {
  341. bit = false;
  342. }
  343. this->_currentBit++;
  344. if(this->_currentBit >= 8) {
  345. this->_currentBit = 0;
  346. this->_currentByte++;
  347. }
  348. return bit;
  349. }
  350. void CCBReader::alignBits() {
  351. if(this->_currentBit) {
  352. this->_currentBit = 0;
  353. this->_currentByte++;
  354. }
  355. }
  356. int CCBReader::readInt(bool pSigned) {
  357. // Read encoded int
  358. int numBits = 0;
  359. while(!this->getBit()) {
  360. numBits++;
  361. }
  362. long long current = 0;
  363. for(int a = numBits - 1; a >= 0; a--) {
  364. if(this->getBit()) {
  365. current |= 1LL << a;
  366. }
  367. }
  368. current |= 1LL << numBits;
  369. int num;
  370. if(pSigned) {
  371. int s = current % 2;
  372. if(s) {
  373. num = static_cast<int>(current / 2);
  374. } else {
  375. num = static_cast<int>(-current / 2);
  376. }
  377. } else {
  378. num = static_cast<int>(current - 1);
  379. }
  380. this->alignBits();
  381. return num;
  382. }
  383. float CCBReader::readFloat()
  384. {
  385. FloatType type = static_cast<FloatType>(this->readByte());
  386. switch (type)
  387. {
  388. case FloatType::_0:
  389. return 0;
  390. case FloatType::_1:
  391. return 1;
  392. case FloatType::MINUS1:
  393. return -1;
  394. case FloatType::_05:
  395. return 0.5f;
  396. case FloatType::INTEGER:
  397. return (float)this->readInt(true);
  398. default:
  399. {
  400. /* using a memcpy since the compiler isn't
  401. * doing the float ptr math correctly on device.
  402. * TODO: still applies in C++ ? */
  403. unsigned char* pF = (this->_bytes + this->_currentByte);
  404. float f = 0;
  405. // N.B - in order to avoid an unaligned memory access crash on 'memcpy()' the the (void*) casts of the source and
  406. // destination pointers are EXTREMELY important for the ARM compiler.
  407. //
  408. // Without a (void*) cast, the ARM compiler makes the assumption that the float* pointer is naturally aligned
  409. // according to it's type size (aligned along 4 byte boundaries) and thus tries to call a more optimized
  410. // version of memcpy() which makes this alignment assumption also. When reading back from a file of course our pointers
  411. // may not be aligned, hence we need to avoid the compiler making this assumption. The (void*) cast serves this purpose,
  412. // and causes the ARM compiler to choose the slower, more generalized (unaligned) version of memcpy()
  413. //
  414. // For more about this compiler behavior, see:
  415. // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3934.html
  416. memcpy((void*) &f, (const void*) pF, sizeof(float));
  417. this->_currentByte += sizeof(float);
  418. return f;
  419. }
  420. }
  421. }
  422. std::string CCBReader::readCachedString()
  423. {
  424. int n = this->readInt(false);
  425. return this->_stringCache[n];
  426. }
  427. Node * CCBReader::readNodeGraph(Node * pParent)
  428. {
  429. /* Read class name. */
  430. std::string className = this->readCachedString();
  431. std::string _jsControlledName;
  432. if(_jsControlled) {
  433. _jsControlledName = this->readCachedString();
  434. }
  435. // Read assignment type and name
  436. TargetType memberVarAssignmentType = static_cast<TargetType>(this->readInt(false));
  437. std::string memberVarAssignmentName;
  438. if(memberVarAssignmentType != TargetType::NONE)
  439. {
  440. memberVarAssignmentName = this->readCachedString();
  441. }
  442. NodeLoader *ccNodeLoader = this->_nodeLoaderLibrary->getNodeLoader(className.c_str());
  443. if (! ccNodeLoader)
  444. {
  445. log("no corresponding node loader for %s", className.c_str());
  446. return nullptr;
  447. }
  448. Node *node = ccNodeLoader->loadNode(pParent, this);
  449. // Set root node
  450. if (! _animationManager->getRootNode())
  451. {
  452. _animationManager->setRootNode(node);
  453. }
  454. // Assign controller
  455. if(_jsControlled && node == _animationManager->getRootNode())
  456. {
  457. _animationManager->setDocumentControllerName(_jsControlledName);
  458. }
  459. // Read animated properties
  460. std::unordered_map<int, Map<std::string, CCBSequenceProperty*>> seqs;
  461. _animatedProps = new std::set<std::string>();
  462. int numSequence = readInt(false);
  463. for (int i = 0; i < numSequence; ++i)
  464. {
  465. int seqId = readInt(false);
  466. Map<std::string, CCBSequenceProperty*> seqNodeProps;
  467. int numProps = readInt(false);
  468. for (int j = 0; j < numProps; ++j)
  469. {
  470. CCBSequenceProperty *seqProp = new (std::nothrow) CCBSequenceProperty();
  471. seqProp->autorelease();
  472. seqProp->setName(readCachedString().c_str());
  473. seqProp->setType(readInt(false));
  474. _animatedProps->insert(seqProp->getName());
  475. int numKeyframes = readInt(false);
  476. for (int k = 0; k < numKeyframes; ++k)
  477. {
  478. CCBKeyframe *keyframe = readKeyframe(static_cast<PropertyType>(seqProp->getType()));
  479. seqProp->getKeyframes().pushBack(keyframe);
  480. }
  481. seqNodeProps.insert(seqProp->getName(), seqProp);
  482. }
  483. seqs[seqId] = seqNodeProps;
  484. }
  485. if (!seqs.empty())
  486. {
  487. _animationManager->addNode(node, seqs);
  488. }
  489. // Read properties
  490. ccNodeLoader->parseProperties(node, pParent, this);
  491. bool isCCBFileNode = (nullptr == dynamic_cast<CCBFile*>(node)) ? false : true;
  492. // Handle sub ccb files (remove middle node)
  493. if (isCCBFileNode)
  494. {
  495. CCBFile *ccbFileNode = (CCBFile*)node;
  496. Node *embeddedNode = ccbFileNode->getCCBFileNode();
  497. embeddedNode->setPosition(ccbFileNode->getPosition());
  498. embeddedNode->setRotation(ccbFileNode->getRotation());
  499. embeddedNode->setScaleX(ccbFileNode->getScaleX());
  500. embeddedNode->setScaleY(ccbFileNode->getScaleY());
  501. embeddedNode->setTag(ccbFileNode->getTag());
  502. embeddedNode->setVisible(true);
  503. //embeddedNode->setIgnoreAnchorPointForPosition(ccbFileNode->isIgnoreAnchorPointForPosition());
  504. _animationManager->moveAnimationsFromNode(ccbFileNode, embeddedNode);
  505. ccbFileNode->setCCBFileNode(nullptr);
  506. node = embeddedNode;
  507. }
  508. if (memberVarAssignmentType != TargetType::NONE)
  509. {
  510. if(!_jsControlled)
  511. {
  512. Ref* target = nullptr;
  513. if(memberVarAssignmentType == TargetType::DOCUMENT_ROOT)
  514. {
  515. target = _animationManager->getRootNode();
  516. }
  517. else if(memberVarAssignmentType == TargetType::OWNER)
  518. {
  519. target = this->_owner;
  520. }
  521. if(target != nullptr)
  522. {
  523. CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
  524. bool assigned = false;
  525. if (memberVarAssignmentType != TargetType::NONE)
  526. {
  527. if(targetAsCCBMemberVariableAssigner != nullptr)
  528. {
  529. assigned = targetAsCCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
  530. }
  531. if(!assigned && this->_CCBMemberVariableAssigner != nullptr)
  532. {
  533. assigned = this->_CCBMemberVariableAssigner->onAssignCCBMemberVariable(target, memberVarAssignmentName.c_str(), node);
  534. }
  535. }
  536. }
  537. }
  538. else
  539. {
  540. if(memberVarAssignmentType == TargetType::DOCUMENT_ROOT)
  541. {
  542. _animationManager->addDocumentOutletName(memberVarAssignmentName);
  543. _animationManager->addDocumentOutletNode(node);
  544. }
  545. else
  546. {
  547. _ownerOutletNames.push_back(memberVarAssignmentName);
  548. _ownerOutletNodes.pushBack(node);
  549. }
  550. }
  551. }
  552. // Assign custom properties.
  553. if (!ccNodeLoader->getCustomProperties().empty())
  554. {
  555. bool customAssigned = false;
  556. if(!_jsControlled)
  557. {
  558. Ref* target = node;
  559. if(target != nullptr)
  560. {
  561. CCBMemberVariableAssigner * targetAsCCBMemberVariableAssigner = dynamic_cast<CCBMemberVariableAssigner *>(target);
  562. if(targetAsCCBMemberVariableAssigner != nullptr)
  563. {
  564. auto& customPropeties = ccNodeLoader->getCustomProperties();
  565. for (auto iter = customPropeties.begin(); iter != customPropeties.end(); ++iter)
  566. {
  567. customAssigned = targetAsCCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second);
  568. if(!customAssigned && this->_CCBMemberVariableAssigner != nullptr)
  569. {
  570. customAssigned = this->_CCBMemberVariableAssigner->onAssignCCBCustomProperty(target, iter->first.c_str(), iter->second);
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577. delete _animatedProps;
  578. _animatedProps = nullptr;
  579. /* Read and add children. */
  580. int numChildren = this->readInt(false);
  581. for(int i = 0; i < numChildren; i++)
  582. {
  583. Node * child = this->readNodeGraph(node);
  584. node->addChild(child);
  585. }
  586. // FIX ISSUE #1860: "onNodeLoaded will be called twice if ccb was added as a CCBFile".
  587. // If it's a sub-ccb node, skip notification to NodeLoaderListener since it will be
  588. // notified at LINE #734: Node * child = this->readNodeGraph(node);
  589. if (!isCCBFileNode)
  590. {
  591. // Call onNodeLoaded
  592. NodeLoaderListener * nodeAsNodeLoaderListener = dynamic_cast<NodeLoaderListener *>(node);
  593. if(nodeAsNodeLoaderListener != nullptr)
  594. {
  595. nodeAsNodeLoaderListener->onNodeLoaded(node, ccNodeLoader);
  596. }
  597. else if(this->_nodeLoaderListener != nullptr)
  598. {
  599. this->_nodeLoaderListener->onNodeLoaded(node, ccNodeLoader);
  600. }
  601. }
  602. return node;
  603. }
  604. CCBKeyframe* CCBReader::readKeyframe(PropertyType type)
  605. {
  606. CCBKeyframe *keyframe = new (std::nothrow) CCBKeyframe();
  607. keyframe->autorelease();
  608. keyframe->setTime(readFloat());
  609. CCBKeyframe::EasingType easingType = static_cast<CCBKeyframe::EasingType>(readInt(false));
  610. float easingOpt = 0;
  611. Value value;
  612. if (easingType == CCBKeyframe::EasingType::CUBIC_IN
  613. || easingType == CCBKeyframe::EasingType::CUBIC_OUT
  614. || easingType == CCBKeyframe::EasingType::CUBIC_INOUT
  615. || easingType == CCBKeyframe::EasingType::ELASTIC_IN
  616. || easingType == CCBKeyframe::EasingType::ELASTIC_OUT
  617. || easingType == CCBKeyframe::EasingType::ELASTIC_INOUT)
  618. {
  619. easingOpt = readFloat();
  620. }
  621. keyframe->setEasingType(easingType);
  622. keyframe->setEasingOpt(easingOpt);
  623. if (type == PropertyType::CHECK)
  624. {
  625. value = readBool();
  626. }
  627. else if (type == PropertyType::BYTE)
  628. {
  629. value = readByte();
  630. }
  631. else if (type == PropertyType::COLOR3)
  632. {
  633. unsigned char r = readByte();
  634. unsigned char g = readByte();
  635. unsigned char b = readByte();
  636. ValueMap colorMap;
  637. colorMap["r"] = r;
  638. colorMap["g"] = g;
  639. colorMap["b"] = b;
  640. value = colorMap;
  641. }
  642. else if (type == PropertyType::DEGREES)
  643. {
  644. value = readFloat();
  645. }
  646. else if (type == PropertyType::SCALE_LOCK || type == PropertyType::POSITION
  647. || type == PropertyType::FLOAT_XY)
  648. {
  649. float a = readFloat();
  650. float b = readFloat();
  651. ValueVector ab;
  652. ab.push_back(Value(a));
  653. ab.push_back(Value(b));
  654. value = ab;
  655. }
  656. else if (type == PropertyType::SPRITEFRAME)
  657. {
  658. std::string spriteSheet = readCachedString();
  659. std::string spriteFile = readCachedString();
  660. SpriteFrame* spriteFrame;
  661. if (spriteSheet.empty())
  662. {
  663. spriteFile = _CCBRootPath + spriteFile;
  664. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(spriteFile);
  665. Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
  666. spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
  667. }
  668. else
  669. {
  670. spriteSheet = _CCBRootPath + spriteSheet;
  671. SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
  672. // Load the sprite sheet only if it is not loaded
  673. if (_loadedSpriteSheets.find(spriteSheet) == _loadedSpriteSheets.end())
  674. {
  675. frameCache->addSpriteFramesWithFile(spriteSheet);
  676. _loadedSpriteSheets.insert(spriteSheet);
  677. }
  678. spriteFrame = frameCache->getSpriteFrameByName(spriteFile);
  679. }
  680. keyframe->setObject(spriteFrame);
  681. }
  682. if (!value.isNull())
  683. keyframe->setValue(value);
  684. return keyframe;
  685. }
  686. bool CCBReader::readCallbackKeyframesForSeq(CCBSequence* seq)
  687. {
  688. int numKeyframes = readInt(false);
  689. if(!numKeyframes) return true;
  690. CCBSequenceProperty* channel = new (std::nothrow) CCBSequenceProperty();
  691. channel->autorelease();
  692. for(int i = 0; i < numKeyframes; ++i) {
  693. float time = readFloat();
  694. std::string callbackName = readCachedString();
  695. int callbackType = readInt(false);
  696. ValueVector valueVector;
  697. valueVector.push_back(Value(callbackName));
  698. valueVector.push_back(Value(callbackType));
  699. CCBKeyframe* keyframe = new (std::nothrow) CCBKeyframe();
  700. keyframe->autorelease();
  701. keyframe->setTime(time);
  702. keyframe->setValue(Value(valueVector));
  703. if(_jsControlled) {
  704. std::stringstream callbackIdentifier;
  705. callbackIdentifier << callbackType;
  706. callbackIdentifier << ":" + callbackName;
  707. _animationManager->getKeyframeCallbacks().push_back(Value(callbackIdentifier.str()));
  708. }
  709. channel->getKeyframes().pushBack(keyframe);
  710. }
  711. seq->setCallbackChannel(channel);
  712. return true;
  713. }
  714. bool CCBReader::readSoundKeyframesForSeq(CCBSequence* seq) {
  715. int numKeyframes = readInt(false);
  716. if(!numKeyframes) return true;
  717. CCBSequenceProperty* channel = new (std::nothrow) CCBSequenceProperty();
  718. channel->autorelease();
  719. for(int i = 0; i < numKeyframes; ++i) {
  720. float time = readFloat();
  721. std::string soundFile = readCachedString();
  722. float pitch = readFloat();
  723. float pan = readFloat();
  724. float gain = readFloat();
  725. ValueVector vec;
  726. vec.push_back(Value(soundFile));
  727. vec.push_back(Value(pitch));
  728. vec.push_back(Value(pan));
  729. vec.push_back(Value(gain));
  730. CCBKeyframe* keyframe = new (std::nothrow) CCBKeyframe();
  731. keyframe->setTime(time);
  732. keyframe->setValue(Value(vec));
  733. channel->getKeyframes().pushBack(keyframe);
  734. keyframe->release();
  735. }
  736. seq->setSoundChannel(channel);
  737. return true;
  738. }
  739. Node * CCBReader::readNodeGraph() {
  740. return this->readNodeGraph(nullptr);
  741. }
  742. bool CCBReader::readSequences()
  743. {
  744. auto& sequences = _animationManager->getSequences();
  745. int numSeqs = readInt(false);
  746. for (int i = 0; i < numSeqs; i++)
  747. {
  748. CCBSequence *seq = new (std::nothrow) CCBSequence();
  749. seq->autorelease();
  750. seq->setDuration(readFloat());
  751. seq->setName(readCachedString().c_str());
  752. seq->setSequenceId(readInt(false));
  753. seq->setChainedSequenceId(readInt(true));
  754. if(!readCallbackKeyframesForSeq(seq)) return false;
  755. if(!readSoundKeyframesForSeq(seq)) return false;
  756. sequences.pushBack(seq);
  757. }
  758. _animationManager->setAutoPlaySequenceId(readInt(true));
  759. return true;
  760. }
  761. std::string CCBReader::lastPathComponent(const char* pPath) {
  762. std::string path(pPath);
  763. size_t slashPos = path.find_last_of("/");
  764. if(slashPos != std::string::npos) {
  765. return path.substr(slashPos + 1, path.length() - slashPos);
  766. }
  767. return path;
  768. }
  769. std::string CCBReader::deletePathExtension(const char* pPath) {
  770. std::string path(pPath);
  771. size_t dotPos = path.find_last_of(".");
  772. if(dotPos != std::string::npos) {
  773. return path.substr(0, dotPos);
  774. }
  775. return path;
  776. }
  777. std::string CCBReader::toLowerCase(const char* pString) {
  778. std::string copy(pString);
  779. std::transform(copy.begin(), copy.end(), copy.begin(), ::tolower);
  780. return copy;
  781. }
  782. bool CCBReader::endsWith(const char* pString, const char* pEnding) {
  783. std::string string(pString);
  784. std::string ending(pEnding);
  785. if(string.length() >= ending.length()) {
  786. return (string.compare(string.length() - ending.length(), ending.length(), ending) == 0);
  787. } else {
  788. return false;
  789. }
  790. }
  791. bool CCBReader::isJSControlled()
  792. {
  793. return _jsControlled;
  794. }
  795. void CCBReader::addOwnerCallbackName(const std::string& name)
  796. {
  797. _ownerCallbackNames.push_back(name);
  798. }
  799. void CCBReader::addOwnerCallbackNode(Node *node)
  800. {
  801. _ownerCallbackNodes.pushBack(node);
  802. }
  803. void CCBReader::addOwnerCallbackControlEvents(Control::EventType type)
  804. {
  805. _ownerOwnerCallbackControlEvents.push_back(Value((int)type));
  806. }
  807. void CCBReader::addDocumentCallbackName(const std::string& name)
  808. {
  809. _animationManager->addDocumentCallbackName(name);
  810. }
  811. void CCBReader::addDocumentCallbackNode(Node *node)
  812. {
  813. _animationManager->addDocumentCallbackNode(node);
  814. }
  815. void CCBReader::addDocumentCallbackControlEvents(Control::EventType eventType)
  816. {
  817. _animationManager->addDocumentCallbackControlEvents(eventType);
  818. }
  819. ValueVector CCBReader::getOwnerCallbackNames()
  820. {
  821. ValueVector ret;
  822. ret.reserve(_ownerCallbackNames.size());
  823. std::vector<std::string>::iterator it = _ownerCallbackNames.begin();
  824. for (; it != _ownerCallbackNames.end(); ++it)
  825. {
  826. ret.push_back(Value(*it));
  827. }
  828. return ret;
  829. }
  830. Vector<Node*>& CCBReader::getOwnerCallbackNodes()
  831. {
  832. return _ownerCallbackNodes;
  833. }
  834. ValueVector& CCBReader::getOwnerCallbackControlEvents()
  835. {
  836. return _ownerOwnerCallbackControlEvents;
  837. }
  838. ValueVector CCBReader::getOwnerOutletNames()
  839. {
  840. ValueVector ret;
  841. ret.reserve(_ownerOutletNames.size());
  842. std::vector<std::string>::iterator it = _ownerOutletNames.begin();
  843. for (; it != _ownerOutletNames.end(); ++it)
  844. {
  845. ret.push_back(Value(*it));
  846. }
  847. return ret;
  848. }
  849. Vector<Node*>& CCBReader::getOwnerOutletNodes()
  850. {
  851. return _ownerOutletNodes;
  852. }
  853. Vector<Node*>& CCBReader::getNodesWithAnimationManagers()
  854. {
  855. return _nodesWithAnimationManagers;
  856. }
  857. Vector<CCBAnimationManager*>& CCBReader::getAnimationManagersForNodes()
  858. {
  859. return _animationManagersForNodes;
  860. }
  861. void CCBReader::addOwnerOutletName(std::string name)
  862. {
  863. _ownerOutletNames.push_back(name);
  864. }
  865. void CCBReader::addOwnerOutletNode(Node *node)
  866. {
  867. if (nullptr == node)
  868. return;
  869. _ownerOutletNodes.pushBack(node);
  870. }
  871. /************************************************************************
  872. Static functions
  873. ************************************************************************/
  874. static float __ccbResolutionScale = 1.0f;
  875. float CCBReader::getResolutionScale()
  876. {
  877. return __ccbResolutionScale;
  878. }
  879. void CCBReader::setResolutionScale(float scale)
  880. {
  881. __ccbResolutionScale = scale;
  882. }
  883. };