WidgetReader.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. #include "editor-support/cocostudio/WidgetReader/WidgetReader.h"
  2. #include "editor-support/cocostudio/CocoLoader.h"
  3. #include "ui/UIButton.h"
  4. #include "editor-support/cocostudio/ActionTimeline/CCActionTimeline.h"
  5. #include "editor-support/cocostudio/CCComExtensionData.h"
  6. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  7. #include "tinyxml2.h"
  8. #include "flatbuffers/flatbuffers.h"
  9. #include "ui/UILayoutComponent.h"
  10. #include "editor-support/cocostudio/ActionTimeline/CSLoader.h"
  11. #include "base/ccUtils.h"
  12. #include "base/CCDirector.h"
  13. USING_NS_CC;
  14. using namespace ui;
  15. using namespace flatbuffers;
  16. /**/
  17. namespace cocostudio
  18. {
  19. const char* P_IgnoreSize = "ignoreSize";
  20. const char* P_SizeType = "sizeType";
  21. const char* P_PositionType = "positionType";
  22. const char* P_SizePercentX = "sizePercentX";
  23. const char* P_SizePercentY = "sizePercentY";
  24. const char* P_PositionPercentX = "positionPercentX";
  25. const char* P_PositionPercentY = "positionPercentY";
  26. const char* P_AdaptScreen = "adaptScreen";
  27. const char* P_Width = "width";
  28. const char* P_Height = "height";
  29. const char* P_Tag = "tag";
  30. const char* P_ActionTag = "actiontag";
  31. const char* P_TouchAble = "touchAble";
  32. const char* P_Name = "name";
  33. const char* P_X = "x";
  34. const char* P_Y = "y";
  35. const char* P_ScaleX = "scaleX";
  36. const char* P_ScaleY = "scaleY";
  37. const char* P_Rotation = "rotation";
  38. const char* P_Visbile = "visible";
  39. const char* P_ZOrder = "ZOrder";
  40. const char* P_LayoutParameter = "layoutParameter";
  41. const char* P_Type = "type";
  42. const char* P_Gravity = "gravity";
  43. const char* P_RelativeName = "relativeName";
  44. const char* P_RelativeToName = "relativeToName";
  45. const char* P_Align = "align";
  46. const char* P_MarginLeft = "marginLeft";
  47. const char* P_MarginRight = "marginRight";
  48. const char* P_MarginTop = "marginTop";
  49. const char* P_MarginDown = "marginDown";
  50. const char* P_Opacity = "opacity";
  51. const char* P_ColorR = "colorR";
  52. const char* P_ColorG = "colorG";
  53. const char* P_ColorB = "colorB";
  54. const char* P_FlipX = "flipX";
  55. const char* P_FlipY = "flipY";
  56. const char* P_AnchorPointX = "anchorPointX";
  57. const char* P_AnchorPointY = "anchorPointY";
  58. const char* P_ResourceType = "resourceType";
  59. const char* P_Path = "path";
  60. const char* P_Layout_PositionPercentXEnabled = "PositionPercentXEnabled";
  61. const char* P_Layout_PositionPercentYEnabled = "PositionPercentYEnabled";
  62. const char* P_Layout_PercentWidthEnable = "PercentWidthEnabled";
  63. const char* P_Layout_PercentHeightEnable = "PercentHeightEnabled";
  64. const char* P_Layout_StretchWidthEnable = "StretchWidthEnable";
  65. const char* P_Layout_StretchHeightEnable = "StretchHeightEnable";
  66. const char* P_Layout_HorizontalEdge = "HorizontalEdge";
  67. const char* P_Layout_VerticalEdge = "VerticalEdge";
  68. const char* P_Layout_LeftMargin = "LeftMargin";
  69. const char* P_Layout_RightMargin = "RightMargin";
  70. const char* P_Layout_TopMargin = "TopMargin";
  71. const char* P_Layout_BottomMargin = "BottomMargin";
  72. const char* P_Layout_BothEdge = "BothEdge";
  73. const char* P_Layout_LeftEdge = "LeftEdge";
  74. const char* P_Layout_RightEdge = "RightEdge";
  75. const char* P_Layout_TopEdge = "TopEdge";
  76. const char* P_Layout_BottomEdge = "BottomEdge";
  77. static WidgetReader* instanceWidgetReader = nullptr;
  78. IMPLEMENT_CLASS_NODE_READER_INFO(WidgetReader)
  79. WidgetReader::WidgetReader()
  80. :_sizePercentX(0.0f),
  81. _sizePercentY(0.0f),
  82. _positionPercentX(0.0f),
  83. _positionPercentY(0.0f),
  84. _width(0.0f),
  85. _height(0.0f),
  86. _opacity(255),
  87. _isAdaptScreen(false)
  88. {
  89. valueToInt = [=](const std::string& str) -> int{
  90. return atoi(str.c_str());
  91. };
  92. valueToBool = [=](const std::string& str) -> bool{
  93. int intValue = valueToInt(str);
  94. if (1 == intValue) {
  95. return true;
  96. }else{
  97. return false;
  98. }
  99. };
  100. valueToFloat = [=](const std::string& str) -> float{
  101. return utils::atof(str.c_str());
  102. };
  103. }
  104. WidgetReader::~WidgetReader()
  105. {
  106. }
  107. WidgetReader* WidgetReader::getInstance()
  108. {
  109. if (!instanceWidgetReader)
  110. {
  111. instanceWidgetReader = new (std::nothrow) WidgetReader();
  112. }
  113. return instanceWidgetReader;
  114. }
  115. void WidgetReader::purge()
  116. {
  117. CC_SAFE_DELETE(instanceWidgetReader);
  118. }
  119. void WidgetReader::destroyInstance()
  120. {
  121. CC_SAFE_DELETE(instanceWidgetReader);
  122. }
  123. void WidgetReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  124. {
  125. bool ignoreSizeExsit = DICTOOL->checkObjectExist_json(options, P_IgnoreSize);
  126. if (ignoreSizeExsit) {
  127. widget->ignoreContentAdaptWithSize(DICTOOL->getBooleanValue_json(options, P_IgnoreSize));
  128. }
  129. widget->setSizeType((Widget::SizeType)DICTOOL->getIntValue_json(options, P_SizeType));
  130. widget->setPositionType((Widget::PositionType)DICTOOL->getIntValue_json(options, P_PositionType));
  131. widget->setSizePercent(Vec2(DICTOOL->getFloatValue_json(options, P_SizePercentX), DICTOOL->getFloatValue_json(options, P_SizePercentY)));
  132. widget->setPositionPercent(Vec2(DICTOOL->getFloatValue_json(options, P_PositionPercentX), DICTOOL->getFloatValue_json(options, P_PositionPercentY)));
  133. /* adapt screen */
  134. float w = 0, h = 0;
  135. bool adaptScrennExsit = DICTOOL->checkObjectExist_json(options, P_AdaptScreen);
  136. if (adaptScrennExsit)
  137. {
  138. bool adaptScrenn = DICTOOL->getBooleanValue_json(options, P_AdaptScreen);
  139. if (adaptScrenn)
  140. {
  141. Size screenSize = Director::getInstance()->getWinSize();
  142. w = screenSize.width;
  143. h = screenSize.height;
  144. }
  145. else
  146. {
  147. w = DICTOOL->getFloatValue_json(options, P_Width);
  148. h = DICTOOL->getFloatValue_json(options, P_Height);
  149. }
  150. }
  151. else
  152. {
  153. w = DICTOOL->getFloatValue_json(options, P_Width);
  154. h = DICTOOL->getFloatValue_json(options, P_Height);
  155. }
  156. widget->setContentSize(Size(w, h));
  157. widget->setTag(DICTOOL->getIntValue_json(options, P_Tag));
  158. widget->setActionTag(DICTOOL->getIntValue_json(options, P_ActionTag));
  159. widget->setTouchEnabled(DICTOOL->getBooleanValue_json(options, P_TouchAble));
  160. const char* name = DICTOOL->getStringValue_json(options, P_Name);
  161. const char* widgetName = name?name:"default";
  162. widget->setName(widgetName);
  163. float x = DICTOOL->getFloatValue_json(options, P_X);
  164. float y = DICTOOL->getFloatValue_json(options, P_Y);
  165. widget->setPosition(Vec2(x,y));
  166. widget->setScaleX(DICTOOL->getFloatValue_json(options, P_ScaleX,1.0));
  167. widget->setScaleY(DICTOOL->getFloatValue_json(options, P_ScaleY,1.0));
  168. widget->setRotation(DICTOOL->getFloatValue_json(options, P_Rotation,0));
  169. bool vb = DICTOOL->checkObjectExist_json(options, P_Visbile);
  170. if (vb)
  171. {
  172. widget->setVisible(DICTOOL->getBooleanValue_json(options, P_Visbile));
  173. }
  174. int z = DICTOOL->getIntValue_json(options, P_ZOrder);
  175. widget->setLocalZOrder(z);
  176. bool layout = DICTOOL->checkObjectExist_json(options, P_LayoutParameter);
  177. if (layout)
  178. {
  179. const rapidjson::Value& layoutParameterDic = DICTOOL->getSubDictionary_json(options, P_LayoutParameter);
  180. int paramType = DICTOOL->getIntValue_json(layoutParameterDic, P_Type);
  181. LayoutParameter* parameter = nullptr;
  182. switch (paramType)
  183. {
  184. case 0:
  185. break;
  186. case 1:
  187. {
  188. parameter = LinearLayoutParameter::create();
  189. int gravity = DICTOOL->getIntValue_json(layoutParameterDic, P_Gravity);
  190. ((LinearLayoutParameter*)parameter)->setGravity((cocos2d::ui::LinearLayoutParameter::LinearGravity)gravity);
  191. break;
  192. }
  193. case 2:
  194. {
  195. parameter = RelativeLayoutParameter::create();
  196. RelativeLayoutParameter* rParameter = (RelativeLayoutParameter*)parameter;
  197. const char* relativeName = DICTOOL->getStringValue_json(layoutParameterDic, P_RelativeName);
  198. rParameter->setRelativeName(relativeName);
  199. const char* relativeToName = DICTOOL->getStringValue_json(layoutParameterDic, P_RelativeToName);
  200. rParameter->setRelativeToWidgetName(relativeToName);
  201. int align = DICTOOL->getIntValue_json(layoutParameterDic, P_Align);
  202. rParameter->setAlign((cocos2d::ui::RelativeLayoutParameter::RelativeAlign)align);
  203. break;
  204. }
  205. default:
  206. break;
  207. }
  208. if (parameter)
  209. {
  210. float mgl = DICTOOL->getFloatValue_json(layoutParameterDic, P_MarginLeft);
  211. float mgt = DICTOOL->getFloatValue_json(layoutParameterDic, P_MarginTop);
  212. float mgr = DICTOOL->getFloatValue_json(layoutParameterDic, P_MarginRight);
  213. float mgb = DICTOOL->getFloatValue_json(layoutParameterDic, P_MarginDown);
  214. parameter->setMargin(Margin(mgl, mgt, mgr, mgb));
  215. widget->setLayoutParameter(parameter);
  216. }
  217. }
  218. }
  219. void WidgetReader::setColorPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  220. {
  221. bool op = DICTOOL->checkObjectExist_json(options, P_Opacity);
  222. if (op)
  223. {
  224. widget->setOpacity(DICTOOL->getIntValue_json(options, P_Opacity));
  225. }
  226. bool cr = DICTOOL->checkObjectExist_json(options, P_ColorR);
  227. bool cg = DICTOOL->checkObjectExist_json(options, P_ColorG);
  228. bool cb = DICTOOL->checkObjectExist_json(options, P_ColorB);
  229. int colorR = cr ? DICTOOL->getIntValue_json(options, P_ColorR) : 255;
  230. int colorG = cg ? DICTOOL->getIntValue_json(options, P_ColorG) : 255;
  231. int colorB = cb ? DICTOOL->getIntValue_json(options, P_ColorB) : 255;
  232. widget->setColor(Color3B(colorR, colorG, colorB));
  233. this->setAnchorPointForWidget(widget, options);
  234. bool flipX = DICTOOL->getBooleanValue_json(options, P_FlipX);
  235. bool flipY = DICTOOL->getBooleanValue_json(options, P_FlipY);
  236. widget->setFlippedX(flipX);
  237. widget->setFlippedY(flipY);
  238. }
  239. void WidgetReader::beginSetBasicProperties(cocos2d::ui::Widget *widget)
  240. {
  241. _position = widget->getPosition();
  242. //set default color
  243. _color = Color3B(255,255,255);
  244. widget->setColor(_color);
  245. _opacity = widget->getOpacity();
  246. _originalAnchorPoint = widget->getAnchorPoint();
  247. }
  248. void WidgetReader::endSetBasicProperties(Widget *widget)
  249. {
  250. Size screenSize = Director::getInstance()->getWinSize();
  251. widget->setPositionPercent(Vec2(_positionPercentX, _positionPercentY));
  252. widget->setSizePercent(Vec2(_sizePercentX, _sizePercentY));
  253. if (_isAdaptScreen) {
  254. _width = screenSize.width;
  255. _height = screenSize.height;
  256. }
  257. widget->setColor(_color);
  258. widget->setOpacity(_opacity);
  259. //the setSize method will be conflict with scale9Width & scale9Height
  260. if (!widget->isIgnoreContentAdaptWithSize()) {
  261. widget->setContentSize(Size(_width, _height));
  262. }
  263. widget->setPosition(_position);
  264. widget->setAnchorPoint(_originalAnchorPoint);
  265. }
  266. std::string WidgetReader::getResourcePath(const rapidjson::Value &dict,
  267. const std::string &key,
  268. cocos2d::ui::Widget::TextureResType texType)
  269. {
  270. std::string jsonPath = GUIReader::getInstance()->getFilePath();
  271. const char* imageFileName = DICTOOL->getStringValue_json(dict, key.c_str());
  272. std::string imageFileName_tp;
  273. if (nullptr != imageFileName)
  274. {
  275. if (texType == ui::Widget::TextureResType::LOCAL) {
  276. imageFileName_tp = jsonPath + imageFileName;
  277. }
  278. else if(texType == ui::Widget::TextureResType::PLIST){
  279. imageFileName_tp = imageFileName;
  280. }
  281. else{
  282. CCASSERT(0, "invalid TextureResType!!!");
  283. }
  284. }
  285. return imageFileName_tp;
  286. }
  287. std::string WidgetReader::getResourcePath(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::ui::Widget::TextureResType texType)
  288. {
  289. stExpCocoNode *backGroundChildren = cocoNode->GetChildArray(cocoLoader);
  290. std::string backgroundValue = backGroundChildren[0].GetValue(cocoLoader);
  291. if (backgroundValue.size() < 3) {
  292. return "";
  293. }
  294. std::string binaryPath = GUIReader::getInstance()->getFilePath();
  295. std::string imageFileName_tp;
  296. if (!backgroundValue.empty())
  297. {
  298. if (texType == ui::Widget::TextureResType::LOCAL) {
  299. imageFileName_tp = binaryPath + backgroundValue;
  300. }
  301. else if(texType == ui::Widget::TextureResType::PLIST){
  302. imageFileName_tp = backgroundValue;
  303. }
  304. else{
  305. CCASSERT(0, "invalid TextureResType!!!");
  306. }
  307. }
  308. return imageFileName_tp;
  309. }
  310. void WidgetReader::setAnchorPointForWidget(cocos2d::ui::Widget *widget, const rapidjson::Value &options)
  311. {
  312. bool isAnchorPointXExists = DICTOOL->checkObjectExist_json(options, P_AnchorPointX);
  313. float anchorPointXInFile;
  314. if (isAnchorPointXExists) {
  315. anchorPointXInFile = DICTOOL->getFloatValue_json(options, P_AnchorPointX);
  316. }else{
  317. anchorPointXInFile = widget->getAnchorPoint().x;
  318. }
  319. bool isAnchorPointYExists = DICTOOL->checkObjectExist_json(options, P_AnchorPointY);
  320. float anchorPointYInFile;
  321. if (isAnchorPointYExists) {
  322. anchorPointYInFile = DICTOOL->getFloatValue_json(options, P_AnchorPointY);
  323. }
  324. else{
  325. anchorPointYInFile = widget->getAnchorPoint().y;
  326. }
  327. if (isAnchorPointXExists || isAnchorPointYExists) {
  328. widget->setAnchorPoint(Vec2(anchorPointXInFile, anchorPointYInFile));
  329. }
  330. }
  331. void WidgetReader::setPropsFromBinary(cocos2d::ui::Widget *widget, cocostudio::CocoLoader *cocoLoader, cocostudio::stExpCocoNode *cocoNode)
  332. {
  333. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  334. this->beginSetBasicProperties(widget);
  335. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  336. std::string key = stChildArray[i].GetName(cocoLoader);
  337. std::string value = stChildArray[i].GetValue(cocoLoader);
  338. CC_BASIC_PROPERTY_BINARY_READER
  339. }
  340. this->endSetBasicProperties(widget);
  341. }
  342. Offset<Table> WidgetReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData, flatbuffers::FlatBufferBuilder *builder)
  343. {
  344. std::string name = "";
  345. long actionTag = 0;
  346. Vec2 rotationSkew;
  347. int zOrder = 0;
  348. bool visible = true;
  349. GLubyte alpha = 255;
  350. int tag = 0;
  351. Vec2 position;
  352. Vec2 scale(1.0f, 1.0f);
  353. Vec2 anchorPoint;
  354. Color4B color(255, 255, 255, 255);
  355. Vec2 size;
  356. bool flipX = false;
  357. bool flipY = false;
  358. bool ignoreSize = false;
  359. bool touchEnabled = false;
  360. std::string frameEvent = "";
  361. std::string customProperty = "";
  362. std::string callbackType = "";
  363. std::string callbackName = "";
  364. bool positionXPercentEnabled = false;
  365. bool positionYPercentEnabled = false;
  366. float positionXPercent = 0;
  367. float positionYPercent = 0;
  368. bool sizeXPercentEnable = false;
  369. bool sizeYPercentEnable = false;
  370. float sizeXPercent = 0;
  371. float sizeYPercent = 0;
  372. bool stretchHorizontalEnabled = false;
  373. bool stretchVerticalEnabled = false;
  374. std::string horizontalEdge;
  375. std::string verticalEdge;
  376. float leftMargin = 0;
  377. float rightMargin = 0;
  378. float topMargin = 0;
  379. float bottomMargin = 0;
  380. // attributes
  381. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  382. while (attribute)
  383. {
  384. std::string attriname = attribute->Name();
  385. std::string value = attribute->Value();
  386. if (attriname == "Name")
  387. {
  388. name = value;
  389. }
  390. else if (attriname == "ActionTag")
  391. {
  392. actionTag = atol(value.c_str());
  393. }
  394. else if (attriname == "RotationSkewX")
  395. {
  396. rotationSkew.x = atof(value.c_str());
  397. }
  398. else if (attriname == "RotationSkewY")
  399. {
  400. rotationSkew.y = atof(value.c_str());
  401. }
  402. else if (attriname == "Rotation")
  403. {
  404. // rotation = atoi(value.c_str());
  405. }
  406. else if (attriname == "FlipX")
  407. {
  408. flipX = (value == "True") ? true : false;
  409. }
  410. else if (attriname == "FlipY")
  411. {
  412. flipY = (value == "True") ? true : false;
  413. }
  414. else if (attriname == "ZOrder")
  415. {
  416. zOrder = atoi(value.c_str());
  417. }
  418. else if (attriname == "Visible")
  419. {
  420. // visible = (value == "True") ? true : false;
  421. }
  422. else if (attriname == "VisibleForFrame")
  423. {
  424. visible = (value == "True") ? true : false;
  425. }
  426. else if (attriname == "Alpha")
  427. {
  428. alpha = atoi(value.c_str());
  429. }
  430. else if (attriname == "Tag")
  431. {
  432. tag = atoi(value.c_str());
  433. }
  434. else if (attriname == "TouchEnable")
  435. {
  436. touchEnabled = (value == "True") ? true : false;
  437. }
  438. else if (attriname == "UserData")
  439. {
  440. customProperty = value;
  441. }
  442. else if (attriname == "FrameEvent")
  443. {
  444. frameEvent = value;
  445. }
  446. else if (attriname == "CallBackType")
  447. {
  448. callbackType = value;
  449. }
  450. else if (attriname == "CallBackName")
  451. {
  452. callbackName = value;
  453. }
  454. else if (attriname == P_Layout_PositionPercentXEnabled)
  455. {
  456. positionXPercentEnabled = value == "True";
  457. }
  458. else if (attriname == P_Layout_PositionPercentYEnabled)
  459. {
  460. positionYPercentEnabled = value == "True";
  461. }
  462. else if (attriname == P_Layout_PercentWidthEnable)
  463. {
  464. sizeXPercentEnable = value == "True";
  465. }
  466. else if (attriname == P_Layout_PercentHeightEnable)
  467. {
  468. sizeYPercentEnable = value == "True";
  469. }
  470. else if (attriname == P_Layout_StretchWidthEnable)
  471. {
  472. stretchHorizontalEnabled = value == "True";
  473. }
  474. else if (attriname == P_Layout_StretchHeightEnable)
  475. {
  476. stretchVerticalEnabled = value == "True";
  477. }
  478. else if (attriname == P_Layout_HorizontalEdge)
  479. {
  480. horizontalEdge = value;
  481. }
  482. else if (attriname == P_Layout_VerticalEdge)
  483. {
  484. verticalEdge = value;
  485. }
  486. else if (attriname == P_Layout_LeftMargin)
  487. {
  488. leftMargin = atof(value.c_str());
  489. }
  490. else if (attriname == P_Layout_RightMargin)
  491. {
  492. rightMargin = atof(value.c_str());
  493. }
  494. else if (attriname == P_Layout_TopMargin)
  495. {
  496. topMargin = atof(value.c_str());
  497. }
  498. else if (attriname == P_Layout_BottomMargin)
  499. {
  500. bottomMargin = atof(value.c_str());
  501. }
  502. attribute = attribute->Next();
  503. }
  504. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  505. while (child)
  506. {
  507. std::string attriname = child->Name();
  508. if (attriname == "Position")
  509. {
  510. attribute = child->FirstAttribute();
  511. while (attribute)
  512. {
  513. attriname = attribute->Name();
  514. std::string value = attribute->Value();
  515. if (attriname == "X")
  516. {
  517. position.x = atof(value.c_str());
  518. }
  519. else if (attriname == "Y")
  520. {
  521. position.y = atof(value.c_str());
  522. }
  523. attribute = attribute->Next();
  524. }
  525. }
  526. else if (attriname == "Scale")
  527. {
  528. attribute = child->FirstAttribute();
  529. while (attribute)
  530. {
  531. attriname = attribute->Name();
  532. std::string value = attribute->Value();
  533. if (attriname == "ScaleX")
  534. {
  535. scale.x = atof(value.c_str());
  536. }
  537. else if (attriname == "ScaleY")
  538. {
  539. scale.y = atof(value.c_str());
  540. }
  541. attribute = attribute->Next();
  542. }
  543. }
  544. else if (attriname == "AnchorPoint")
  545. {
  546. attribute = child->FirstAttribute();
  547. while (attribute)
  548. {
  549. attriname = attribute->Name();
  550. std::string value = attribute->Value();
  551. if (attriname == "ScaleX")
  552. {
  553. anchorPoint.x = atof(value.c_str());
  554. }
  555. else if (attriname == "ScaleY")
  556. {
  557. anchorPoint.y = atof(value.c_str());
  558. }
  559. attribute = attribute->Next();
  560. }
  561. }
  562. else if (attriname == "CColor")
  563. {
  564. attribute = child->FirstAttribute();
  565. while (attribute)
  566. {
  567. attriname = attribute->Name();
  568. std::string value = attribute->Value();
  569. if (attriname == "A")
  570. {
  571. color.a = atoi(value.c_str());
  572. }
  573. else if (attriname == "R")
  574. {
  575. color.r = atoi(value.c_str());
  576. }
  577. else if (attriname == "G")
  578. {
  579. color.g = atoi(value.c_str());
  580. }
  581. else if (attriname == "B")
  582. {
  583. color.b = atoi(value.c_str());
  584. }
  585. attribute = attribute->Next();
  586. }
  587. }
  588. else if (attriname == "Size")
  589. {
  590. attribute = child->FirstAttribute();
  591. while (attribute)
  592. {
  593. attriname = attribute->Name();
  594. std::string value = attribute->Value();
  595. if (attriname == "X")
  596. {
  597. size.x = atof(value.c_str());
  598. }
  599. else if (attriname == "Y")
  600. {
  601. size.y = atof(value.c_str());
  602. }
  603. attribute = attribute->Next();
  604. }
  605. }
  606. else if (attriname == "PrePosition")
  607. {
  608. attribute = child->FirstAttribute();
  609. while (attribute)
  610. {
  611. attriname = attribute->Name();
  612. std::string value = attribute->Value();
  613. if (attriname == "X")
  614. {
  615. positionXPercent = atof(value.c_str());
  616. }
  617. else if (attriname == "Y")
  618. {
  619. positionYPercent = atof(value.c_str());
  620. }
  621. attribute = attribute->Next();
  622. }
  623. }
  624. else if (attriname == "PreSize")
  625. {
  626. attribute = child->FirstAttribute();
  627. while (attribute)
  628. {
  629. attriname = attribute->Name();
  630. std::string value = attribute->Value();
  631. if (attriname == "X")
  632. {
  633. sizeXPercent = atof(value.c_str());
  634. }
  635. else if (attriname == "Y")
  636. {
  637. sizeYPercent = atof(value.c_str());
  638. }
  639. attribute = attribute->Next();
  640. }
  641. }
  642. child = child->NextSiblingElement();
  643. }
  644. RotationSkew f_rotationskew(rotationSkew.x, rotationSkew.y);
  645. Position f_position(position.x, position.y);
  646. Scale f_scale(scale.x, scale.y);
  647. AnchorPoint f_anchortpoint(anchorPoint.x, anchorPoint.y);
  648. Color f_color(color.a, color.r, color.g, color.b);
  649. FlatSize f_size(size.x, size.y);
  650. auto f_layoutComponent = CreateLayoutComponentTable(*builder,
  651. positionXPercentEnabled,
  652. positionYPercentEnabled,
  653. positionXPercent,
  654. positionYPercent,
  655. sizeXPercentEnable,
  656. sizeYPercentEnable,
  657. sizeXPercent,
  658. sizeYPercent,
  659. stretchHorizontalEnabled,
  660. stretchVerticalEnabled,
  661. builder->CreateString(horizontalEdge),
  662. builder->CreateString(verticalEdge),
  663. leftMargin,
  664. rightMargin,
  665. topMargin,
  666. bottomMargin);
  667. auto options = CreateWidgetOptions(*builder,
  668. builder->CreateString(name),
  669. (int32_t)actionTag,
  670. &f_rotationskew,
  671. zOrder,
  672. visible,
  673. alpha,
  674. tag,
  675. &f_position,
  676. &f_scale,
  677. &f_anchortpoint,
  678. &f_color,
  679. &f_size,
  680. flipX,
  681. flipY,
  682. ignoreSize,
  683. touchEnabled,
  684. builder->CreateString(frameEvent),
  685. builder->CreateString(customProperty),
  686. builder->CreateString(callbackType),
  687. builder->CreateString(callbackName),
  688. f_layoutComponent);
  689. return *(Offset<Table>*)(&options);
  690. }
  691. void WidgetReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *widgetOptions)
  692. {
  693. Widget* widget = static_cast<Widget*>(node);
  694. auto options = (WidgetOptions*)widgetOptions;
  695. widget->setCascadeColorEnabled(true);
  696. widget->setCascadeOpacityEnabled(true);
  697. widget->setAnchorPoint(Vec2::ZERO);
  698. widget->setUnifySizeEnabled(true);
  699. bool ignoreSize = options->ignoreSize() != 0;
  700. widget->ignoreContentAdaptWithSize(ignoreSize);
  701. widget->setUnifySizeEnabled(false);
  702. widget->setLayoutComponentEnabled(true);
  703. widget->ignoreContentAdaptWithSize(false);
  704. Size contentSize(options->size()->width(), options->size()->height());
  705. widget->setContentSize(contentSize);
  706. int tag = options->tag();
  707. widget->setTag(tag);
  708. int actionTag = options->actionTag();
  709. widget->setActionTag(actionTag);
  710. std::string customProperty = options->customProperty()->c_str();
  711. ComExtensionData* extensionData = ComExtensionData::create();
  712. extensionData->setCustomProperty(customProperty);
  713. extensionData->setActionTag(actionTag);
  714. if (node->getComponent(ComExtensionData::COMPONENT_NAME))
  715. {
  716. node->removeComponent(ComExtensionData::COMPONENT_NAME);
  717. }
  718. node->addComponent(extensionData);
  719. bool touchEnabled = options->touchEnabled() != 0;
  720. widget->setTouchEnabled(touchEnabled);
  721. std::string name = options->name()->c_str();
  722. widget->setName(name);
  723. Vec2 position(options->position()->x(), options->position()->y());
  724. widget->setPosition(position);
  725. float scaleX = options->scale()->scaleX();
  726. widget->setScaleX(scaleX);
  727. float scaleY = options->scale()->scaleY();
  728. widget->setScaleY(scaleY);
  729. float rotationSkewX = options->rotationSkew()->rotationSkewX();
  730. widget->setRotationSkewX(rotationSkewX);
  731. float rotationSkewY = options->rotationSkew()->rotationSkewY();
  732. widget->setRotationSkewY(rotationSkewY);
  733. bool visible = options->visible() != 0;
  734. widget->setVisible(visible);
  735. int zOrder = options->zOrder();
  736. widget->setLocalZOrder(zOrder);
  737. auto f_color = options->color();
  738. Color3B color(f_color->r(), f_color->g(), f_color->b());
  739. widget->setColor(color);
  740. int alpha = options->alpha();
  741. widget->setOpacity(alpha);
  742. auto f_anchorPoint = options->anchorPoint();
  743. Vec2 anchorPoint(f_anchorPoint->scaleX(), f_anchorPoint->scaleY());
  744. widget->setAnchorPoint(anchorPoint);
  745. bool flippedX = options->flipX() != 0;
  746. widget->setFlippedX(flippedX);
  747. bool flippedY = options->flipY() != 0;
  748. widget->setFlippedY(flippedY);
  749. std::string callbackType = options->callBackType()->c_str();
  750. widget->setCallbackType(callbackType);
  751. std::string callbackName = options->callBackName()->c_str();
  752. widget->setCallbackName(callbackName);
  753. setLayoutComponentPropsWithFlatBuffers(widget, widgetOptions);
  754. }
  755. void WidgetReader::setLayoutComponentPropsWithFlatBuffers(cocos2d::Node* node, const flatbuffers::Table* nodeOptions)
  756. {
  757. auto layoutComponentTable = ((WidgetOptions*)nodeOptions)->layoutComponent();
  758. if (!layoutComponentTable) return;
  759. auto layoutComponent = ui::LayoutComponent::bindLayoutComponent(node);
  760. bool positionXPercentEnabled = layoutComponentTable->positionXPercentEnabled() != 0;
  761. bool positionYPercentEnabled = layoutComponentTable->positionYPercentEnabled() != 0;
  762. float positionXPercent = layoutComponentTable->positionXPercent();
  763. float positionYPercent = layoutComponentTable->positionYPercent();
  764. bool sizeXPercentEnable = layoutComponentTable->sizeXPercentEnable() != 0;
  765. bool sizeYPercentEnable = layoutComponentTable->sizeYPercentEnable() != 0;
  766. float sizeXPercent = layoutComponentTable->sizeXPercent();
  767. float sizeYPercent = layoutComponentTable->sizeYPercent();
  768. bool stretchHorizontalEnabled = layoutComponentTable->stretchHorizontalEnabled() != 0;
  769. bool stretchVerticalEnabled = layoutComponentTable->stretchVerticalEnabled() != 0;
  770. std::string horizontalEdge = layoutComponentTable->horizontalEdge()->c_str();
  771. std::string verticalEdge = layoutComponentTable->verticalEdge()->c_str();
  772. float leftMargin = layoutComponentTable->leftMargin();
  773. float rightMargin = layoutComponentTable->rightMargin();
  774. float topMargin = layoutComponentTable->topMargin();
  775. float bottomMargin = layoutComponentTable->bottomMargin();
  776. layoutComponent->setPositionPercentXEnabled(positionXPercentEnabled);
  777. layoutComponent->setPositionPercentYEnabled(positionYPercentEnabled);
  778. layoutComponent->setPositionPercentX(positionXPercent);
  779. layoutComponent->setPositionPercentY(positionYPercent);
  780. layoutComponent->setPercentWidthEnabled(sizeXPercentEnable);
  781. layoutComponent->setPercentHeightEnabled(sizeYPercentEnable);
  782. layoutComponent->setPercentWidth(sizeXPercent);
  783. layoutComponent->setPercentHeight(sizeYPercent);
  784. layoutComponent->setStretchWidthEnabled(stretchHorizontalEnabled);
  785. layoutComponent->setStretchHeightEnabled(stretchVerticalEnabled);
  786. ui::LayoutComponent::HorizontalEdge horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::None;
  787. if (horizontalEdge == P_Layout_LeftEdge)
  788. {
  789. horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Left;
  790. }
  791. else if (horizontalEdge == P_Layout_RightEdge)
  792. {
  793. horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Right;
  794. }
  795. else if (horizontalEdge == P_Layout_BothEdge)
  796. {
  797. horizontalEdgeType = ui::LayoutComponent::HorizontalEdge::Center;
  798. }
  799. layoutComponent->setHorizontalEdge(horizontalEdgeType);
  800. ui::LayoutComponent::VerticalEdge verticalEdgeType = ui::LayoutComponent::VerticalEdge::None;
  801. if (verticalEdge == P_Layout_TopEdge)
  802. {
  803. verticalEdgeType = ui::LayoutComponent::VerticalEdge::Top;
  804. }
  805. else if (verticalEdge == P_Layout_BottomEdge)
  806. {
  807. verticalEdgeType = ui::LayoutComponent::VerticalEdge::Bottom;
  808. }
  809. else if (verticalEdge == P_Layout_BothEdge)
  810. {
  811. verticalEdgeType = ui::LayoutComponent::VerticalEdge::Center;
  812. }
  813. layoutComponent->setVerticalEdge(verticalEdgeType);
  814. layoutComponent->setTopMargin(topMargin);
  815. layoutComponent->setBottomMargin(bottomMargin);
  816. layoutComponent->setLeftMargin(leftMargin);
  817. layoutComponent->setRightMargin(rightMargin);
  818. }
  819. Node* WidgetReader::createNodeWithFlatBuffers(const flatbuffers::Table *widgetOptions)
  820. {
  821. Widget* widget = Widget::create();
  822. setPropsWithFlatBuffers(widget, (Table*)widgetOptions);
  823. return widget;
  824. }
  825. std::string WidgetReader::getResourcePath(const std::string &path, cocos2d::ui::Widget::TextureResType texType)
  826. {
  827. std::string filePath = GUIReader::getInstance()->getFilePath();
  828. const char* imageFileName = path.c_str();
  829. std::string imageFileName_tp;
  830. if (nullptr != imageFileName && 0 != strcmp("", imageFileName))
  831. {
  832. if (texType == ui::Widget::TextureResType::LOCAL) {
  833. imageFileName_tp = filePath + imageFileName;
  834. }
  835. else if(texType == ui::Widget::TextureResType::PLIST){
  836. imageFileName_tp = imageFileName;
  837. }
  838. else{
  839. CCASSERT(0, "invalid TextureResType!!!");
  840. }
  841. }
  842. return imageFileName_tp;
  843. }
  844. }