TextReader.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #include "editor-support/cocostudio/WidgetReader/TextReader/TextReader.h"
  2. #include "ui/UIText.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/LocalizationManager.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_TouchScaleEnable = "touchScaleEnable";
  15. static const char* P_Text = "text";
  16. static const char* P_FontSize = "fontSize";
  17. static const char* P_FontName = "fontName";
  18. static const char* P_AreaWidth = "areaWidth";
  19. static const char* P_AreaHeight = "areaHeight";
  20. static const char* P_HAlignment = "hAlignment";
  21. static const char* P_VAlignment = "vAlignment";
  22. static TextReader* instanceTextReader = nullptr;
  23. IMPLEMENT_CLASS_NODE_READER_INFO(TextReader)
  24. TextReader::TextReader()
  25. {
  26. }
  27. TextReader::~TextReader()
  28. {
  29. }
  30. TextReader* TextReader::getInstance()
  31. {
  32. if (!instanceTextReader)
  33. {
  34. instanceTextReader = new (std::nothrow) TextReader();
  35. }
  36. return instanceTextReader;
  37. }
  38. void TextReader::destroyInstance()
  39. {
  40. CC_SAFE_DELETE(instanceTextReader);
  41. }
  42. void TextReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  43. {
  44. this->beginSetBasicProperties(widget);
  45. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  46. Text* label = static_cast<Text*>(widget);
  47. std::string binaryFilePath = GUIReader::getInstance()->getFilePath();
  48. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  49. std::string key = stChildArray[i].GetName(cocoLoader);
  50. std::string value = stChildArray[i].GetValue(cocoLoader);
  51. //read all basic properties of widget
  52. CC_BASIC_PROPERTY_BINARY_READER
  53. //read all color related properties of widget
  54. CC_COLOR_PROPERTY_BINARY_READER
  55. else if (key == P_TouchScaleEnable) {
  56. label->setTouchScaleChangeEnabled(valueToBool(value));
  57. }
  58. else if(key == P_Text){
  59. label->setString(value);
  60. }else if(key == P_FontSize){
  61. label->setFontSize(valueToInt(value));
  62. }else if(key == P_FontName){
  63. std::string fontFilePath;
  64. fontFilePath = binaryFilePath.append(value);
  65. if (FileUtils::getInstance()->isFileExist(fontFilePath)) {
  66. label->setFontName(fontFilePath);
  67. }else{
  68. label->setFontName(value);
  69. }
  70. }else if(key == P_AreaWidth){
  71. label->setTextAreaSize(Size(valueToFloat(value), label->getTextAreaSize().height));
  72. }else if(key == P_AreaHeight){
  73. label->setTextAreaSize(Size(label->getTextAreaSize().width, valueToFloat(value)));
  74. }else if(key == P_HAlignment){
  75. label->setTextHorizontalAlignment((TextHAlignment)valueToInt(value));
  76. }else if(key == P_VAlignment){
  77. label->setTextVerticalAlignment((TextVAlignment)valueToInt(value));
  78. }
  79. } //end of for loop
  80. this->endSetBasicProperties(widget);
  81. }
  82. void TextReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  83. {
  84. WidgetReader::setPropsFromJsonDictionary(widget, options);
  85. std::string jsonPath = GUIReader::getInstance()->getFilePath();
  86. Text* label = static_cast<Text*>(widget);
  87. bool touchScaleChangeAble = DICTOOL->getBooleanValue_json(options, P_TouchScaleEnable);
  88. label->setTouchScaleChangeEnabled(touchScaleChangeAble);
  89. const char* text = DICTOOL->getStringValue_json(options, P_Text,"Text Label");
  90. label->setString(text);
  91. label->setFontSize(DICTOOL->getIntValue_json(options, P_FontSize,20));
  92. std::string fontName = DICTOOL->getStringValue_json(options, P_FontName, "");
  93. std::string fontFilePath = jsonPath.append(fontName);
  94. if (FileUtils::getInstance()->isFileExist(fontFilePath))
  95. {
  96. label->setFontName(fontFilePath);
  97. }
  98. else{
  99. label->setFontName(fontName);
  100. }
  101. bool aw = DICTOOL->checkObjectExist_json(options, P_AreaWidth);
  102. bool ah = DICTOOL->checkObjectExist_json(options, P_AreaHeight);
  103. if (aw && ah)
  104. {
  105. Size size = Size(DICTOOL->getFloatValue_json(options, P_AreaWidth),DICTOOL->getFloatValue_json(options,P_AreaHeight));
  106. label->setTextAreaSize(size);
  107. }
  108. bool ha = DICTOOL->checkObjectExist_json(options, P_HAlignment);
  109. if (ha)
  110. {
  111. label->setTextHorizontalAlignment((TextHAlignment)DICTOOL->getIntValue_json(options, P_HAlignment));
  112. }
  113. bool va = DICTOOL->checkObjectExist_json(options, P_VAlignment);
  114. if (va)
  115. {
  116. label->setTextVerticalAlignment((TextVAlignment)DICTOOL->getIntValue_json(options, P_VAlignment));
  117. }
  118. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  119. }
  120. Offset<Table> TextReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  121. flatbuffers::FlatBufferBuilder *builder)
  122. {
  123. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  124. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  125. bool touchScaleEnabled = false;
  126. bool isCustomSize = false;
  127. std::string fontName = "";
  128. int fontSize = 20;
  129. std::string text = "Text Label";
  130. bool isLocalized = false;
  131. int areaWidth = 0;
  132. int areaHeight = 0;
  133. int h_alignment = 0;
  134. int v_alignment = 0;
  135. bool outlineEnabled = false;
  136. Color4B outlineColor = Color4B::BLACK;
  137. int outlineSize = 1;
  138. bool shadowEnabled = false;
  139. Color4B shadowColor = Color4B::BLACK;
  140. Size shadowOffset = Size(2, -2);
  141. int shadowBlurRadius = 0;
  142. std::string path = "";
  143. std::string plistFile = "";
  144. int resourceType = 0;
  145. // attributes
  146. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  147. while (attribute)
  148. {
  149. std::string name = attribute->Name();
  150. std::string value = attribute->Value();
  151. if (name == "TouchScaleChangeAble")
  152. {
  153. touchScaleEnabled = (value == "True") ? true : false;
  154. }
  155. else if (name == "LabelText")
  156. {
  157. text = value;
  158. }
  159. else if (name == "IsLocalized")
  160. {
  161. isLocalized = (value == "True") ? true : false;
  162. }
  163. else if (name == "FontSize")
  164. {
  165. fontSize = atoi(value.c_str());
  166. }
  167. else if (name == "FontName")
  168. {
  169. fontName = value;
  170. }
  171. else if (name == "AreaWidth")
  172. {
  173. areaWidth = atoi(value.c_str());
  174. }
  175. else if (name == "AreaHeight")
  176. {
  177. areaHeight = atoi(value.c_str());
  178. }
  179. else if (name == "HorizontalAlignmentType")
  180. {
  181. if (value == "HT_Left")
  182. {
  183. h_alignment = 0;
  184. }
  185. else if (value == "HT_Center")
  186. {
  187. h_alignment = 1;
  188. }
  189. else if (value == "HT_Right")
  190. {
  191. h_alignment = 2;
  192. }
  193. }
  194. else if (name == "VerticalAlignmentType")
  195. {
  196. if (value == "VT_Top")
  197. {
  198. v_alignment = 0;
  199. }
  200. else if (value == "VT_Center")
  201. {
  202. v_alignment = 1;
  203. }
  204. else if (value == "VT_Bottom")
  205. {
  206. v_alignment = 2;
  207. }
  208. }
  209. else if (name == "IsCustomSize")
  210. {
  211. isCustomSize = (value == "True") ? true : false;
  212. }
  213. else if (name == "OutlineEnabled")
  214. {
  215. outlineEnabled = (value == "True") ? true : false;
  216. }
  217. else if (name == "OutlineSize")
  218. {
  219. outlineSize = atoi(value.c_str());
  220. }
  221. else if (name == "ShadowEnabled")
  222. {
  223. shadowEnabled = (value == "True") ? true : false;
  224. }
  225. else if (name == "ShadowOffsetX")
  226. {
  227. shadowOffset.width = atof(value.c_str());
  228. }
  229. else if (name == "ShadowOffsetY")
  230. {
  231. shadowOffset.height = atof(value.c_str());
  232. }
  233. else if (name == "ShadowBlurRadius")
  234. {
  235. shadowBlurRadius = atoi(value.c_str());
  236. }
  237. attribute = attribute->Next();
  238. }
  239. // child elements
  240. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  241. while (child)
  242. {
  243. std::string name = child->Name();
  244. if (name == "FontResource")
  245. {
  246. attribute = child->FirstAttribute();
  247. while (attribute)
  248. {
  249. name = attribute->Name();
  250. std::string value = attribute->Value();
  251. if (name == "Path")
  252. {
  253. path = value;
  254. }
  255. else if (name == "Type")
  256. {
  257. resourceType = 0;
  258. }
  259. else if (name == "Plist")
  260. {
  261. plistFile = value;
  262. }
  263. attribute = attribute->Next();
  264. }
  265. }
  266. else if (name == "OutlineColor")
  267. {
  268. attribute = child->FirstAttribute();
  269. while (attribute)
  270. {
  271. name = attribute->Name();
  272. std::string value = attribute->Value();
  273. if (name == "A")
  274. {
  275. outlineColor.a = atoi(value.c_str());
  276. }
  277. else if (name == "R")
  278. {
  279. outlineColor.r = atoi(value.c_str());
  280. }
  281. else if (name == "G")
  282. {
  283. outlineColor.g = atoi(value.c_str());
  284. }
  285. else if (name == "B")
  286. {
  287. outlineColor.b = atoi(value.c_str());
  288. }
  289. attribute = attribute->Next();
  290. }
  291. }
  292. else if (name == "ShadowColor")
  293. {
  294. attribute = child->FirstAttribute();
  295. while (attribute)
  296. {
  297. name = attribute->Name();
  298. std::string value = attribute->Value();
  299. if (name == "A")
  300. {
  301. shadowColor.a = atoi(value.c_str());
  302. }
  303. else if (name == "R")
  304. {
  305. shadowColor.r = atoi(value.c_str());
  306. }
  307. else if (name == "G")
  308. {
  309. shadowColor.g = atoi(value.c_str());
  310. }
  311. else if (name == "B")
  312. {
  313. shadowColor.b = atoi(value.c_str());
  314. }
  315. attribute = attribute->Next();
  316. }
  317. }
  318. child = child->NextSiblingElement();
  319. }
  320. flatbuffers::Color f_outlineColor(outlineColor.a, outlineColor.r, outlineColor.g, outlineColor.b);
  321. flatbuffers::Color f_shadowColor(shadowColor.a, shadowColor.r, shadowColor.g, shadowColor.b);
  322. auto options = CreateTextOptions(*builder,
  323. widgetOptions,
  324. CreateResourceData(*builder,
  325. builder->CreateString(path),
  326. builder->CreateString(plistFile),
  327. resourceType),
  328. builder->CreateString(fontName),
  329. fontSize,
  330. builder->CreateString(text),
  331. areaWidth,
  332. areaHeight,
  333. h_alignment,
  334. v_alignment,
  335. touchScaleEnabled,
  336. isCustomSize,
  337. outlineEnabled,
  338. &f_outlineColor,
  339. outlineSize,
  340. shadowEnabled,
  341. &f_shadowColor,
  342. shadowOffset.width,
  343. shadowOffset.height,
  344. shadowBlurRadius,
  345. isLocalized);
  346. return *(Offset<Table>*)(&options);
  347. }
  348. void TextReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textOptions)
  349. {
  350. Text* label = static_cast<Text*>(node);
  351. auto options = (TextOptions*)textOptions;
  352. bool touchScaleEnabled = options->touchScaleEnable() != 0;
  353. label->setTouchScaleChangeEnabled(touchScaleEnabled);
  354. int fontSize = options->fontSize();
  355. label->setFontSize(fontSize);
  356. Size areaSize = Size(options->areaWidth(), options->areaHeight());
  357. if (!areaSize.equals(Size::ZERO))
  358. {
  359. label->setTextAreaSize(areaSize);
  360. }
  361. auto resourceData = options->fontResource();
  362. std::string path = resourceData->path()->c_str();
  363. if (!path.empty() && FileUtils::getInstance()->isFileExist(path))
  364. {
  365. label->setFontName(path);
  366. }
  367. else
  368. {
  369. std::string fontName = options->fontName()->c_str();
  370. label->setFontName(fontName);
  371. }
  372. TextHAlignment h_alignment = (TextHAlignment)options->hAlignment();
  373. label->setTextHorizontalAlignment(h_alignment);
  374. TextVAlignment v_alignment = (TextVAlignment)options->vAlignment();
  375. label->setTextVerticalAlignment((TextVAlignment)v_alignment);
  376. bool outlineEnabled = options->outlineEnabled() != 0;
  377. if (outlineEnabled)
  378. {
  379. auto f_outlineColor = options->outlineColor();
  380. if (f_outlineColor)
  381. {
  382. Color4B outlineColor(f_outlineColor->r(), f_outlineColor->g(), f_outlineColor->b(), f_outlineColor->a());
  383. label->enableOutline(outlineColor, options->outlineSize());
  384. }
  385. }
  386. bool shadowEnabled = options->shadowEnabled() != 0;
  387. if (shadowEnabled)
  388. {
  389. auto f_shadowColor = options->shadowColor();
  390. if (f_shadowColor)
  391. {
  392. Color4B shadowColor(f_shadowColor->r(), f_shadowColor->g(), f_shadowColor->b(), f_shadowColor->a());
  393. label->enableShadow(shadowColor, Size(options->shadowOffsetX(), options->shadowOffsetY()), options->shadowBlurRadius());
  394. }
  395. }
  396. std::string text = options->text()->c_str();
  397. bool isLocalized = options->isLocalized() != 0;
  398. if (isLocalized)
  399. {
  400. ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
  401. label->setString(lm->getLocalizationString(text));
  402. }
  403. else
  404. {
  405. label->setString(text);
  406. }
  407. // Save node color before set widget properties
  408. auto oldColor = node->getColor();
  409. auto widgetReader = WidgetReader::getInstance();
  410. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  411. // restore node color and set color to text to fix shadow & outline color won't show correct bug
  412. node->setColor(oldColor);
  413. auto optionsWidget = (WidgetOptions*)options->widgetOptions();
  414. auto f_color = optionsWidget->color();
  415. Color4B color(f_color->r(), f_color->g(), f_color->b(), f_color->a());
  416. ((Text *)node)->setTextColor(color);
  417. label->setUnifySizeEnabled(false);
  418. bool IsCustomSize = options->isCustomSize() != 0;
  419. label->ignoreContentAdaptWithSize(!IsCustomSize);
  420. auto widgetOptions = options->widgetOptions();
  421. if (!label->isIgnoreContentAdaptWithSize())
  422. {
  423. Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
  424. label->setContentSize(contentSize);
  425. }
  426. }
  427. Node* TextReader::createNodeWithFlatBuffers(const flatbuffers::Table *textOptions)
  428. {
  429. Text* text = Text::create();
  430. setPropsWithFlatBuffers(text, (Table*)textOptions);
  431. return text;
  432. }
  433. }