LoadingBarReader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
  2. #include "ui/UILoadingBar.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_CapInsetsX = "capInsetsX";
  17. static const char* P_CapInsetsY = "capInsetsY";
  18. static const char* P_CapInsetsWidth = "capInsetsWidth";
  19. static const char* P_CapInsetsHeight = "capInsetsHeight";
  20. static const char* P_TextureData = "textureData";
  21. static const char* P_Direction = "direction";
  22. static const char* P_Percent = "percent";
  23. static LoadingBarReader* instanceLoadingBar = nullptr;
  24. IMPLEMENT_CLASS_NODE_READER_INFO(LoadingBarReader)
  25. LoadingBarReader::LoadingBarReader()
  26. {
  27. }
  28. LoadingBarReader::~LoadingBarReader()
  29. {
  30. }
  31. LoadingBarReader* LoadingBarReader::getInstance()
  32. {
  33. if (!instanceLoadingBar)
  34. {
  35. instanceLoadingBar = new (std::nothrow) LoadingBarReader();
  36. }
  37. return instanceLoadingBar;
  38. }
  39. void LoadingBarReader::destroyInstance()
  40. {
  41. CC_SAFE_DELETE(instanceLoadingBar);
  42. }
  43. void LoadingBarReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  44. {
  45. WidgetReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  46. LoadingBar* loadingBar = static_cast<LoadingBar*>(widget);
  47. this->beginSetBasicProperties(widget);
  48. float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
  49. int percent = loadingBar->getPercent();
  50. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  51. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  52. std::string key = stChildArray[i].GetName(cocoLoader);
  53. std::string value = stChildArray[i].GetValue(cocoLoader);
  54. //read all basic properties of widget
  55. CC_BASIC_PROPERTY_BINARY_READER
  56. //read all color related properties of widget
  57. CC_COLOR_PROPERTY_BINARY_READER
  58. else if (key == P_Scale9Enable) {
  59. loadingBar->setScale9Enabled(valueToBool(value));
  60. }
  61. else if (key == P_TextureData){
  62. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  63. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  64. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  65. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  66. loadingBar->loadTexture(backgroundValue, imageFileNameType);
  67. }
  68. else if(key == P_CapInsetsX){
  69. capsx = valueToFloat(value);
  70. }else if(key == P_CapInsetsY){
  71. capsy = valueToFloat(value);
  72. }else if(key == P_CapInsetsWidth){
  73. capsWidth = valueToFloat(value);
  74. }else if(key == P_CapInsetsHeight){
  75. capsHeight = valueToFloat(value);
  76. }else if(key == P_Direction){
  77. loadingBar->setDirection((LoadingBar::Direction)valueToInt(value));
  78. }else if(key == P_Percent){
  79. percent = valueToInt(value);
  80. }
  81. } //end of for loop
  82. if (loadingBar->isScale9Enabled()) {
  83. loadingBar->setCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
  84. }
  85. loadingBar->setPercent(percent);
  86. this->endSetBasicProperties(widget);
  87. }
  88. void LoadingBarReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  89. {
  90. WidgetReader::setPropsFromJsonDictionary(widget, options);
  91. LoadingBar* loadingBar = static_cast<LoadingBar*>(widget);
  92. const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_TextureData);
  93. int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
  94. std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
  95. loadingBar->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  96. /* gui mark add load bar scale9 parse */
  97. bool scale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
  98. loadingBar->setScale9Enabled(scale9Enable);
  99. float cx = DICTOOL->getFloatValue_json(options, P_CapInsetsX);
  100. float cy = DICTOOL->getFloatValue_json(options, P_CapInsetsY);
  101. float cw = DICTOOL->getFloatValue_json(options, P_CapInsetsWidth,1);
  102. float ch = DICTOOL->getFloatValue_json(options, P_CapInsetsHeight,1);
  103. if (scale9Enable) {
  104. loadingBar->setCapInsets(Rect(cx, cy, cw, ch));
  105. }
  106. float width = DICTOOL->getFloatValue_json(options, P_Width);
  107. float height = DICTOOL->getFloatValue_json(options, P_Height);
  108. loadingBar->setContentSize(Size(width, height));
  109. /**/
  110. loadingBar->setDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, P_Direction)));
  111. loadingBar->setPercent(DICTOOL->getIntValue_json(options, P_Percent,100));
  112. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  113. }
  114. Offset<Table> LoadingBarReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  115. flatbuffers::FlatBufferBuilder *builder)
  116. {
  117. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  118. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  119. std::string path = "";
  120. std::string plistFile = "";
  121. int resourceType = 0;
  122. int percent = 80;
  123. int direction = 0;
  124. // attributes
  125. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  126. while (attribute)
  127. {
  128. std::string name = attribute->Name();
  129. std::string value = attribute->Value();
  130. if (name == "ProgressType")
  131. {
  132. direction = (value == "Left_To_Right") ? 0 : 1;
  133. }
  134. else if (name == "ProgressInfo")
  135. {
  136. percent = atoi(value.c_str());
  137. }
  138. attribute = attribute->Next();
  139. }
  140. // child elements
  141. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  142. while (child)
  143. {
  144. std::string name = child->Name();
  145. if (name == "ImageFileData")
  146. {
  147. std::string texture = "";
  148. std::string texturePng = "";
  149. attribute = child->FirstAttribute();
  150. while (attribute)
  151. {
  152. name = attribute->Name();
  153. std::string value = attribute->Value();
  154. if (name == "Path")
  155. {
  156. path = value;
  157. }
  158. else if (name == "Type")
  159. {
  160. resourceType = getResourceType(value);
  161. }
  162. else if (name == "Plist")
  163. {
  164. plistFile = value;
  165. texture = value;
  166. }
  167. attribute = attribute->Next();
  168. }
  169. if (resourceType == 1)
  170. {
  171. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  172. fbs->_textures.push_back(builder->CreateString(texture));
  173. }
  174. }
  175. child = child->NextSiblingElement();
  176. }
  177. auto options = CreateLoadingBarOptions(*builder,
  178. widgetOptions,
  179. CreateResourceData(*builder,
  180. builder->CreateString(path),
  181. builder->CreateString(plistFile),
  182. resourceType),
  183. percent,
  184. direction);
  185. return *(Offset<Table>*)(&options);
  186. }
  187. void LoadingBarReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *loadingBarOptions)
  188. {
  189. LoadingBar* loadingBar = static_cast<LoadingBar*>(node);
  190. auto options = (LoadingBarOptions*)loadingBarOptions;
  191. bool fileExist = false;
  192. std::string errorFilePath = "";
  193. auto imageFileNameDic = options->textureData();
  194. int imageFileNameType = imageFileNameDic->resourceType();
  195. std::string imageFileName = imageFileNameDic->path()->c_str();
  196. switch (imageFileNameType)
  197. {
  198. case 0:
  199. {
  200. if (FileUtils::getInstance()->isFileExist(imageFileName))
  201. {
  202. fileExist = true;
  203. }
  204. else if (SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName))
  205. {
  206. fileExist = true;
  207. imageFileNameType = 1;
  208. }
  209. else
  210. {
  211. errorFilePath = imageFileName;
  212. fileExist = false;
  213. }
  214. break;
  215. }
  216. case 1:
  217. {
  218. std::string plist = imageFileNameDic->plistFile()->c_str();
  219. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  220. if (spriteFrame)
  221. {
  222. fileExist = true;
  223. }
  224. else
  225. {
  226. if (FileUtils::getInstance()->isFileExist(plist))
  227. {
  228. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  229. ValueMap metadata = value["metadata"].asValueMap();
  230. std::string textureFileName = metadata["textureFileName"].asString();
  231. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  232. {
  233. errorFilePath = textureFileName;
  234. }
  235. }
  236. else
  237. {
  238. errorFilePath = plist;
  239. }
  240. fileExist = false;
  241. }
  242. break;
  243. }
  244. default:
  245. break;
  246. }
  247. if (fileExist)
  248. {
  249. loadingBar->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  250. }
  251. int direction = options->direction();
  252. loadingBar->setDirection(LoadingBar::Direction(direction));
  253. int percent = options->percent();
  254. loadingBar->setPercent(percent);
  255. auto widgetReader = WidgetReader::getInstance();
  256. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  257. }
  258. Node* LoadingBarReader::createNodeWithFlatBuffers(const flatbuffers::Table *loadingBarOptions)
  259. {
  260. LoadingBar* loadingBar = LoadingBar::create();
  261. setPropsWithFlatBuffers(loadingBar, (Table*)loadingBarOptions);
  262. return loadingBar;
  263. }
  264. int LoadingBarReader::getResourceType(std::string key)
  265. {
  266. if(key == "Normal" || key == "Default")
  267. {
  268. return 0;
  269. }
  270. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  271. if(fbs->_isSimulator)
  272. {
  273. if(key == "MarkedSubImage")
  274. {
  275. return 0;
  276. }
  277. }
  278. return 1;
  279. }
  280. }