PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/cocos/editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.cpp

https://github.com/dumganhar/cocos2d-x
C++ | 376 lines | 274 code | 73 blank | 29 comment | 59 complexity | 66d3ecd5c1c345fd3ce709b793cb14cb MD5 | raw file
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/WidgetReader/LoadingBarReader/LoadingBarReader.h"
  21. #include "ui/UILoadingBar.h"
  22. #include "2d/CCSpriteFrameCache.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "editor-support/cocostudio/CocoLoader.h"
  25. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  26. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  27. #include "tinyxml2.h"
  28. #include "flatbuffers/flatbuffers.h"
  29. USING_NS_CC;
  30. using namespace ui;
  31. using namespace flatbuffers;
  32. namespace cocostudio
  33. {
  34. static const char* P_Scale9Enable = "scale9Enable";
  35. static const char* P_CapInsetsX = "capInsetsX";
  36. static const char* P_CapInsetsY = "capInsetsY";
  37. static const char* P_CapInsetsWidth = "capInsetsWidth";
  38. static const char* P_CapInsetsHeight = "capInsetsHeight";
  39. static const char* P_TextureData = "textureData";
  40. static const char* P_Direction = "direction";
  41. static const char* P_Percent = "percent";
  42. static LoadingBarReader* instanceLoadingBar = nullptr;
  43. IMPLEMENT_CLASS_NODE_READER_INFO(LoadingBarReader)
  44. LoadingBarReader::LoadingBarReader()
  45. {
  46. }
  47. LoadingBarReader::~LoadingBarReader()
  48. {
  49. }
  50. LoadingBarReader* LoadingBarReader::getInstance()
  51. {
  52. if (!instanceLoadingBar)
  53. {
  54. instanceLoadingBar = new (std::nothrow) LoadingBarReader();
  55. }
  56. return instanceLoadingBar;
  57. }
  58. void LoadingBarReader::destroyInstance()
  59. {
  60. CC_SAFE_DELETE(instanceLoadingBar);
  61. }
  62. void LoadingBarReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  63. {
  64. WidgetReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  65. LoadingBar* loadingBar = static_cast<LoadingBar*>(widget);
  66. this->beginSetBasicProperties(widget);
  67. float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
  68. int percent = loadingBar->getPercent();
  69. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  70. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  71. std::string key = stChildArray[i].GetName(cocoLoader);
  72. std::string value = stChildArray[i].GetValue(cocoLoader);
  73. //read all basic properties of widget
  74. CC_BASIC_PROPERTY_BINARY_READER
  75. //read all color related properties of widget
  76. CC_COLOR_PROPERTY_BINARY_READER
  77. else if (key == P_Scale9Enable) {
  78. loadingBar->setScale9Enabled(valueToBool(value));
  79. }
  80. else if (key == P_TextureData){
  81. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  82. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  83. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  84. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  85. loadingBar->loadTexture(backgroundValue, imageFileNameType);
  86. }
  87. else if(key == P_CapInsetsX){
  88. capsx = valueToFloat(value);
  89. }else if(key == P_CapInsetsY){
  90. capsy = valueToFloat(value);
  91. }else if(key == P_CapInsetsWidth){
  92. capsWidth = valueToFloat(value);
  93. }else if(key == P_CapInsetsHeight){
  94. capsHeight = valueToFloat(value);
  95. }else if(key == P_Direction){
  96. loadingBar->setDirection((LoadingBar::Direction)valueToInt(value));
  97. }else if(key == P_Percent){
  98. percent = valueToInt(value);
  99. }
  100. } //end of for loop
  101. if (loadingBar->isScale9Enabled()) {
  102. loadingBar->setCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
  103. }
  104. loadingBar->setPercent(percent);
  105. this->endSetBasicProperties(widget);
  106. }
  107. void LoadingBarReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  108. {
  109. WidgetReader::setPropsFromJsonDictionary(widget, options);
  110. LoadingBar* loadingBar = static_cast<LoadingBar*>(widget);
  111. const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_TextureData);
  112. int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
  113. std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
  114. loadingBar->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  115. /* gui mark add load bar scale9 parse */
  116. bool scale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
  117. loadingBar->setScale9Enabled(scale9Enable);
  118. float cx = DICTOOL->getFloatValue_json(options, P_CapInsetsX);
  119. float cy = DICTOOL->getFloatValue_json(options, P_CapInsetsY);
  120. float cw = DICTOOL->getFloatValue_json(options, P_CapInsetsWidth,1);
  121. float ch = DICTOOL->getFloatValue_json(options, P_CapInsetsHeight,1);
  122. if (scale9Enable) {
  123. loadingBar->setCapInsets(Rect(cx, cy, cw, ch));
  124. }
  125. float width = DICTOOL->getFloatValue_json(options, P_Width);
  126. float height = DICTOOL->getFloatValue_json(options, P_Height);
  127. loadingBar->setContentSize(Size(width, height));
  128. /**/
  129. loadingBar->setDirection(LoadingBar::Direction(DICTOOL->getIntValue_json(options, P_Direction)));
  130. loadingBar->setPercent(DICTOOL->getIntValue_json(options, P_Percent,100));
  131. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  132. }
  133. Offset<Table> LoadingBarReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  134. flatbuffers::FlatBufferBuilder *builder)
  135. {
  136. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  137. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  138. std::string path = "";
  139. std::string plistFile = "";
  140. int resourceType = 0;
  141. int percent = 80;
  142. int direction = 0;
  143. // attributes
  144. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  145. while (attribute)
  146. {
  147. std::string name = attribute->Name();
  148. std::string value = attribute->Value();
  149. if (name == "ProgressType")
  150. {
  151. direction = (value == "Left_To_Right") ? 0 : 1;
  152. }
  153. else if (name == "ProgressInfo")
  154. {
  155. percent = atoi(value.c_str());
  156. }
  157. attribute = attribute->Next();
  158. }
  159. // child elements
  160. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  161. while (child)
  162. {
  163. std::string name = child->Name();
  164. if (name == "ImageFileData")
  165. {
  166. std::string texture = "";
  167. std::string texturePng = "";
  168. attribute = child->FirstAttribute();
  169. while (attribute)
  170. {
  171. name = attribute->Name();
  172. std::string value = attribute->Value();
  173. if (name == "Path")
  174. {
  175. path = value;
  176. }
  177. else if (name == "Type")
  178. {
  179. resourceType = getResourceType(value);
  180. }
  181. else if (name == "Plist")
  182. {
  183. plistFile = value;
  184. texture = value;
  185. }
  186. attribute = attribute->Next();
  187. }
  188. if (resourceType == 1)
  189. {
  190. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  191. fbs->_textures.push_back(builder->CreateString(texture));
  192. }
  193. }
  194. child = child->NextSiblingElement();
  195. }
  196. auto options = CreateLoadingBarOptions(*builder,
  197. widgetOptions,
  198. CreateResourceData(*builder,
  199. builder->CreateString(path),
  200. builder->CreateString(plistFile),
  201. resourceType),
  202. percent,
  203. direction);
  204. return *(Offset<Table>*)(&options);
  205. }
  206. void LoadingBarReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *loadingBarOptions)
  207. {
  208. LoadingBar* loadingBar = static_cast<LoadingBar*>(node);
  209. auto options = (LoadingBarOptions*)loadingBarOptions;
  210. bool fileExist = false;
  211. std::string errorFilePath = "";
  212. auto imageFileNameDic = options->textureData();
  213. int imageFileNameType = imageFileNameDic->resourceType();
  214. std::string imageFileName = imageFileNameDic->path()->c_str();
  215. switch (imageFileNameType)
  216. {
  217. case 0:
  218. {
  219. if (FileUtils::getInstance()->isFileExist(imageFileName))
  220. {
  221. fileExist = true;
  222. }
  223. else if (SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName))
  224. {
  225. fileExist = true;
  226. imageFileNameType = 1;
  227. }
  228. else
  229. {
  230. errorFilePath = imageFileName;
  231. fileExist = false;
  232. }
  233. break;
  234. }
  235. case 1:
  236. {
  237. std::string plist = imageFileNameDic->plistFile()->c_str();
  238. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  239. if (spriteFrame)
  240. {
  241. fileExist = true;
  242. }
  243. else
  244. {
  245. if (FileUtils::getInstance()->isFileExist(plist))
  246. {
  247. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  248. ValueMap metadata = value["metadata"].asValueMap();
  249. std::string textureFileName = metadata["textureFileName"].asString();
  250. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  251. {
  252. errorFilePath = textureFileName;
  253. }
  254. }
  255. else
  256. {
  257. errorFilePath = plist;
  258. }
  259. fileExist = false;
  260. }
  261. break;
  262. }
  263. default:
  264. break;
  265. }
  266. if (fileExist)
  267. {
  268. loadingBar->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  269. }
  270. int direction = options->direction();
  271. loadingBar->setDirection(LoadingBar::Direction(direction));
  272. int percent = options->percent();
  273. loadingBar->setPercent(percent);
  274. auto widgetReader = WidgetReader::getInstance();
  275. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  276. }
  277. Node* LoadingBarReader::createNodeWithFlatBuffers(const flatbuffers::Table *loadingBarOptions)
  278. {
  279. LoadingBar* loadingBar = LoadingBar::create();
  280. setPropsWithFlatBuffers(loadingBar, (Table*)loadingBarOptions);
  281. return loadingBar;
  282. }
  283. int LoadingBarReader::getResourceType(std::string key)
  284. {
  285. if(key == "Normal" || key == "Default")
  286. {
  287. return 0;
  288. }
  289. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  290. if(fbs->_isSimulator)
  291. {
  292. if(key == "MarkedSubImage")
  293. {
  294. return 0;
  295. }
  296. }
  297. return 1;
  298. }
  299. }