TextFieldReader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #include "editor-support/cocostudio/WidgetReader/TextFieldReader/TextFieldReader.h"
  2. #include "ui/UITextField.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 TextFieldReader* instanceTextFieldReader = nullptr;
  15. static const char* P_PlaceHolder = "placeHolder";
  16. static const char* P_Text = "text";
  17. static const char* P_FontSize = "fontSize";
  18. static const char* P_FontName = "fontName";
  19. static const char* P_TouchSizeWidth = "touchSizeWidth";
  20. static const char* P_TouchSizeHeight = "touchSizeHeight";
  21. static const char* P_MaxLengthEnable = "maxLengthEnable";
  22. static const char* P_MaxLength = "maxLength";
  23. static const char* P_PasswordEnable = "passwordEnable";
  24. static const char* P_PasswordStyleText = "passwordStyleText";
  25. IMPLEMENT_CLASS_NODE_READER_INFO(TextFieldReader)
  26. TextFieldReader::TextFieldReader()
  27. {
  28. }
  29. TextFieldReader::~TextFieldReader()
  30. {
  31. }
  32. TextFieldReader* TextFieldReader::getInstance()
  33. {
  34. if (!instanceTextFieldReader)
  35. {
  36. instanceTextFieldReader = new (std::nothrow) TextFieldReader();
  37. }
  38. return instanceTextFieldReader;
  39. }
  40. void TextFieldReader::destroyInstance()
  41. {
  42. CC_SAFE_DELETE(instanceTextFieldReader);
  43. }
  44. void TextFieldReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode* cocoNode)
  45. {
  46. this->beginSetBasicProperties(widget);
  47. TextField* textField = static_cast<TextField*>(widget);
  48. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  49. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  50. std::string key = stChildArray[i].GetName(cocoLoader);
  51. std::string value = stChildArray[i].GetValue(cocoLoader);
  52. //read all basic properties of widget
  53. CC_BASIC_PROPERTY_BINARY_READER
  54. //read all color related properties of widget
  55. CC_COLOR_PROPERTY_BINARY_READER
  56. else if(key == P_PlaceHolder){
  57. textField->setPlaceHolder(value);
  58. }else if(key == P_Text){
  59. textField->setString(value);
  60. }else if(key == P_FontSize){
  61. textField->setFontSize(valueToInt(value));
  62. }else if(key == P_FontName){
  63. textField->setFontName(value);
  64. }else if(key == P_TouchSizeWidth){
  65. textField->setTouchSize(Size(valueToFloat(value), textField->getTouchSize().height));
  66. }else if(key == P_TouchSizeHeight){
  67. textField->setTouchSize(Size(textField->getTouchSize().width, valueToFloat(value)));
  68. }else if (key == P_MaxLengthEnable){
  69. textField->setMaxLengthEnabled(valueToBool(value));
  70. }else if(key == P_MaxLength){
  71. textField->setMaxLength(valueToInt(value));
  72. }else if(key == P_PasswordEnable){
  73. textField->setPasswordEnabled(valueToBool(value));
  74. }else if(key == P_PasswordStyleText){
  75. textField->setPasswordStyleText(value.c_str());
  76. }
  77. } //end of for loop
  78. this->endSetBasicProperties(widget);
  79. }
  80. void TextFieldReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  81. {
  82. WidgetReader::setPropsFromJsonDictionary(widget, options);
  83. TextField* textField = static_cast<TextField*>(widget);
  84. bool ph = DICTOOL->checkObjectExist_json(options, P_PlaceHolder);
  85. if (ph)
  86. {
  87. textField->setPlaceHolder(DICTOOL->getStringValue_json(options, P_PlaceHolder,"input words here"));
  88. }
  89. textField->setString(DICTOOL->getStringValue_json(options, P_Text,"Text Tield"));
  90. textField->setFontSize(DICTOOL->getIntValue_json(options, P_FontSize,20));
  91. std::string jsonPath = GUIReader::getInstance()->getFilePath();
  92. std::string fontName = DICTOOL->getStringValue_json(options, P_FontName, "");
  93. std::string fontFilePath = jsonPath.append(fontName);
  94. if (FileUtils::getInstance()->isFileExist(fontFilePath))
  95. textField->setFontName(fontFilePath);
  96. else
  97. textField->setFontName(fontName);
  98. bool tsw = DICTOOL->checkObjectExist_json(options, P_TouchSizeWidth);
  99. bool tsh = DICTOOL->checkObjectExist_json(options, P_TouchSizeHeight);
  100. if (tsw && tsh)
  101. {
  102. textField->setTouchSize(Size(DICTOOL->getFloatValue_json(options, P_TouchSizeWidth), DICTOOL->getFloatValue_json(options,P_TouchSizeHeight)));
  103. }
  104. // float dw = DICTOOL->getFloatValue_json(options, "width");
  105. // float dh = DICTOOL->getFloatValue_json(options, "height");
  106. // if (dw > 0.0f || dh > 0.0f)
  107. // {
  108. // //textField->setSize(Size(dw, dh));
  109. // }
  110. bool maxLengthEnable = DICTOOL->getBooleanValue_json(options, P_MaxLengthEnable);
  111. textField->setMaxLengthEnabled(maxLengthEnable);
  112. if (maxLengthEnable)
  113. {
  114. int maxLength = DICTOOL->getIntValue_json(options, P_MaxLength,10);
  115. textField->setMaxLength(maxLength);
  116. }
  117. bool passwordEnable = DICTOOL->getBooleanValue_json(options, P_PasswordEnable);
  118. textField->setPasswordEnabled(passwordEnable);
  119. if (passwordEnable)
  120. {
  121. textField->setPasswordStyleText(DICTOOL->getStringValue_json(options, P_PasswordStyleText,"*"));
  122. }
  123. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  124. }
  125. Offset<Table> TextFieldReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  126. flatbuffers::FlatBufferBuilder *builder)
  127. {
  128. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  129. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  130. std::string path = "";
  131. std::string plistFile = "";
  132. int resourceType = 0;
  133. std::string fontName = "";
  134. int fontSize = 20;
  135. std::string text = "";
  136. bool isLocalized = false;
  137. std::string placeHolder = "Text Field";
  138. bool passwordEnabled = false;
  139. std::string passwordStyleText = "*";
  140. bool maxLengthEnabled = false;
  141. int maxLength = 10;
  142. int areaWidth = 0;
  143. int areaHeight = 0;
  144. bool isCustomSize = false;
  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 == "PlaceHolderText")
  152. {
  153. placeHolder = value;
  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 == "MaxLengthEnable")
  172. {
  173. maxLengthEnabled = (value == "True") ? true : false;
  174. }
  175. else if (name == "MaxLengthText")
  176. {
  177. maxLength = atoi(value.c_str());
  178. }
  179. else if (name == "PasswordEnable")
  180. {
  181. passwordEnabled = (value == "True") ? true : false;
  182. }
  183. else if (name == "PasswordStyleText")
  184. {
  185. passwordStyleText = value;
  186. }
  187. else if (name == "IsCustomSize")
  188. {
  189. isCustomSize = (value == "True") ? true : false;
  190. }
  191. attribute = attribute->Next();
  192. }
  193. // child elements
  194. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  195. while (child)
  196. {
  197. std::string name = child->Name();
  198. if (name == "FontResource")
  199. {
  200. attribute = child->FirstAttribute();
  201. while (attribute)
  202. {
  203. name = attribute->Name();
  204. std::string value = attribute->Value();
  205. if (name == "Path")
  206. {
  207. path = value;
  208. }
  209. else if (name == "Type")
  210. {
  211. resourceType = 0;
  212. }
  213. else if (name == "Plist")
  214. {
  215. plistFile = value;
  216. }
  217. attribute = attribute->Next();
  218. }
  219. }
  220. child = child->NextSiblingElement();
  221. }
  222. auto options = CreateTextFieldOptions(*builder,
  223. widgetOptions,
  224. CreateResourceData(*builder,
  225. builder->CreateString(path),
  226. builder->CreateString(plistFile),
  227. resourceType),
  228. builder->CreateString(fontName),
  229. fontSize,
  230. builder->CreateString(text),
  231. builder->CreateString(placeHolder),
  232. passwordEnabled,
  233. builder->CreateString(passwordStyleText),
  234. maxLengthEnabled,
  235. maxLength,
  236. areaWidth,
  237. areaHeight,
  238. isCustomSize,
  239. isLocalized);
  240. return *(Offset<Table>*)(&options);
  241. }
  242. void TextFieldReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textFieldOptions)
  243. {
  244. TextField* textField = static_cast<TextField*>(node);
  245. auto options = (TextFieldOptions*)textFieldOptions;
  246. std::string placeholder = options->placeHolder()->c_str();
  247. textField->setPlaceHolder(placeholder);
  248. std::string text = options->text()->c_str();
  249. bool isLocalized = options->isLocalized() != 0;
  250. if (isLocalized)
  251. {
  252. ILocalizationManager* lm = LocalizationHelper::getCurrentManager();
  253. std::string localizedTxt = lm->getLocalizationString(text);
  254. std::string::size_type newlineIndex = localizedTxt.find("\n");
  255. if (newlineIndex != std::string::npos)
  256. localizedTxt = localizedTxt.substr(0, newlineIndex);
  257. textField->setString(localizedTxt);
  258. }
  259. else
  260. {
  261. textField->setString(text);
  262. }
  263. int fontSize = options->fontSize();
  264. textField->setFontSize(fontSize);
  265. std::string fontName = options->fontName()->c_str();
  266. textField->setFontName(fontName);
  267. bool maxLengthEnabled = options->maxLengthEnabled() != 0;
  268. textField->setMaxLengthEnabled(maxLengthEnabled);
  269. if (maxLengthEnabled)
  270. {
  271. int maxLength = options->maxLength();
  272. textField->setMaxLength(maxLength);
  273. }
  274. bool passwordEnabled = options->passwordEnabled() != 0;
  275. textField->setPasswordEnabled(passwordEnabled);
  276. if (passwordEnabled)
  277. {
  278. std::string passwordStyleText = options->passwordStyleText()->c_str();
  279. textField->setPasswordStyleText(passwordStyleText.c_str());
  280. }
  281. bool fileExist = false;
  282. std::string errorFilePath = "";
  283. auto resourceData = options->fontResource();
  284. std::string path = resourceData->path()->c_str();
  285. if (path != "")
  286. {
  287. if (FileUtils::getInstance()->isFileExist(path))
  288. {
  289. fileExist = true;
  290. }
  291. else
  292. {
  293. errorFilePath = path;
  294. fileExist = false;
  295. }
  296. if (fileExist)
  297. {
  298. textField->setFontName(path);
  299. }
  300. }
  301. auto widgetReader = WidgetReader::getInstance();
  302. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  303. textField->setUnifySizeEnabled(false);
  304. textField->ignoreContentAdaptWithSize(false);
  305. auto widgetOptions = options->widgetOptions();
  306. if (!textField->isIgnoreContentAdaptWithSize())
  307. {
  308. ((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
  309. Size contentSize(widgetOptions->size()->width(), widgetOptions->size()->height());
  310. textField->setContentSize(contentSize);
  311. }
  312. }
  313. Node* TextFieldReader::createNodeWithFlatBuffers(const flatbuffers::Table *textFieldOptions)
  314. {
  315. TextField* textField = TextField::create();
  316. setPropsWithFlatBuffers(textField, (Table*)textFieldOptions);
  317. return textField;
  318. }
  319. }