ButtonReader.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. #include "editor-support/cocostudio/WidgetReader/ButtonReader/ButtonReader.h"
  2. #include "ui/UIButton.h"
  3. #include "2d/CCSpriteFrameCache.h"
  4. #include "2d/CCLabel.h"
  5. #include "platform/CCFileUtils.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 "editor-support/cocostudio/LocalizationManager.h"
  10. #include "tinyxml2.h"
  11. #include "flatbuffers/flatbuffers.h"
  12. USING_NS_CC;
  13. using namespace ui;
  14. using namespace flatbuffers;
  15. namespace cocostudio
  16. {
  17. static const char* P_Scale9Enable = "scale9Enable";
  18. static const char* P_NormalData = "normalData";
  19. static const char* P_PressedData = "pressedData";
  20. static const char* P_DisabledData = "disabledData";
  21. static const char* P_Text = "text";
  22. static const char* P_CapInsetsX = "capInsetsX";
  23. static const char* P_CapInsetsY = "capInsetsY";
  24. static const char* P_CapInsetsWidth = "capInsetsWidth";
  25. static const char* P_CapInsetsHeight = "capInsetsHeight";
  26. static const char* P_Scale9Width = "scale9Width";
  27. static const char* P_Scale9Height = "scale9Height";
  28. static const char* P_TextColorR = "textColorR";
  29. static const char* P_TextColorG = "textColorG";
  30. static const char* P_TextColorB = "textColorB";
  31. static const char* P_FontSize = "fontSize";
  32. static const char* P_FontName = "fontName";
  33. static ButtonReader* instanceButtonReader = nullptr;
  34. IMPLEMENT_CLASS_NODE_READER_INFO(ButtonReader)
  35. ButtonReader::ButtonReader()
  36. {
  37. }
  38. ButtonReader::~ButtonReader()
  39. {
  40. }
  41. ButtonReader* ButtonReader::getInstance()
  42. {
  43. if (!instanceButtonReader)
  44. {
  45. instanceButtonReader = new (std::nothrow) ButtonReader();
  46. }
  47. return instanceButtonReader;
  48. }
  49. void ButtonReader::purge()
  50. {
  51. CC_SAFE_DELETE(instanceButtonReader);
  52. }
  53. void ButtonReader::destroyInstance()
  54. {
  55. CC_SAFE_DELETE(instanceButtonReader);
  56. }
  57. void ButtonReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  58. {
  59. WidgetReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  60. Button *button = static_cast<Button*>(widget);
  61. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  62. this->beginSetBasicProperties(widget);
  63. float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
  64. int cri = 255, cgi = 255, cbi = 255;
  65. float scale9Width = 0.0f, scale9Height = 0.0f;
  66. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  67. std::string key = stChildArray[i].GetName(cocoLoader);
  68. std::string value = stChildArray[i].GetValue(cocoLoader);
  69. //read all basic properties of widget
  70. CC_BASIC_PROPERTY_BINARY_READER
  71. //read all color related properties of widget
  72. CC_COLOR_PROPERTY_BINARY_READER
  73. else if (key == P_Scale9Enable) {
  74. button->setScale9Enabled(valueToBool(value));
  75. }
  76. else if (key == P_NormalData){
  77. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  78. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  79. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  80. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  81. button->loadTextureNormal(backgroundValue, imageFileNameType);
  82. }
  83. else if (key == P_PressedData){
  84. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  85. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  86. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  87. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  88. button->loadTexturePressed(backgroundValue, imageFileNameType);
  89. }
  90. else if (key == P_DisabledData){
  91. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  92. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  93. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  94. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  95. button->loadTextureDisabled(backgroundValue, imageFileNameType);
  96. }else if (key == P_Text){
  97. button->setTitleText(value);
  98. }
  99. else if(key == P_CapInsetsX){
  100. capsx = valueToFloat(value);
  101. }else if(key == P_CapInsetsY){
  102. capsy = valueToFloat(value);
  103. }else if(key == P_CapInsetsWidth){
  104. capsWidth = valueToFloat(value);
  105. }else if(key == P_CapInsetsHeight){
  106. capsHeight = valueToFloat(value);
  107. }else if(key == P_Scale9Width){
  108. scale9Width = valueToFloat(value);
  109. }else if(key == P_Scale9Height){
  110. scale9Height = valueToFloat(value);
  111. }else if(key == P_TextColorR){
  112. cri = valueToInt(value);
  113. }else if(key == P_TextColorG){
  114. cgi = valueToInt(value);
  115. }else if(key == P_TextColorB){
  116. cbi = valueToInt(value);
  117. }else if(key == P_FontSize){
  118. button->setTitleFontSize(valueToFloat(value));
  119. }else if(key == P_FontName){
  120. button->setTitleFontName(value);
  121. }
  122. } //end of for loop
  123. this->endSetBasicProperties(widget);
  124. if (button->isScale9Enabled()) {
  125. button->setCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
  126. button->setContentSize(Size(scale9Width, scale9Height));
  127. }
  128. button->setTitleColor(Color3B(cri, cgi, cbi));
  129. }
  130. void ButtonReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  131. {
  132. WidgetReader::setPropsFromJsonDictionary(widget, options);
  133. Button* button = static_cast<Button*>(widget);
  134. bool scale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
  135. button->setScale9Enabled(scale9Enable);
  136. const rapidjson::Value& normalDic = DICTOOL->getSubDictionary_json(options, P_NormalData);
  137. int normalType = DICTOOL->getIntValue_json(normalDic, P_ResourceType);
  138. std::string normalTexturePath = this->getResourcePath(normalDic, P_Path, (Widget::TextureResType)normalType);
  139. button->loadTextureNormal(normalTexturePath, (Widget::TextureResType)normalType);
  140. const rapidjson::Value& pressedDic = DICTOOL->getSubDictionary_json(options, P_PressedData);
  141. int pressedType = DICTOOL->getIntValue_json(pressedDic, P_ResourceType);
  142. std::string pressedTexturePath = this->getResourcePath(pressedDic, P_Path, (Widget::TextureResType)pressedType);
  143. button->loadTexturePressed(pressedTexturePath, (Widget::TextureResType)pressedType);
  144. const rapidjson::Value& disabledDic = DICTOOL->getSubDictionary_json(options, P_DisabledData);
  145. int disabledType = DICTOOL->getIntValue_json(disabledDic, P_ResourceType);
  146. std::string disabledTexturePath = this->getResourcePath(disabledDic, P_Path, (Widget::TextureResType)disabledType);
  147. button->loadTextureDisabled(disabledTexturePath, (Widget::TextureResType)disabledType);
  148. if (scale9Enable)
  149. {
  150. float cx = DICTOOL->getFloatValue_json(options, P_CapInsetsX);
  151. float cy = DICTOOL->getFloatValue_json(options, P_CapInsetsY);
  152. float cw = DICTOOL->getFloatValue_json(options, P_CapInsetsWidth);
  153. float ch = DICTOOL->getFloatValue_json(options, P_CapInsetsHeight);
  154. button->setCapInsets(Rect(cx, cy, cw, ch));
  155. bool sw = DICTOOL->checkObjectExist_json(options, P_Scale9Width);
  156. bool sh = DICTOOL->checkObjectExist_json(options, P_Scale9Height);
  157. if (sw && sh)
  158. {
  159. float swf = DICTOOL->getFloatValue_json(options, P_Scale9Width);
  160. float shf = DICTOOL->getFloatValue_json(options, P_Scale9Height);
  161. button->setContentSize(Size(swf, shf));
  162. }
  163. }
  164. bool tt = DICTOOL->checkObjectExist_json(options, P_Text);
  165. if (tt)
  166. {
  167. const char* text = DICTOOL->getStringValue_json(options, P_Text);
  168. if (text)
  169. {
  170. button->setTitleText(text);
  171. }
  172. }
  173. int cri = DICTOOL->getIntValue_json(options, P_TextColorR,255);
  174. int cgi = DICTOOL->getIntValue_json(options, P_TextColorG,255);
  175. int cbi = DICTOOL->getIntValue_json(options, P_TextColorB,255);
  176. button->setTitleColor(Color3B(cri,cgi,cbi));
  177. button->setTitleFontSize(DICTOOL->getIntValue_json(options, P_FontSize,14));
  178. button->setTitleFontName(DICTOOL->getStringValue_json(options, P_FontName, ""));
  179. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  180. }
  181. Offset<Table> ButtonReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData, flatbuffers::FlatBufferBuilder *builder)
  182. {
  183. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  184. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  185. bool displaystate = true;
  186. bool scale9Enabled = false;
  187. Rect capInsets;
  188. std::string text = "";
  189. bool isLocalized = false;
  190. int fontSize = 14;
  191. std::string fontName = "";
  192. cocos2d::Size scale9Size;
  193. Color4B textColor(255, 255, 255, 255);
  194. std::string normalPath = "";
  195. std::string normalPlistFile = "";
  196. int normalResourceType = 0;
  197. std::string pressedPath = "";
  198. std::string pressedPlistFile = "";
  199. int pressedResourceType = 0;
  200. std::string disabledPath = "";
  201. std::string disabledPlistFile = "";
  202. int disabledResourceType = 0;
  203. std::string fontResourcePath = "";
  204. std::string fontResourcePlistFile = "";
  205. int fontResourceResourceType = 0;
  206. bool outlineEnabled = false;
  207. Color4B outlineColor = Color4B::BLACK;
  208. int outlineSize = 1;
  209. bool shadowEnabled = false;
  210. Color4B shadowColor = Color4B::BLACK;
  211. Size shadowOffset = Size(2, -2);
  212. int shadowBlurRadius = 0;
  213. // attributes
  214. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  215. while (attribute)
  216. {
  217. std::string name = attribute->Name();
  218. std::string value = attribute->Value();
  219. if (name == "Scale9Enable")
  220. {
  221. if (value == "True")
  222. {
  223. scale9Enabled = true;
  224. }
  225. }
  226. else if (name == "Scale9OriginX")
  227. {
  228. capInsets.origin.x = atof(value.c_str());
  229. }
  230. else if (name == "Scale9OriginY")
  231. {
  232. capInsets.origin.y = atof(value.c_str());
  233. }
  234. else if (name == "Scale9Width")
  235. {
  236. capInsets.size.width = atof(value.c_str());
  237. }
  238. else if (name == "Scale9Height")
  239. {
  240. capInsets.size.height = atof(value.c_str());
  241. }
  242. else if (name == "ButtonText")
  243. {
  244. text = value;
  245. }
  246. else if (name == "IsLocalized")
  247. {
  248. isLocalized = (value == "True") ? true : false;
  249. }
  250. else if (name == "FontSize")
  251. {
  252. fontSize = atoi(value.c_str());
  253. }
  254. else if (name == "FontName")
  255. {
  256. fontName = value;
  257. }
  258. else if (name == "DisplayState")
  259. {
  260. displaystate = (value == "True") ? true : false;
  261. }
  262. else if (name == "OutlineEnabled")
  263. {
  264. outlineEnabled = (value == "True") ? true : false;
  265. }
  266. else if (name == "OutlineSize")
  267. {
  268. outlineSize = atoi(value.c_str());
  269. }
  270. else if (name == "ShadowEnabled")
  271. {
  272. shadowEnabled = (value == "True") ? true : false;
  273. }
  274. else if (name == "ShadowOffsetX")
  275. {
  276. shadowOffset.width = atof(value.c_str());
  277. }
  278. else if (name == "ShadowOffsetY")
  279. {
  280. shadowOffset.height = atof(value.c_str());
  281. }
  282. else if (name == "ShadowBlurRadius")
  283. {
  284. shadowBlurRadius = atoi(value.c_str());
  285. }
  286. attribute = attribute->Next();
  287. }
  288. // child elements
  289. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  290. while (child)
  291. {
  292. std::string name = child->Name();
  293. if (name == "Size" && scale9Enabled)
  294. {
  295. attribute = child->FirstAttribute();
  296. while (attribute)
  297. {
  298. name = attribute->Name();
  299. std::string value = attribute->Value();
  300. if (name == "X")
  301. {
  302. scale9Size.width = atof(value.c_str());
  303. }
  304. else if (name == "Y")
  305. {
  306. scale9Size.height = atof(value.c_str());
  307. }
  308. attribute = attribute->Next();
  309. }
  310. }
  311. else if (name == "TextColor")
  312. {
  313. attribute = child->FirstAttribute();
  314. while (attribute)
  315. {
  316. name = attribute->Name();
  317. std::string value = attribute->Value();
  318. if (name == "R")
  319. {
  320. textColor.r = atoi(value.c_str());
  321. }
  322. else if (name == "G")
  323. {
  324. textColor.g = atoi(value.c_str());
  325. }
  326. else if (name == "B")
  327. {
  328. textColor.b = atoi(value.c_str());
  329. }
  330. attribute = attribute->Next();
  331. }
  332. }
  333. else if (name == "DisabledFileData")
  334. {
  335. std::string texture = "";
  336. std::string texturePng = "";
  337. attribute = child->FirstAttribute();
  338. while (attribute)
  339. {
  340. name = attribute->Name();
  341. std::string value = attribute->Value();
  342. if (name == "Path")
  343. {
  344. disabledPath = value;
  345. }
  346. else if (name == "Type")
  347. {
  348. disabledResourceType = getResourceType(value);
  349. }
  350. else if (name == "Plist")
  351. {
  352. disabledPlistFile = value;
  353. texture = value;
  354. }
  355. attribute = attribute->Next();
  356. }
  357. if (disabledResourceType == 1)
  358. {
  359. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  360. fbs->_textures.push_back(builder->CreateString(texture));
  361. }
  362. }
  363. else if (name == "PressedFileData")
  364. {
  365. std::string texture = "";
  366. std::string texturePng = "";
  367. attribute = child->FirstAttribute();
  368. while (attribute)
  369. {
  370. name = attribute->Name();
  371. std::string value = attribute->Value();
  372. if (name == "Path")
  373. {
  374. pressedPath = value;
  375. }
  376. else if (name == "Type")
  377. {
  378. pressedResourceType = getResourceType(value);
  379. }
  380. else if (name == "Plist")
  381. {
  382. pressedPlistFile = value;
  383. texture = value;
  384. }
  385. attribute = attribute->Next();
  386. }
  387. if (pressedResourceType == 1)
  388. {
  389. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  390. fbs->_textures.push_back(builder->CreateString(texture));
  391. }
  392. }
  393. else if (name == "NormalFileData")
  394. {
  395. std::string texture = "";
  396. std::string texturePng = "";
  397. attribute = child->FirstAttribute();
  398. while (attribute)
  399. {
  400. name = attribute->Name();
  401. std::string value = attribute->Value();
  402. if (name == "Path")
  403. {
  404. normalPath = value;
  405. }
  406. else if (name == "Type")
  407. {
  408. normalResourceType = getResourceType(value);
  409. }
  410. else if (name == "Plist")
  411. {
  412. normalPlistFile = value;
  413. texture = value;
  414. }
  415. attribute = attribute->Next();
  416. }
  417. if (normalResourceType == 1)
  418. {
  419. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  420. fbs->_textures.push_back(builder->CreateString(texture));
  421. }
  422. }
  423. else if (name == "FontResource")
  424. {
  425. attribute = child->FirstAttribute();
  426. while (attribute)
  427. {
  428. name = attribute->Name();
  429. std::string value = attribute->Value();
  430. if (name == "Path")
  431. {
  432. fontResourcePath = value;
  433. }
  434. else if (name == "Type")
  435. {
  436. fontResourceResourceType = getResourceType(value);
  437. }
  438. else if (name == "Plist")
  439. {
  440. fontResourcePlistFile = value;
  441. }
  442. attribute = attribute->Next();
  443. }
  444. }
  445. else if (name == "OutlineColor")
  446. {
  447. attribute = child->FirstAttribute();
  448. while (attribute)
  449. {
  450. name = attribute->Name();
  451. std::string value = attribute->Value();
  452. if (name == "A")
  453. {
  454. outlineColor.a = atoi(value.c_str());
  455. }
  456. else if (name == "R")
  457. {
  458. outlineColor.r = atoi(value.c_str());
  459. }
  460. else if (name == "G")
  461. {
  462. outlineColor.g = atoi(value.c_str());
  463. }
  464. else if (name == "B")
  465. {
  466. outlineColor.b = atoi(value.c_str());
  467. }
  468. attribute = attribute->Next();
  469. }
  470. }
  471. else if (name == "ShadowColor")
  472. {
  473. attribute = child->FirstAttribute();
  474. while (attribute)
  475. {
  476. name = attribute->Name();
  477. std::string value = attribute->Value();
  478. if (name == "A")
  479. {
  480. shadowColor.a = atoi(value.c_str());
  481. }
  482. else if (name == "R")
  483. {
  484. shadowColor.r = atoi(value.c_str());
  485. }
  486. else if (name == "G")
  487. {
  488. shadowColor.g = atoi(value.c_str());
  489. }
  490. else if (name == "B")
  491. {
  492. shadowColor.b = atoi(value.c_str());
  493. }
  494. attribute = attribute->Next();
  495. }
  496. }
  497. child = child->NextSiblingElement();
  498. }
  499. Color f_textColor(255, textColor.r, textColor.g, textColor.b);
  500. CapInsets f_capInsets(capInsets.origin.x, capInsets.origin.y, capInsets.size.width, capInsets.size.height);
  501. FlatSize f_scale9Size(scale9Size.width, scale9Size.height);
  502. flatbuffers::Color f_outlineColor(outlineColor.a, outlineColor.r, outlineColor.g, outlineColor.b);
  503. flatbuffers::Color f_shadowColor(shadowColor.a, shadowColor.r, shadowColor.g, shadowColor.b);
  504. auto options = CreateButtonOptions(*builder,
  505. widgetOptions,
  506. CreateResourceData(*builder,
  507. builder->CreateString(normalPath),
  508. builder->CreateString(normalPlistFile),
  509. normalResourceType),
  510. CreateResourceData(*builder,
  511. builder->CreateString(pressedPath),
  512. builder->CreateString(pressedPlistFile),
  513. pressedResourceType),
  514. CreateResourceData(*builder,
  515. builder->CreateString(disabledPath),
  516. builder->CreateString(disabledPlistFile),
  517. disabledResourceType),
  518. CreateResourceData(*builder,
  519. builder->CreateString(fontResourcePath),
  520. builder->CreateString(fontResourcePlistFile),
  521. fontResourceResourceType),
  522. builder->CreateString(text),
  523. builder->CreateString(fontName),
  524. fontSize,
  525. &f_textColor,
  526. &f_capInsets,
  527. &f_scale9Size,
  528. scale9Enabled,
  529. displaystate,
  530. outlineEnabled,
  531. &f_outlineColor,
  532. outlineSize,
  533. shadowEnabled,
  534. &f_shadowColor,
  535. shadowOffset.width,
  536. shadowOffset.height,
  537. shadowBlurRadius,
  538. isLocalized);
  539. return *(Offset<Table>*)(&options);
  540. }
  541. void ButtonReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *buttonOptions)
  542. {
  543. Button* button = static_cast<Button*>(node);
  544. auto options = (ButtonOptions*)buttonOptions;
  545. bool scale9Enabled = options->scale9Enabled() != 0;
  546. button->setScale9Enabled(scale9Enabled);
  547. bool normalFileExist = false;
  548. std::string normalErrorFilePath = "";
  549. auto normalDic = options->normalData();
  550. int normalType = normalDic->resourceType();
  551. std::string normalTexturePath = normalDic->path()->c_str();
  552. switch (normalType)
  553. {
  554. case 0:
  555. if (FileUtils::getInstance()->isFileExist(normalTexturePath))
  556. {
  557. normalFileExist = true;
  558. }
  559. else if (SpriteFrameCache::getInstance()->getSpriteFrameByName(normalTexturePath))
  560. {
  561. normalFileExist = true;
  562. normalType = 1;
  563. }
  564. else
  565. {
  566. normalErrorFilePath = normalTexturePath;
  567. normalFileExist = false;
  568. }
  569. break;
  570. case 1:
  571. {
  572. std::string plist = normalDic->plistFile()->c_str();
  573. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(normalTexturePath);
  574. if (spriteFrame)
  575. {
  576. normalFileExist = true;
  577. }
  578. else
  579. {
  580. if (FileUtils::getInstance()->isFileExist(plist))
  581. {
  582. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  583. ValueMap metadata = value["metadata"].asValueMap();
  584. std::string textureFileName = metadata["textureFileName"].asString();
  585. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  586. {
  587. normalErrorFilePath = textureFileName;
  588. }
  589. }
  590. else
  591. {
  592. normalErrorFilePath = plist;
  593. }
  594. normalFileExist = false;
  595. }
  596. break;
  597. }
  598. default:
  599. break;
  600. }
  601. if (normalFileExist)
  602. {
  603. button->loadTextureNormal(normalTexturePath, (Widget::TextureResType)normalType);
  604. }
  605. bool pressedFileExist = false;
  606. std::string pressedErrorFilePath = "";
  607. auto pressedDic = options->pressedData();
  608. int pressedType = pressedDic->resourceType();
  609. std::string pressedTexturePath = pressedDic->path()->c_str();
  610. switch (pressedType)
  611. {
  612. case 0:
  613. {
  614. if (FileUtils::getInstance()->isFileExist(pressedTexturePath))
  615. {
  616. pressedFileExist = true;
  617. }
  618. else
  619. {
  620. pressedErrorFilePath = pressedTexturePath;
  621. pressedFileExist = false;
  622. }
  623. break;
  624. }
  625. case 1:
  626. {
  627. std::string plist = pressedDic->plistFile()->c_str();
  628. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(pressedTexturePath);
  629. if (spriteFrame)
  630. {
  631. pressedFileExist = true;
  632. }
  633. else
  634. {
  635. if (FileUtils::getInstance()->isFileExist(plist))
  636. {
  637. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  638. ValueMap metadata = value["metadata"].asValueMap();
  639. std::string textureFileName = metadata["textureFileName"].asString();
  640. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  641. {
  642. pressedErrorFilePath = textureFileName;
  643. }
  644. }
  645. else
  646. {
  647. pressedErrorFilePath = plist;
  648. }
  649. pressedFileExist = false;
  650. }
  651. break;
  652. }
  653. default:
  654. break;
  655. }
  656. if (pressedFileExist)
  657. {
  658. button->loadTexturePressed(pressedTexturePath, (Widget::TextureResType)pressedType);
  659. }
  660. bool disabledFileExist = false;
  661. std::string disabledErrorFilePath = "";
  662. auto disabledDic = options->disabledData();
  663. int disabledType = disabledDic->resourceType();
  664. std::string disabledTexturePath = disabledDic->path()->c_str();
  665. switch (disabledType)
  666. {
  667. case 0:
  668. {
  669. if (FileUtils::getInstance()->isFileExist(disabledTexturePath))
  670. {
  671. disabledFileExist = true;
  672. }
  673. else
  674. {
  675. disabledErrorFilePath = disabledTexturePath;
  676. disabledFileExist = false;
  677. }
  678. break;
  679. }
  680. case 1:
  681. {
  682. std::string plist = disabledDic->plistFile()->c_str();
  683. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(disabledTexturePath);
  684. if (spriteFrame)
  685. {
  686. disabledFileExist = true;
  687. }
  688. else
  689. {
  690. if (FileUtils::getInstance()->isFileExist(plist))
  691. {
  692. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  693. ValueMap metadata = value["metadata"].asValueMap();
  694. std::string textureFileName = metadata["textureFileName"].asString();
  695. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  696. {
  697. disabledErrorFilePath = textureFileName;
  698. }
  699. }
  700. else
  701. {
  702. disabledErrorFilePath = plist;
  703. }
  704. disabledFileExist = false;
  705. }
  706. break;
  707. }
  708. default:
  709. break;
  710. }
  711. if (disabledFileExist)
  712. {
  713. button->loadTextureDisabled(disabledTexturePath, (Widget::TextureResType)disabledType);
  714. }
  715. std::string titleText = options->text()->c_str();
  716. bool isLocalized = options->isLocalized() != 0;
  717. if (isLocalized)
  718. {
  719. ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
  720. button->setTitleText(lm->getLocalizationString(titleText));
  721. }
  722. else
  723. {
  724. button->setTitleText(titleText);
  725. }
  726. auto textColor = options->textColor();
  727. Color3B titleColor(textColor->r(), textColor->g(), textColor->b());
  728. button->setTitleColor(titleColor);
  729. int titleFontSize = options->fontSize();
  730. button->setTitleFontSize(titleFontSize);
  731. std::string titleFontName = options->fontName()->c_str();
  732. button->setTitleFontName(titleFontName);
  733. auto resourceData = options->fontResource();
  734. bool fileExist = false;
  735. std::string errorFilePath = "";
  736. std::string path = resourceData->path()->c_str();
  737. if (path != "")
  738. {
  739. if (FileUtils::getInstance()->isFileExist(path))
  740. {
  741. fileExist = true;
  742. }
  743. else
  744. {
  745. errorFilePath = path;
  746. fileExist = false;
  747. }
  748. if (fileExist)
  749. {
  750. button->setTitleFontName(path);
  751. }
  752. }
  753. bool displaystate = options->displaystate() != 0;
  754. button->setBright(displaystate);
  755. button->setEnabled(displaystate);
  756. bool outlineEnabled = options->outlineEnabled() != 0;
  757. if (outlineEnabled)
  758. {
  759. auto f_outlineColor = options->outlineColor();
  760. if (f_outlineColor)
  761. {
  762. Color4B outlineColor(f_outlineColor->r(), f_outlineColor->g(), f_outlineColor->b(), f_outlineColor->a());
  763. auto label = button->getTitleRenderer();
  764. label->enableOutline(outlineColor, options->outlineSize());
  765. }
  766. }
  767. bool shadowEnabled = options->shadowEnabled() != 0;
  768. if (shadowEnabled)
  769. {
  770. auto f_shadowColor = options->shadowColor();
  771. if (f_shadowColor)
  772. {
  773. Color4B shadowColor(f_shadowColor->r(), f_shadowColor->g(), f_shadowColor->b(), f_shadowColor->a());
  774. auto label = button->getTitleRenderer();
  775. label->enableShadow(shadowColor, Size(options->shadowOffsetX(), options->shadowOffsetY()), options->shadowBlurRadius());
  776. }
  777. }
  778. auto widgetReader = WidgetReader::getInstance();
  779. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  780. if (scale9Enabled)
  781. {
  782. button->setUnifySizeEnabled(false);
  783. button->ignoreContentAdaptWithSize(false);
  784. auto f_capInsets = options->capInsets();
  785. Rect capInsets(f_capInsets->x(), f_capInsets->y(), f_capInsets->width(), f_capInsets->height());
  786. button->setCapInsets(capInsets);
  787. Size scale9Size(options->scale9Size()->width(), options->scale9Size()->height());
  788. button->setContentSize(scale9Size);
  789. }
  790. else
  791. {
  792. Size contentSize(options->widgetOptions()->size()->width(), options->widgetOptions()->size()->height());
  793. button->setContentSize(contentSize);
  794. }
  795. button->setBright(displaystate);
  796. }
  797. Node* ButtonReader::createNodeWithFlatBuffers(const flatbuffers::Table *buttonOptions)
  798. {
  799. Button* button = Button::create();
  800. setPropsWithFlatBuffers(button, (Table*)buttonOptions);
  801. return button;
  802. }
  803. int ButtonReader::getResourceType(std::string key)
  804. {
  805. if(key == "Normal" || key == "Default")
  806. {
  807. return 0;
  808. }
  809. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  810. if(fbs->_isSimulator)
  811. {
  812. if(key == "MarkedSubImage")
  813. {
  814. return 0;
  815. }
  816. }
  817. return 1;
  818. }
  819. }