SliderReader.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. #include "editor-support/cocostudio/WidgetReader/SliderReader/SliderReader.h"
  2. #include "ui/UISlider.h"
  3. #include "2d/CCSpriteFrameCache.h"
  4. #include "platform/CCFileUtils.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_Scale9Enable = "scale9Enable";
  16. static const char* P_Percent = "percent";
  17. static const char* P_BarFileNameData = "barFileNameData";
  18. static const char* P_Length = "length";
  19. static const char* P_BallNormalData = "ballNormalData";
  20. static const char* P_BallPressedData = "ballPressedData";
  21. static const char* P_BallDisabledData = "ballDisabledData";
  22. static const char* P_ProgressBarData = "progressBarData";
  23. static SliderReader* instanceSliderReader = nullptr;
  24. IMPLEMENT_CLASS_NODE_READER_INFO(SliderReader)
  25. SliderReader::SliderReader()
  26. {
  27. }
  28. SliderReader::~SliderReader()
  29. {
  30. }
  31. SliderReader* SliderReader::getInstance()
  32. {
  33. if (!instanceSliderReader)
  34. {
  35. instanceSliderReader = new (std::nothrow) SliderReader();
  36. }
  37. return instanceSliderReader;
  38. }
  39. void SliderReader::destroyInstance()
  40. {
  41. CC_SAFE_DELETE(instanceSliderReader);
  42. }
  43. void SliderReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode* cocoNode)
  44. {
  45. this->beginSetBasicProperties(widget);
  46. Slider* slider = static_cast<Slider*>(widget);
  47. float barLength = 0.0f;
  48. int percent = slider->getPercent();
  49. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  50. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  51. std::string key = stChildArray[i].GetName(cocoLoader);
  52. std::string value = stChildArray[i].GetValue(cocoLoader);
  53. //read all basic properties of widget
  54. CC_BASIC_PROPERTY_BINARY_READER
  55. //read all color related properties of widget
  56. CC_COLOR_PROPERTY_BINARY_READER
  57. //control custom properties
  58. else if (key == P_Scale9Enable) {
  59. slider->setScale9Enabled(valueToBool(value));
  60. }
  61. else if(key == P_Percent){
  62. percent = valueToInt(value);
  63. }else if(key == P_BarFileNameData){
  64. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  65. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  66. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  67. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  68. slider->loadBarTexture(backgroundValue, imageFileNameType);
  69. }else if(key == P_Length){
  70. barLength = valueToFloat(value);
  71. }else if(key == P_BallNormalData){
  72. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  73. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  74. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  75. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  76. slider->loadSlidBallTextureNormal(backgroundValue, imageFileNameType);
  77. }else if(key == P_BallPressedData){
  78. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  79. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  80. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  81. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  82. slider->loadSlidBallTexturePressed(backgroundValue, imageFileNameType);
  83. }else if(key == P_BallDisabledData){
  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. slider->loadSlidBallTextureDisabled(backgroundValue, imageFileNameType);
  89. }else if(key == P_ProgressBarData){
  90. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  91. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  92. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  93. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  94. slider->loadProgressBarTexture(backgroundValue, imageFileNameType);
  95. }
  96. } //end of for loop
  97. if (slider->isScale9Enabled()) {
  98. slider->setContentSize(Size(barLength, slider->getContentSize().height));
  99. }
  100. slider->setPercent(percent);
  101. this->endSetBasicProperties(widget);
  102. }
  103. void SliderReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  104. {
  105. WidgetReader::setPropsFromJsonDictionary(widget, options);
  106. Slider* slider = static_cast<Slider*>(widget);
  107. bool barTextureScale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
  108. slider->setScale9Enabled(barTextureScale9Enable);
  109. slider->setPercent(DICTOOL->getIntValue_json(options, P_Percent));
  110. // bool bt = DICTOOL->checkObjectExist_json(options, P_BarFileName);
  111. float barLength = DICTOOL->getFloatValue_json(options, P_Length,290);
  112. const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_BarFileNameData);
  113. int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
  114. std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
  115. slider->loadBarTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  116. if (barTextureScale9Enable)
  117. {
  118. slider->setContentSize(Size(barLength, slider->getContentSize().height));
  119. }
  120. //loading normal slider ball texture
  121. const rapidjson::Value& normalDic = DICTOOL->getSubDictionary_json(options, P_BallNormalData);
  122. int normalType = DICTOOL->getIntValue_json(normalDic, P_ResourceType);
  123. imageFileName = this->getResourcePath(normalDic, P_Path, (Widget::TextureResType)normalType);
  124. slider->loadSlidBallTextureNormal(imageFileName, (Widget::TextureResType)normalType);
  125. //loading slider ball press texture
  126. const rapidjson::Value& pressedDic = DICTOOL->getSubDictionary_json(options, P_BallPressedData);
  127. int pressedType = DICTOOL->getIntValue_json(pressedDic, P_ResourceType);
  128. std::string pressedFileName = this->getResourcePath(pressedDic, P_Path, (Widget::TextureResType)pressedType);
  129. slider->loadSlidBallTexturePressed(pressedFileName, (Widget::TextureResType)pressedType);
  130. //loading slider ball disable texture
  131. const rapidjson::Value& disabledDic = DICTOOL->getSubDictionary_json(options, P_BallDisabledData);
  132. int disabledType = DICTOOL->getIntValue_json(disabledDic, P_ResourceType);
  133. std::string disabledFileName = this->getResourcePath(disabledDic, P_Path, (Widget::TextureResType)disabledType);
  134. slider->loadSlidBallTextureDisabled(disabledFileName, (Widget::TextureResType)disabledType);
  135. //load slider progress texture
  136. const rapidjson::Value& progressBarDic = DICTOOL->getSubDictionary_json(options, P_ProgressBarData);
  137. int progressBarType = DICTOOL->getIntValue_json(progressBarDic, P_ResourceType);
  138. std::string progressBarFileName = this->getResourcePath(progressBarDic, P_Path, (Widget::TextureResType)progressBarType);
  139. slider->loadProgressBarTexture(progressBarFileName, (Widget::TextureResType)progressBarType);
  140. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  141. }
  142. Offset<Table> SliderReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  143. flatbuffers::FlatBufferBuilder *builder)
  144. {
  145. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  146. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  147. std::string barFileNamePath = "";
  148. std::string barFileNamePlistFile = "";
  149. int barFileNameResourceType = 0;
  150. std::string ballNormalPath = "";
  151. std::string ballNormalPlistFile = "";
  152. int ballNormalResourceType = 0;
  153. std::string ballPressedPath = "";
  154. std::string ballPressedPlistFile = "";
  155. int ballPressedResourceType = 0;
  156. std::string ballDisabledPath = "";
  157. std::string ballDisabledPlistFile = "";
  158. int ballDisabledResourceType = 0;
  159. std::string progressBarPath = "";
  160. std::string progressBarPlistFile = "";
  161. int progressBarResourceType = 0;
  162. int percent = 0;
  163. bool displaystate = true;
  164. // attributes
  165. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  166. while (attribute)
  167. {
  168. std::string name = attribute->Name();
  169. std::string value = attribute->Value();
  170. if (name == "PercentInfo")
  171. {
  172. percent = atoi(value.c_str());
  173. }
  174. else if (name == "DisplayState")
  175. {
  176. displaystate = (value == "True") ? true : false;
  177. }
  178. attribute = attribute->Next();
  179. }
  180. // child elements
  181. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  182. while (child)
  183. {
  184. std::string name = child->Name();
  185. if (name == "BackGroundData")
  186. {
  187. std::string texture = "";
  188. std::string texturePng = "";
  189. attribute = child->FirstAttribute();
  190. while (attribute)
  191. {
  192. name = attribute->Name();
  193. std::string value = attribute->Value();
  194. if (name == "Path")
  195. {
  196. barFileNamePath = value;
  197. }
  198. else if (name == "Type")
  199. {
  200. barFileNameResourceType = getResourceType(value);
  201. }
  202. else if (name == "Plist")
  203. {
  204. barFileNamePlistFile = value;
  205. texture = value;
  206. }
  207. attribute = attribute->Next();
  208. }
  209. if (barFileNameResourceType == 1)
  210. {
  211. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  212. fbs->_textures.push_back(builder->CreateString(texture));
  213. }
  214. }
  215. else if (name == "BallNormalData")
  216. {
  217. std::string texture = "";
  218. std::string texturePng = "";
  219. attribute = child->FirstAttribute();
  220. while (attribute)
  221. {
  222. name = attribute->Name();
  223. std::string value = attribute->Value();
  224. if (name == "Path")
  225. {
  226. ballNormalPath = value;
  227. }
  228. else if (name == "Type")
  229. {
  230. ballNormalResourceType = getResourceType(value);
  231. }
  232. else if (name == "Plist")
  233. {
  234. ballNormalPlistFile = value;
  235. texture = value;
  236. }
  237. attribute = attribute->Next();
  238. }
  239. if (ballNormalResourceType == 1)
  240. {
  241. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  242. fbs->_textures.push_back(builder->CreateString(texture));
  243. }
  244. }
  245. else if (name == "BallPressedData")
  246. {
  247. std::string texture = "";
  248. std::string texturePng = "";
  249. attribute = child->FirstAttribute();
  250. while (attribute)
  251. {
  252. name = attribute->Name();
  253. std::string value = attribute->Value();
  254. if (name == "Path")
  255. {
  256. ballPressedPath = value;
  257. }
  258. else if (name == "Type")
  259. {
  260. ballPressedResourceType = getResourceType(value);
  261. }
  262. else if (name == "Plist")
  263. {
  264. ballPressedPlistFile = value;
  265. texture = value;
  266. }
  267. attribute = attribute->Next();
  268. }
  269. if (ballPressedResourceType == 1)
  270. {
  271. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  272. fbs->_textures.push_back(builder->CreateString(texture));
  273. }
  274. }
  275. else if (name == "BallDisabledData")
  276. {
  277. std::string texture = "";
  278. std::string texturePng = "";
  279. attribute = child->FirstAttribute();
  280. while (attribute)
  281. {
  282. name = attribute->Name();
  283. std::string value = attribute->Value();
  284. if (name == "Path")
  285. {
  286. ballDisabledPath = value;
  287. }
  288. else if (name == "Type")
  289. {
  290. ballDisabledResourceType = getResourceType(value);
  291. }
  292. else if (name == "Plist")
  293. {
  294. ballDisabledPlistFile = value;
  295. texture = value;
  296. }
  297. attribute = attribute->Next();
  298. }
  299. if (ballDisabledResourceType == 1)
  300. {
  301. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  302. fbs->_textures.push_back(builder->CreateString(texture));
  303. }
  304. }
  305. else if (name == "ProgressBarData")
  306. {
  307. std::string texture = "";
  308. std::string texturePng = "";
  309. attribute = child->FirstAttribute();
  310. while (attribute)
  311. {
  312. name = attribute->Name();
  313. std::string value = attribute->Value();
  314. if (name == "Path")
  315. {
  316. progressBarPath = value;
  317. }
  318. else if (name == "Type")
  319. {
  320. progressBarResourceType = getResourceType(value);
  321. }
  322. else if (name == "Plist")
  323. {
  324. progressBarPlistFile = value;
  325. texture = value;
  326. }
  327. attribute = attribute->Next();
  328. }
  329. if (progressBarResourceType == 1)
  330. {
  331. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  332. fbs->_textures.push_back(builder->CreateString(texture));
  333. }
  334. }
  335. child = child->NextSiblingElement();
  336. }
  337. auto options = CreateSliderOptions(*builder,
  338. widgetOptions,
  339. CreateResourceData(*builder,
  340. builder->CreateString(barFileNamePath),
  341. builder->CreateString(barFileNamePlistFile),
  342. barFileNameResourceType),
  343. CreateResourceData(*builder,
  344. builder->CreateString(ballNormalPath),
  345. builder->CreateString(ballNormalPlistFile),
  346. ballNormalResourceType),
  347. CreateResourceData(*builder,
  348. builder->CreateString(ballPressedPath),
  349. builder->CreateString(ballPressedPlistFile),
  350. ballPressedResourceType),
  351. CreateResourceData(*builder,
  352. builder->CreateString(ballDisabledPath),
  353. builder->CreateString(ballDisabledPlistFile),
  354. ballDisabledResourceType),
  355. CreateResourceData(*builder,
  356. builder->CreateString(progressBarPath),
  357. builder->CreateString(progressBarPlistFile),
  358. progressBarResourceType),
  359. percent,
  360. displaystate);
  361. return *(Offset<Table>*)(&options);
  362. }
  363. void SliderReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *sliderOptions)
  364. {
  365. Slider* slider = static_cast<Slider*>(node);
  366. auto options = (SliderOptions*)sliderOptions;
  367. int percent = options->percent();
  368. //slider->setPercent(percent);
  369. bool imageFileExist = false;
  370. std::string imageErrorFilePath = "";
  371. auto imageFileNameDic = options->barFileNameData();
  372. int imageFileNameType = imageFileNameDic->resourceType();
  373. std::string imageFileName = imageFileNameDic->path()->c_str();
  374. switch (imageFileNameType)
  375. {
  376. case 0:
  377. {
  378. if (FileUtils::getInstance()->isFileExist(imageFileName))
  379. {
  380. imageFileExist = true;
  381. }
  382. else if(SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName))
  383. {
  384. imageFileExist = true;
  385. imageFileNameType = 1;
  386. }
  387. else
  388. {
  389. imageErrorFilePath = imageFileName;
  390. imageFileExist = false;
  391. }
  392. break;
  393. }
  394. case 1:
  395. {
  396. std::string plist = imageFileNameDic->plistFile()->c_str();
  397. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  398. if (spriteFrame)
  399. {
  400. imageFileExist = true;
  401. }
  402. else
  403. {
  404. if (FileUtils::getInstance()->isFileExist(plist))
  405. {
  406. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  407. ValueMap metadata = value["metadata"].asValueMap();
  408. std::string textureFileName = metadata["textureFileName"].asString();
  409. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  410. {
  411. imageErrorFilePath = textureFileName;
  412. }
  413. }
  414. else
  415. {
  416. imageErrorFilePath = plist;
  417. }
  418. imageFileExist = false;
  419. }
  420. break;
  421. }
  422. default:
  423. break;
  424. }
  425. if (imageFileExist)
  426. {
  427. slider->loadBarTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  428. }
  429. //else
  430. //{
  431. // auto label = Label::create();
  432. // label->setString(__String::createWithFormat("%s missed", imageErrorFilePath.c_str())->getCString());
  433. // slider->addChild(label);
  434. //}
  435. //loading normal slider ball texture
  436. bool normalFileExist = false;
  437. std::string normalErrorFilePath = "";
  438. auto normalDic = options->ballNormalData();
  439. int normalType = normalDic->resourceType();
  440. std::string normalFileName = normalDic->path()->c_str();
  441. switch (normalType)
  442. {
  443. case 0:
  444. {
  445. if (FileUtils::getInstance()->isFileExist(normalFileName))
  446. {
  447. normalFileExist = true;
  448. }
  449. else if(SpriteFrameCache::getInstance()->getSpriteFrameByName(normalFileName))
  450. {
  451. normalFileExist = true;
  452. normalType = 1;
  453. }
  454. else
  455. {
  456. normalErrorFilePath = normalFileName;
  457. normalFileExist = false;
  458. }
  459. break;
  460. }
  461. case 1:
  462. {
  463. std::string plist = normalDic->plistFile()->c_str();
  464. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(normalFileName);
  465. if (spriteFrame)
  466. {
  467. normalFileExist = true;
  468. }
  469. else
  470. {
  471. if (FileUtils::getInstance()->isFileExist(plist))
  472. {
  473. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  474. ValueMap metadata = value["metadata"].asValueMap();
  475. std::string textureFileName = metadata["textureFileName"].asString();
  476. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  477. {
  478. normalErrorFilePath = textureFileName;
  479. }
  480. }
  481. else
  482. {
  483. normalErrorFilePath = plist;
  484. }
  485. normalFileExist = false;
  486. }
  487. break;
  488. }
  489. default:
  490. break;
  491. }
  492. if (normalFileExist)
  493. {
  494. slider->loadSlidBallTextureNormal(normalFileName, (Widget::TextureResType)normalType);
  495. }
  496. //else
  497. //{
  498. // auto label = Label::create();
  499. // label->setString(__String::createWithFormat("%s missed", normalErrorFilePath.c_str())->getCString());
  500. // slider->addChild(label);
  501. //}
  502. //loading slider ball press texture
  503. bool pressedFileExist = false;
  504. std::string pressedErrorFilePath = "";
  505. auto pressedDic = options->ballPressedData();
  506. int pressedType = pressedDic->resourceType();
  507. std::string pressedFileName = pressedDic->path()->c_str();
  508. switch (pressedType)
  509. {
  510. case 0:
  511. {
  512. if (FileUtils::getInstance()->isFileExist(pressedFileName))
  513. {
  514. pressedFileExist = true;
  515. }
  516. else if(SpriteFrameCache::getInstance()->getSpriteFrameByName(pressedFileName))
  517. {
  518. pressedFileExist = true;
  519. pressedType = 1;
  520. }
  521. else
  522. {
  523. pressedErrorFilePath = pressedFileName;
  524. pressedFileExist = false;
  525. }
  526. break;
  527. }
  528. case 1:
  529. {
  530. std::string plist = pressedDic->plistFile()->c_str();
  531. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(pressedFileName);
  532. if (spriteFrame)
  533. {
  534. pressedFileExist = true;
  535. }
  536. else
  537. {
  538. if (FileUtils::getInstance()->isFileExist(plist))
  539. {
  540. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  541. ValueMap metadata = value["metadata"].asValueMap();
  542. std::string textureFileName = metadata["textureFileName"].asString();
  543. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  544. {
  545. pressedErrorFilePath = textureFileName;
  546. }
  547. }
  548. else
  549. {
  550. pressedErrorFilePath = plist;
  551. }
  552. pressedFileExist = false;
  553. }
  554. break;
  555. }
  556. default:
  557. break;
  558. }
  559. if (pressedFileExist)
  560. {
  561. slider->loadSlidBallTexturePressed(pressedFileName, (Widget::TextureResType)pressedType);
  562. }
  563. //else
  564. //{
  565. // auto label = Label::create();
  566. // label->setString(__String::createWithFormat("%s missed", pressedErrorFilePath.c_str())->getCString());
  567. // slider->addChild(label);
  568. //}
  569. //loading slider ball disable texture
  570. bool disabledFileExist = false;
  571. std::string disabledErrorFilePath = "";
  572. auto disabledDic = options->ballDisabledData();
  573. int disabledType = disabledDic->resourceType();
  574. std::string disabledFileName = disabledDic->path()->c_str();
  575. switch (disabledType)
  576. {
  577. case 0:
  578. {
  579. if (FileUtils::getInstance()->isFileExist(disabledFileName))
  580. {
  581. disabledFileExist = true;
  582. }
  583. else if(SpriteFrameCache::getInstance()->getSpriteFrameByName(disabledFileName))
  584. {
  585. disabledFileExist = true;
  586. disabledType = 1;
  587. }
  588. else
  589. {
  590. disabledErrorFilePath = disabledFileName;
  591. disabledFileExist = false;
  592. }
  593. break;
  594. }
  595. case 1:
  596. {
  597. std::string plist = disabledDic->plistFile()->c_str();
  598. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(disabledFileName);
  599. if (spriteFrame)
  600. {
  601. disabledFileExist = true;
  602. }
  603. else
  604. {
  605. if (FileUtils::getInstance()->isFileExist(plist))
  606. {
  607. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  608. ValueMap metadata = value["metadata"].asValueMap();
  609. std::string textureFileName = metadata["textureFileName"].asString();
  610. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  611. {
  612. disabledErrorFilePath = textureFileName;
  613. }
  614. }
  615. else
  616. {
  617. disabledErrorFilePath = plist;
  618. }
  619. disabledFileExist = false;
  620. }
  621. break;
  622. }
  623. default:
  624. break;
  625. }
  626. if (disabledFileExist)
  627. {
  628. slider->loadSlidBallTextureDisabled(disabledFileName, (Widget::TextureResType)disabledType);
  629. }
  630. //else
  631. //{
  632. // auto label = Label::create();
  633. // label->setString(__String::createWithFormat("%s missed", disabledErrorFilePath.c_str())->getCString());
  634. // slider->addChild(label);
  635. //}
  636. //load slider progress texture
  637. bool progressFileExist = false;
  638. std::string progressErrorFilePath = "";
  639. auto progressBarDic = options->progressBarData();
  640. int progressBarType = progressBarDic->resourceType();
  641. std::string progressBarFileName = progressBarDic->path()->c_str();
  642. switch (progressBarType)
  643. {
  644. case 0:
  645. {
  646. if (FileUtils::getInstance()->isFileExist(progressBarFileName))
  647. {
  648. progressFileExist = true;
  649. }
  650. else if(SpriteFrameCache::getInstance()->getSpriteFrameByName(progressBarFileName))
  651. {
  652. progressFileExist = true;
  653. progressBarType = 1;
  654. }
  655. else
  656. {
  657. progressErrorFilePath = progressBarFileName;
  658. progressFileExist = false;
  659. }
  660. break;
  661. }
  662. case 1:
  663. {
  664. std::string plist = progressBarDic->plistFile()->c_str();
  665. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(progressBarFileName);
  666. if (spriteFrame)
  667. {
  668. progressFileExist = true;
  669. }
  670. else
  671. {
  672. if (FileUtils::getInstance()->isFileExist(plist))
  673. {
  674. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  675. ValueMap metadata = value["metadata"].asValueMap();
  676. std::string textureFileName = metadata["textureFileName"].asString();
  677. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  678. {
  679. progressErrorFilePath = textureFileName;
  680. }
  681. }
  682. else
  683. {
  684. progressErrorFilePath = plist;
  685. }
  686. progressFileExist = false;
  687. }
  688. break;
  689. }
  690. default:
  691. break;
  692. }
  693. if (progressFileExist)
  694. {
  695. slider->loadProgressBarTexture(progressBarFileName, (Widget::TextureResType)progressBarType);
  696. }
  697. //else
  698. //{
  699. // auto label = Label::create();
  700. // label->setString(__String::createWithFormat("%s missed", progressErrorFilePath.c_str())->getCString());
  701. // slider->addChild(label);
  702. //}
  703. bool displaystate = options->displaystate() != 0;
  704. slider->setBright(displaystate);
  705. slider->setEnabled(displaystate);
  706. auto widgetReader = WidgetReader::getInstance();
  707. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  708. slider->setPercent(percent);
  709. }
  710. Node* SliderReader::createNodeWithFlatBuffers(const flatbuffers::Table *sliderOptions)
  711. {
  712. Slider* slider = Slider::create();
  713. setPropsWithFlatBuffers(slider, (Table*)sliderOptions);
  714. return slider;
  715. }
  716. int SliderReader::getResourceType(std::string key)
  717. {
  718. if(key == "Normal" || key == "Default")
  719. {
  720. return 0;
  721. }
  722. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  723. if(fbs->_isSimulator)
  724. {
  725. if(key == "MarkedSubImage")
  726. {
  727. return 0;
  728. }
  729. }
  730. return 1;
  731. }
  732. }