ScrollViewReader.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. #include "editor-support/cocostudio/WidgetReader/ScrollViewReader/ScrollViewReader.h"
  2. #include "ui/UIScrollView.h"
  3. #include "platform/CCFileUtils.h"
  4. #include "2d/CCSpriteFrameCache.h"
  5. #include "editor-support/cocostudio/CocoLoader.h"
  6. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  7. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  8. #include "tinyxml2.h"
  9. #include "flatbuffers/flatbuffers.h"
  10. USING_NS_CC;
  11. using namespace ui;
  12. using namespace flatbuffers;
  13. namespace cocostudio
  14. {
  15. static const char* P_InnerWidth = "innerWidth";
  16. static const char* P_InnerHeight = "innerHeight";
  17. static const char* P_Direction = "direction";
  18. static const char* P_BounceEnable = "bounceEnable";
  19. static ScrollViewReader* instanceScrollViewReader = nullptr;
  20. IMPLEMENT_CLASS_NODE_READER_INFO(ScrollViewReader)
  21. ScrollViewReader::ScrollViewReader()
  22. {
  23. }
  24. ScrollViewReader::~ScrollViewReader()
  25. {
  26. }
  27. ScrollViewReader* ScrollViewReader::getInstance()
  28. {
  29. if (!instanceScrollViewReader)
  30. {
  31. instanceScrollViewReader = new (std::nothrow) ScrollViewReader();
  32. }
  33. return instanceScrollViewReader;
  34. }
  35. void ScrollViewReader::destroyInstance()
  36. {
  37. CC_SAFE_DELETE(instanceScrollViewReader);
  38. }
  39. void ScrollViewReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode* cocoNode)
  40. {
  41. //TODO: need to refactor...
  42. LayoutReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  43. ScrollView* scrollView = static_cast<ScrollView*>(widget);
  44. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  45. float innerWidth;
  46. float innerHeight;
  47. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  48. std::string key = stChildArray[i].GetName(cocoLoader);
  49. std::string value = stChildArray[i].GetValue(cocoLoader);
  50. if (key == P_InnerWidth) {
  51. innerWidth = valueToFloat(value);
  52. }
  53. else if (key == P_InnerHeight) {
  54. innerHeight = valueToFloat(value);
  55. }
  56. else if (key == P_Direction) {
  57. scrollView->setDirection((ScrollView::Direction)valueToInt(value));
  58. }
  59. else if (key == P_BounceEnable) {
  60. scrollView->setBounceEnabled(valueToBool(value));
  61. }
  62. } //end of for loop
  63. scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
  64. }
  65. void ScrollViewReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  66. {
  67. LayoutReader::setPropsFromJsonDictionary(widget, options);
  68. ScrollView* scrollView = static_cast<ScrollView*>(widget);
  69. float innerWidth = DICTOOL->getFloatValue_json(options, P_InnerWidth, 200);
  70. float innerHeight = DICTOOL->getFloatValue_json(options, P_InnerHeight, 200);
  71. scrollView->setInnerContainerSize(Size(innerWidth, innerHeight));
  72. int direction = DICTOOL->getFloatValue_json(options, P_Direction, 1);
  73. scrollView->setDirection((ScrollView::Direction)direction);
  74. scrollView->setBounceEnabled(DICTOOL->getBooleanValue_json(options, P_BounceEnable));
  75. LayoutReader::setColorPropsFromJsonDictionary(widget, options);
  76. }
  77. Offset<Table> ScrollViewReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  78. flatbuffers::FlatBufferBuilder *builder)
  79. {
  80. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  81. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  82. std::string path = "";
  83. std::string plistFile = "";
  84. int resourceType = 0;
  85. bool clipEnabled = false;
  86. Color3B bgColor;
  87. Color3B bgStartColor;
  88. Color3B bgEndColor;
  89. int colorType = 0;
  90. GLubyte bgColorOpacity = 255;
  91. Vec2 colorVector(0.0f, -0.5f);
  92. Rect capInsets;
  93. Size scale9Size;
  94. bool backGroundScale9Enabled = false;
  95. Size innerSize(200, 300);
  96. int direction = 0;
  97. bool bounceEnabled = false;
  98. bool scrollbarEnabled = true;
  99. bool scrollbarAutoHide = true;
  100. float scrollbarAutoHideTime = 0.2f;
  101. // attributes
  102. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  103. while (attribute)
  104. {
  105. std::string name = attribute->Name();
  106. std::string value = attribute->Value();
  107. if (name == "ClipAble")
  108. {
  109. clipEnabled = FLATSTR_TO_BOOL(value);
  110. }
  111. else if (name == "ComboBoxIndex")
  112. {
  113. colorType = atoi(value.c_str());
  114. }
  115. else if (name == "BackColorAlpha")
  116. {
  117. bgColorOpacity = atoi(value.c_str());
  118. }
  119. else if (name == "Scale9Enable")
  120. {
  121. backGroundScale9Enabled = FLATSTR_TO_BOOL(value);
  122. }
  123. else if (name == "Scale9OriginX")
  124. {
  125. capInsets.origin.x = atof(value.c_str());
  126. }
  127. else if (name == "Scale9OriginY")
  128. {
  129. capInsets.origin.y = atof(value.c_str());
  130. }
  131. else if (name == "Scale9Width")
  132. {
  133. capInsets.size.width = atof(value.c_str());
  134. }
  135. else if (name == "Scale9Height")
  136. {
  137. capInsets.size.height = atof(value.c_str());
  138. }
  139. else if (name == "ScrollDirectionType")
  140. {
  141. if (value == "Vertical")
  142. {
  143. direction = 1;
  144. }
  145. else if (value == "Horizontal")
  146. {
  147. direction = 2;
  148. }
  149. else if (value == "Vertical_Horizontal")
  150. {
  151. direction = 3;
  152. }
  153. }
  154. else if (name == "IsBounceEnabled")
  155. {
  156. bounceEnabled = FLATSTR_TO_BOOL(value);
  157. }
  158. else if (name.compare("BarEnabled") == 0)
  159. {
  160. scrollbarEnabled = FLATSTR_TO_BOOL(value);
  161. }
  162. else if (name.compare("BarAutoHide") == 0)
  163. {
  164. scrollbarAutoHide = FLATSTR_TO_BOOL(value);
  165. }
  166. else if (name.compare("BarAutoHideTime") == 0)
  167. {
  168. scrollbarAutoHideTime = atof(value.c_str());
  169. }
  170. attribute = attribute->Next();
  171. }
  172. // child elements
  173. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  174. while (child)
  175. {
  176. std::string name = child->Name();
  177. if (name == "InnerNodeSize")
  178. {
  179. attribute = child->FirstAttribute();
  180. while (attribute)
  181. {
  182. name = attribute->Name();
  183. std::string value = attribute->Value();
  184. if (name == "Width")
  185. {
  186. innerSize.width = atof(value.c_str());
  187. }
  188. else if (name == "Height")
  189. {
  190. innerSize.height = atof(value.c_str());
  191. }
  192. attribute = attribute->Next();
  193. }
  194. }
  195. else if (name == "Size" && backGroundScale9Enabled)
  196. {
  197. attribute = child->FirstAttribute();
  198. while (attribute)
  199. {
  200. name = attribute->Name();
  201. std::string value = attribute->Value();
  202. if (name == "X")
  203. {
  204. scale9Size.width = atof(value.c_str());
  205. }
  206. else if (name == "Y")
  207. {
  208. scale9Size.height = atof(value.c_str());
  209. }
  210. attribute = attribute->Next();
  211. }
  212. }
  213. else if (name == "SingleColor")
  214. {
  215. attribute = child->FirstAttribute();
  216. while (attribute)
  217. {
  218. name = attribute->Name();
  219. std::string value = attribute->Value();
  220. if (name == "R")
  221. {
  222. bgColor.r = atoi(value.c_str());
  223. }
  224. else if (name == "G")
  225. {
  226. bgColor.g = atoi(value.c_str());
  227. }
  228. else if (name == "B")
  229. {
  230. bgColor.b = atoi(value.c_str());
  231. }
  232. attribute = attribute->Next();
  233. }
  234. }
  235. else if (name == "EndColor")
  236. {
  237. attribute = child->FirstAttribute();
  238. while (attribute)
  239. {
  240. name = attribute->Name();
  241. std::string value = attribute->Value();
  242. if (name == "R")
  243. {
  244. bgEndColor.r = atoi(value.c_str());
  245. }
  246. else if (name == "G")
  247. {
  248. bgEndColor.g = atoi(value.c_str());
  249. }
  250. else if (name == "B")
  251. {
  252. bgEndColor.b = atoi(value.c_str());
  253. }
  254. attribute = attribute->Next();
  255. }
  256. }
  257. else if (name == "FirstColor")
  258. {
  259. attribute = child->FirstAttribute();
  260. while (attribute)
  261. {
  262. name = attribute->Name();
  263. std::string value = attribute->Value();
  264. if (name == "R")
  265. {
  266. bgStartColor.r = atoi(value.c_str());
  267. }
  268. else if (name == "G")
  269. {
  270. bgStartColor.g = atoi(value.c_str());
  271. }
  272. else if (name == "B")
  273. {
  274. bgStartColor.b = atoi(value.c_str());
  275. }
  276. attribute = attribute->Next();
  277. }
  278. }
  279. else if (name == "ColorVector")
  280. {
  281. attribute = child->FirstAttribute();
  282. while (attribute)
  283. {
  284. name = attribute->Name();
  285. std::string value = attribute->Value();
  286. if (name == "ScaleX")
  287. {
  288. colorVector.x = atof(value.c_str());
  289. }
  290. else if (name == "ScaleY")
  291. {
  292. colorVector.y = atof(value.c_str());
  293. }
  294. attribute = attribute->Next();
  295. }
  296. }
  297. else if (name == "FileData")
  298. {
  299. std::string texture = "";
  300. std::string texturePng = "";
  301. attribute = child->FirstAttribute();
  302. while (attribute)
  303. {
  304. name = attribute->Name();
  305. std::string value = attribute->Value();
  306. if (name == "Path")
  307. {
  308. path = value;
  309. }
  310. else if (name == "Type")
  311. {
  312. resourceType = getResourceType(value);
  313. }
  314. else if (name == "Plist")
  315. {
  316. plistFile = value;
  317. texture = value;
  318. }
  319. attribute = attribute->Next();
  320. }
  321. if (resourceType == 1)
  322. {
  323. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  324. fbs->_textures.push_back(builder->CreateString(texture));
  325. }
  326. }
  327. child = child->NextSiblingElement();
  328. }
  329. Color f_bgColor(255, bgColor.r, bgColor.g, bgColor.b);
  330. Color f_bgStartColor(255, bgStartColor.r, bgStartColor.g, bgStartColor.b);
  331. Color f_bgEndColor(255, bgEndColor.r, bgEndColor.g, bgEndColor.b);
  332. ColorVector f_colorVector(colorVector.x, colorVector.y);
  333. CapInsets f_capInsets(capInsets.origin.x, capInsets.origin.y, capInsets.size.width, capInsets.size.height);
  334. FlatSize f_scale9Size(scale9Size.width, scale9Size.height);
  335. FlatSize f_innerSize(innerSize.width, innerSize.height);
  336. auto options = CreateScrollViewOptions(*builder,
  337. widgetOptions,
  338. CreateResourceData(*builder,
  339. builder->CreateString(path),
  340. builder->CreateString(plistFile),
  341. resourceType),
  342. clipEnabled,
  343. &f_bgColor,
  344. &f_bgStartColor,
  345. &f_bgEndColor,
  346. colorType,
  347. bgColorOpacity,
  348. &f_colorVector,
  349. &f_capInsets,
  350. &f_scale9Size,
  351. backGroundScale9Enabled,
  352. &f_innerSize,
  353. direction,
  354. bounceEnabled,
  355. scrollbarEnabled,
  356. scrollbarAutoHide,
  357. scrollbarAutoHideTime);
  358. return *(Offset<Table>*)(&options);
  359. }
  360. void ScrollViewReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *scrollViewOptions)
  361. {
  362. ScrollView* scrollView = static_cast<ScrollView*>(node);
  363. auto options = (ScrollViewOptions*)scrollViewOptions;
  364. bool clipEnabled = options->clipEnabled() != 0;
  365. scrollView->setClippingEnabled(clipEnabled);
  366. bool backGroundScale9Enabled = options->backGroundScale9Enabled() != 0;
  367. scrollView->setBackGroundImageScale9Enabled(backGroundScale9Enabled);
  368. auto f_bgColor = options->bgColor();
  369. Color3B bgColor(f_bgColor->r(), f_bgColor->g(), f_bgColor->b());
  370. auto f_bgStartColor = options->bgStartColor();
  371. Color3B bgStartColor(f_bgStartColor->r(), f_bgStartColor->g(), f_bgStartColor->b());
  372. auto f_bgEndColor = options->bgEndColor();
  373. Color3B bgEndColor(f_bgEndColor->r(), f_bgEndColor->g(), f_bgEndColor->b());
  374. auto f_colorVecor = options->colorVector();
  375. Vec2 colorVector(f_colorVecor->vectorX(), f_colorVecor->vectorY());
  376. scrollView->setBackGroundColorVector(colorVector);
  377. int bgColorOpacity = options->bgColorOpacity();
  378. int colorType = options->colorType();
  379. scrollView->setBackGroundColorType(Layout::BackGroundColorType(colorType));
  380. scrollView->setBackGroundColor(bgStartColor, bgEndColor);
  381. scrollView->setBackGroundColor(bgColor);
  382. scrollView->setBackGroundColorOpacity(bgColorOpacity);
  383. bool fileExist = false;
  384. std::string errorFilePath = "";
  385. auto imageFileNameDic = options->backGroundImageData();
  386. int imageFileNameType = imageFileNameDic->resourceType();
  387. std::string imageFileName = imageFileNameDic->path()->c_str();
  388. if (imageFileName != "")
  389. {
  390. switch (imageFileNameType)
  391. {
  392. case 0:
  393. {
  394. if (FileUtils::getInstance()->isFileExist(imageFileName))
  395. {
  396. fileExist = true;
  397. }
  398. else
  399. {
  400. errorFilePath = imageFileName;
  401. fileExist = false;
  402. }
  403. break;
  404. }
  405. case 1:
  406. {
  407. std::string plist = imageFileNameDic->plistFile()->c_str();
  408. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  409. if (spriteFrame)
  410. {
  411. fileExist = true;
  412. }
  413. else
  414. {
  415. if (FileUtils::getInstance()->isFileExist(plist))
  416. {
  417. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  418. ValueMap metadata = value["metadata"].asValueMap();
  419. std::string textureFileName = metadata["textureFileName"].asString();
  420. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  421. {
  422. errorFilePath = textureFileName;
  423. }
  424. }
  425. else
  426. {
  427. errorFilePath = plist;
  428. }
  429. fileExist = false;
  430. }
  431. break;
  432. }
  433. default:
  434. break;
  435. }
  436. if (fileExist)
  437. {
  438. scrollView->setBackGroundImage(imageFileName, (Widget::TextureResType)imageFileNameType);
  439. }
  440. }
  441. auto widgetOptions = options->widgetOptions();
  442. auto f_color = widgetOptions->color();
  443. Color3B color(f_color->r(), f_color->g(), f_color->b());
  444. scrollView->setColor(color);
  445. int opacity = widgetOptions->alpha();
  446. scrollView->setOpacity(opacity);
  447. auto f_innerSize = options->innerSize();
  448. Size innerSize(f_innerSize->width(), f_innerSize->height());
  449. scrollView->setInnerContainerSize(innerSize);
  450. int direction = options->direction();
  451. scrollView->setDirection((ScrollView::Direction)direction);
  452. bool bounceEnabled = options->bounceEnabled() != 0;
  453. scrollView->setBounceEnabled(bounceEnabled);
  454. bool scrollbarEnabled = options->scrollbarEnabeld() != 0;
  455. scrollView->setScrollBarEnabled(scrollbarEnabled);
  456. if (scrollbarEnabled)
  457. {
  458. bool scrollbarAutoHide = options->scrollbarAutoHide() != 0;
  459. scrollView->setScrollBarAutoHideEnabled(scrollbarAutoHide);
  460. float barAutoHideTime = options->scrollbarAutoHideTime();
  461. scrollView->setScrollBarAutoHideTime(barAutoHideTime);
  462. }
  463. auto widgetReader = WidgetReader::getInstance();
  464. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  465. if (backGroundScale9Enabled)
  466. {
  467. auto f_capInsets = options->capInsets();
  468. Rect capInsets(f_capInsets->x(), f_capInsets->y(), f_capInsets->width(), f_capInsets->height());
  469. scrollView->setBackGroundImageCapInsets(capInsets);
  470. auto f_scale9Size = options->scale9Size();
  471. Size scale9Size(f_scale9Size->width(), f_scale9Size->height());
  472. scrollView->setContentSize(scale9Size);
  473. }
  474. else
  475. {
  476. if (!scrollView->isIgnoreContentAdaptWithSize())
  477. {
  478. Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
  479. scrollView->setContentSize(contentSize);
  480. }
  481. }
  482. }
  483. Node* ScrollViewReader::createNodeWithFlatBuffers(const flatbuffers::Table *scrollViewOptions)
  484. {
  485. ScrollView* scrollView = ScrollView::create();
  486. setPropsWithFlatBuffers(scrollView, (Table*)scrollViewOptions);
  487. return scrollView;
  488. }
  489. int ScrollViewReader::getResourceType(std::string key)
  490. {
  491. if (key == "Normal" || key == "Default")
  492. {
  493. return 0;
  494. }
  495. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  496. if (fbs->_isSimulator)
  497. {
  498. if (key == "MarkedSubImage")
  499. {
  500. return 0;
  501. }
  502. }
  503. return 1;
  504. }
  505. }