CCSGUIReader.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCSGUIReader.h"
  21. #include <fstream>
  22. #include <iostream>
  23. #include "ui/CocosGUI.h"
  24. #include "platform/CCFileUtils.h"
  25. #include "2d/CCSpriteFrameCache.h"
  26. #include "base/CCDirector.h"
  27. #include "base/ccUtils.h"
  28. #include "editor-support/cocostudio/CCActionManagerEx.h"
  29. #include "editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.h"
  30. #include "editor-support/cocostudio/WidgetReader/CheckBoxReader/CheckBoxReader.h"
  31. #include "editor-support/cocostudio/WidgetReader/SliderReader/SliderReader.h"
  32. #include "editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.h"
  33. #include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
  34. #include "editor-support/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
  35. #include "editor-support/cocostudio/WidgetReader/TextReader/TextReader.h"
  36. #include "editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
  37. #include "editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
  38. #include "editor-support/cocostudio/WidgetReader/LayoutReader/LayoutReader.h"
  39. #include "editor-support/cocostudio/WidgetReader/PageViewReader/PageViewReader.h"
  40. #include "editor-support/cocostudio/WidgetReader/ScrollViewReader/ScrollViewReader.h"
  41. #include "editor-support/cocostudio/WidgetReader/ListViewReader/ListViewReader.h"
  42. #include "editor-support/cocostudio/CocoLoader.h"
  43. #include "tinyxml2.h"
  44. using namespace cocos2d;
  45. using namespace cocos2d::ui;
  46. namespace cocostudio {
  47. static GUIReader* sharedReader = nullptr;
  48. GUIReader::GUIReader():
  49. m_strFilePath("")
  50. {
  51. ObjectFactory* factoryCreate = ObjectFactory::getInstance();
  52. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ButtonReader));
  53. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(CheckBoxReader));
  54. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(SliderReader));
  55. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ImageViewReader));
  56. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LoadingBarReader));
  57. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextAtlasReader));
  58. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextReader));
  59. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextBMFontReader));
  60. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextFieldReader));
  61. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LayoutReader));
  62. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(PageViewReader));
  63. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ScrollViewReader));
  64. factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ListViewReader));
  65. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Button));
  66. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(CheckBox));
  67. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ImageView));
  68. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Text));
  69. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextAtlas));
  70. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextBMFont));
  71. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(LoadingBar));
  72. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Slider));
  73. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextField));
  74. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Layout));
  75. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ListView));
  76. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(PageView));
  77. factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ScrollView));
  78. }
  79. GUIReader::~GUIReader()
  80. {
  81. }
  82. GUIReader* GUIReader::getInstance()
  83. {
  84. if (!sharedReader)
  85. {
  86. sharedReader = new (std::nothrow) GUIReader();
  87. }
  88. return sharedReader;
  89. }
  90. void GUIReader::destroyInstance()
  91. {
  92. CC_SAFE_DELETE(sharedReader);
  93. }
  94. int GUIReader::getVersionInteger(const char *str)
  95. {
  96. std::string strVersion = str;
  97. size_t length = strVersion.length();
  98. if (length < 7)
  99. {
  100. return 0;
  101. }
  102. size_t pos = strVersion.find_first_of(".");
  103. std::string t = strVersion.substr(0,pos);
  104. strVersion = strVersion.substr(pos+1,strVersion.length()-1);
  105. pos = strVersion.find_first_of(".");
  106. std::string h = strVersion.substr(0,pos);
  107. strVersion = strVersion.substr(pos+1,strVersion.length()-1);
  108. pos = strVersion.find_first_of(".");
  109. std::string te = strVersion.substr(0,pos);
  110. strVersion = strVersion.substr(pos+1,strVersion.length()-1);
  111. pos = strVersion.find_first_of(".");
  112. std::string s = strVersion.substr(0,pos);
  113. int it = atoi(t.c_str());
  114. int ih = atoi(h.c_str());
  115. int ite = atoi(te.c_str());
  116. int is = atoi(s.c_str());
  117. int iVersion = it*1000+ih*100+ite*10+is;
  118. // CCLOG("iversion %d",iVersion);
  119. return iVersion;
  120. /************************/
  121. }
  122. void GUIReader::storeFileDesignSize(const char *fileName, const cocos2d::Size &size)
  123. {
  124. std::string keyWidth = fileName;
  125. keyWidth.append("width");
  126. std::string keyHeight = fileName;
  127. keyHeight.append("height");
  128. _fileDesignSizes[keyWidth] = cocos2d::Value(size.width);
  129. _fileDesignSizes[keyHeight] = cocos2d::Value(size.height);
  130. }
  131. cocos2d::Size GUIReader::getFileDesignSize(const char* fileName) const
  132. {
  133. std::string keyWidth = fileName;
  134. keyWidth.append("width");
  135. std::string keyHeight = fileName;
  136. keyHeight.append("height");
  137. float w = _fileDesignSizes.at(keyWidth).asFloat();
  138. float h = _fileDesignSizes.at(keyHeight).asFloat();
  139. return Size(w, h);
  140. }
  141. void GUIReader::registerTypeAndCallBack(const std::string& classType,
  142. ObjectFactory::Instance ins,
  143. Ref *object,
  144. SEL_ParseEvent callBack)
  145. {
  146. ObjectFactory* factoryCreate = ObjectFactory::getInstance();
  147. ObjectFactory::TInfo t(classType, ins);
  148. factoryCreate->registerType(t);
  149. if (object)
  150. {
  151. _mapObject.insert(ParseObjectMap::value_type(classType, object));
  152. }
  153. if (callBack)
  154. {
  155. _mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
  156. }
  157. }
  158. void GUIReader::registerTypeAndCallBack(const std::string& classType,
  159. ObjectFactory::InstanceFunc ins,
  160. Ref *object,
  161. SEL_ParseEvent callBack)
  162. {
  163. ObjectFactory* factoryCreate = ObjectFactory::getInstance();
  164. ObjectFactory::TInfo t(classType, ins);
  165. factoryCreate->registerType(t);
  166. if (object)
  167. {
  168. _mapObject.insert(ParseObjectMap::value_type(classType, object));
  169. }
  170. if (callBack)
  171. {
  172. _mapParseSelector.insert(ParseCallBackMap::value_type(classType, callBack));
  173. }
  174. }
  175. Widget* GUIReader::widgetFromJsonFile(const char *fileName)
  176. {
  177. std::string jsonpath;
  178. rapidjson::Document jsonDict;
  179. jsonpath = fileName;
  180. // jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
  181. size_t pos = jsonpath.find_last_of('/');
  182. m_strFilePath = jsonpath.substr(0,pos+1);
  183. std::string contentStr = FileUtils::getInstance()->getStringFromFile(jsonpath);
  184. jsonDict.Parse<0>(contentStr.c_str());
  185. if (jsonDict.HasParseError())
  186. {
  187. CCLOG("GetParseError %d\n",jsonDict.GetParseError());
  188. }
  189. Widget* widget = nullptr;
  190. const char* fileVersion = DICTOOL->getStringValue_json(jsonDict, "version");
  191. WidgetPropertiesReader * pReader = nullptr;
  192. if (fileVersion)
  193. {
  194. int versionInteger = getVersionInteger(fileVersion);
  195. if (versionInteger < 250)
  196. {
  197. pReader = new (std::nothrow) WidgetPropertiesReader0250();
  198. widget = pReader->createWidget(jsonDict, m_strFilePath.c_str(), fileName);
  199. }
  200. else
  201. {
  202. pReader = new (std::nothrow) WidgetPropertiesReader0300();
  203. widget = pReader->createWidget(jsonDict, m_strFilePath.c_str(), fileName);
  204. }
  205. }
  206. else
  207. {
  208. pReader = new (std::nothrow) WidgetPropertiesReader0250();
  209. widget = pReader->createWidget(jsonDict, m_strFilePath.c_str(), fileName);
  210. }
  211. CC_SAFE_DELETE(pReader);
  212. return widget;
  213. }
  214. std::string WidgetPropertiesReader::getWidgetReaderClassName(Widget* widget)
  215. {
  216. std::string readerName;
  217. // 1st., custom widget parse properties of parent widget with parent widget reader
  218. if (dynamic_cast<Button*>(widget))
  219. {
  220. readerName = "ButtonReader";
  221. }
  222. else if (dynamic_cast<CheckBox*>(widget))
  223. {
  224. readerName = "CheckBoxReader";
  225. }
  226. else if (dynamic_cast<ImageView*>(widget))
  227. {
  228. readerName = "ImageViewReader";
  229. }
  230. else if (dynamic_cast<TextAtlas*>(widget))
  231. {
  232. readerName = "TextAtlasReader";
  233. }
  234. else if (dynamic_cast<TextBMFont*>(widget))
  235. {
  236. readerName = "TextBMFontReader";
  237. }
  238. else if (dynamic_cast<Text*>(widget))
  239. {
  240. readerName = "TextReader";
  241. }
  242. else if (dynamic_cast<LoadingBar*>(widget))
  243. {
  244. readerName = "LoadingBarReader";
  245. }
  246. else if (dynamic_cast<Slider*>(widget))
  247. {
  248. readerName = "SliderReader";
  249. }
  250. else if (dynamic_cast<TextField*>(widget))
  251. {
  252. readerName = "TextFieldReader";
  253. }
  254. else if (dynamic_cast<ListView*>(widget))
  255. {
  256. readerName = "ListViewReader";
  257. }
  258. else if (dynamic_cast<PageView*>(widget))
  259. {
  260. readerName = "PageViewReader";
  261. }
  262. else if (dynamic_cast<ScrollView*>(widget))
  263. {
  264. readerName = "ScrollViewReader";
  265. }
  266. else if (dynamic_cast<Layout*>(widget))
  267. {
  268. readerName = "LayoutReader";
  269. }
  270. else if (dynamic_cast<Widget*>(widget))
  271. {
  272. readerName = "WidgetReader";
  273. }
  274. return readerName;
  275. }
  276. std::string WidgetPropertiesReader::getGUIClassName(const std::string &name)
  277. {
  278. std::string convertedClassName = name;
  279. if (name == "Panel")
  280. {
  281. convertedClassName = "Layout";
  282. }
  283. else if (name == "TextArea")
  284. {
  285. convertedClassName = "Text";
  286. }
  287. else if (name == "TextButton")
  288. {
  289. convertedClassName = "Button";
  290. }
  291. else if (name == "Label")
  292. {
  293. convertedClassName = "Text";
  294. }
  295. else if (name == "LabelAtlas")
  296. {
  297. convertedClassName = "TextAtlas";
  298. }
  299. else if (name == "LabelBMFont")
  300. {
  301. convertedClassName = "TextBMFont";
  302. }
  303. return convertedClassName;
  304. }
  305. cocos2d::ui::Widget* WidgetPropertiesReader::createGUI(const std::string &classname)
  306. {
  307. std::string name = this->getGUIClassName(classname);
  308. Ref* object = ObjectFactory::getInstance()->createObject(name);
  309. return dynamic_cast<ui::Widget*>(object);
  310. }
  311. WidgetReaderProtocol* WidgetPropertiesReader::createWidgetReaderProtocol(const std::string &classname)
  312. {
  313. Ref* object = ObjectFactory::getInstance()->createObject(classname);
  314. return dynamic_cast<WidgetReaderProtocol*>(object);
  315. }
  316. Widget* GUIReader::widgetFromBinaryFile(const char *fileName)
  317. {
  318. std::string jsonpath;
  319. rapidjson::Document jsonDict;
  320. jsonpath = fileName;
  321. // jsonpath = CCFileUtils::getInstance()->fullPathForFilename(fileName);
  322. size_t pos = jsonpath.find_last_of('/');
  323. m_strFilePath = jsonpath.substr(0,pos+1);
  324. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
  325. auto fileData = FileUtils::getInstance()->getDataFromFile(fullPath);
  326. auto fileDataBytes = fileData.getBytes();
  327. auto fileDataSize = fileData.getSize();
  328. const char* fileVersion = "";
  329. ui::Widget* widget = nullptr;
  330. if (fileDataBytes != nullptr && fileDataSize > 0)
  331. {
  332. CocoLoader tCocoLoader;
  333. if(true == tCocoLoader.ReadCocoBinBuff((char*)fileDataBytes))
  334. {
  335. stExpCocoNode* tpRootCocoNode = tCocoLoader.GetRootCocoNode();
  336. rapidjson::Type tType = tpRootCocoNode->GetType(&tCocoLoader);
  337. if (rapidjson::kObjectType == tType || rapidjson::kArrayType == tType)
  338. {
  339. stExpCocoNode *tpChildArray = tpRootCocoNode->GetChildArray(&tCocoLoader);
  340. for (int i = 0; i < tpRootCocoNode->GetChildNum(); ++i) {
  341. std::string key = tpChildArray[i].GetName(&tCocoLoader);
  342. if (key == "version") {
  343. fileVersion = tpChildArray[i].GetValue(&tCocoLoader);
  344. break;
  345. }
  346. }
  347. WidgetPropertiesReader * pReader = nullptr;
  348. if (fileVersion)
  349. {
  350. int versionInteger = getVersionInteger(fileVersion);
  351. if (versionInteger < 250)
  352. {
  353. CCASSERT(0, "You current studio doesn't support binary format, please upgrade to the latest version!");
  354. pReader = new (std::nothrow) WidgetPropertiesReader0250();
  355. widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
  356. }
  357. else
  358. {
  359. pReader = new (std::nothrow) WidgetPropertiesReader0300();
  360. widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
  361. }
  362. }
  363. else
  364. {
  365. pReader = new (std::nothrow) WidgetPropertiesReader0250();
  366. widget = pReader->createWidgetFromBinary(&tCocoLoader, tpRootCocoNode, fileName);
  367. }
  368. CC_SAFE_DELETE(pReader);
  369. }
  370. }
  371. }
  372. return widget;
  373. }
  374. std::string WidgetPropertiesReader::getWidgetReaderClassName(const std::string& classname)
  375. {
  376. // create widget reader to parse properties of widget
  377. std::string readerName = classname;
  378. if (readerName == "Panel")
  379. {
  380. readerName = "Layout";
  381. }
  382. else if (readerName == "TextArea")
  383. {
  384. readerName = "Text";
  385. }
  386. else if (readerName == "TextButton")
  387. {
  388. readerName = "Button";
  389. }
  390. else if (readerName == "Label")
  391. {
  392. readerName = "Text";
  393. }
  394. else if (readerName == "LabelAtlas")
  395. {
  396. readerName = "TextAtlas";
  397. }
  398. else if (readerName == "LabelBMFont")
  399. {
  400. readerName = "TextBMFont";
  401. }
  402. readerName.append("Reader");
  403. return readerName;
  404. }
  405. void WidgetPropertiesReader::setAnchorPointForWidget(cocos2d::ui::Widget *widget, const rapidjson::Value &options)
  406. {
  407. bool isAnchorPointXExists = DICTOOL->checkObjectExist_json(options, "anchorPointX");
  408. float anchorPointXInFile;
  409. if (isAnchorPointXExists) {
  410. anchorPointXInFile = DICTOOL->getFloatValue_json(options, "anchorPointX");
  411. }else{
  412. anchorPointXInFile = widget->getAnchorPoint().x;
  413. }
  414. bool isAnchorPointYExists = DICTOOL->checkObjectExist_json(options, "anchorPointY");
  415. float anchorPointYInFile;
  416. if (isAnchorPointYExists) {
  417. anchorPointYInFile = DICTOOL->getFloatValue_json(options, "anchorPointY");
  418. }
  419. else{
  420. anchorPointYInFile = widget->getAnchorPoint().y;
  421. }
  422. if (isAnchorPointXExists || isAnchorPointYExists) {
  423. widget->setAnchorPoint(Vec2(anchorPointXInFile, anchorPointYInFile));
  424. }
  425. }
  426. Widget* WidgetPropertiesReader0250::createWidget(const rapidjson::Value& data, const char* fullPath, const char* fileName)
  427. {
  428. m_strFilePath = fullPath;
  429. int texturesCount = DICTOOL->getArrayCount_json(data, "textures");
  430. for (int i=0; i<texturesCount; i++)
  431. {
  432. const char* file = DICTOOL->getStringValueFromArray_json(data, "textures", i);
  433. std::string tp = fullPath;
  434. tp.append(file);
  435. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp);
  436. }
  437. float fileDesignWidth = DICTOOL->getFloatValue_json(data, "designWidth");
  438. float fileDesignHeight = DICTOOL->getFloatValue_json(data, "designHeight");
  439. if (fileDesignWidth <= 0 || fileDesignHeight <= 0) {
  440. CCLOGERROR("Read design size error!\n");
  441. Size winSize = Director::getInstance()->getWinSize();
  442. GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
  443. }
  444. else
  445. {
  446. GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
  447. }
  448. const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data, "widgetTree");
  449. Widget* widget = widgetFromJsonDictionary(widgetTree);
  450. /* *********temp********* */
  451. if (widget->getContentSize().equals(Size::ZERO))
  452. {
  453. Layout* rootWidget = dynamic_cast<Layout*>(widget);
  454. rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
  455. }
  456. /* ********************** */
  457. // widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight));
  458. const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation");
  459. /* *********temp********* */
  460. // ActionManager::getInstance()->releaseActions();
  461. /* ********************** */
  462. // CCLOG("file name == [%s]",fileName);
  463. Ref* rootWidget = (Ref*) widget;
  464. ActionManagerEx::getInstance()->initWithDictionary(fileName,actions,rootWidget);
  465. return widget;
  466. }
  467. Widget* WidgetPropertiesReader0250::widgetFromJsonDictionary(const rapidjson::Value&data)
  468. {
  469. Widget* widget = nullptr;
  470. const char* classname = DICTOOL->getStringValue_json(data, "classname");
  471. const rapidjson::Value& uiOptions = DICTOOL->getSubDictionary_json(data, "options");
  472. if (classname && strcmp(classname, "Button") == 0)
  473. {
  474. widget = cocos2d::ui::Button::create();
  475. setPropsForButtonFromJsonDictionary(widget, uiOptions);
  476. }
  477. else if (classname && strcmp(classname, "CheckBox") == 0)
  478. {
  479. widget = CheckBox::create();
  480. setPropsForCheckBoxFromJsonDictionary(widget, uiOptions);
  481. }
  482. else if (classname && strcmp(classname, "Label") == 0)
  483. {
  484. widget = cocos2d::ui::Text::create();
  485. setPropsForLabelFromJsonDictionary(widget, uiOptions);
  486. }
  487. else if (classname && strcmp(classname, "LabelAtlas") == 0)
  488. {
  489. widget = cocos2d::ui::TextAtlas::create();
  490. setPropsForLabelAtlasFromJsonDictionary(widget, uiOptions);
  491. }
  492. else if (classname && strcmp(classname, "LoadingBar") == 0)
  493. {
  494. widget = cocos2d::ui::LoadingBar::create();
  495. setPropsForLoadingBarFromJsonDictionary(widget, uiOptions);
  496. }else if (classname && strcmp(classname, "ScrollView") == 0){
  497. widget = cocos2d::ui::ScrollView::create();
  498. setPropsForScrollViewFromJsonDictionary(widget, uiOptions);
  499. }
  500. else if (classname && strcmp(classname, "TextArea") == 0)
  501. {
  502. widget = cocos2d::ui::Text::create();
  503. setPropsForLabelFromJsonDictionary(widget, uiOptions);
  504. }
  505. else if (classname && strcmp(classname, "TextButton") == 0)
  506. {
  507. widget = cocos2d::ui::Button::create();
  508. setPropsForButtonFromJsonDictionary(widget, uiOptions);
  509. }
  510. else if (classname && strcmp(classname, "TextField") == 0)
  511. {
  512. widget = cocos2d::ui::TextField::create();
  513. setPropsForTextFieldFromJsonDictionary(widget, uiOptions);
  514. }
  515. else if (classname && strcmp(classname, "ImageView") == 0)
  516. {
  517. widget = cocos2d::ui::ImageView::create();
  518. setPropsForImageViewFromJsonDictionary(widget, uiOptions);
  519. }
  520. else if (classname && strcmp(classname, "Panel") == 0)
  521. {
  522. widget = Layout::create();
  523. setPropsForLayoutFromJsonDictionary(widget, uiOptions);
  524. }
  525. else if (classname && strcmp(classname, "Slider") == 0)
  526. {
  527. widget = cocos2d::ui::Slider::create();
  528. setPropsForSliderFromJsonDictionary(widget, uiOptions);
  529. }
  530. else if (classname && strcmp(classname, "LabelBMFont") == 0)
  531. {
  532. widget = cocos2d::ui::TextBMFont::create();
  533. setPropsForLabelBMFontFromJsonDictionary(widget, uiOptions);
  534. }
  535. else if (classname && strcmp(classname, "DragPanel") == 0)
  536. {
  537. widget = cocos2d::ui::ScrollView::create();
  538. setPropsForScrollViewFromJsonDictionary(widget, uiOptions);
  539. }
  540. int childrenCount = DICTOOL->getArrayCount_json(data, "children");
  541. for (int i=0;i<childrenCount;i++)
  542. {
  543. const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(data, "children", i);
  544. Widget* child = widgetFromJsonDictionary(subData);
  545. if (child)
  546. {
  547. widget->addChild(child);
  548. }
  549. }
  550. return widget;
  551. }
  552. void WidgetPropertiesReader0250::setPropsForWidgetFromJsonDictionary(Widget*widget,const rapidjson::Value&options)
  553. {
  554. bool ignoreSizeExsit = DICTOOL->checkObjectExist_json(options, "ignoreSize");
  555. if (ignoreSizeExsit)
  556. {
  557. widget->ignoreContentAdaptWithSize(DICTOOL->getBooleanValue_json(options, "ignoreSize"));
  558. }
  559. float w = DICTOOL->getFloatValue_json(options, "width");
  560. float h = DICTOOL->getFloatValue_json(options, "height");
  561. widget->setContentSize(Size(w, h));
  562. widget->setTag(DICTOOL->getIntValue_json(options, "tag"));
  563. widget->setActionTag(DICTOOL->getIntValue_json(options, "actiontag"));
  564. widget->setTouchEnabled(DICTOOL->getBooleanValue_json(options, "touchAble"));
  565. const char* name = DICTOOL->getStringValue_json(options, "name");
  566. const char* widgetName = name?name:"default";
  567. widget->setName(widgetName);
  568. float x = DICTOOL->getFloatValue_json(options, "x");
  569. float y = DICTOOL->getFloatValue_json(options, "y");
  570. widget->setPosition(Vec2(x,y));
  571. bool sx = DICTOOL->checkObjectExist_json(options, "scaleX");
  572. if (sx)
  573. {
  574. widget->setScaleX(DICTOOL->getFloatValue_json(options, "scaleX"));
  575. }
  576. bool sy = DICTOOL->checkObjectExist_json(options, "scaleY");
  577. if (sy)
  578. {
  579. widget->setScaleY(DICTOOL->getFloatValue_json(options, "scaleY"));
  580. }
  581. bool rt = DICTOOL->checkObjectExist_json(options, "rotation");
  582. if (rt)
  583. {
  584. widget->setRotation(DICTOOL->getFloatValue_json(options, "rotation"));
  585. }
  586. bool vb = DICTOOL->checkObjectExist_json(options, "visible");
  587. if (vb)
  588. {
  589. widget->setVisible(DICTOOL->getBooleanValue_json(options, "visible"));
  590. }
  591. int z = DICTOOL->getIntValue_json(options, "ZOrder");
  592. widget->setLocalZOrder(z);
  593. }
  594. void WidgetPropertiesReader0250::setColorPropsForWidgetFromJsonDictionary(Widget *widget, const rapidjson::Value&options)
  595. {
  596. bool op = DICTOOL->checkObjectExist_json(options, "opacity");
  597. if (op)
  598. {
  599. widget->setOpacity(DICTOOL->getIntValue_json(options, "opacity"));
  600. }
  601. bool cr = DICTOOL->checkObjectExist_json(options, "colorR");
  602. bool cg = DICTOOL->checkObjectExist_json(options, "colorG");
  603. bool cb = DICTOOL->checkObjectExist_json(options, "colorB");
  604. int colorR = cr ? DICTOOL->getIntValue_json(options, "colorR") : 255;
  605. int colorG = cg ? DICTOOL->getIntValue_json(options, "colorG") : 255;
  606. int colorB = cb ? DICTOOL->getIntValue_json(options, "colorB") : 255;
  607. widget->setColor(Color3B(colorR, colorG, colorB));
  608. this->setAnchorPointForWidget(widget, options);
  609. bool flipX = DICTOOL->getBooleanValue_json(options, "flipX");
  610. bool flipY = DICTOOL->getBooleanValue_json(options, "flipY");
  611. widget->setFlippedX(flipX);
  612. widget->setFlippedY(flipY);
  613. }
  614. void WidgetPropertiesReader0250::setPropsForButtonFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  615. {
  616. setPropsForWidgetFromJsonDictionary(widget, options);
  617. cocos2d::ui::Button* button = static_cast<Button*>(widget);
  618. bool scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
  619. button->setScale9Enabled(scale9Enable);
  620. std::string tp_n = m_strFilePath;
  621. std::string tp_p = m_strFilePath;
  622. std::string tp_d = m_strFilePath;
  623. const char* normalFileName = DICTOOL->getStringValue_json(options, "normal");
  624. const char* pressedFileName = DICTOOL->getStringValue_json(options, "pressed");
  625. const char* disabledFileName = DICTOOL->getStringValue_json(options, "disabled");
  626. const char* normalFileName_tp = (normalFileName && (strcmp(normalFileName, "") != 0))?tp_n.append(normalFileName).c_str():nullptr;
  627. const char* pressedFileName_tp = (pressedFileName && (strcmp(pressedFileName, "") != 0))?tp_p.append(pressedFileName).c_str():nullptr;
  628. const char* disabledFileName_tp = (disabledFileName && (strcmp(disabledFileName, "") != 0))?tp_d.append(disabledFileName).c_str():nullptr;
  629. bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
  630. if (scale9Enable)
  631. {
  632. float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
  633. float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
  634. float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
  635. float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
  636. if (useMergedTexture)
  637. {
  638. button->loadTextures(normalFileName, pressedFileName, disabledFileName,Widget::TextureResType::PLIST);
  639. }
  640. else
  641. {
  642. button->loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
  643. }
  644. button->setCapInsets(Rect(cx, cy, cw, ch));
  645. bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
  646. bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
  647. if (sw && sh)
  648. {
  649. float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
  650. float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
  651. button->setContentSize(Size(swf, shf));
  652. }
  653. }
  654. else
  655. {
  656. if (useMergedTexture)
  657. {
  658. button->loadTextures(normalFileName, pressedFileName, disabledFileName,Widget::TextureResType::PLIST);
  659. }
  660. else
  661. {
  662. button->loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp);
  663. }
  664. }
  665. bool tt = DICTOOL->checkObjectExist_json(options, "text");
  666. if (tt)
  667. {
  668. const char* text = DICTOOL->getStringValue_json(options, "text");
  669. if (text)
  670. {
  671. button->setTitleText(text);
  672. }
  673. }
  674. bool cr = DICTOOL->checkObjectExist_json(options, "textColorR");
  675. bool cg = DICTOOL->checkObjectExist_json(options, "textColorG");
  676. bool cb = DICTOOL->checkObjectExist_json(options, "textColorB");
  677. int cri = cr?DICTOOL->getIntValue_json(options, "textColorR"):255;
  678. int cgi = cg?DICTOOL->getIntValue_json(options, "textColorG"):255;
  679. int cbi = cb?DICTOOL->getIntValue_json(options, "textColorB"):255;
  680. button->setTitleColor(Color3B(cri,cgi,cbi));
  681. bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
  682. if (fs)
  683. {
  684. button->setTitleFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
  685. }
  686. bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
  687. if (fn)
  688. {
  689. const char * szTemp = DICTOOL->getStringValue_json(options, "fontName");
  690. if (szTemp && *szTemp)
  691. button->setTitleFontName(szTemp);
  692. else
  693. button->setTitleFontName(std::string(""));
  694. }
  695. setColorPropsForWidgetFromJsonDictionary(widget,options);
  696. }
  697. void WidgetPropertiesReader0250::setPropsForCheckBoxFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  698. {
  699. setPropsForWidgetFromJsonDictionary(widget, options);
  700. CheckBox* checkBox = static_cast<CheckBox*>(widget);
  701. const char* backGroundFileName = DICTOOL->getStringValue_json(options, "backGroundBox");
  702. const char* backGroundSelectedFileName = DICTOOL->getStringValue_json(options, "backGroundBoxSelected");
  703. const char* frontCrossFileName = DICTOOL->getStringValue_json(options, "frontCross");
  704. const char* backGroundDisabledFileName = DICTOOL->getStringValue_json(options, "backGroundBoxDisabled");
  705. const char* frontCrossDisabledFileName = DICTOOL->getStringValue_json(options, "frontCrossDisabled");
  706. std::string tp_b = m_strFilePath;
  707. std::string tp_bs = m_strFilePath;
  708. std::string tp_c = m_strFilePath;
  709. std::string tp_bd = m_strFilePath;
  710. std::string tp_cd = m_strFilePath;
  711. const char* backGroundFileName_tp = (backGroundFileName && (strcmp(backGroundFileName, "") != 0))?tp_b.append(backGroundFileName).c_str():nullptr;
  712. const char* backGroundSelectedFileName_tp = (backGroundSelectedFileName && (strcmp(backGroundSelectedFileName, "") != 0))?tp_bs.append(backGroundSelectedFileName).c_str():nullptr;
  713. const char* frontCrossFileName_tp = (frontCrossFileName && (strcmp(frontCrossFileName, "") != 0))?tp_c.append(frontCrossFileName).c_str():nullptr;
  714. const char* backGroundDisabledFileName_tp = (backGroundDisabledFileName && (strcmp(backGroundDisabledFileName, "") != 0))?tp_bd.append(backGroundDisabledFileName).c_str():nullptr;
  715. const char* frontCrossDisabledFileName_tp = (frontCrossDisabledFileName && (strcmp(frontCrossDisabledFileName, "") != 0))?tp_cd.append(frontCrossDisabledFileName).c_str():nullptr;
  716. bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
  717. if (useMergedTexture)
  718. {
  719. checkBox->loadTextures(backGroundFileName, backGroundSelectedFileName, frontCrossFileName,backGroundDisabledFileName,frontCrossDisabledFileName,Widget::TextureResType::PLIST);
  720. }
  721. else
  722. {
  723. checkBox->loadTextures(backGroundFileName_tp, backGroundSelectedFileName_tp, frontCrossFileName_tp,backGroundDisabledFileName_tp,frontCrossDisabledFileName_tp);
  724. }
  725. checkBox->setSelected(DICTOOL->getBooleanValue_json(options, "selectedState"));
  726. setColorPropsForWidgetFromJsonDictionary(widget,options);
  727. }
  728. void WidgetPropertiesReader0250::setPropsForImageViewFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  729. {
  730. setPropsForWidgetFromJsonDictionary(widget, options);
  731. cocos2d::ui::ImageView* imageView = static_cast<ImageView*>(widget);
  732. const char* imageFileName = DICTOOL->getStringValue_json(options, "fileName");
  733. bool scale9EnableExist = DICTOOL->checkObjectExist_json(options, "scale9Enable");
  734. bool scale9Enable = false;
  735. if (scale9EnableExist)
  736. {
  737. scale9Enable = DICTOOL->getBooleanValue_json(options, "scale9Enable");
  738. }
  739. imageView->setScale9Enabled(scale9Enable);
  740. std::string tp_i = m_strFilePath;
  741. const char* imageFileName_tp = nullptr;
  742. if (imageFileName && (strcmp(imageFileName, "") != 0))
  743. {
  744. imageFileName_tp = tp_i.append(imageFileName).c_str();
  745. }
  746. bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
  747. if (scale9Enable)
  748. {
  749. if (useMergedTexture)
  750. {
  751. imageView->loadTexture(imageFileName,Widget::TextureResType::PLIST);
  752. }
  753. else
  754. {
  755. imageView->loadTexture(imageFileName_tp);
  756. }
  757. bool sw = DICTOOL->checkObjectExist_json(options, "scale9Width");
  758. bool sh = DICTOOL->checkObjectExist_json(options, "scale9Height");
  759. if (sw && sh)
  760. {
  761. float swf = DICTOOL->getFloatValue_json(options, "scale9Width");
  762. float shf = DICTOOL->getFloatValue_json(options, "scale9Height");
  763. imageView->setContentSize(Size(swf, shf));
  764. }
  765. float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
  766. float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
  767. float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
  768. float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
  769. imageView->setCapInsets(Rect(cx, cy, cw, ch));
  770. }
  771. else
  772. {
  773. if (useMergedTexture)
  774. {
  775. imageView->loadTexture(imageFileName,Widget::TextureResType::PLIST);
  776. }
  777. else
  778. {
  779. imageView->loadTexture(imageFileName_tp);
  780. }
  781. }
  782. setColorPropsForWidgetFromJsonDictionary(widget,options);
  783. }
  784. void WidgetPropertiesReader0250::setPropsForLabelFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  785. {
  786. setPropsForWidgetFromJsonDictionary(widget, options);
  787. cocos2d::ui::Text* label = static_cast<cocos2d::ui::Text*>(widget);
  788. bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, "touchScaleEnable");
  789. label->setTouchScaleChangeEnabled(touchScaleChangeAble);
  790. const char* text = DICTOOL->getStringValue_json(options, "text");
  791. label->setString(text);
  792. bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
  793. if (fs)
  794. {
  795. label->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
  796. }
  797. bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
  798. if (fn)
  799. {
  800. const char * szTemp = DICTOOL->getStringValue_json(options, "fontName");
  801. if (szTemp && *szTemp)
  802. label->setFontName(szTemp);
  803. else
  804. label->setFontName(std::string(""));
  805. }
  806. bool aw = DICTOOL->checkObjectExist_json(options, "areaWidth");
  807. bool ah = DICTOOL->checkObjectExist_json(options, "areaHeight");
  808. if (aw && ah)
  809. {
  810. Size size = Size(DICTOOL->getFloatValue_json(options, "areaWidth"),DICTOOL->getFloatValue_json(options,"areaHeight"));
  811. label->setTextAreaSize(size);
  812. }
  813. bool ha = DICTOOL->checkObjectExist_json(options, "hAlignment");
  814. if (ha)
  815. {
  816. label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, "hAlignment"));
  817. }
  818. bool va = DICTOOL->checkObjectExist_json(options, "vAlignment");
  819. if (va)
  820. {
  821. label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, "vAlignment"));
  822. }
  823. setColorPropsForWidgetFromJsonDictionary(widget,options);
  824. }
  825. void WidgetPropertiesReader0250::setPropsForLabelAtlasFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  826. {
  827. setPropsForWidgetFromJsonDictionary(widget, options);
  828. cocos2d::ui::TextAtlas* labelAtlas = static_cast<cocos2d::ui::TextAtlas*>(widget);
  829. bool sv = DICTOOL->checkObjectExist_json(options, "stringValue");
  830. bool cmf = DICTOOL->checkObjectExist_json(options, "charMapFile");
  831. bool iw = DICTOOL->checkObjectExist_json(options, "itemWidth");
  832. bool ih = DICTOOL->checkObjectExist_json(options, "itemHeight");
  833. bool scm = DICTOOL->checkObjectExist_json(options, "startCharMap");
  834. if (sv && cmf && iw && ih && scm && (strcmp(DICTOOL->getStringValue_json(options, "charMapFile"), "") != 0))
  835. {
  836. std::string tp_c = m_strFilePath;
  837. const char* cmft = DICTOOL->getStringValue_json(options, "charMapFile");
  838. const char* cmf_tp = tp_c.append(cmft).c_str();
  839. labelAtlas->setProperty(DICTOOL->getStringValue_json(options, "stringValue"),cmf_tp,DICTOOL->getIntValue_json(options, "itemWidth"),DICTOOL->getIntValue_json(options,"itemHeight"),DICTOOL->getStringValue_json(options, "startCharMap"));
  840. labelAtlas->setProperty(DICTOOL->getStringValue_json(options, "stringValue"),cmf_tp,DICTOOL->getIntValue_json(options, "itemWidth") / CC_CONTENT_SCALE_FACTOR() ,DICTOOL->getIntValue_json(options,"itemHeight") / CC_CONTENT_SCALE_FACTOR(), DICTOOL->getStringValue_json(options, "startCharMap"));
  841. }
  842. setColorPropsForWidgetFromJsonDictionary(widget,options);
  843. }
  844. void WidgetPropertiesReader0250::setPropsForLayoutFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  845. {
  846. setPropsForWidgetFromJsonDictionary(widget, options);
  847. Layout* containerWidget = static_cast<Layout*>(widget);
  848. if (!dynamic_cast<cocos2d::ui::ScrollView*>(containerWidget)
  849. && !dynamic_cast<cocos2d::ui::ListView*>(containerWidget))
  850. {
  851. containerWidget->setClippingEnabled(DICTOOL->getBooleanValue_json(options, "clipAble"));
  852. }
  853. Layout* panel = (Layout*)widget;
  854. bool backGroundScale9Enable = DICTOOL->getBooleanValue_json(options, "backGroundScale9Enable");
  855. panel->setBackGroundImageScale9Enabled(backGroundScale9Enable);
  856. int cr = DICTOOL->getIntValue_json(options, "bgColorR");
  857. int cg = DICTOOL->getIntValue_json(options, "bgColorG");
  858. int cb = DICTOOL->getIntValue_json(options, "bgColorB");
  859. int scr = DICTOOL->getIntValue_json(options, "bgStartColorR");
  860. int scg = DICTOOL->getIntValue_json(options, "bgStartColorG");
  861. int scb = DICTOOL->getIntValue_json(options, "bgStartColorB");
  862. int ecr = DICTOOL->getIntValue_json(options, "bgEndColorR");
  863. int ecg = DICTOOL->getIntValue_json(options, "bgEndColorG");
  864. int ecb = DICTOOL->getIntValue_json(options, "bgEndColorB");
  865. float bgcv1 = DICTOOL->getFloatValue_json(options, "vectorX");
  866. float bgcv2 = DICTOOL->getFloatValue_json(options, "vectorY");
  867. panel->setBackGroundColorVector(Vec2(bgcv1, bgcv2));
  868. int co = DICTOOL->getIntValue_json(options, "bgColorOpacity");
  869. int colorType = DICTOOL->getIntValue_json(options, "colorType");
  870. panel->setBackGroundColorType(Layout::BackGroundColorType(colorType));
  871. panel->setBackGroundColor(Color3B(scr, scg, scb),Color3B(ecr, ecg, ecb));
  872. panel->setBackGroundColor(Color3B(cr, cg, cb));
  873. panel->setBackGroundColorOpacity(co);
  874. std::string tp_b = m_strFilePath;
  875. const char* imageFileName = DICTOOL->getStringValue_json(options, "backGroundImage");
  876. const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
  877. bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
  878. if (backGroundScale9Enable)
  879. {
  880. float cx = DICTOOL->getFloatValue_json(options, "capInsetsX");
  881. float cy = DICTOOL->getFloatValue_json(options, "capInsetsY");
  882. float cw = DICTOOL->getFloatValue_json(options, "capInsetsWidth");
  883. float ch = DICTOOL->getFloatValue_json(options, "capInsetsHeight");
  884. if (useMergedTexture)
  885. {
  886. panel->setBackGroundImage(imageFileName,Widget::TextureResType::PLIST);
  887. }
  888. else
  889. {
  890. panel->setBackGroundImage(imageFileName_tp);
  891. }
  892. panel->setBackGroundImageCapInsets(Rect(cx, cy, cw, ch));
  893. }
  894. else
  895. {
  896. if (useMergedTexture)
  897. {
  898. panel->setBackGroundImage(imageFileName,Widget::TextureResType::PLIST);
  899. }
  900. else
  901. {
  902. panel->setBackGroundImage(imageFileName_tp);
  903. }
  904. }
  905. setColorPropsForWidgetFromJsonDictionary(widget,options);
  906. }
  907. void WidgetPropertiesReader0250::setPropsForScrollViewFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  908. {
  909. setPropsForLayoutFromJsonDictionary(widget, options);
  910. cocos2d::ui::ScrollView* scrollView = static_cast<cocos2d::ui::ScrollView*>(widget);
  911. float innerWidth = DICTOOL->getFloatValue_json(options, "innerWidth");
  912. float innerHeight = DICTOOL->getFloatValue_json(options, "innerHeight");
  913. scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
  914. int direction = DICTOOL->getFloatValue_json(options, "direction");
  915. scrollView->setDirection((ScrollView::Direction)direction);
  916. scrollView->setBounceEnabled(DICTOOL->getBooleanValue_json(options, "bounceEnable"));
  917. setColorPropsForWidgetFromJsonDictionary(widget,options);
  918. }
  919. void WidgetPropertiesReader0250::setPropsForSliderFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  920. {
  921. setPropsForWidgetFromJsonDictionary(widget, options);
  922. cocos2d::ui::Slider* slider = static_cast<cocos2d::ui::Slider*>(widget);
  923. bool barTextureScale9Enable = DICTOOL->getBooleanValue_json(options, "barTextureScale9Enable");
  924. slider->setScale9Enabled(barTextureScale9Enable);
  925. bool bt = DICTOOL->checkObjectExist_json(options, "barFileName");
  926. float barLength = DICTOOL->getFloatValue_json(options, "length");
  927. bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
  928. if (bt)
  929. {
  930. if (barTextureScale9Enable)
  931. {
  932. std::string tp_b = m_strFilePath;
  933. const char* imageFileName = DICTOOL->getStringValue_json(options, "barFileName");
  934. const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
  935. if (useMergedTexture)
  936. {
  937. slider->loadBarTexture(imageFileName,Widget::TextureResType::PLIST);
  938. }
  939. else
  940. {
  941. slider->loadBarTexture(imageFileName_tp);
  942. }
  943. slider->setContentSize(Size(barLength, slider->getContentSize().height));
  944. }
  945. else
  946. {
  947. std::string tp_b = m_strFilePath;
  948. const char* imageFileName = DICTOOL->getStringValue_json(options, "barFileName");
  949. const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
  950. if (useMergedTexture)
  951. {
  952. slider->loadBarTexture(imageFileName,Widget::TextureResType::PLIST);
  953. }
  954. else
  955. {
  956. slider->loadBarTexture(imageFileName_tp);
  957. }
  958. }
  959. }
  960. std::string tp_n = m_strFilePath;
  961. std::string tp_p = m_strFilePath;
  962. std::string tp_d = m_strFilePath;
  963. const char* normalFileName = DICTOOL->getStringValue_json(options, "ballNormal");
  964. const char* pressedFileName = DICTOOL->getStringValue_json(options, "ballPressed");
  965. const char* disabledFileName = DICTOOL->getStringValue_json(options, "ballDisabled");
  966. const char* normalFileName_tp = (normalFileName && (strcmp(normalFileName, "") != 0))?tp_n.append(normalFileName).c_str():nullptr;
  967. const char* pressedFileName_tp = (pressedFileName && (strcmp(pressedFileName, "") != 0))?tp_p.append(pressedFileName).c_str():nullptr;
  968. const char* disabledFileName_tp = (disabledFileName && (strcmp(disabledFileName, "") != 0))?tp_d.append(disabledFileName).c_str():nullptr;
  969. if (useMergedTexture)
  970. {
  971. slider->loadSlidBallTextures(normalFileName,pressedFileName,disabledFileName,Widget::TextureResType::PLIST);
  972. }
  973. else
  974. {
  975. slider->loadSlidBallTextures(normalFileName_tp,pressedFileName_tp,disabledFileName_tp);
  976. }
  977. slider->setPercent(DICTOOL->getIntValue_json(options, "percent"));
  978. std::string tp_b = m_strFilePath;
  979. const char* imageFileName = DICTOOL->getStringValue_json(options, "progressBarFileName");
  980. const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
  981. if (useMergedTexture)
  982. {
  983. slider->loadProgressBarTexture(imageFileName, Widget::TextureResType::PLIST);
  984. }
  985. else
  986. {
  987. slider->loadProgressBarTexture(imageFileName_tp);
  988. }
  989. setColorPropsForWidgetFromJsonDictionary(widget,options);
  990. }
  991. void WidgetPropertiesReader0250::setPropsForTextFieldFromJsonDictionary(Widget*widget,const rapidjson::Value& options)
  992. {
  993. setPropsForWidgetFromJsonDictionary(widget, options);
  994. cocos2d::ui::TextField* textField = static_cast<cocos2d::ui::TextField*>(widget);
  995. bool ph = DICTOOL->checkObjectExist_json(options, "placeHolder");
  996. if (ph)
  997. {
  998. textField->setPlaceHolder(DICTOOL->getStringValue_json(options, "placeHolder"));
  999. }
  1000. textField->setString(DICTOOL->getStringValue_json(options, "text"));
  1001. bool fs = DICTOOL->checkObjectExist_json(options, "fontSize");
  1002. if (fs)
  1003. {
  1004. textField->setFontSize(DICTOOL->getIntValue_json(options, "fontSize"));
  1005. }
  1006. bool fn = DICTOOL->checkObjectExist_json(options, "fontName");
  1007. if (fn)
  1008. {
  1009. const char * szTemp = DICTOOL->getStringValue_json(options, "fontName");
  1010. if (szTemp && *szTemp)
  1011. textField->setFontName(szTemp);
  1012. else
  1013. textField->setFontName(std::string(""));
  1014. }
  1015. bool tsw = DICTOOL->checkObjectExist_json(options, "touchSizeWidth");
  1016. bool tsh = DICTOOL->checkObjectExist_json(options, "touchSizeHeight");
  1017. if (tsw && tsh)
  1018. {
  1019. textField->setTouchSize(Size(DICTOOL->getFloatValue_json(options, "touchSizeWidth"), DICTOOL->getFloatValue_json(options,"touchSizeHeight")));
  1020. }
  1021. float dw = DICTOOL->getFloatValue_json(options, "width");
  1022. float dh = DICTOOL->getFloatValue_json(options, "height");
  1023. if (dw > 0.0f || dh > 0.0f)
  1024. {
  1025. //textField->setSize(Size(dw, dh));
  1026. }
  1027. bool maxLengthEnable = DICTOOL->getBooleanValue_json(options, "maxLengthEnable");
  1028. textField->setMaxLengthEnabled(maxLengthEnable);
  1029. if (maxLengthEnable)
  1030. {
  1031. int maxLength = DICTOOL->getIntValue_json(options, "maxLength");
  1032. textField->setMaxLength(maxLength);
  1033. }
  1034. bool passwordEnable = DICTOOL->getBooleanValue_json(options, "passwordEnable");
  1035. textField->setPasswordEnabled(passwordEnable);
  1036. if (passwordEnable)
  1037. {
  1038. textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, "passwordStyleText"));
  1039. }
  1040. setColorPropsForWidgetFromJsonDictionary(widget,options);
  1041. }
  1042. void WidgetPropertiesReader0250::setPropsForLoadingBarFromJsonDictionary(Widget *widget, const rapidjson::Value&options)
  1043. {
  1044. setPropsForWidgetFromJsonDictionary(widget, options);
  1045. cocos2d::ui::LoadingBar* loadingBar = static_cast<cocos2d::ui::LoadingBar*>(widget);
  1046. bool useMergedTexture = DICTOOL->getBooleanValue_json(options, "useMergedTexture");
  1047. std::string tp_b = m_strFilePath;
  1048. const char*imageFileName = DICTOOL->getStringValue_json(options, "texture");
  1049. const char* imageFileName_tp = (imageFileName && (strcmp(imageFileName, "") != 0))?tp_b.append(imageFileName).c_str():nullptr;
  1050. if (useMergedTexture)
  1051. {
  1052. loadingBar->loadTexture(imageFileName,Widget::TextureResType::PLIST);
  1053. }
  1054. else
  1055. {
  1056. loadingBar->loadTexture(imageFileName_tp);
  1057. }
  1058. loadingBar->setDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, "direction")));
  1059. loadingBar->setPercent(DICTOOL->getIntValue_json(options, "percent"));
  1060. setColorPropsForWidgetFromJsonDictionary(widget,options);
  1061. }
  1062. void WidgetPropertiesReader0250::setPropsForLabelBMFontFromJsonDictionary(Widget *widget, const rapidjson::Value&options)
  1063. {
  1064. setPropsForWidgetFromJsonDictionary(widget, options);
  1065. cocos2d::ui::TextBMFont* labelBMFont = static_cast<cocos2d::ui::TextBMFont*>(widget);
  1066. std::string tp_c = m_strFilePath;
  1067. const char* cmft = DICTOOL->getStringValue_json(options, "fileName");
  1068. const char* cmf_tp = tp_c.append(cmft).c_str();
  1069. labelBMFont->setFntFile(cmf_tp);
  1070. const char* text = DICTOOL->getStringValue_json(options, "text");
  1071. labelBMFont->setString(text);
  1072. setColorPropsForWidgetFromJsonDictionary(widget,options);
  1073. }
  1074. void WidgetPropertiesReader0250::setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* /*reader*/, Widget* /*widget*/, const rapidjson::Value& /*options*/)
  1075. {
  1076. }
  1077. void WidgetPropertiesReader0250::setPropsForAllCustomWidgetFromJsonDictionary(const std::string& /*classType*/,
  1078. cocos2d::ui::Widget* /*widget*/,
  1079. const rapidjson::Value& /*customOptions*/)
  1080. {
  1081. }
  1082. Widget* WidgetPropertiesReader0250::createWidgetFromBinary(CocoLoader* /*cocoLoader*/,
  1083. stExpCocoNode* /*pCocoNode*/,
  1084. const char* /*fileName*/)
  1085. {
  1086. return nullptr;
  1087. }
  1088. Widget* WidgetPropertiesReader0250::widgetFromBinary(CocoLoader* /*cocoLoader*/,
  1089. stExpCocoNode* /*pCocoNode*/)
  1090. {
  1091. return nullptr;
  1092. }
  1093. void WidgetPropertiesReader0250::setPropsForAllWidgetFromBinary(WidgetReaderProtocol* /*reader*/,
  1094. cocos2d::ui::Widget* /*widget*/,
  1095. CocoLoader* /*cocoLoader*/,
  1096. stExpCocoNode* /*pCocoNode*/)
  1097. {}
  1098. /*0.3.0.0~1.0.0.0*/
  1099. Widget* WidgetPropertiesReader0300::createWidget(const rapidjson::Value& data, const char* fullPath, const char* fileName)
  1100. {
  1101. m_strFilePath = fullPath;
  1102. int texturesCount = DICTOOL->getArrayCount_json(data, "textures");
  1103. for (int i=0; i<texturesCount; i++)
  1104. {
  1105. const char* file = DICTOOL->getStringValueFromArray_json(data, "textures", i);
  1106. std::string tp = fullPath;
  1107. tp.append(file);
  1108. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(tp);
  1109. }
  1110. float fileDesignWidth = DICTOOL->getFloatValue_json(data, "designWidth");
  1111. float fileDesignHeight = DICTOOL->getFloatValue_json(data, "designHeight");
  1112. if (fileDesignWidth <= 0 || fileDesignHeight <= 0) {
  1113. CCLOGERROR("Read design size error!\n");
  1114. Size winSize = Director::getInstance()->getWinSize();
  1115. GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
  1116. }
  1117. else
  1118. {
  1119. GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
  1120. }
  1121. const rapidjson::Value& widgetTree = DICTOOL->getSubDictionary_json(data, "widgetTree");
  1122. Widget* widget = widgetFromJsonDictionary(widgetTree);
  1123. /* *********temp********* */
  1124. if (widget->getContentSize().equals(Size::ZERO))
  1125. {
  1126. Layout* rootWidget = dynamic_cast<Layout*>(widget);
  1127. rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
  1128. }
  1129. /* ********************** */
  1130. // widget->setFileDesignSize(Size(fileDesignWidth, fileDesignHeight));
  1131. const rapidjson::Value& actions = DICTOOL->getSubDictionary_json(data, "animation");
  1132. /* *********temp********* */
  1133. // ActionManager::getInstance()->releaseActions();
  1134. /* ********************** */
  1135. // CCLOG("file name == [%s]",fileName);
  1136. Ref* rootWidget = (Ref*) widget;
  1137. ActionManagerEx::getInstance()->initWithDictionary(fileName,actions,rootWidget);
  1138. return widget;
  1139. }
  1140. cocos2d::ui::Widget* WidgetPropertiesReader0300::createWidgetFromBinary(CocoLoader* cocoLoader,stExpCocoNode* cocoNode, const char* fileName)
  1141. {
  1142. stExpCocoNode *tpChildArray = cocoNode->GetChildArray(cocoLoader);
  1143. float fileDesignWidth = 0.0f;
  1144. float fileDesignHeight = 0.0f;
  1145. Widget* widget = nullptr;
  1146. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  1147. std::string key = tpChildArray[i].GetName(cocoLoader);
  1148. if (key == "textures") {
  1149. int texturesCount = tpChildArray[i].GetChildNum();
  1150. for (int j=0; j<texturesCount; j++)
  1151. {
  1152. std::string file;
  1153. stExpCocoNode *textureCountsArray = tpChildArray[i].GetChildArray(cocoLoader);
  1154. file = textureCountsArray[j].GetValue(cocoLoader);
  1155. SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
  1156. }
  1157. }else if (key == "designWidth"){
  1158. fileDesignWidth = utils::atof(tpChildArray[i].GetValue(cocoLoader));
  1159. }else if (key == "designHeight"){
  1160. fileDesignHeight = utils::atof(tpChildArray[i].GetValue(cocoLoader));
  1161. }else if (key == "widgetTree"){
  1162. if (fileDesignWidth <= 0 || fileDesignHeight <= 0) {
  1163. CCLOGERROR("Read design size error!\n");
  1164. Size winSize = Director::getInstance()->getWinSize();
  1165. GUIReader::getInstance()->storeFileDesignSize(fileName, winSize);
  1166. }
  1167. else
  1168. {
  1169. GUIReader::getInstance()->storeFileDesignSize(fileName, Size(fileDesignWidth, fileDesignHeight));
  1170. }
  1171. stExpCocoNode *widgetTreeNode = &tpChildArray[i];
  1172. rapidjson::Type tType = tpChildArray[i].GetType(cocoLoader);
  1173. if (rapidjson::kObjectType == tType)
  1174. {
  1175. widget = widgetFromBinary(cocoLoader, widgetTreeNode);
  1176. }
  1177. if (widget->getContentSize().equals(Size::ZERO))
  1178. {
  1179. Layout* rootWidget = dynamic_cast<Layout*>(widget);
  1180. rootWidget->setContentSize(Size(fileDesignWidth, fileDesignHeight));
  1181. }
  1182. }
  1183. }
  1184. /* ********************** */
  1185. /* ********************** */
  1186. stExpCocoNode *optionChildNode = cocoNode->GetChildArray(cocoLoader);
  1187. for (int k = 0; k < cocoNode->GetChildNum(); ++k) {
  1188. std::string key = optionChildNode[k].GetName(cocoLoader);
  1189. if (key == "animation") {
  1190. Ref* rootWidget = (Ref*) widget;
  1191. ActionManagerEx::getInstance()->initWithBinary(fileName,rootWidget,cocoLoader, &optionChildNode[k]);
  1192. break;
  1193. }
  1194. }
  1195. return widget;
  1196. }
  1197. Widget* WidgetPropertiesReader0300::widgetFromBinary(CocoLoader* cocoLoader, stExpCocoNode* cocoNode)
  1198. {
  1199. Widget* widget = nullptr;
  1200. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  1201. stExpCocoNode *optionsNode = nullptr;
  1202. stExpCocoNode *childrenNode = nullptr;
  1203. int elementCount = cocoNode->GetChildNum();
  1204. std::string classname;
  1205. for (int i = 0; i < elementCount; ++i) {
  1206. std::string key = stChildArray[i].GetName(cocoLoader);
  1207. std::string value = stChildArray[i].GetValue(cocoLoader);
  1208. if (key == "classname" )
  1209. {
  1210. if (!value.empty())
  1211. {
  1212. classname = value;
  1213. widget = this->createGUI(classname);
  1214. }
  1215. else
  1216. {
  1217. CCLOG("Warning!!! classname not found!");
  1218. }
  1219. }else if(key == "children"){
  1220. childrenNode = &stChildArray[i];
  1221. }else if(key == "options"){
  1222. optionsNode = &stChildArray[i];
  1223. }
  1224. }
  1225. std::string readerName = this->getWidgetReaderClassName(classname);
  1226. WidgetReaderProtocol* reader = this->createWidgetReaderProtocol(readerName);
  1227. if (reader)
  1228. {
  1229. // widget parse with widget reader
  1230. setPropsForAllWidgetFromBinary(reader, widget, cocoLoader,optionsNode);
  1231. }
  1232. else
  1233. {
  1234. // 1st., custom widget parse properties of parent widget with parent widget reader
  1235. readerName = this->getWidgetReaderClassName(widget);
  1236. reader = this->createWidgetReaderProtocol(readerName);
  1237. if (reader && widget) {
  1238. setPropsForAllWidgetFromBinary(reader, widget, cocoLoader, optionsNode);
  1239. // 2nd., custom widget parse with custom reader
  1240. //2nd. parse custom property
  1241. const char* customProperty = nullptr;
  1242. stExpCocoNode *optionChildNode = optionsNode->GetChildArray(cocoLoader);
  1243. for (int k = 0; k < optionsNode->GetChildNum(); ++k) {
  1244. std::string key = optionChildNode[k].GetName(cocoLoader);
  1245. if (key == "customProperty") {
  1246. customProperty = optionChildNode[k].GetValue(cocoLoader);
  1247. break;
  1248. }
  1249. }
  1250. rapidjson::Document customJsonDict;
  1251. customJsonDict.Parse<0>(customProperty);
  1252. if (customJsonDict.HasParseError())
  1253. {
  1254. CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
  1255. }
  1256. setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
  1257. }else{
  1258. CCLOG("Widget or WidgetReader doesn't exists!!! Please check your csb file.");
  1259. }
  1260. }
  1261. //parse children
  1262. if (nullptr != childrenNode) {
  1263. rapidjson::Type tType22 = childrenNode->GetType(cocoLoader);
  1264. if (tType22 == rapidjson::kArrayType) {
  1265. int childrenCount = childrenNode->GetChildNum();
  1266. stExpCocoNode* innerChildArray = childrenNode->GetChildArray(cocoLoader);
  1267. for (int i=0; i < childrenCount; ++i) {
  1268. rapidjson::Type tType = innerChildArray[i].GetType(cocoLoader);
  1269. if (tType == rapidjson::kObjectType) {
  1270. Widget *child = widgetFromBinary(cocoLoader, &innerChildArray[i]);
  1271. if (child)
  1272. {
  1273. PageView* pageView = dynamic_cast<PageView*>(widget);
  1274. if (pageView)
  1275. {
  1276. pageView->addPage(static_cast<Layout*>(child));
  1277. }
  1278. else
  1279. {
  1280. ListView* listView = dynamic_cast<ListView*>(widget);
  1281. if (listView)
  1282. {
  1283. listView->pushBackCustomItem(child);
  1284. }
  1285. else
  1286. {
  1287. if (nullptr == dynamic_cast<Layout*>(widget))
  1288. {
  1289. if (child->getPositionType() == ui::Widget::PositionType::PERCENT)
  1290. {
  1291. child->setPositionPercent(Vec2(child->getPositionPercent().x + widget->getAnchorPoint().x,
  1292. child->getPositionPercent().y + widget->getAnchorPoint().y));
  1293. }
  1294. child->setPosition(Vec2(child->getPositionX() + widget->getAnchorPointInPoints().x,
  1295. child->getPositionY() + widget->getAnchorPointInPoints().y));
  1296. }
  1297. widget->addChild(child);
  1298. }
  1299. }
  1300. }
  1301. }
  1302. }
  1303. }
  1304. }
  1305. return widget;
  1306. }
  1307. void WidgetPropertiesReader0300::setPropsForAllWidgetFromBinary(WidgetReaderProtocol* reader,
  1308. cocos2d::ui::Widget* widget,
  1309. CocoLoader* cocoLoader,
  1310. stExpCocoNode* cocoNode)
  1311. {
  1312. reader->setPropsFromBinary(widget, cocoLoader, cocoNode);
  1313. }
  1314. void WidgetPropertiesReader0300::setPropsForAllCustomWidgetFromBinary(const std::string& /*classType*/,
  1315. cocos2d::ui::Widget* /*widget*/,
  1316. CocoLoader* /*cocoLoader*/,
  1317. stExpCocoNode* /*pCocoNode*/) {
  1318. //TODO: custom property
  1319. }
  1320. Widget* WidgetPropertiesReader0300::widgetFromJsonDictionary(const rapidjson::Value& data)
  1321. {
  1322. const char* classname = DICTOOL->getStringValue_json(data, "classname");
  1323. const rapidjson::Value& uiOptions = DICTOOL->getSubDictionary_json(data, "options");
  1324. Widget* widget = this->createGUI(classname);
  1325. // CCLOG("classname = %s", classname);
  1326. std::string readerName = this->getWidgetReaderClassName(classname);
  1327. WidgetReaderProtocol* reader = this->createWidgetReaderProtocol(readerName);
  1328. if (reader)
  1329. {
  1330. // widget parse with widget reader
  1331. setPropsForAllWidgetFromJsonDictionary(reader, widget, uiOptions);
  1332. }
  1333. else
  1334. {
  1335. readerName = this->getWidgetReaderClassName(widget);
  1336. reader = dynamic_cast<WidgetReaderProtocol*>(ObjectFactory::getInstance()->createObject(readerName));
  1337. if (reader && widget) {
  1338. setPropsForAllWidgetFromJsonDictionary(reader, widget, uiOptions);
  1339. // 2nd., custom widget parse with custom reader
  1340. const char* customProperty = DICTOOL->getStringValue_json(uiOptions, "customProperty");
  1341. rapidjson::Document customJsonDict;
  1342. customJsonDict.Parse<0>(customProperty);
  1343. if (customJsonDict.HasParseError())
  1344. {
  1345. CCLOG("GetParseError %d\n", customJsonDict.GetParseError());
  1346. }
  1347. setPropsForAllCustomWidgetFromJsonDictionary(classname, widget, customJsonDict);
  1348. }else{
  1349. CCLOG("Widget or WidgetReader doesn't exists!!! Please check your json file.");
  1350. }
  1351. }
  1352. int childrenCount = DICTOOL->getArrayCount_json(data, "children");
  1353. for (int i = 0; i < childrenCount; i++)
  1354. {
  1355. const rapidjson::Value& subData = DICTOOL->getDictionaryFromArray_json(data, "children", i);
  1356. cocos2d::ui::Widget* child = widgetFromJsonDictionary(subData);
  1357. if (child)
  1358. {
  1359. PageView* pageView = dynamic_cast<PageView*>(widget);
  1360. if (pageView)
  1361. {
  1362. pageView->addPage(static_cast<Layout*>(child));
  1363. }
  1364. else
  1365. {
  1366. ListView* listView = dynamic_cast<ListView*>(widget);
  1367. if (listView)
  1368. {
  1369. listView->pushBackCustomItem(child);
  1370. }
  1371. else
  1372. {
  1373. if (nullptr == dynamic_cast<Layout*>(widget))
  1374. {
  1375. if (child->getPositionType() == ui::Widget::PositionType::PERCENT)
  1376. {
  1377. child->setPositionPercent(Vec2(child->getPositionPercent().x + widget->getAnchorPoint().x, child->getPositionPercent().y + widget->getAnchorPoint().y));
  1378. }
  1379. child->setPosition(Vec2(child->getPositionX() + widget->getAnchorPointInPoints().x, child->getPositionY() + widget->getAnchorPointInPoints().y));
  1380. }
  1381. widget->addChild(child);
  1382. }
  1383. }
  1384. }
  1385. }
  1386. return widget;
  1387. }
  1388. void WidgetPropertiesReader0300::setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol *reader, Widget *widget, const rapidjson::Value &options)
  1389. {
  1390. reader->setPropsFromJsonDictionary(widget, options);
  1391. }
  1392. void WidgetPropertiesReader0300::setPropsForAllCustomWidgetFromJsonDictionary(const std::string &classType,
  1393. cocos2d::ui::Widget *widget,
  1394. const rapidjson::Value &customOptions)
  1395. {
  1396. GUIReader* guiReader = GUIReader::getInstance();
  1397. std::map<std::string, Ref*> *object_map = guiReader->getParseObjectMap();
  1398. Ref* object = (*object_map)[classType];
  1399. std::map<std::string, SEL_ParseEvent> *selector_map = guiReader->getParseCallBackMap();
  1400. SEL_ParseEvent selector = (*selector_map)[classType];
  1401. if (object && selector)
  1402. {
  1403. (object->*selector)(classType, widget, customOptions);
  1404. }
  1405. }
  1406. }