TextBMFontReader.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "editor-support/cocostudio/WidgetReader/TextBMFontReader/TextBMFontReader.h"
  2. #include "2d/CCFontAtlasCache.h"
  3. #include "ui/UITextBMFont.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/LocalizationManager.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_FileNameData = "fileNameData";
  16. static const char* P_Text = "text";
  17. static TextBMFontReader* instanceTextBMFontReader = nullptr;
  18. IMPLEMENT_CLASS_NODE_READER_INFO(TextBMFontReader)
  19. TextBMFontReader::TextBMFontReader()
  20. {
  21. }
  22. TextBMFontReader::~TextBMFontReader()
  23. {
  24. }
  25. TextBMFontReader* TextBMFontReader::getInstance()
  26. {
  27. if (!instanceTextBMFontReader)
  28. {
  29. instanceTextBMFontReader = new (std::nothrow) TextBMFontReader();
  30. }
  31. return instanceTextBMFontReader;
  32. }
  33. void TextBMFontReader::destroyInstance()
  34. {
  35. CC_SAFE_DELETE(instanceTextBMFontReader);
  36. }
  37. void TextBMFontReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  38. {
  39. this->beginSetBasicProperties(widget);
  40. TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
  41. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  42. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  43. std::string key = stChildArray[i].GetName(cocoLoader);
  44. std::string value = stChildArray[i].GetValue(cocoLoader);
  45. //read all basic properties of widget
  46. CC_BASIC_PROPERTY_BINARY_READER
  47. //read all color related properties of widget
  48. CC_COLOR_PROPERTY_BINARY_READER
  49. else if(key == P_FileNameData){
  50. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  51. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  52. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  53. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  54. if (imageFileNameType == (Widget::TextureResType)0) {
  55. labelBMFont->setFntFile(backgroundValue);
  56. }
  57. }else if(key == P_Text){
  58. labelBMFont->setString(value);
  59. }
  60. } //end of for loop
  61. this->endSetBasicProperties(widget);
  62. }
  63. void TextBMFontReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  64. {
  65. WidgetReader::setPropsFromJsonDictionary(widget, options);
  66. std::string jsonPath = GUIReader::getInstance()->getFilePath();
  67. TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
  68. const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_FileNameData);
  69. int cmfType = DICTOOL->getIntValue_json(cmftDic, P_ResourceType);
  70. switch (cmfType)
  71. {
  72. case 0:
  73. {
  74. std::string tp_c = jsonPath;
  75. const char* cmfPath = DICTOOL->getStringValue_json(cmftDic, P_Path);
  76. const char* cmf_tp = tp_c.append(cmfPath).c_str();
  77. labelBMFont->setFntFile(cmf_tp);
  78. break;
  79. }
  80. case 1:
  81. CCLOG("Wrong res type of LabelAtlas!");
  82. break;
  83. default:
  84. break;
  85. }
  86. const char* text = DICTOOL->getStringValue_json(options, P_Text,"Text Label");
  87. labelBMFont->setString(text);
  88. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  89. }
  90. Offset<Table> TextBMFontReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  91. flatbuffers::FlatBufferBuilder *builder)
  92. {
  93. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  94. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  95. std::string text = "Fnt Text Label";
  96. bool isLocalized = false;
  97. std::string path = "";
  98. std::string plistFlie = "";
  99. int resourceType = 0;
  100. // attributes
  101. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  102. while (attribute)
  103. {
  104. std::string name = attribute->Name();
  105. std::string value = attribute->Value();
  106. if (name == "LabelText")
  107. {
  108. text = value;
  109. }
  110. else if (name == "IsLocalized")
  111. {
  112. isLocalized = (value == "True") ? true : false;
  113. }
  114. attribute = attribute->Next();
  115. }
  116. // child elements
  117. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  118. while (child)
  119. {
  120. std::string name = child->Name();
  121. if (name == "LabelBMFontFile_CNB")
  122. {
  123. attribute = child->FirstAttribute();
  124. while (attribute)
  125. {
  126. name = attribute->Name();
  127. std::string value = attribute->Value();
  128. if (name == "Path")
  129. {
  130. path = value;
  131. }
  132. else if (name == "Type")
  133. {
  134. resourceType = 0;
  135. }
  136. else if (name == "Plist")
  137. {
  138. plistFlie = value;
  139. }
  140. attribute = attribute->Next();
  141. }
  142. }
  143. child = child->NextSiblingElement();
  144. }
  145. auto options = CreateTextBMFontOptions(*builder,
  146. widgetOptions,
  147. CreateResourceData(*builder,
  148. builder->CreateString(path),
  149. builder->CreateString(plistFlie),
  150. resourceType),
  151. builder->CreateString(text),
  152. isLocalized);
  153. return *(Offset<Table>*)(&options);
  154. }
  155. void TextBMFontReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textBMFontOptions)
  156. {
  157. TextBMFont* labelBMFont = static_cast<TextBMFont*>(node);
  158. auto options = (TextBMFontOptions*)textBMFontOptions;
  159. auto cmftDic = options->fileNameData();
  160. bool fileExist = false;
  161. std::string errorFilePath = "";
  162. std::string errorContent = "";
  163. std::string path = cmftDic->path()->c_str();
  164. int cmfType = cmftDic->resourceType();
  165. switch (cmfType)
  166. {
  167. case 0:
  168. {
  169. if (FileUtils::getInstance()->isFileExist(path))
  170. {
  171. FontAtlas* newAtlas = FontAtlasCache::getFontAtlasFNT(path);
  172. if (newAtlas)
  173. {
  174. fileExist = true;
  175. }
  176. else
  177. {
  178. errorContent = "has problem";
  179. fileExist = false;
  180. }
  181. }
  182. break;
  183. }
  184. default:
  185. break;
  186. }
  187. if (fileExist)
  188. {
  189. labelBMFont->setFntFile(path);
  190. }
  191. std::string text = options->text()->c_str();
  192. bool isLocalized = options->isLocalized() != 0;
  193. if (isLocalized)
  194. {
  195. ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
  196. labelBMFont->setString(lm->getLocalizationString(text));
  197. }
  198. else
  199. {
  200. labelBMFont->setString(text);
  201. }
  202. auto widgetReader = WidgetReader::getInstance();
  203. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  204. labelBMFont->ignoreContentAdaptWithSize(true);
  205. }
  206. Node* TextBMFontReader::createNodeWithFlatBuffers(const flatbuffers::Table *textBMFontOptions)
  207. {
  208. TextBMFont* textBMFont = TextBMFont::create();
  209. setPropsWithFlatBuffers(textBMFont, (Table*)textBMFontOptions);
  210. return textBMFont;
  211. }
  212. }