LayoutReader.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. #include "editor-support/cocostudio/WidgetReader/LayoutReader/LayoutReader.h"
  2. #include "ui/UILayout.h"
  3. #include "editor-support/cocostudio/CocoLoader.h"
  4. #include "ui/UIScrollView.h"
  5. #include "ui/UIPageView.h"
  6. #include "ui/UIListView.h"
  7. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  8. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  9. #include "base/CCDirector.h"
  10. #include "platform/CCFileUtils.h"
  11. #include "2d/CCSpriteFrameCache.h"
  12. #include "tinyxml2.h"
  13. #include "flatbuffers/flatbuffers.h"
  14. USING_NS_CC;
  15. using namespace ui;
  16. using namespace flatbuffers;
  17. namespace cocostudio
  18. {
  19. static const char* P_CapInsetsX = "capInsetsX";
  20. static const char* P_CapInsetsY = "capInsetsY";
  21. static const char* P_CapInsetsWidth = "capInsetsWidth";
  22. static const char* P_CapInsetsHeight = "capInsetsHeight";
  23. static const char* P_ClipAble = "clipAble";
  24. static const char* P_BackGroundScale9Enable = "backGroundScale9Enable";
  25. static const char* P_BgColorR = "bgColorR";
  26. static const char* P_BgColorG = "bgColorG";
  27. static const char* P_BgColorB = "bgColorB";
  28. static const char* P_BgStartColorR = "bgStartColorR";
  29. static const char* P_BgStartColorG = "bgStartColorG";
  30. static const char* P_BgStartColorB = "bgStartColorB";
  31. static const char* P_BgEndColorR = "bgEndColorR";
  32. static const char* P_BgEndColorG = "bgEndColorG";
  33. static const char* P_BgEndColorB = "bgEndColorB";
  34. static const char* P_VectorX = "vectorX";
  35. static const char* P_VectorY = "vectorY";
  36. static const char* P_BgColorOpacity = "bgColorOpacity";
  37. static const char* P_ColorType = "colorType";
  38. static const char* P_BackGroundImageData = "backGroundImageData";
  39. static const char* P_LayoutType = "layoutType";
  40. static LayoutReader* instanceLayoutReader = nullptr;
  41. IMPLEMENT_CLASS_NODE_READER_INFO(LayoutReader)
  42. LayoutReader::LayoutReader()
  43. {
  44. }
  45. LayoutReader::~LayoutReader()
  46. {
  47. }
  48. LayoutReader* LayoutReader::getInstance()
  49. {
  50. if (!instanceLayoutReader)
  51. {
  52. instanceLayoutReader = new (std::nothrow) LayoutReader();
  53. }
  54. return instanceLayoutReader;
  55. }
  56. void LayoutReader::destroyInstance()
  57. {
  58. CC_SAFE_DELETE(instanceLayoutReader);
  59. }
  60. void LayoutReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  61. {
  62. WidgetReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  63. Layout* panel = static_cast<Layout*>(widget);
  64. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  65. this->beginSetBasicProperties(widget);
  66. int cr=0, cg = 0, cb = 0;
  67. int scr=0, scg=0, scb=0;
  68. int ecr=0, ecg=0, ecb= 0;
  69. float bgcv1 = 0.0f, bgcv2= 0.0f;
  70. float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
  71. Layout::Type layoutType = Layout::Type::ABSOLUTE;
  72. int bgColorOpacity = panel->getBackGroundColorOpacity();
  73. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  74. std::string key = stChildArray[i].GetName(cocoLoader);
  75. std::string value = stChildArray[i].GetValue(cocoLoader);
  76. //read all basic properties of widget
  77. CC_BASIC_PROPERTY_BINARY_READER
  78. //read all color related properties of widget
  79. CC_COLOR_PROPERTY_BINARY_READER
  80. else if(key == P_AdaptScreen){
  81. _isAdaptScreen = valueToBool(value);
  82. }
  83. else if( key == P_ClipAble){
  84. panel->setClippingEnabled(valueToBool(value));
  85. }else if(key == P_BackGroundScale9Enable){
  86. panel->setBackGroundImageScale9Enabled(valueToBool(value));
  87. }else if(key == P_BgColorR){
  88. cr = valueToInt(value);
  89. }else if(key == P_BgColorG){
  90. cg = valueToInt(value);
  91. }else if(key == P_BgColorB)
  92. {
  93. cb = valueToInt(value);
  94. }else if(key == P_BgStartColorR){
  95. scr = valueToInt(value);
  96. }else if(key == P_BgStartColorG){
  97. scg = valueToInt(value);
  98. }else if(key == P_BgStartColorB)
  99. {
  100. scb = valueToInt(value);
  101. }
  102. else if(key == P_BgEndColorR){
  103. ecr = valueToInt(value);
  104. }else if(key == P_BgEndColorG){
  105. ecg = valueToInt(value);
  106. }else if(key == P_BgEndColorB)
  107. {
  108. ecb = valueToInt(value);
  109. }else if (key == P_VectorX){
  110. bgcv1 = valueToFloat(value);
  111. }else if(key == P_VectorY){
  112. bgcv2 = valueToFloat(value);
  113. }else if(key == P_BgColorOpacity){
  114. bgColorOpacity = valueToInt(value);
  115. }else if( key == P_ColorType){
  116. panel->setBackGroundColorType(Layout::BackGroundColorType(valueToInt(value)));
  117. }else if (key == P_BackGroundImageData){
  118. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  119. if (backGroundChildren) {
  120. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  121. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  122. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  123. panel->setBackGroundImage(backgroundValue, imageFileNameType);
  124. }
  125. }else if(key == P_CapInsetsX){
  126. capsx = valueToFloat(value);
  127. }else if(key == P_CapInsetsY){
  128. capsy = valueToFloat(value);
  129. }else if(key == P_CapInsetsWidth){
  130. capsWidth = valueToFloat(value);
  131. }else if(key == P_CapInsetsHeight){
  132. capsHeight = valueToFloat(value);
  133. }else if (key == P_LayoutType){
  134. layoutType = (Layout::Type)valueToInt(value);
  135. }
  136. }
  137. panel->setBackGroundColor(Color3B(scr, scg, scb),Color3B(ecr, ecg, ecb));
  138. panel->setBackGroundColor(Color3B(cr, cg, cb));
  139. panel->setBackGroundColorVector(Vec2(bgcv1, bgcv2));
  140. panel->setBackGroundColorOpacity(bgColorOpacity);
  141. panel->setBackGroundImageColor(Color3B(_color.r, _color.g, _color.b));
  142. panel->setBackGroundImageOpacity(_opacity);
  143. if (panel->isBackGroundImageScale9Enabled()) {
  144. panel->setBackGroundImageCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
  145. }
  146. panel->setLayoutType(layoutType);
  147. this->endSetBasicProperties(widget);
  148. }
  149. void LayoutReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  150. {
  151. WidgetReader::setPropsFromJsonDictionary(widget, options);
  152. Layout* panel = static_cast<Layout*>(widget);
  153. /* adapt screen gui */
  154. float w = 0, h = 0;
  155. bool adaptScrennExsit = DICTOOL->checkObjectExist_json(options, P_AdaptScreen);
  156. if (adaptScrennExsit)
  157. {
  158. bool adaptScrenn = DICTOOL->getBooleanValue_json(options, P_AdaptScreen);
  159. if (adaptScrenn)
  160. {
  161. Size screenSize = Director::getInstance()->getWinSize();
  162. w = screenSize.width;
  163. h = screenSize.height;
  164. }
  165. else
  166. {
  167. w = DICTOOL->getFloatValue_json(options, P_Width);
  168. h = DICTOOL->getFloatValue_json(options, P_Height);
  169. }
  170. }
  171. else
  172. {
  173. w = DICTOOL->getFloatValue_json(options, P_Width);
  174. h = DICTOOL->getFloatValue_json(options, P_Height);
  175. }
  176. panel->setContentSize(Size(w, h));
  177. /**/
  178. panel->setClippingEnabled(DICTOOL->getBooleanValue_json(options, P_ClipAble));
  179. bool backGroundScale9Enable = DICTOOL->getBooleanValue_json(options, P_BackGroundScale9Enable);
  180. panel->setBackGroundImageScale9Enabled(backGroundScale9Enable);
  181. int cr;
  182. int cg;
  183. int cb;
  184. int scr;
  185. int scg;
  186. int scb;
  187. int ecr;
  188. int ecg;
  189. int ecb;
  190. if (dynamic_cast<ui::PageView*>(widget)) {
  191. cr = DICTOOL->getIntValue_json(options, P_BgColorR,150);
  192. cg = DICTOOL->getIntValue_json(options, P_BgColorG,150);
  193. cb = DICTOOL->getIntValue_json(options, P_BgColorB,100);
  194. scr = DICTOOL->getIntValue_json(options, P_BgStartColorR,255);
  195. scg = DICTOOL->getIntValue_json(options, P_BgStartColorG,255);
  196. scb = DICTOOL->getIntValue_json(options, P_BgStartColorB,255);
  197. ecr = DICTOOL->getIntValue_json(options, P_BgEndColorR,255);
  198. ecg = DICTOOL->getIntValue_json(options, P_BgEndColorG,150);
  199. ecb = DICTOOL->getIntValue_json(options, P_BgEndColorB,100);
  200. }else if(dynamic_cast<ui::ListView*>(widget)){
  201. cr = DICTOOL->getIntValue_json(options, P_BgColorR,150);
  202. cg = DICTOOL->getIntValue_json(options, P_BgColorG,150);
  203. cb = DICTOOL->getIntValue_json(options, P_BgColorB,255);
  204. scr = DICTOOL->getIntValue_json(options, P_BgStartColorR,255);
  205. scg = DICTOOL->getIntValue_json(options, P_BgStartColorG,255);
  206. scb = DICTOOL->getIntValue_json(options, P_BgStartColorB,255);
  207. ecr = DICTOOL->getIntValue_json(options, P_BgEndColorR,150);
  208. ecg = DICTOOL->getIntValue_json(options, P_BgEndColorG,150);
  209. ecb = DICTOOL->getIntValue_json(options, P_BgEndColorB,255);
  210. }else if(dynamic_cast<ui::ScrollView*>(widget)){
  211. cr = DICTOOL->getIntValue_json(options, P_BgColorR,255);
  212. cg = DICTOOL->getIntValue_json(options, P_BgColorG,150);
  213. cb = DICTOOL->getIntValue_json(options, P_BgColorB,100);
  214. scr = DICTOOL->getIntValue_json(options, P_BgStartColorR,255);
  215. scg = DICTOOL->getIntValue_json(options, P_BgStartColorG,255);
  216. scb = DICTOOL->getIntValue_json(options, P_BgStartColorB,255);
  217. ecr = DICTOOL->getIntValue_json(options, P_BgEndColorR,255);
  218. ecg = DICTOOL->getIntValue_json(options, P_BgEndColorG,150);
  219. ecb = DICTOOL->getIntValue_json(options, P_BgEndColorB,100);
  220. }else{
  221. cr = DICTOOL->getIntValue_json(options, P_BgColorR,150);
  222. cg = DICTOOL->getIntValue_json(options, P_BgColorG,200);
  223. cb = DICTOOL->getIntValue_json(options, P_BgColorB,255);
  224. scr = DICTOOL->getIntValue_json(options, P_BgStartColorR,255);
  225. scg = DICTOOL->getIntValue_json(options, P_BgStartColorG,255);
  226. scb = DICTOOL->getIntValue_json(options, P_BgStartColorB,255);
  227. ecr = DICTOOL->getIntValue_json(options, P_BgEndColorR,150);
  228. ecg = DICTOOL->getIntValue_json(options, P_BgEndColorG,200);
  229. ecb = DICTOOL->getIntValue_json(options, P_BgEndColorB,255);
  230. }
  231. float bgcv1 = DICTOOL->getFloatValue_json(options, P_VectorX);
  232. float bgcv2 = DICTOOL->getFloatValue_json(options, P_VectorY,-0.5);
  233. panel->setBackGroundColorVector(Vec2(bgcv1, bgcv2));
  234. int co = DICTOOL->getIntValue_json(options, P_BgColorOpacity,100);
  235. int colorType = DICTOOL->getIntValue_json(options, P_ColorType,1);
  236. panel->setBackGroundColorType(Layout::BackGroundColorType(colorType));
  237. panel->setBackGroundColor(Color3B(scr, scg, scb),Color3B(ecr, ecg, ecb));
  238. panel->setBackGroundColor(Color3B(cr, cg, cb));
  239. panel->setBackGroundColorOpacity(co);
  240. const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_BackGroundImageData);
  241. int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
  242. std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
  243. panel->setBackGroundImage(imageFileName, (Widget::TextureResType)imageFileNameType);
  244. if (backGroundScale9Enable)
  245. {
  246. float cx = DICTOOL->getFloatValue_json(options, P_CapInsetsX);
  247. float cy = DICTOOL->getFloatValue_json(options, P_CapInsetsY);
  248. float cw = DICTOOL->getFloatValue_json(options, P_CapInsetsWidth,1);
  249. float ch = DICTOOL->getFloatValue_json(options, P_CapInsetsHeight,1);
  250. panel->setBackGroundImageCapInsets(Rect(cx, cy, cw, ch));
  251. }
  252. bool layoutTypeExsit = DICTOOL->checkObjectExist_json(options, P_LayoutType);
  253. if (layoutTypeExsit)
  254. {
  255. panel->setLayoutType((Layout::Type)DICTOOL->getIntValue_json(options, P_LayoutType));
  256. }
  257. int bgimgcr = DICTOOL->getIntValue_json(options, P_ColorR,255);
  258. int bgimgcg = DICTOOL->getIntValue_json(options, P_ColorG,255);
  259. int bgimgcb = DICTOOL->getIntValue_json(options, P_ColorB,255);
  260. panel->setBackGroundImageColor(Color3B(bgimgcr, bgimgcg, bgimgcb));
  261. int bgimgopacity = DICTOOL->getIntValue_json(options, P_Opacity, 255);
  262. panel->setBackGroundImageOpacity(bgimgopacity);
  263. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  264. }
  265. Offset<Table> LayoutReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  266. flatbuffers::FlatBufferBuilder *builder)
  267. {
  268. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  269. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  270. std::string path = "";
  271. std::string plistFile = "";
  272. int resourceType = 0;
  273. bool clipEnabled = false;
  274. Color3B bgColor;
  275. Color3B bgStartColor;
  276. Color3B bgEndColor;
  277. int colorType = 0;
  278. GLubyte bgColorOpacity = 255;
  279. Vec2 colorVector(0.0f, -0.5f);
  280. Rect capInsets;
  281. Size scale9Size;
  282. bool backGroundScale9Enabled = false;
  283. // attributes
  284. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  285. while (attribute)
  286. {
  287. std::string name = attribute->Name();
  288. std::string value = attribute->Value();
  289. if (name == "ClipAble")
  290. {
  291. clipEnabled = (value == "True") ? true : false;
  292. }
  293. else if (name == "ComboBoxIndex")
  294. {
  295. colorType = atoi(value.c_str());
  296. }
  297. else if (name == "BackColorAlpha")
  298. {
  299. bgColorOpacity = atoi(value.c_str());
  300. }
  301. else if (name == "Scale9Enable")
  302. {
  303. if (value == "True")
  304. {
  305. backGroundScale9Enabled = true;
  306. }
  307. }
  308. else if (name == "Scale9OriginX")
  309. {
  310. capInsets.origin.x = atof(value.c_str());
  311. }
  312. else if (name == "Scale9OriginY")
  313. {
  314. capInsets.origin.y = atof(value.c_str());
  315. }
  316. else if (name == "Scale9Width")
  317. {
  318. capInsets.size.width = atof(value.c_str());
  319. }
  320. else if (name == "Scale9Height")
  321. {
  322. capInsets.size.height = atof(value.c_str());
  323. }
  324. attribute = attribute->Next();
  325. }
  326. // child elements
  327. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  328. while (child)
  329. {
  330. std::string name = child->Name();
  331. if (name == "Size" && backGroundScale9Enabled)
  332. {
  333. attribute = child->FirstAttribute();
  334. while (attribute)
  335. {
  336. name = attribute->Name();
  337. std::string value = attribute->Value();
  338. if (name == "X")
  339. {
  340. scale9Size.width = atof(value.c_str());
  341. }
  342. else if (name == "Y")
  343. {
  344. scale9Size.height = atof(value.c_str());
  345. }
  346. attribute = attribute->Next();
  347. }
  348. }
  349. else if (name == "SingleColor")
  350. {
  351. attribute = child->FirstAttribute();
  352. while (attribute)
  353. {
  354. name = attribute->Name();
  355. std::string value = attribute->Value();
  356. if (name == "R")
  357. {
  358. bgColor.r = atoi(value.c_str());
  359. }
  360. else if (name == "G")
  361. {
  362. bgColor.g = atoi(value.c_str());
  363. }
  364. else if (name == "B")
  365. {
  366. bgColor.b = atoi(value.c_str());
  367. }
  368. attribute = attribute->Next();
  369. }
  370. }
  371. else if (name == "EndColor")
  372. {
  373. attribute = child->FirstAttribute();
  374. while (attribute)
  375. {
  376. name = attribute->Name();
  377. std::string value = attribute->Value();
  378. if (name == "R")
  379. {
  380. bgEndColor.r = atoi(value.c_str());
  381. }
  382. else if (name == "G")
  383. {
  384. bgEndColor.g = atoi(value.c_str());
  385. }
  386. else if (name == "B")
  387. {
  388. bgEndColor.b = atoi(value.c_str());
  389. }
  390. attribute = attribute->Next();
  391. }
  392. }
  393. else if (name == "FirstColor")
  394. {
  395. attribute = child->FirstAttribute();
  396. while (attribute)
  397. {
  398. name = attribute->Name();
  399. std::string value = attribute->Value();
  400. if (name == "R")
  401. {
  402. bgStartColor.r = atoi(value.c_str());
  403. }
  404. else if (name == "G")
  405. {
  406. bgStartColor.g = atoi(value.c_str());
  407. }
  408. else if (name == "B")
  409. {
  410. bgStartColor.b = atoi(value.c_str());
  411. }
  412. attribute = attribute->Next();
  413. }
  414. }
  415. else if (name == "ColorVector")
  416. {
  417. attribute = child->FirstAttribute();
  418. while (attribute)
  419. {
  420. name = attribute->Name();
  421. std::string value = attribute->Value();
  422. if (name == "ScaleX")
  423. {
  424. colorVector.x = atof(value.c_str());
  425. }
  426. else if (name == "ScaleY")
  427. {
  428. colorVector.y = atof(value.c_str());
  429. }
  430. attribute = attribute->Next();
  431. }
  432. }
  433. else if (name == "FileData")
  434. {
  435. std::string texture = "";
  436. std::string texturePng = "";
  437. attribute = child->FirstAttribute();
  438. while (attribute)
  439. {
  440. name = attribute->Name();
  441. std::string value = attribute->Value();
  442. if (name == "Path")
  443. {
  444. path = value;
  445. }
  446. else if (name == "Type")
  447. {
  448. resourceType = getResourceType(value);
  449. }
  450. else if (name == "Plist")
  451. {
  452. plistFile = value;
  453. texture = value;
  454. }
  455. attribute = attribute->Next();
  456. }
  457. if (resourceType == 1)
  458. {
  459. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  460. fbs->_textures.push_back(builder->CreateString(texture));
  461. }
  462. }
  463. child = child->NextSiblingElement();
  464. }
  465. Color f_bgColor(255, bgColor.r, bgColor.g, bgColor.b);
  466. Color f_bgStartColor(255, bgStartColor.r, bgStartColor.g, bgStartColor.b);
  467. Color f_bgEndColor(255, bgEndColor.r, bgEndColor.g, bgEndColor.b);
  468. ColorVector f_colorVector(colorVector.x, colorVector.y);
  469. CapInsets f_capInsets(capInsets.origin.x, capInsets.origin.y, capInsets.size.width, capInsets.size.height);
  470. FlatSize f_scale9Size(scale9Size.width, scale9Size.height);
  471. auto options = CreatePanelOptions(*builder,
  472. widgetOptions,
  473. CreateResourceData(*builder,
  474. builder->CreateString(path),
  475. builder->CreateString(plistFile),
  476. resourceType),
  477. clipEnabled,
  478. &f_bgColor,
  479. &f_bgStartColor,
  480. &f_bgEndColor,
  481. colorType,
  482. bgColorOpacity,
  483. &f_colorVector,
  484. &f_capInsets,
  485. &f_scale9Size,
  486. backGroundScale9Enabled);
  487. return *(Offset<Table>*)(&options);
  488. }
  489. void LayoutReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *layoutOptions)
  490. {
  491. Layout* panel = static_cast<Layout*>(node);
  492. auto options = (PanelOptions*)layoutOptions;
  493. bool clipEnabled = options->clipEnabled() != 0;
  494. panel->setClippingEnabled(clipEnabled);
  495. bool backGroundScale9Enabled = options->backGroundScale9Enabled() != 0;
  496. panel->setBackGroundImageScale9Enabled(backGroundScale9Enabled);
  497. auto f_bgColor = options->bgColor();
  498. Color3B bgColor(f_bgColor->r(), f_bgColor->g(), f_bgColor->b());
  499. auto f_bgStartColor = options->bgStartColor();
  500. Color3B bgStartColor(f_bgStartColor->r(), f_bgStartColor->g(), f_bgStartColor->b());
  501. auto f_bgEndColor = options->bgEndColor();
  502. Color3B bgEndColor(f_bgEndColor->r(), f_bgEndColor->g(), f_bgEndColor->b());
  503. auto f_colorVecor = options->colorVector();
  504. Vec2 colorVector(f_colorVecor->vectorX(), f_colorVecor->vectorY());
  505. panel->setBackGroundColorVector(colorVector);
  506. int bgColorOpacity = options->bgColorOpacity();
  507. int colorType = options->colorType();
  508. panel->setBackGroundColorType(Layout::BackGroundColorType(colorType));
  509. panel->setBackGroundColor(bgStartColor, bgEndColor);
  510. panel->setBackGroundColor(bgColor);
  511. panel->setBackGroundColorOpacity(bgColorOpacity);
  512. bool fileExist = false;
  513. std::string errorFilePath = "";
  514. auto imageFileNameDic = options->backGroundImageData();
  515. int imageFileNameType = imageFileNameDic->resourceType();
  516. std::string imageFileName = imageFileNameDic->path()->c_str();
  517. if (imageFileName != "")
  518. {
  519. switch (imageFileNameType)
  520. {
  521. case 0:
  522. {
  523. if (FileUtils::getInstance()->isFileExist(imageFileName))
  524. {
  525. fileExist = true;
  526. }
  527. else
  528. {
  529. errorFilePath = imageFileName;
  530. fileExist = false;
  531. }
  532. break;
  533. }
  534. case 1:
  535. {
  536. std::string plist = imageFileNameDic->plistFile()->c_str();
  537. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  538. if (spriteFrame)
  539. {
  540. fileExist = true;
  541. }
  542. else
  543. {
  544. if (FileUtils::getInstance()->isFileExist(plist))
  545. {
  546. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  547. ValueMap metadata = value["metadata"].asValueMap();
  548. std::string textureFileName = metadata["textureFileName"].asString();
  549. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  550. {
  551. errorFilePath = textureFileName;
  552. }
  553. }
  554. else
  555. {
  556. errorFilePath = plist;
  557. }
  558. fileExist = false;
  559. }
  560. break;
  561. }
  562. default:
  563. break;
  564. }
  565. if (fileExist)
  566. {
  567. panel->setBackGroundImage(imageFileName, (Widget::TextureResType)imageFileNameType);
  568. }
  569. }
  570. auto widgetOptions = options->widgetOptions();
  571. auto f_color = widgetOptions->color();
  572. Color3B color(f_color->r(), f_color->g(), f_color->b());
  573. panel->setColor(color);
  574. int opacity = widgetOptions->alpha();
  575. panel->setOpacity(opacity);
  576. auto widgetReader = WidgetReader::getInstance();
  577. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  578. if (backGroundScale9Enabled)
  579. {
  580. auto f_capInsets = options->capInsets();
  581. Rect capInsets(f_capInsets->x(), f_capInsets->y(), f_capInsets->width(), f_capInsets->height());
  582. panel->setBackGroundImageCapInsets(capInsets);
  583. auto f_scale9Size = options->scale9Size();
  584. Size scale9Size(f_scale9Size->width(), f_scale9Size->height());
  585. panel->setContentSize(scale9Size);
  586. }
  587. else
  588. {
  589. if (!panel->isIgnoreContentAdaptWithSize())
  590. {
  591. Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
  592. panel->setContentSize(contentSize);
  593. }
  594. }
  595. }
  596. Node* LayoutReader::createNodeWithFlatBuffers(const flatbuffers::Table *layoutOptions)
  597. {
  598. Layout* layout = Layout::create();
  599. setPropsWithFlatBuffers(layout, (Table*)layoutOptions);
  600. return layout;
  601. }
  602. int LayoutReader::getResourceType(std::string key)
  603. {
  604. if(key == "Normal" || key == "Default")
  605. {
  606. return 0;
  607. }
  608. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  609. if(fbs->_isSimulator)
  610. {
  611. if(key == "MarkedSubImage")
  612. {
  613. return 0;
  614. }
  615. }
  616. return 1;
  617. }
  618. }