TextAtlasReader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "editor-support/cocostudio/WidgetReader/TextAtlasReader/TextAtlasReader.h"
  2. #include "ui/UITextAtlas.h"
  3. #include "platform/CCFileUtils.h"
  4. #include "editor-support/cocostudio/CocoLoader.h"
  5. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  6. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  7. #include "tinyxml2.h"
  8. #include "flatbuffers/flatbuffers.h"
  9. USING_NS_CC;
  10. using namespace ui;
  11. using namespace flatbuffers;
  12. namespace cocostudio
  13. {
  14. static const char* P_StringValue = "stringValue";
  15. static const char* P_CharMapFileData = "charMapFileData";
  16. static const char* P_ItemWidth = "itemWidth";
  17. static const char* P_ItemHeight = "itemHeight";
  18. static const char* P_StartCharMap = "startCharMap";
  19. static TextAtlasReader* instanceTextAtlasReader = nullptr;
  20. IMPLEMENT_CLASS_NODE_READER_INFO(TextAtlasReader)
  21. TextAtlasReader::TextAtlasReader()
  22. {
  23. }
  24. TextAtlasReader::~TextAtlasReader()
  25. {
  26. }
  27. TextAtlasReader* TextAtlasReader::getInstance()
  28. {
  29. if (!instanceTextAtlasReader)
  30. {
  31. instanceTextAtlasReader = new (std::nothrow) TextAtlasReader();
  32. }
  33. return instanceTextAtlasReader;
  34. }
  35. void TextAtlasReader::destroyInstance()
  36. {
  37. CC_SAFE_DELETE(instanceTextAtlasReader);
  38. }
  39. void TextAtlasReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  40. {
  41. this->beginSetBasicProperties(widget);
  42. TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget);
  43. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  44. Widget::TextureResType type;
  45. std::string charMapFileName;
  46. std::string stringValue;
  47. std::string startCharMap;
  48. float itemWidth;
  49. float itemHeight;
  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. else if (key == P_StringValue) {
  58. stringValue = value;
  59. }
  60. else if(key == P_CharMapFileData){
  61. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  62. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  63. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  64. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  65. charMapFileName = backgroundValue;
  66. type = imageFileNameType;
  67. }else if(key == P_ItemWidth){
  68. itemWidth = valueToFloat(value);
  69. }else if(key == P_ItemHeight){
  70. itemHeight = valueToFloat(value);
  71. }else if(key == P_StartCharMap){
  72. startCharMap = value;
  73. }
  74. } //end of for loop
  75. if (type == (Widget::TextureResType)0) {
  76. labelAtlas->setProperty(stringValue, charMapFileName, itemWidth, itemHeight, startCharMap);
  77. }
  78. this->endSetBasicProperties(widget);
  79. }
  80. void TextAtlasReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  81. {
  82. WidgetReader::setPropsFromJsonDictionary(widget, options);
  83. std::string jsonPath = GUIReader::getInstance()->getFilePath();
  84. TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget);
  85. // bool sv = DICTOOL->checkObjectExist_json(options, P_StringValue);
  86. // bool cmf = DICTOOL->checkObjectExist_json(options, P_CharMapFile);
  87. // bool iw = DICTOOL->checkObjectExist_json(options, P_ItemWidth);
  88. // bool ih = DICTOOL->checkObjectExist_json(options, P_ItemHeight);
  89. // bool scm = DICTOOL->checkObjectExist_json(options, P_StartCharMap);
  90. const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_CharMapFileData);
  91. int cmfType = DICTOOL->getIntValue_json(cmftDic, P_ResourceType);
  92. switch (cmfType)
  93. {
  94. case 0:
  95. {
  96. std::string tp_c = jsonPath;
  97. const char* cmfPath = DICTOOL->getStringValue_json(cmftDic, P_Path);
  98. const char* cmf_tp = tp_c.append(cmfPath).c_str();
  99. labelAtlas->setProperty(DICTOOL->getStringValue_json(options, P_StringValue,"12345678"),
  100. cmf_tp,
  101. DICTOOL->getIntValue_json(options, P_ItemWidth,24),
  102. DICTOOL->getIntValue_json(options,P_ItemHeight,32),
  103. DICTOOL->getStringValue_json(options, P_StartCharMap));
  104. break;
  105. }
  106. case 1:
  107. CCLOG("Wrong res type of LabelAtlas!");
  108. break;
  109. default:
  110. break;
  111. }
  112. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  113. }
  114. Offset<Table> TextAtlasReader::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. std::string stringValue = "0123456789";
  123. int itemWidth = 0;
  124. int itemHeight = 0;
  125. std::string startCharMap = "";
  126. // attributes
  127. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  128. while (attribute)
  129. {
  130. std::string name = attribute->Name();
  131. std::string value = attribute->Value();
  132. if (name == "LabelText")
  133. {
  134. stringValue = value;
  135. }
  136. else if (name == "CharWidth")
  137. {
  138. itemWidth = atoi(value.c_str());
  139. }
  140. else if (name == "CharHeight")
  141. {
  142. itemHeight = atoi(value.c_str());
  143. }
  144. else if (name == "StartChar")
  145. {
  146. startCharMap = value;
  147. }
  148. attribute = attribute->Next();
  149. }
  150. // child elements
  151. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  152. while (child)
  153. {
  154. std::string name = child->Name();
  155. if (name == "LabelAtlasFileImage_CNB")
  156. {
  157. std::string texture = "";
  158. std::string texturePng = "";
  159. attribute = child->FirstAttribute();
  160. while (attribute)
  161. {
  162. name = attribute->Name();
  163. std::string value = attribute->Value();
  164. if (name == "Path")
  165. {
  166. path = value;
  167. }
  168. else if (name == "Type")
  169. {
  170. resourceType = 0;
  171. }
  172. else if (name == "Plist")
  173. {
  174. plistFile = value;
  175. texture = value;
  176. }
  177. attribute = attribute->Next();
  178. }
  179. }
  180. child = child->NextSiblingElement();
  181. }
  182. auto options = CreateTextAtlasOptions(*builder,
  183. widgetOptions,
  184. CreateResourceData(*builder,
  185. builder->CreateString(path),
  186. builder->CreateString(plistFile),
  187. resourceType),
  188. builder->CreateString(stringValue),
  189. builder->CreateString(startCharMap),
  190. itemWidth,
  191. itemHeight
  192. );
  193. return *(Offset<Table>*)(&options);
  194. }
  195. void TextAtlasReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textAtlasOptions)
  196. {
  197. TextAtlas* labelAtlas = static_cast<TextAtlas*>(node);
  198. auto options = (TextAtlasOptions*)textAtlasOptions;
  199. auto cmftDic = options->charMapFileData();
  200. int cmfType = cmftDic->resourceType();
  201. switch (cmfType)
  202. {
  203. case 0:
  204. {
  205. const char* cmfPath = cmftDic->path()->c_str();
  206. bool fileExist = false;
  207. std::string errorFilePath = "";
  208. if (FileUtils::getInstance()->isFileExist(cmfPath))
  209. {
  210. fileExist = true;
  211. std::string stringValue = options->stringValue()->c_str();
  212. int itemWidth = options->itemWidth();
  213. int itemHeight = options->itemHeight();
  214. labelAtlas->setProperty(stringValue,
  215. cmfPath,
  216. itemWidth,
  217. itemHeight,
  218. options->startCharMap()->c_str());
  219. }
  220. else
  221. {
  222. errorFilePath = cmfPath;
  223. fileExist = false;
  224. }
  225. break;
  226. }
  227. case 1:
  228. CCLOG("Wrong res type of LabelAtlas!");
  229. break;
  230. default:
  231. break;
  232. }
  233. auto widgetReader = WidgetReader::getInstance();
  234. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  235. labelAtlas->ignoreContentAdaptWithSize(true);
  236. }
  237. Node* TextAtlasReader::createNodeWithFlatBuffers(const flatbuffers::Table *textAtlasOptions)
  238. {
  239. TextAtlas* textAtlas = TextAtlas::create();
  240. setPropsWithFlatBuffers(textAtlas, (Table*)textAtlasOptions);
  241. return textAtlas;
  242. }
  243. }