PageViewReader.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #include "editor-support/cocostudio/WidgetReader/PageViewReader/PageViewReader.h"
  2. #include "ui/UIPageView.h"
  3. #include "ui/UILayout.h"
  4. #include "platform/CCFileUtils.h"
  5. #include "2d/CCSpriteFrameCache.h"
  6. #include "editor-support/cocostudio/CocoLoader.h"
  7. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  8. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  9. #include "tinyxml2.h"
  10. #include "flatbuffers/flatbuffers.h"
  11. USING_NS_CC;
  12. using namespace ui;
  13. using namespace flatbuffers;
  14. namespace cocostudio
  15. {
  16. static PageViewReader* instancePageViewReader = nullptr;
  17. IMPLEMENT_CLASS_NODE_READER_INFO(PageViewReader)
  18. PageViewReader::PageViewReader()
  19. {
  20. }
  21. PageViewReader::~PageViewReader()
  22. {
  23. }
  24. PageViewReader* PageViewReader::getInstance()
  25. {
  26. if (!instancePageViewReader)
  27. {
  28. instancePageViewReader = new (std::nothrow) PageViewReader();
  29. }
  30. return instancePageViewReader;
  31. }
  32. void PageViewReader::destroyInstance()
  33. {
  34. CC_SAFE_DELETE(instancePageViewReader);
  35. }
  36. void PageViewReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  37. {
  38. LayoutReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  39. }
  40. void PageViewReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  41. {
  42. LayoutReader::setPropsFromJsonDictionary(widget, options);
  43. }
  44. Offset<Table> PageViewReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  45. flatbuffers::FlatBufferBuilder *builder)
  46. {
  47. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  48. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  49. std::string path = "";
  50. std::string plistFile = "";
  51. int resourceType = 0;
  52. bool clipEnabled = false;
  53. Color3B bgColor;
  54. Color3B bgStartColor;
  55. Color3B bgEndColor;
  56. int colorType = 0;
  57. GLubyte bgColorOpacity = 255;
  58. Vec2 colorVector(0.0f, -0.5f);
  59. Rect capInsets;
  60. Size scale9Size;
  61. bool backGroundScale9Enabled = false;
  62. // attributes
  63. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  64. while (attribute)
  65. {
  66. std::string name = attribute->Name();
  67. std::string value = attribute->Value();
  68. if (name == "ClipAble")
  69. {
  70. clipEnabled = (value == "True") ? true : false;
  71. }
  72. else if (name == "ComboBoxIndex")
  73. {
  74. colorType = atoi(value.c_str());
  75. }
  76. else if (name == "BackColorAlpha")
  77. {
  78. bgColorOpacity = atoi(value.c_str());
  79. }
  80. else if (name == "Scale9Enable")
  81. {
  82. if (value == "True")
  83. {
  84. backGroundScale9Enabled = true;
  85. }
  86. }
  87. else if (name == "Scale9OriginX")
  88. {
  89. capInsets.origin.x = atof(value.c_str());
  90. }
  91. else if (name == "Scale9OriginY")
  92. {
  93. capInsets.origin.y = atof(value.c_str());
  94. }
  95. else if (name == "Scale9Width")
  96. {
  97. capInsets.size.width = atof(value.c_str());
  98. }
  99. else if (name == "Scale9Height")
  100. {
  101. capInsets.size.height = atof(value.c_str());
  102. }
  103. attribute = attribute->Next();
  104. }
  105. // child elements
  106. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  107. while (child)
  108. {
  109. std::string name = child->Name();
  110. if (name == "Size" && backGroundScale9Enabled)
  111. {
  112. attribute = child->FirstAttribute();
  113. while (attribute)
  114. {
  115. name = attribute->Name();
  116. std::string value = attribute->Value();
  117. if (name == "X")
  118. {
  119. scale9Size.width = atof(value.c_str());
  120. }
  121. else if (name == "Y")
  122. {
  123. scale9Size.height = atof(value.c_str());
  124. }
  125. attribute = attribute->Next();
  126. }
  127. }
  128. else if (name == "SingleColor")
  129. {
  130. attribute = child->FirstAttribute();
  131. while (attribute)
  132. {
  133. name = attribute->Name();
  134. std::string value = attribute->Value();
  135. if (name == "R")
  136. {
  137. bgColor.r = atoi(value.c_str());
  138. }
  139. else if (name == "G")
  140. {
  141. bgColor.g = atoi(value.c_str());
  142. }
  143. else if (name == "B")
  144. {
  145. bgColor.b = atoi(value.c_str());
  146. }
  147. attribute = attribute->Next();
  148. }
  149. }
  150. else if (name == "EndColor")
  151. {
  152. attribute = child->FirstAttribute();
  153. while (attribute)
  154. {
  155. name = attribute->Name();
  156. std::string value = attribute->Value();
  157. if (name == "R")
  158. {
  159. bgEndColor.r = atoi(value.c_str());
  160. }
  161. else if (name == "G")
  162. {
  163. bgEndColor.g = atoi(value.c_str());
  164. }
  165. else if (name == "B")
  166. {
  167. bgEndColor.b = atoi(value.c_str());
  168. }
  169. attribute = attribute->Next();
  170. }
  171. }
  172. else if (name == "FirstColor")
  173. {
  174. attribute = child->FirstAttribute();
  175. while (attribute)
  176. {
  177. name = attribute->Name();
  178. std::string value = attribute->Value();
  179. if (name == "R")
  180. {
  181. bgStartColor.r = atoi(value.c_str());
  182. }
  183. else if (name == "G")
  184. {
  185. bgStartColor.g = atoi(value.c_str());
  186. }
  187. else if (name == "B")
  188. {
  189. bgStartColor.b = atoi(value.c_str());
  190. }
  191. attribute = attribute->Next();
  192. }
  193. }
  194. else if (name == "ColorVector")
  195. {
  196. attribute = child->FirstAttribute();
  197. while (attribute)
  198. {
  199. name = attribute->Name();
  200. std::string value = attribute->Value();
  201. if (name == "ScaleX")
  202. {
  203. colorVector.x = atof(value.c_str());
  204. }
  205. else if (name == "ScaleY")
  206. {
  207. colorVector.y = atof(value.c_str());
  208. }
  209. attribute = attribute->Next();
  210. }
  211. }
  212. else if (name == "FileData")
  213. {
  214. std::string texture = "";
  215. std::string texturePng = "";
  216. attribute = child->FirstAttribute();
  217. while (attribute)
  218. {
  219. name = attribute->Name();
  220. std::string value = attribute->Value();
  221. if (name == "Path")
  222. {
  223. path = value;
  224. }
  225. else if (name == "Type")
  226. {
  227. resourceType = getResourceType(value);
  228. }
  229. else if (name == "Plist")
  230. {
  231. plistFile = value;
  232. texture = value;
  233. }
  234. attribute = attribute->Next();
  235. }
  236. if (resourceType == 1)
  237. {
  238. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  239. fbs->_textures.push_back(builder->CreateString(texture));
  240. }
  241. }
  242. child = child->NextSiblingElement();
  243. }
  244. Color f_bgColor(255, bgColor.r, bgColor.g, bgColor.b);
  245. Color f_bgStartColor(255, bgStartColor.r, bgStartColor.g, bgStartColor.b);
  246. Color f_bgEndColor(255, bgEndColor.r, bgEndColor.g, bgEndColor.b);
  247. ColorVector f_colorVector(colorVector.x, colorVector.y);
  248. CapInsets f_capInsets(capInsets.origin.x, capInsets.origin.y, capInsets.size.width, capInsets.size.height);
  249. FlatSize f_scale9Size(scale9Size.width, scale9Size.height);
  250. auto options = CreatePageViewOptions(*builder,
  251. widgetOptions,
  252. CreateResourceData(*builder,
  253. builder->CreateString(path),
  254. builder->CreateString(plistFile),
  255. resourceType),
  256. clipEnabled,
  257. &f_bgColor,
  258. &f_bgStartColor,
  259. &f_bgEndColor,
  260. colorType,
  261. bgColorOpacity,
  262. &f_colorVector,
  263. &f_capInsets,
  264. &f_scale9Size,
  265. backGroundScale9Enabled);
  266. return *(Offset<Table>*)(&options);
  267. }
  268. void PageViewReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *pageViewOptions)
  269. {
  270. PageView* pageView = static_cast<PageView*>(node);
  271. auto options = (PageViewOptions*)pageViewOptions;
  272. bool clipEnabled = options->clipEnabled() != 0;
  273. pageView->setClippingEnabled(clipEnabled);
  274. bool backGroundScale9Enabled = options->backGroundScale9Enabled() != 0;
  275. pageView->setBackGroundImageScale9Enabled(backGroundScale9Enabled);
  276. auto f_bgColor = options->bgColor();
  277. Color3B bgColor(f_bgColor->r(), f_bgColor->g(), f_bgColor->b());
  278. auto f_bgStartColor = options->bgStartColor();
  279. Color3B bgStartColor(f_bgStartColor->r(), f_bgStartColor->g(), f_bgStartColor->b());
  280. auto f_bgEndColor = options->bgEndColor();
  281. Color3B bgEndColor(f_bgEndColor->r(), f_bgEndColor->g(), f_bgEndColor->b());
  282. auto f_colorVecor = options->colorVector();
  283. Vec2 colorVector(f_colorVecor->vectorX(), f_colorVecor->vectorY());
  284. pageView->setBackGroundColorVector(colorVector);
  285. int bgColorOpacity = options->bgColorOpacity();
  286. int colorType = options->colorType();
  287. pageView->setBackGroundColorType(Layout::BackGroundColorType(colorType));
  288. pageView->setBackGroundColor(bgStartColor, bgEndColor);
  289. pageView->setBackGroundColor(bgColor);
  290. pageView->setBackGroundColorOpacity(bgColorOpacity);
  291. bool fileExist = false;
  292. std::string errorFilePath = "";
  293. auto imageFileNameDic = options->backGroundImageData();
  294. int imageFileNameType = imageFileNameDic->resourceType();
  295. std::string imageFileName = imageFileNameDic->path()->c_str();
  296. if (imageFileName != "")
  297. {
  298. switch (imageFileNameType)
  299. {
  300. case 0:
  301. {
  302. if (FileUtils::getInstance()->isFileExist(imageFileName))
  303. {
  304. fileExist = true;
  305. }
  306. else
  307. {
  308. errorFilePath = imageFileName;
  309. fileExist = false;
  310. }
  311. break;
  312. }
  313. case 1:
  314. {
  315. std::string plist = imageFileNameDic->plistFile()->c_str();
  316. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  317. if (spriteFrame)
  318. {
  319. fileExist = true;
  320. }
  321. else
  322. {
  323. if (FileUtils::getInstance()->isFileExist(plist))
  324. {
  325. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  326. ValueMap metadata = value["metadata"].asValueMap();
  327. std::string textureFileName = metadata["textureFileName"].asString();
  328. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  329. {
  330. errorFilePath = textureFileName;
  331. }
  332. }
  333. else
  334. {
  335. errorFilePath = plist;
  336. }
  337. fileExist = false;
  338. }
  339. break;
  340. }
  341. default:
  342. break;
  343. }
  344. if (fileExist)
  345. {
  346. pageView->setBackGroundImage(imageFileName, (Widget::TextureResType)imageFileNameType);
  347. }
  348. }
  349. auto widgetOptions = options->widgetOptions();
  350. auto f_color = widgetOptions->color();
  351. Color3B color(f_color->r(), f_color->g(), f_color->b());
  352. pageView->setColor(color);
  353. int opacity = widgetOptions->alpha();
  354. pageView->setOpacity(opacity);
  355. auto widgetReader = WidgetReader::getInstance();
  356. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  357. if (backGroundScale9Enabled)
  358. {
  359. auto f_capInsets = options->capInsets();
  360. Rect capInsets(f_capInsets->x(), f_capInsets->y(), f_capInsets->width(), f_capInsets->height());
  361. pageView->setBackGroundImageCapInsets(capInsets);
  362. auto f_scale9Size = options->scale9Size();
  363. Size scale9Size(f_scale9Size->width(), f_scale9Size->height());
  364. pageView->setContentSize(scale9Size);
  365. }
  366. else
  367. {
  368. if (!pageView->isIgnoreContentAdaptWithSize())
  369. {
  370. Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
  371. pageView->setContentSize(contentSize);
  372. }
  373. }
  374. }
  375. Node* PageViewReader::createNodeWithFlatBuffers(const flatbuffers::Table *pageViewOptions)
  376. {
  377. PageView* pageView = PageView::create();
  378. setPropsWithFlatBuffers(pageView, (Table*)pageViewOptions);
  379. return pageView;
  380. }
  381. int PageViewReader::getResourceType(std::string key)
  382. {
  383. if(key == "Normal" || key == "Default")
  384. {
  385. return 0;
  386. }
  387. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  388. if(fbs->_isSimulator)
  389. {
  390. if(key == "MarkedSubImage")
  391. {
  392. return 0;
  393. }
  394. }
  395. return 1;
  396. }
  397. }