CSLoader.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/ActionTimeline/CSLoader.h"
  21. #include "base/ObjectFactory.h"
  22. #include "base/CCDirector.h"
  23. #include "base/ccUTF8.h"
  24. #include "ui/CocosGUI.h"
  25. #include "2d/CCSpriteFrameCache.h"
  26. #include "2d/CCParticleSystemQuad.h"
  27. #include "2d/CCTMXTiledMap.h"
  28. #include "platform/CCFileUtils.h"
  29. #include "editor-support/cocostudio/ActionTimeline/CCActionTimelineCache.h"
  30. #include "editor-support/cocostudio/ActionTimeline/CCActionTimeline.h"
  31. #include "editor-support/cocostudio/ActionTimeline/CCActionTimelineNode.h"
  32. #include "editor-support/cocostudio/CCSGUIReader.h"
  33. #include "editor-support/cocostudio/CocoStudio.h"
  34. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  35. #include "editor-support/cocostudio/WidgetReader/NodeReaderProtocol.h"
  36. #include "editor-support/cocostudio/WidgetReader/NodeReaderDefine.h"
  37. #include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
  38. #include "editor-support/cocostudio/WidgetReader/SingleNodeReader/SingleNodeReader.h"
  39. #include "editor-support/cocostudio/WidgetReader/SpriteReader/SpriteReader.h"
  40. #include "editor-support/cocostudio/WidgetReader/ParticleReader/ParticleReader.h"
  41. #include "editor-support/cocostudio/WidgetReader/GameMapReader/GameMapReader.h"
  42. #include "editor-support/cocostudio/WidgetReader/ProjectNodeReader/ProjectNodeReader.h"
  43. #include "editor-support/cocostudio/WidgetReader/ComAudioReader/ComAudioReader.h"
  44. #include "editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.h"
  45. #include "editor-support/cocostudio/WidgetReader/CheckBoxReader/CheckBoxReader.h"
  46. #include "editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.h"
  47. #include "editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
  48. #include "editor-support/cocostudio/WidgetReader/TextReader/TextReader.h"
  49. #include "editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
  50. #include "editor-support/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
  51. #include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
  52. #include "editor-support/cocostudio/WidgetReader/SliderReader/SliderReader.h"
  53. #include "editor-support/cocostudio/WidgetReader/LayoutReader/LayoutReader.h"
  54. #include "editor-support/cocostudio/WidgetReader/ScrollViewReader/ScrollViewReader.h"
  55. #include "editor-support/cocostudio/WidgetReader/PageViewReader/PageViewReader.h"
  56. #include "editor-support/cocostudio/WidgetReader/ListViewReader/ListViewReader.h"
  57. #include "editor-support/cocostudio/WidgetReader/ArmatureNodeReader/ArmatureNodeReader.h"
  58. #include "editor-support/cocostudio/WidgetReader/Node3DReader/Node3DReader.h"
  59. #include "editor-support/cocostudio/WidgetReader/Sprite3DReader/Sprite3DReader.h"
  60. #include "editor-support/cocostudio/WidgetReader/UserCameraReader/UserCameraReader.h"
  61. #include "editor-support/cocostudio/WidgetReader/Particle3DReader/Particle3DReader.h"
  62. #include "editor-support/cocostudio/WidgetReader/GameNode3DReader/GameNode3DReader.h"
  63. #include "editor-support/cocostudio/WidgetReader/Light3DReader/Light3DReader.h"
  64. #include "editor-support/cocostudio/WidgetReader/TabControlReader/TabControlReader.h"
  65. #include "editor-support/cocostudio/WidgetReader/SkeletonReader/BoneNodeReader.h"
  66. #include "editor-support/cocostudio/WidgetReader/SkeletonReader/SkeletonNodeReader.h"
  67. #include "flatbuffers/flatbuffers.h"
  68. #include "flatbuffers/util.h"
  69. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  70. #include "editor-support/cocostudio/WidgetCallBackHandlerProtocol.h"
  71. #include <fstream>
  72. using namespace cocos2d::ui;
  73. using namespace cocostudio;
  74. using namespace cocostudio::timeline;
  75. /* peterson */
  76. using namespace flatbuffers;
  77. /**/
  78. NS_CC_BEGIN
  79. static const char* ClassName_Node = "Node";
  80. static const char* ClassName_SubGraph = "SubGraph";
  81. static const char* ClassName_Sprite = "Sprite";
  82. static const char* ClassName_Particle = "Particle";
  83. static const char* ClassName_TMXTiledMap = "TMXTiledMap";
  84. static const char* ClassName_Panel = "Panel";
  85. static const char* ClassName_Button = "Button";
  86. static const char* ClassName_CheckBox = "CheckBox";
  87. static const char* ClassName_ImageView = "ImageView";
  88. static const char* ClassName_TextAtlas = "TextAtlas";
  89. static const char* ClassName_LabelAtlas = "LabelAtlas";
  90. static const char* ClassName_LabelBMFont= "LabelBMFont";
  91. static const char* ClassName_TextBMFont = "TextBMFont";
  92. static const char* ClassName_Text = "Text";
  93. static const char* ClassName_LoadingBar = "LoadingBar";
  94. static const char* ClassName_TextField = "TextField";
  95. static const char* ClassName_Slider = "Slider";
  96. static const char* ClassName_Layout = "Layout";
  97. static const char* ClassName_ScrollView = "ScrollView";
  98. static const char* ClassName_ListView = "ListView";
  99. static const char* ClassName_PageView = "PageView";
  100. static const char* ClassName_Widget = "Widget";
  101. static const char* ClassName_Label = "Label";
  102. static const char* ClassName_ComAudio = "ComAudio";
  103. static const char* NODE = "nodeTree";
  104. static const char* CHILDREN = "children";
  105. static const char* CLASSNAME = "classname";
  106. static const char* FILE_PATH = "fileName";
  107. static const char* PLIST_FILE = "plistFile";
  108. static const char* TMX_FILE = "tmxFile";
  109. static const char* TMX_STRING = "tmxString";
  110. static const char* RESOURCE_PATH = "resourcePath";
  111. static const char* COMPONENTS = "components";
  112. static const char* COMPONENT_TYPE = "componentType";
  113. static const char* COMPONENT_NAME = "componentName";
  114. static const char* COMPONENT_ENABLED = "componentEnabled";
  115. static const char* COMPONENT_AUDIO_FILE_PATH = "comAudioFilePath";
  116. static const char* COMPONENT_LOOP = "comAudioloop";
  117. static const char* TAG = "tag";
  118. static const char* ACTION_TAG = "actionTag";
  119. static const char* OPTIONS = "options";
  120. static const char* WIDTH = "width";
  121. static const char* HEIGHT = "height";
  122. static const char* X = "x";
  123. static const char* Y = "y";
  124. static const char* SCALE_X = "scaleX";
  125. static const char* SCALE_Y = "scaleY";
  126. static const char* SKEW_X = "skewX";
  127. static const char* SKEW_Y = "skewY";
  128. static const char* ROTATION = "rotation";
  129. static const char* ROTATION_SKEW_X = "rotationSkewX";
  130. static const char* ROTATION_SKEW_Y = "rotationSkewY";
  131. static const char* ANCHOR_X = "anchorPointX";
  132. static const char* ANCHOR_Y = "anchorPointY";
  133. static const char* ALPHA = "opacity";
  134. static const char* RED = "colorR";
  135. static const char* GREEN = "colorG";
  136. static const char* BLUE = "colorB";
  137. static const char* ZORDER = "ZOrder";
  138. static const char* PARTICLE_NUM = "particleNum";
  139. static const char* FLIPX = "flipX";
  140. static const char* FLIPY = "flipY";
  141. static const char* VISIBLE = "visible";
  142. static const char* TEXTURES = "textures";
  143. static const char* TEXTURES_PNG = "texturesPng";
  144. static const char* MONO_COCOS2D_VERSION = "cocos2dVersion";
  145. // CSLoader
  146. static CSLoader* _sharedCSLoader = nullptr;
  147. CSLoader* CSLoader::getInstance()
  148. {
  149. if (! _sharedCSLoader)
  150. {
  151. _sharedCSLoader = new (std::nothrow) CSLoader();
  152. _sharedCSLoader->init();
  153. }
  154. return _sharedCSLoader;
  155. }
  156. void CSLoader::destroyInstance()
  157. {
  158. CC_SAFE_DELETE(_sharedCSLoader);
  159. ActionTimelineCache::destroyInstance();
  160. }
  161. CSLoader::CSLoader()
  162. : _recordJsonPath(true)
  163. , _jsonPath("")
  164. , _monoCocos2dxVersion("")
  165. , _rootNode(nullptr)
  166. , _csBuildID("2.1.0.0")
  167. {
  168. CREATE_CLASS_NODE_READER_INFO(NodeReader);
  169. CREATE_CLASS_NODE_READER_INFO(SingleNodeReader);
  170. CREATE_CLASS_NODE_READER_INFO(SpriteReader);
  171. CREATE_CLASS_NODE_READER_INFO(ParticleReader);
  172. CREATE_CLASS_NODE_READER_INFO(GameMapReader);
  173. CREATE_CLASS_NODE_READER_INFO(ButtonReader);
  174. CREATE_CLASS_NODE_READER_INFO(CheckBoxReader);
  175. CREATE_CLASS_NODE_READER_INFO(ImageViewReader);
  176. CREATE_CLASS_NODE_READER_INFO(TextBMFontReader);
  177. CREATE_CLASS_NODE_READER_INFO(TextReader);
  178. CREATE_CLASS_NODE_READER_INFO(TextFieldReader);
  179. CREATE_CLASS_NODE_READER_INFO(TextAtlasReader);
  180. CREATE_CLASS_NODE_READER_INFO(LoadingBarReader);
  181. CREATE_CLASS_NODE_READER_INFO(SliderReader);
  182. CREATE_CLASS_NODE_READER_INFO(LayoutReader);
  183. CREATE_CLASS_NODE_READER_INFO(ScrollViewReader);
  184. CREATE_CLASS_NODE_READER_INFO(PageViewReader);
  185. CREATE_CLASS_NODE_READER_INFO(ListViewReader);
  186. CREATE_CLASS_NODE_READER_INFO(ArmatureNodeReader);
  187. CREATE_CLASS_NODE_READER_INFO(Node3DReader);
  188. CREATE_CLASS_NODE_READER_INFO(Sprite3DReader);
  189. CREATE_CLASS_NODE_READER_INFO(UserCameraReader);
  190. CREATE_CLASS_NODE_READER_INFO(Particle3DReader);
  191. CREATE_CLASS_NODE_READER_INFO(GameNode3DReader);
  192. CREATE_CLASS_NODE_READER_INFO(Light3DReader);
  193. CREATE_CLASS_NODE_READER_INFO(TabControlReader);
  194. CREATE_CLASS_NODE_READER_INFO(BoneNodeReader);
  195. CREATE_CLASS_NODE_READER_INFO(SkeletonNodeReader);
  196. }
  197. void CSLoader::purge()
  198. {
  199. }
  200. void CSLoader::init()
  201. {
  202. using namespace std::placeholders;
  203. _funcs.insert(Pair(ClassName_Node, std::bind(&CSLoader::loadSimpleNode, this, _1)));
  204. _funcs.insert(Pair(ClassName_SubGraph, std::bind(&CSLoader::loadSubGraph, this, _1)));
  205. _funcs.insert(Pair(ClassName_Sprite, std::bind(&CSLoader::loadSprite, this, _1)));
  206. _funcs.insert(Pair(ClassName_Particle, std::bind(&CSLoader::loadParticle, this, _1)));
  207. _funcs.insert(Pair(ClassName_TMXTiledMap, std::bind(&CSLoader::loadTMXTiledMap, this, _1)));
  208. _funcs.insert(Pair(ClassName_LabelAtlas,std::bind(&CSLoader::loadWidget, this, _1)));
  209. _funcs.insert(Pair(ClassName_LabelBMFont,std::bind(&CSLoader::loadWidget, this, _1)));
  210. _funcs.insert(Pair(ClassName_Panel, std::bind(&CSLoader::loadWidget, this, _1)));
  211. _funcs.insert(Pair(ClassName_Button, std::bind(&CSLoader::loadWidget, this, _1)));
  212. _funcs.insert(Pair(ClassName_CheckBox, std::bind(&CSLoader::loadWidget, this, _1)));
  213. _funcs.insert(Pair(ClassName_ImageView, std::bind(&CSLoader::loadWidget, this, _1)));
  214. _funcs.insert(Pair(ClassName_TextAtlas, std::bind(&CSLoader::loadWidget, this, _1)));
  215. _funcs.insert(Pair(ClassName_TextBMFont,std::bind(&CSLoader::loadWidget, this, _1)));
  216. _funcs.insert(Pair(ClassName_Text, std::bind(&CSLoader::loadWidget, this, _1)));
  217. _funcs.insert(Pair(ClassName_LoadingBar,std::bind(&CSLoader::loadWidget, this, _1)));
  218. _funcs.insert(Pair(ClassName_TextField, std::bind(&CSLoader::loadWidget, this, _1)));
  219. _funcs.insert(Pair(ClassName_Slider, std::bind(&CSLoader::loadWidget, this, _1)));
  220. _funcs.insert(Pair(ClassName_Layout, std::bind(&CSLoader::loadWidget, this, _1)));
  221. _funcs.insert(Pair(ClassName_ScrollView,std::bind(&CSLoader::loadWidget, this, _1)));
  222. _funcs.insert(Pair(ClassName_ListView, std::bind(&CSLoader::loadWidget, this, _1)));
  223. _funcs.insert(Pair(ClassName_PageView, std::bind(&CSLoader::loadWidget, this, _1)));
  224. _funcs.insert(Pair(ClassName_Widget, std::bind(&CSLoader::loadWidget, this, _1)));
  225. _funcs.insert(Pair(ClassName_Label, std::bind(&CSLoader::loadWidget, this, _1)));
  226. _componentFuncs.insert(ComponentPair(ClassName_ComAudio, std::bind(&CSLoader::loadComAudio, this, _1)));
  227. }
  228. Node* CSLoader::createNode(const std::string& filename)
  229. {
  230. std::string path = filename;
  231. size_t pos = path.find_last_of('.');
  232. std::string suffix = path.substr(pos + 1, path.length());
  233. CSLoader* load = CSLoader::getInstance();
  234. if (suffix == "csb")
  235. {
  236. return load->createNodeWithFlatBuffersFile(filename);
  237. }
  238. else if (suffix == "json" || suffix == "ExportJson")
  239. {
  240. return load->createNodeFromJson(filename);
  241. }
  242. return nullptr;
  243. }
  244. Node* CSLoader::createNode(const std::string &filename, const ccNodeLoadCallback &callback)
  245. {
  246. std::string path = filename;
  247. size_t pos = path.find_last_of('.');
  248. std::string suffix = path.substr(pos + 1, path.length());
  249. CSLoader* load = CSLoader::getInstance();
  250. if (suffix == "csb")
  251. {
  252. return load->createNodeWithFlatBuffersFile(filename, callback);
  253. }
  254. return nullptr;
  255. }
  256. Node* CSLoader::createNodeWithVisibleSize(const std::string& filename)
  257. {
  258. auto node = createNode(filename);
  259. if (node != nullptr)
  260. {
  261. Size frameSize = Director::getInstance()->getVisibleSize();
  262. node->setContentSize(frameSize);
  263. ui::Helper::doLayout(node);
  264. }
  265. return node;
  266. }
  267. Node* CSLoader::createNodeWithVisibleSize(const std::string &filename, const ccNodeLoadCallback &callback)
  268. {
  269. auto node = createNode(filename, callback);
  270. if (node != nullptr)
  271. {
  272. Size frameSize = Director::getInstance()->getVisibleSize();
  273. node->setContentSize(frameSize);
  274. ui::Helper::doLayout(node);
  275. }
  276. return node;
  277. }
  278. std::string CSLoader::getExtentionName(const std::string& name)
  279. {
  280. std::string path = name;
  281. size_t pos = path.find_last_of('.');
  282. std::string result = path.substr(pos + 1, path.length());
  283. return result;
  284. }
  285. ActionTimeline* CSLoader::createTimeline(const std::string &filename)
  286. {
  287. std::string suffix = getExtentionName(filename);
  288. ActionTimelineCache* cache = ActionTimelineCache::getInstance();
  289. if (suffix == "csb")
  290. {
  291. return cache->createActionWithFlatBuffersFile(filename);
  292. }
  293. else if (suffix == "json" || suffix == "ExportJson")
  294. {
  295. return cache->createActionFromJson(filename);
  296. }
  297. return nullptr;
  298. }
  299. ActionTimeline* CSLoader::createTimeline(const Data& data, const std::string& filename)
  300. {
  301. std::string suffix = getExtentionName(filename);
  302. ActionTimelineCache* cache = ActionTimelineCache::getInstance();
  303. if (suffix == "csb")
  304. {
  305. return cache->createActionWithDataBuffer(data, filename);
  306. }
  307. else if (suffix == "json" || suffix == "ExportJson")
  308. {
  309. std::string content((char *)data.getBytes(), data.getSize());
  310. return cache->createActionFromContent(filename, content);
  311. }
  312. return nullptr;
  313. }
  314. /*
  315. ActionTimelineNode* CSLoader::createActionTimelineNode(const std::string& filename)
  316. {
  317. Node* root = createNode(filename);
  318. ActionTimeline* action = createTimeline(filename);
  319. if(root && action)
  320. {
  321. root->runAction(action);
  322. action->gotoFrameAndPlay(0);
  323. }
  324. ActionTimelineNode* node = ActionTimelineNode::create(root, action);
  325. return node;
  326. }
  327. ActionTimelineNode* CSLoader::createActionTimelineNode(const std::string& filename, int startIndex, int endIndex, bool loop)
  328. {
  329. ActionTimelineNode* node = createActionTimelineNode(filename);
  330. ActionTimeline* action = node->getActionTimeline();
  331. if(action)
  332. action->gotoFrameAndPlay(startIndex, endIndex, loop);
  333. return node;
  334. }
  335. */
  336. Node* CSLoader::createNodeFromJson(const std::string& filename)
  337. {
  338. if (_recordJsonPath)
  339. {
  340. std::string jsonPath = filename.substr(0, filename.find_last_of('/') + 1);
  341. GUIReader::getInstance()->setFilePath(jsonPath);
  342. _jsonPath = jsonPath;
  343. }
  344. else
  345. {
  346. GUIReader::getInstance()->setFilePath("");
  347. _jsonPath = "";
  348. }
  349. Node* node = loadNodeWithFile(filename);
  350. return node;
  351. }
  352. Node* CSLoader::loadNodeWithFile(const std::string& fileName)
  353. {
  354. // Read content from file
  355. std::string contentStr = FileUtils::getInstance()->getStringFromFile(fileName);
  356. Node* node = loadNodeWithContent(contentStr);
  357. // Load animation data from file
  358. ActionTimelineCache::getInstance()->loadAnimationActionWithContent(fileName, contentStr);
  359. return node;
  360. }
  361. Node* CSLoader::loadNodeWithContent(const std::string& content)
  362. {
  363. rapidjson::Document doc;
  364. doc.Parse<0>(content.c_str());
  365. if (doc.HasParseError())
  366. {
  367. CCLOG("GetParseError %d\n", doc.GetParseError());
  368. }
  369. // cocos2dx version mono editor is based on
  370. _monoCocos2dxVersion = DICTOOL->getStringValue_json(doc, MONO_COCOS2D_VERSION, "");
  371. // decode plist
  372. int length = DICTOOL->getArrayCount_json(doc, TEXTURES);
  373. for(int i=0; i<length; i++)
  374. {
  375. std::string plist = DICTOOL->getStringValueFromArray_json(doc, TEXTURES, i);
  376. std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
  377. plist = _jsonPath + plist;
  378. png = _jsonPath + png;
  379. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
  380. }
  381. // decode node tree
  382. const rapidjson::Value& subJson = DICTOOL->getSubDictionary_json(doc, NODE);
  383. Node* root = loadNode(subJson);
  384. root->release();
  385. return root;
  386. }
  387. Node* CSLoader::loadNode(const rapidjson::Value& json)
  388. {
  389. Node* node = nullptr;
  390. std::string nodeType = DICTOOL->getStringValue_json(json, CLASSNAME);
  391. NodeCreateFunc func = _funcs[nodeType];
  392. if (func != nullptr)
  393. {
  394. const rapidjson::Value& options = DICTOOL->getSubDictionary_json(json, OPTIONS);
  395. node = func(options);
  396. // component
  397. if (node)
  398. {
  399. const rapidjson::Value& components = DICTOOL->getSubDictionary_json(options, COMPONENTS);
  400. int componentSize = DICTOOL->getArrayCount_json(options, COMPONENTS, 0);
  401. for (int i = 0; i < componentSize; ++i)
  402. {
  403. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(components, COMPONENTS, i);
  404. Component* component = loadComponent(dic);
  405. if (component)
  406. {
  407. node->addComponent(component);
  408. }
  409. }
  410. }
  411. }
  412. if(node)
  413. {
  414. int length = DICTOOL->getArrayCount_json(json, CHILDREN, 0);
  415. for (int i = 0; i<length; i++)
  416. {
  417. const rapidjson::Value &dic = DICTOOL->getSubDictionary_json(json, CHILDREN, i);
  418. Node* child = loadNode(dic);
  419. if (child)
  420. {
  421. PageView* pageView = dynamic_cast<PageView*>(node);
  422. ListView* listView = dynamic_cast<ListView*>(node);
  423. if (pageView)
  424. {
  425. Layout* layout = dynamic_cast<Layout*>(child);
  426. if (layout)
  427. {
  428. pageView->addPage(layout);
  429. }
  430. }
  431. else if (listView)
  432. {
  433. Widget* widget = dynamic_cast<Widget*>(child);
  434. if (widget)
  435. {
  436. listView->pushBackCustomItem(widget);
  437. }
  438. }
  439. else
  440. {
  441. if (_monoCocos2dxVersion != "3.x")
  442. {
  443. Widget* widget = dynamic_cast<Widget*>(child);
  444. Widget* parent = dynamic_cast<Widget*>(node);
  445. if (widget
  446. && parent
  447. && !dynamic_cast<Layout*>(parent))
  448. {
  449. if (widget->getPositionType() == ui::Widget::PositionType::PERCENT)
  450. {
  451. widget->setPositionPercent(Vec2(widget->getPositionPercent().x + parent->getAnchorPoint().x, widget->getPositionPercent().y + parent->getAnchorPoint().y));
  452. widget->setPosition(Vec2(widget->getPositionX() + parent->getAnchorPointInPoints().x, widget->getPositionY() + parent->getAnchorPointInPoints().y));
  453. }
  454. else
  455. {
  456. Size parentSize = parent->getContentSize();
  457. widget->setPosition(Vec2(widget->getPositionX() + parentSize.width * parent->getAnchorPoint().x,
  458. widget->getPositionY() + parentSize.height * parent->getAnchorPoint().y));
  459. }
  460. }
  461. }
  462. node->addChild(child);
  463. }
  464. child->release();
  465. }
  466. }
  467. }
  468. else
  469. {
  470. CCLOG("Not supported NodeType: %s", nodeType.c_str());
  471. }
  472. return node;
  473. }
  474. void CSLoader::initNode(Node* node, const rapidjson::Value& json)
  475. {
  476. float width = DICTOOL->getFloatValue_json(json, WIDTH);
  477. float height = DICTOOL->getFloatValue_json(json, HEIGHT);
  478. float x = DICTOOL->getFloatValue_json(json, X);
  479. float y = DICTOOL->getFloatValue_json(json, Y);
  480. float scalex = DICTOOL->getFloatValue_json(json, SCALE_X, 1);
  481. float scaley = DICTOOL->getFloatValue_json(json, SCALE_Y, 1);
  482. float rotation = DICTOOL->getFloatValue_json(json, ROTATION);
  483. float rotationSkewX = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_X);
  484. float rotationSkewY = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_Y);
  485. float skewx = DICTOOL->getFloatValue_json(json, SKEW_X);
  486. float skewy = DICTOOL->getFloatValue_json(json, SKEW_Y);
  487. float anchorx = DICTOOL->getFloatValue_json(json, ANCHOR_X, 0.5f);
  488. float anchory = DICTOOL->getFloatValue_json(json, ANCHOR_Y, 0.5f);
  489. GLubyte alpha = (GLubyte)DICTOOL->getIntValue_json(json, ALPHA, 255);
  490. GLubyte red = (GLubyte)DICTOOL->getIntValue_json(json, RED, 255);
  491. GLubyte green = (GLubyte)DICTOOL->getIntValue_json(json, GREEN, 255);
  492. GLubyte blue = (GLubyte)DICTOOL->getIntValue_json(json, BLUE, 255);
  493. int zorder = DICTOOL->getIntValue_json(json, ZORDER);
  494. int tag = DICTOOL->getIntValue_json(json, TAG);
  495. int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
  496. bool visible = DICTOOL->getBooleanValue_json(json, VISIBLE);
  497. if(x != 0 || y != 0)
  498. node->setPosition(Point(x, y));
  499. if(scalex != 1)
  500. node->setScaleX(scalex);
  501. if(scaley != 1)
  502. node->setScaleY(scaley);
  503. if (rotation != 0)
  504. node->setRotation(rotation);
  505. if(rotationSkewX != 0)
  506. node->setRotationSkewX(rotationSkewX);
  507. if(rotationSkewY != 0)
  508. node->setRotationSkewY(rotationSkewY);
  509. if(skewx != 0)
  510. node->setSkewX(skewx);
  511. if(skewy != 0)
  512. node->setSkewY(skewy);
  513. if(anchorx != 0.5f || anchory != 0.5f)
  514. node->setAnchorPoint(Point(anchorx, anchory));
  515. if(width != 0 || height != 0)
  516. node->setContentSize(Size(width, height));
  517. if(zorder != 0)
  518. node->setLocalZOrder(zorder);
  519. if(visible != true)
  520. node->setVisible(visible);
  521. if(alpha != 255)
  522. {
  523. node->setOpacity(alpha);
  524. }
  525. if(red != 255 || green != 255 || blue != 255)
  526. {
  527. node->setColor(Color3B(red, green, blue));
  528. }
  529. node->setTag(tag);
  530. node->setUserObject(ActionTimelineData::create(actionTag));
  531. }
  532. Node* CSLoader::loadSimpleNode(const rapidjson::Value& json)
  533. {
  534. Node* node = Node::create();
  535. // fix memory leak for v3.3
  536. //node->retain();
  537. initNode(node, json);
  538. return node;
  539. }
  540. Node* CSLoader::loadSubGraph(const rapidjson::Value& json)
  541. {
  542. const char* filePath = DICTOOL->getStringValue_json(json, FILE_PATH);
  543. Node* node = nullptr;
  544. if (filePath && strcmp("", filePath) != 0)
  545. {
  546. node = createNode(filePath);
  547. }
  548. else
  549. {
  550. node = Node::create();
  551. }
  552. // fix memory leak for v3.3
  553. //node->retain();
  554. initNode(node, json);
  555. return node;
  556. }
  557. Node* CSLoader::loadSprite(const rapidjson::Value& json)
  558. {
  559. const char* filePath = DICTOOL->getStringValue_json(json, FILE_PATH);
  560. Sprite *sprite = nullptr;
  561. if(filePath != nullptr)
  562. {
  563. std::string path = filePath;
  564. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
  565. if(!spriteFrame)
  566. {
  567. path = _jsonPath + path;
  568. sprite = Sprite::create(path);
  569. }
  570. else
  571. {
  572. sprite = Sprite::createWithSpriteFrame(spriteFrame);
  573. }
  574. if(!sprite)
  575. {
  576. sprite = Sprite::create();
  577. CCLOG("filePath is empty. Create a sprite with no texture");
  578. }
  579. }
  580. else
  581. {
  582. sprite = Sprite::create();
  583. }
  584. // fix memory leak for v3.3
  585. //sprite->retain();
  586. initNode(sprite, json);
  587. bool flipX = DICTOOL->getBooleanValue_json(json, FLIPX);
  588. bool flipY = DICTOOL->getBooleanValue_json(json, FLIPY);
  589. if(flipX != false)
  590. sprite->setFlippedX(flipX);
  591. if(flipY != false)
  592. sprite->setFlippedY(flipY);
  593. return sprite;
  594. }
  595. Node* CSLoader::loadParticle(const rapidjson::Value& json)
  596. {
  597. const char* filePath = DICTOOL->getStringValue_json(json, PLIST_FILE);
  598. int num = DICTOOL->getIntValue_json(json, PARTICLE_NUM);
  599. ParticleSystemQuad* particle = ParticleSystemQuad::create(filePath);
  600. particle->setTotalParticles(num);
  601. // fix memory leak for v3.3
  602. //particle->retain();
  603. initNode(particle, json);
  604. return particle;
  605. }
  606. Node* CSLoader::loadTMXTiledMap(const rapidjson::Value &json)
  607. {
  608. const char* tmxFile = DICTOOL->getStringValue_json(json, TMX_FILE);
  609. const char* tmxString = DICTOOL->getStringValue_json(json, TMX_STRING);
  610. const char* resourcePath = DICTOOL->getStringValue_json(json, RESOURCE_PATH);
  611. TMXTiledMap* tmx = nullptr;
  612. if (tmxFile && strcmp("", tmxFile) != 0)
  613. {
  614. tmx = TMXTiledMap::create(tmxFile);
  615. }
  616. else if ((tmxString && strcmp("", tmxString) != 0)
  617. && (resourcePath && strcmp("", resourcePath) != 0))
  618. {
  619. tmx = TMXTiledMap::createWithXML(tmxString, resourcePath);
  620. }
  621. return tmx;
  622. }
  623. Node* CSLoader::loadWidget(const rapidjson::Value& json)
  624. {
  625. const char* str = DICTOOL->getStringValue_json(json, CLASSNAME);
  626. if(str == nullptr)
  627. return nullptr;
  628. std::string classname = str;
  629. WidgetPropertiesReader0300* widgetPropertiesReader = new (std::nothrow) WidgetPropertiesReader0300();
  630. Widget* widget = nullptr;
  631. if (isWidget(classname))
  632. {
  633. std::string readerName = getGUIClassName(classname);
  634. readerName.append("Reader");
  635. std::string guiClassName = getGUIClassName(classname);
  636. widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(guiClassName));
  637. // fix memory leak for v3.3
  638. //widget->retain();
  639. WidgetReaderProtocol* reader = dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
  640. if (reader && widget)
  641. {
  642. widgetPropertiesReader->setPropsForAllWidgetFromJsonDictionary(reader, widget, json);
  643. }
  644. }
  645. else if (isCustomWidget(classname))
  646. {
  647. widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(classname));
  648. //fix memory leak for v3.3
  649. //widget->retain();
  650. //
  651. // 1st., custom widget parse properties of parent widget with parent widget reader
  652. std::string readerName = getWidgetReaderClassName(widget);
  653. WidgetReaderProtocol* reader = dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
  654. if (reader && widget)
  655. {
  656. widgetPropertiesReader->setPropsForAllWidgetFromJsonDictionary(reader, widget, json);
  657. // 2nd., custom widget parse with custom reader
  658. const char* customProperty = DICTOOL->getStringValue_json(json, "customProperty");
  659. rapidjson::Document customJsonDict;
  660. customJsonDict.Parse<0>(customProperty);
  661. if (customJsonDict.HasParseError())
  662. {
  663. CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
  664. }
  665. widgetPropertiesReader->setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
  666. }
  667. else
  668. {
  669. CCLOG("Widget or WidgetReader doesn't exists!!! Please check your protocol buffers file.");
  670. }
  671. }
  672. CC_SAFE_DELETE(widgetPropertiesReader);
  673. if (widget)
  674. {
  675. float rotationSkewX = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_X);
  676. float rotationSkewY = DICTOOL->getFloatValue_json(json, ROTATION_SKEW_Y);
  677. float skewx = DICTOOL->getFloatValue_json(json, SKEW_X);
  678. float skewy = DICTOOL->getFloatValue_json(json, SKEW_Y);
  679. if(rotationSkewX != 0)
  680. widget->setRotationSkewX(rotationSkewX);
  681. if(rotationSkewY != 0)
  682. widget->setRotationSkewY(rotationSkewY);
  683. if(skewx != 0)
  684. widget->setSkewX(skewx);
  685. if(skewy != 0)
  686. widget->setSkewY(skewy);
  687. int actionTag = DICTOOL->getIntValue_json(json, ACTION_TAG);
  688. widget->setUserObject(ActionTimelineData::create(actionTag));
  689. }
  690. return widget;
  691. }
  692. Component* CSLoader::loadComponent(const rapidjson::Value &json)
  693. {
  694. Component* component = nullptr;
  695. std::string componentType = DICTOOL->getStringValue_json(json, COMPONENT_TYPE);
  696. ComponentCreateFunc func = _componentFuncs[componentType];
  697. if (func != nullptr)
  698. {
  699. component = func(json);
  700. }
  701. return component;
  702. }
  703. Component* CSLoader::loadComAudio(const rapidjson::Value &json)
  704. {
  705. ComAudio* audio = ComAudio::create();
  706. const char* name = DICTOOL->getStringValue_json(json, COMPONENT_NAME);
  707. bool enabled = DICTOOL->getBooleanValue_json(json, COMPONENT_ENABLED);
  708. audio->setName(name);
  709. audio->setEnabled(enabled);
  710. const char* filePath = DICTOOL->getStringValue_json(json, COMPONENT_AUDIO_FILE_PATH);
  711. bool loop = DICTOOL->getBooleanValue_json(json, COMPONENT_LOOP);
  712. audio->setFile(filePath);
  713. audio->setLoop(loop);
  714. return audio;
  715. }
  716. cocos2d::Node* CSLoader::createNode(const Data& data)
  717. {
  718. return createNode(data, nullptr);
  719. }
  720. Node * CSLoader::createNode(const Data& data, const ccNodeLoadCallback &callback)
  721. {
  722. CSLoader * loader = CSLoader::getInstance();
  723. Node * node = nullptr;
  724. do
  725. {
  726. CC_BREAK_IF(data.isNull() || data.getSize() <= 0);
  727. auto csparsebinary = GetCSParseBinary(data.getBytes());
  728. CC_BREAK_IF(nullptr == csparsebinary);
  729. auto csBuildId = csparsebinary->version();
  730. if (csBuildId)
  731. {
  732. CCASSERT(strcmp(loader->_csBuildID.c_str(), csBuildId->c_str()) == 0,
  733. StringUtils::format("%s%s%s%s%s%s%s%s%s%s",
  734. "The reader build id of your Cocos exported file(",
  735. csBuildId->c_str(),
  736. ") and the reader build id in your Cocos2d-x(",
  737. loader->_csBuildID.c_str(),
  738. ") are not match.\n",
  739. "Please get the correct reader(build id ",
  740. csBuildId->c_str(),
  741. ")from ",
  742. "http://www.cocos2d-x.org/filedown/cocos-reader",
  743. " and replace it in your Cocos2d-x").c_str());
  744. }
  745. // decode plist
  746. auto textures = csparsebinary->textures();
  747. int textureSize = csparsebinary->textures()->size();
  748. CCLOG("textureSize = %d", textureSize);
  749. for (int i = 0; i < textureSize; ++i)
  750. {
  751. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
  752. }
  753. node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
  754. } while (0);
  755. loader->reconstructNestNode(node);
  756. return node;
  757. }
  758. Node* CSLoader::createNodeWithFlatBuffersFile(const std::string &filename)
  759. {
  760. return createNodeWithFlatBuffersFile(filename, nullptr);
  761. }
  762. Node* CSLoader::createNodeWithFlatBuffersFile(const std::string &filename, const ccNodeLoadCallback &callback)
  763. {
  764. Node* node = nodeWithFlatBuffersFile(filename, callback);
  765. reconstructNestNode(node);
  766. return node;
  767. }
  768. inline void CSLoader::reconstructNestNode(cocos2d::Node * node)
  769. {
  770. /* To reconstruct nest node as WidgetCallBackHandlerProtocol. */
  771. auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol *>(node);
  772. if (callbackHandler)
  773. {
  774. _callbackHandlers.popBack();
  775. if (_callbackHandlers.empty())
  776. {
  777. _rootNode = nullptr;
  778. CCLOG("Call back handler container has been clear.");
  779. }
  780. else
  781. {
  782. _rootNode = _callbackHandlers.back();
  783. CCLOG("after pop back _rootNode name = %s", _rootNode->getName().c_str());
  784. }
  785. }
  786. }
  787. Node* CSLoader::nodeWithFlatBuffersFile(const std::string &fileName)
  788. {
  789. return nodeWithFlatBuffersFile(fileName, nullptr);
  790. }
  791. Node* CSLoader::nodeWithFlatBuffersFile(const std::string &fileName, const ccNodeLoadCallback &callback)
  792. {
  793. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
  794. CC_ASSERT(FileUtils::getInstance()->isFileExist(fullPath));
  795. Data buf = FileUtils::getInstance()->getDataFromFile(fullPath);
  796. if (buf.isNull())
  797. {
  798. CCLOG("CSLoader::nodeWithFlatBuffersFile - failed read file: %s", fileName.c_str());
  799. CC_ASSERT(false);
  800. return nullptr;
  801. }
  802. auto csparsebinary = GetCSParseBinary(buf.getBytes());
  803. auto csBuildId = csparsebinary->version();
  804. if (csBuildId)
  805. {
  806. CCASSERT(strcmp(_csBuildID.c_str(), csBuildId->c_str()) == 0,
  807. StringUtils::format("%s%s%s%s%s%s%s%s%s%s",
  808. "The reader build id of your Cocos exported file(",
  809. csBuildId->c_str(),
  810. ") and the reader build id in your Cocos2d-x(",
  811. _csBuildID.c_str(),
  812. ") are not match.\n",
  813. "Please get the correct reader(build id ",
  814. csBuildId->c_str(),
  815. ")from ",
  816. "http://www.cocos2d-x.org/filedown/cocos-reader",
  817. " and replace it in your Cocos2d-x").c_str());
  818. }
  819. // decode plist
  820. auto textures = csparsebinary->textures();
  821. int textureSize = textures->size();
  822. for (int i = 0; i < textureSize; ++i)
  823. {
  824. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
  825. }
  826. Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
  827. return node;
  828. }
  829. Node* CSLoader::nodeWithFlatBuffers(const flatbuffers::NodeTree *nodetree)
  830. {
  831. return nodeWithFlatBuffers(nodetree, nullptr);
  832. }
  833. Node* CSLoader::nodeWithFlatBuffers(const flatbuffers::NodeTree *nodetree, const ccNodeLoadCallback &callback)
  834. {
  835. if (nodetree == nullptr)
  836. return nullptr;
  837. {
  838. Node* node = nullptr;
  839. std::string classname = nodetree->classname()->c_str();
  840. auto options = nodetree->options();
  841. if (classname == "ProjectNode")
  842. {
  843. auto reader = ProjectNodeReader::getInstance();
  844. auto projectNodeOptions = (ProjectNodeOptions*)options->data();
  845. std::string filePath = projectNodeOptions->fileName()->c_str();
  846. cocostudio::timeline::ActionTimeline* action = nullptr;
  847. if (filePath != "" && FileUtils::getInstance()->isFileExist(filePath))
  848. {
  849. Data buf = FileUtils::getInstance()->getDataFromFile(filePath);
  850. node = createNode(buf, callback);
  851. action = createTimeline(buf, filePath);
  852. }
  853. else
  854. {
  855. node = Node::create();
  856. }
  857. reader->setPropsWithFlatBuffers(node, options->data());
  858. if (action)
  859. {
  860. action->setTimeSpeed(projectNodeOptions->innerActionSpeed());
  861. node->runAction(action);
  862. action->gotoFrameAndPause(0);
  863. }
  864. }
  865. else if (classname == "SimpleAudio")
  866. {
  867. node = Node::create();
  868. auto reader = ComAudioReader::getInstance();
  869. Component* component = reader->createComAudioWithFlatBuffers(options->data());
  870. if (component)
  871. {
  872. component->setName(PlayableFrame::PLAYABLE_EXTENTION);
  873. node->addComponent(component);
  874. reader->setPropsWithFlatBuffers(node, options->data());
  875. }
  876. }
  877. else
  878. {
  879. std::string customClassName = nodetree->customClassName()->c_str();
  880. if (customClassName != "")
  881. {
  882. classname = customClassName;
  883. }
  884. std::string readername = getGUIClassName(classname);
  885. readername.append("Reader");
  886. NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
  887. if (reader)
  888. {
  889. node = reader->createNodeWithFlatBuffers(options->data());
  890. }
  891. Widget* widget = dynamic_cast<Widget*>(node);
  892. if (widget)
  893. {
  894. std::string callbackName = widget->getCallbackName();
  895. std::string callbackType = widget->getCallbackType();
  896. bindCallback(callbackName, callbackType, widget, _rootNode);
  897. }
  898. /* To reconstruct nest node as WidgetCallBackHandlerProtocol. */
  899. auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol *>(node);
  900. if (callbackHandler)
  901. {
  902. _callbackHandlers.pushBack(node);
  903. _rootNode = _callbackHandlers.back();
  904. }
  905. /**/
  906. // _loadingNodeParentHierarchy.push_back(node);
  907. }
  908. // If node is invalid, there is no necessity to process children of node.
  909. if (!node)
  910. {
  911. return nullptr;
  912. }
  913. auto children = nodetree->children();
  914. int size = children->size();
  915. for (int i = 0; i < size; ++i)
  916. {
  917. auto subNodeTree = children->Get(i);
  918. Node* child = nodeWithFlatBuffers(subNodeTree, callback);
  919. if (child)
  920. {
  921. PageView* pageView = dynamic_cast<PageView*>(node);
  922. ListView* listView = dynamic_cast<ListView*>(node);
  923. if (pageView)
  924. {
  925. Layout* layout = dynamic_cast<Layout*>(child);
  926. if (layout)
  927. {
  928. pageView->addPage(layout);
  929. }
  930. }
  931. else if (listView)
  932. {
  933. Widget* widget = dynamic_cast<Widget*>(child);
  934. if (widget)
  935. {
  936. listView->pushBackCustomItem(widget);
  937. }
  938. }
  939. else
  940. {
  941. node->addChild(child);
  942. }
  943. if (callback)
  944. {
  945. callback(child);
  946. }
  947. }
  948. }
  949. // _loadingNodeParentHierarchy.pop_back();
  950. return node;
  951. }
  952. }
  953. bool CSLoader::bindCallback(const std::string &callbackName,
  954. const std::string &callbackType,
  955. cocos2d::ui::Widget *sender,
  956. cocos2d::Node *handler)
  957. {
  958. if (callbackName.empty())
  959. return false;
  960. auto callbackHandler = dynamic_cast<WidgetCallBackHandlerProtocol *>(handler);
  961. if (callbackHandler) //The handler can handle callback
  962. {
  963. if (callbackType == "Click")
  964. {
  965. Widget::ccWidgetClickCallback callbackFunc = callbackHandler->onLocateClickCallback(callbackName);
  966. if (callbackFunc)
  967. {
  968. sender->addClickEventListener(callbackFunc);
  969. return true;
  970. }
  971. }
  972. else if (callbackType == "Touch")
  973. {
  974. Widget::ccWidgetTouchCallback callbackFunc = callbackHandler->onLocateTouchCallback(callbackName);
  975. if (callbackFunc)
  976. {
  977. sender->addTouchEventListener(callbackFunc);
  978. return true;
  979. }
  980. }
  981. else if (callbackType == "Event")
  982. {
  983. Widget::ccWidgetEventCallback callbackFunc = callbackHandler->onLocateEventCallback(callbackName);
  984. if (callbackFunc)
  985. {
  986. sender->addCCSEventListener(callbackFunc);
  987. return true;
  988. }
  989. }
  990. }
  991. CCLOG("callBackName %s cannot be found", callbackName.c_str());
  992. return false;
  993. }
  994. bool CSLoader::isWidget(const std::string &type)
  995. {
  996. return (type == ClassName_Panel
  997. || type == ClassName_Button
  998. || type == ClassName_CheckBox
  999. || type == ClassName_ImageView
  1000. || type == ClassName_TextAtlas
  1001. || type == ClassName_LabelAtlas
  1002. || type == ClassName_LabelBMFont
  1003. || type == ClassName_TextBMFont
  1004. || type == ClassName_Text
  1005. || type == ClassName_LoadingBar
  1006. || type == ClassName_TextField
  1007. || type == ClassName_Slider
  1008. || type == ClassName_Layout
  1009. || type == ClassName_ScrollView
  1010. || type == ClassName_ListView
  1011. || type == ClassName_PageView
  1012. || type == ClassName_Widget
  1013. || type == ClassName_Label);
  1014. }
  1015. bool CSLoader::isCustomWidget(const std::string &type)
  1016. {
  1017. Widget* widget = dynamic_cast<Widget*>(ObjectFactory::getInstance()->createObject(type));
  1018. if (widget)
  1019. {
  1020. CC_SAFE_DELETE(widget);
  1021. return true;
  1022. }
  1023. return false;
  1024. }
  1025. std::string CSLoader::getGUIClassName(const std::string &name)
  1026. {
  1027. std::string convertedClassName = name;
  1028. if (name == "Panel")
  1029. {
  1030. convertedClassName = "Layout";
  1031. }
  1032. else if (name == "TextArea")
  1033. {
  1034. convertedClassName = "Text";
  1035. }
  1036. else if (name == "TextButton")
  1037. {
  1038. convertedClassName = "Button";
  1039. }
  1040. else if (name == "Label")
  1041. {
  1042. convertedClassName = "Text";
  1043. }
  1044. else if (name == "LabelAtlas")
  1045. {
  1046. convertedClassName = "TextAtlas";
  1047. }
  1048. else if (name == "LabelBMFont")
  1049. {
  1050. convertedClassName = "TextBMFont";
  1051. }
  1052. return convertedClassName;
  1053. }
  1054. std::string CSLoader::getWidgetReaderClassName(Widget* widget)
  1055. {
  1056. std::string readerName;
  1057. // 1st., custom widget parse properties of parent widget with parent widget reader
  1058. if (dynamic_cast<Button*>(widget))
  1059. {
  1060. readerName = "ButtonReader";
  1061. }
  1062. else if (dynamic_cast<CheckBox*>(widget))
  1063. {
  1064. readerName = "CheckBoxReader";
  1065. }
  1066. else if (dynamic_cast<ImageView*>(widget))
  1067. {
  1068. readerName = "ImageViewReader";
  1069. }
  1070. else if (dynamic_cast<TextAtlas*>(widget))
  1071. {
  1072. readerName = "TextAtlasReader";
  1073. }
  1074. else if (dynamic_cast<TextBMFont*>(widget))
  1075. {
  1076. readerName = "TextBMFontReader";
  1077. }
  1078. else if (dynamic_cast<Text*>(widget))
  1079. {
  1080. readerName = "TextReader";
  1081. }
  1082. else if (dynamic_cast<LoadingBar*>(widget))
  1083. {
  1084. readerName = "LoadingBarReader";
  1085. }
  1086. else if (dynamic_cast<Slider*>(widget))
  1087. {
  1088. readerName = "SliderReader";
  1089. }
  1090. else if (dynamic_cast<TextField*>(widget))
  1091. {
  1092. readerName = "TextFieldReader";
  1093. }
  1094. else if (dynamic_cast<ListView*>(widget))
  1095. {
  1096. readerName = "ListViewReader";
  1097. }
  1098. else if (dynamic_cast<PageView*>(widget))
  1099. {
  1100. readerName = "PageViewReader";
  1101. }
  1102. else if (dynamic_cast<ScrollView*>(widget))
  1103. {
  1104. readerName = "ScrollViewReader";
  1105. }
  1106. else if (dynamic_cast<Layout*>(widget))
  1107. {
  1108. readerName = "LayoutReader";
  1109. }
  1110. else if (dynamic_cast<Widget*>(widget))
  1111. {
  1112. readerName = "WidgetReader";
  1113. }
  1114. return readerName;
  1115. }
  1116. void CSLoader::registReaderObject(const std::string &className,
  1117. ObjectFactory::Instance ins)
  1118. {
  1119. ObjectFactory::TInfo t;
  1120. t._class = className;
  1121. t._fun = ins;
  1122. ObjectFactory::getInstance()->registerType(t);
  1123. }
  1124. Node* CSLoader::createNodeWithFlatBuffersForSimulator(const std::string& filename)
  1125. {
  1126. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  1127. fbs->_isSimulator = true;
  1128. FlatBufferBuilder* builder = fbs->createFlatBuffersWithXMLFileForSimulator(filename);
  1129. auto csparsebinary = GetCSParseBinary(builder->GetBufferPointer());
  1130. // decode plist
  1131. auto textures = csparsebinary->textures();
  1132. int textureSize = csparsebinary->textures()->size();
  1133. // CCLOG("textureSize = %d", textureSize);
  1134. for (int i = 0; i < textureSize; ++i)
  1135. {
  1136. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
  1137. }
  1138. auto nodeTree = csparsebinary->nodeTree();
  1139. Node* node = nodeWithFlatBuffersForSimulator(nodeTree);
  1140. _rootNode = nullptr;
  1141. fbs->deleteFlatBufferBuilder();
  1142. return node;
  1143. }
  1144. Node* CSLoader::nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree *nodetree)
  1145. {
  1146. Node* node = nullptr;
  1147. std::string classname = nodetree->classname()->c_str();
  1148. auto options = nodetree->options();
  1149. if (classname == "ProjectNode")
  1150. {
  1151. auto reader = ProjectNodeReader::getInstance();
  1152. auto projectNodeOptions = (ProjectNodeOptions*)options->data();
  1153. std::string filePath = projectNodeOptions->fileName()->c_str();
  1154. cocostudio::timeline::ActionTimeline* action = nullptr;
  1155. if (filePath != "" && FileUtils::getInstance()->isFileExist(filePath))
  1156. {
  1157. node = createNodeWithFlatBuffersForSimulator(filePath);
  1158. action = cocostudio::timeline::ActionTimelineCache::getInstance()->createActionWithFlatBuffersForSimulator(filePath);
  1159. }
  1160. else
  1161. {
  1162. node = Node::create();
  1163. }
  1164. reader->setPropsWithFlatBuffers(node, options->data());
  1165. if (action)
  1166. {
  1167. action->setTimeSpeed(projectNodeOptions->innerActionSpeed());
  1168. node->runAction(action);
  1169. action->gotoFrameAndPause(0);
  1170. }
  1171. }
  1172. else if (classname == "SimpleAudio")
  1173. {
  1174. node = Node::create();
  1175. auto reader = ComAudioReader::getInstance();
  1176. Component* component = reader->createComAudioWithFlatBuffers(options->data());
  1177. if (component)
  1178. {
  1179. node->addComponent(component);
  1180. reader->setPropsWithFlatBuffers(node, options->data());
  1181. }
  1182. }
  1183. else
  1184. {
  1185. std::string readername = getGUIClassName(classname);
  1186. readername.append("Reader");
  1187. NodeReaderProtocol* reader = dynamic_cast<NodeReaderProtocol*>(ObjectFactory::getInstance()->createObject(readername));
  1188. if (reader)
  1189. {
  1190. node = reader->createNodeWithFlatBuffers(options->data());
  1191. }
  1192. Widget* widget = dynamic_cast<Widget*>(node);
  1193. if (widget)
  1194. {
  1195. std::string callbackName = widget->getCallbackName();
  1196. std::string callbackType = widget->getCallbackType();
  1197. bindCallback(callbackName, callbackType, widget, _rootNode);
  1198. }
  1199. if (_rootNode == nullptr)
  1200. {
  1201. _rootNode = node;
  1202. }
  1203. // _loadingNodeParentHierarchy.push_back(node);
  1204. }
  1205. // If node is invalid, there is no necessity to process children of node.
  1206. if (!node)
  1207. {
  1208. return nullptr;
  1209. }
  1210. auto children = nodetree->children();
  1211. int size = children->size();
  1212. for (int i = 0; i < size; ++i)
  1213. {
  1214. auto subNodeTree = children->Get(i);
  1215. Node* child = nodeWithFlatBuffersForSimulator(subNodeTree);
  1216. if (child)
  1217. {
  1218. PageView* pageView = dynamic_cast<PageView*>(node);
  1219. ListView* listView = dynamic_cast<ListView*>(node);
  1220. if (pageView)
  1221. {
  1222. Layout* layout = dynamic_cast<Layout*>(child);
  1223. if (layout)
  1224. {
  1225. pageView->addPage(layout);
  1226. }
  1227. }
  1228. else if (listView)
  1229. {
  1230. Widget* widget = dynamic_cast<Widget*>(child);
  1231. if (widget)
  1232. {
  1233. listView->pushBackCustomItem(widget);
  1234. }
  1235. }
  1236. else
  1237. {
  1238. node->addChild(child);
  1239. }
  1240. }
  1241. }
  1242. // _loadingNodeParentHierarchy.pop_back();
  1243. return node;
  1244. }
  1245. NS_CC_END