ImageViewReader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include "editor-support/cocostudio/WidgetReader/ImageViewReader/ImageViewReader.h"
  2. #include "ui/UIImageView.h"
  3. #include "platform/CCFileUtils.h"
  4. #include "2d/CCSpriteFrame.h"
  5. #include "2d/CCSpriteFrameCache.h"
  6. #include "editor-support/cocostudio/CocoLoader.h"
  7. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  8. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  9. #include "tinyxml2.h"
  10. #include "flatbuffers/flatbuffers.h"
  11. USING_NS_CC;
  12. using namespace ui;
  13. using namespace flatbuffers;
  14. namespace cocostudio
  15. {
  16. static const char* P_Scale9Enable = "scale9Enable";
  17. static const char* P_FileNameData = "fileNameData";
  18. static const char* P_CapInsetsX = "capInsetsX";
  19. static const char* P_CapInsetsY = "capInsetsY";
  20. static const char* P_CapInsetsWidth = "capInsetsWidth";
  21. static const char* P_CapInsetsHeight = "capInsetsHeight";
  22. static const char* P_Scale9Width = "scale9Width";
  23. static const char* P_Scale9Height = "scale9Height";
  24. static ImageViewReader* instanceImageViewReader = nullptr;
  25. IMPLEMENT_CLASS_NODE_READER_INFO(ImageViewReader)
  26. ImageViewReader::ImageViewReader()
  27. {
  28. }
  29. ImageViewReader::~ImageViewReader()
  30. {
  31. }
  32. ImageViewReader* ImageViewReader::getInstance()
  33. {
  34. if (!instanceImageViewReader)
  35. {
  36. instanceImageViewReader = new (std::nothrow) ImageViewReader();
  37. }
  38. return instanceImageViewReader;
  39. }
  40. void ImageViewReader::destroyInstance()
  41. {
  42. CC_SAFE_DELETE(instanceImageViewReader);
  43. }
  44. void ImageViewReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
  45. {
  46. WidgetReader::setPropsFromBinary(widget, cocoLoader, cocoNode);
  47. ImageView* imageView = static_cast<ImageView*>(widget);
  48. this->beginSetBasicProperties(widget);
  49. float capsx = 0.0f, capsy = 0.0, capsWidth = 0.0, capsHeight = 0.0f;
  50. stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
  51. for (int i = 0; i < cocoNode->GetChildNum(); ++i) {
  52. std::string key = stChildArray[i].GetName(cocoLoader);
  53. std::string value = stChildArray[i].GetValue(cocoLoader);
  54. //read all basic properties of widget
  55. CC_BASIC_PROPERTY_BINARY_READER
  56. //read all color related properties of widget
  57. CC_COLOR_PROPERTY_BINARY_READER
  58. else if (key == P_Scale9Enable) {
  59. imageView->setScale9Enabled(valueToBool(value));
  60. }
  61. else if (key == P_FileNameData){
  62. stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader);
  63. std::string resType = backGroundChildren[2].GetValue(cocoLoader);
  64. Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType);
  65. std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType);
  66. imageView->loadTexture(backgroundValue, imageFileNameType);
  67. }
  68. else if(key == P_Scale9Width){
  69. imageView->setContentSize(Size(valueToFloat(value), imageView->getContentSize().height));
  70. }else if(key == P_Scale9Height){
  71. imageView->setContentSize(Size(imageView->getContentSize().width, valueToFloat(value)));
  72. }
  73. else if(key == P_CapInsetsX){
  74. capsx = valueToFloat(value);
  75. }else if(key == P_CapInsetsY){
  76. capsy = valueToFloat(value);
  77. }else if(key == P_CapInsetsWidth){
  78. capsWidth = valueToFloat(value);
  79. }else if(key == P_CapInsetsHeight){
  80. capsHeight = valueToFloat(value);
  81. }
  82. } //end of for loop
  83. if (imageView->isScale9Enabled()) {
  84. imageView->setCapInsets(Rect(capsx, capsy, capsWidth, capsHeight));
  85. }
  86. this->endSetBasicProperties(widget);
  87. }
  88. void ImageViewReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
  89. {
  90. WidgetReader::setPropsFromJsonDictionary(widget, options);
  91. ImageView* imageView = static_cast<ImageView*>(widget);
  92. const rapidjson::Value& imageFileNameDic = DICTOOL->getSubDictionary_json(options, P_FileNameData);
  93. int imageFileNameType = DICTOOL->getIntValue_json(imageFileNameDic, P_ResourceType);
  94. const std::string& imageFilePath = DICTOOL->getStringValue_json(imageFileNameDic, P_Path);
  95. if (!imageFilePath.empty()) {
  96. std::string imageFileName = this->getResourcePath(imageFileNameDic, P_Path, (Widget::TextureResType)imageFileNameType);
  97. imageView->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  98. }
  99. bool scale9EnableExist = DICTOOL->checkObjectExist_json(options, P_Scale9Enable);
  100. bool scale9Enable = false;
  101. if (scale9EnableExist)
  102. {
  103. scale9Enable = DICTOOL->getBooleanValue_json(options, P_Scale9Enable);
  104. }
  105. imageView->setScale9Enabled(scale9Enable);
  106. if (scale9Enable)
  107. {
  108. float swf = DICTOOL->getFloatValue_json(options, P_Scale9Width,80.0f);
  109. float shf = DICTOOL->getFloatValue_json(options, P_Scale9Height,80.0f);
  110. imageView->setContentSize(Size(swf, shf));
  111. float cx = DICTOOL->getFloatValue_json(options, P_CapInsetsX);
  112. float cy = DICTOOL->getFloatValue_json(options, P_CapInsetsY);
  113. float cw = DICTOOL->getFloatValue_json(options, P_CapInsetsWidth,1.0f);
  114. float ch = DICTOOL->getFloatValue_json(options, P_CapInsetsHeight,1.0f);
  115. imageView->setCapInsets(Rect(cx, cy, cw, ch));
  116. }
  117. WidgetReader::setColorPropsFromJsonDictionary(widget, options);
  118. }
  119. Offset<Table> ImageViewReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  120. flatbuffers::FlatBufferBuilder *builder)
  121. {
  122. auto temp = WidgetReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  123. auto widgetOptions = *(Offset<WidgetOptions>*)(&temp);
  124. bool scale9Enabled = false;
  125. Rect capInsets;
  126. cocos2d::Size scale9Size;
  127. std::string path = "";
  128. std::string plistFile = "";
  129. int resourceType = 0;
  130. // attributes
  131. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  132. while (attribute)
  133. {
  134. std::string name = attribute->Name();
  135. std::string value = attribute->Value();
  136. if (name == "Scale9Enable")
  137. {
  138. if (value == "True")
  139. {
  140. scale9Enabled = true;
  141. }
  142. }
  143. else if (name == "Scale9OriginX")
  144. {
  145. capInsets.origin.x = atof(value.c_str());
  146. }
  147. else if (name == "Scale9OriginY")
  148. {
  149. capInsets.origin.y = atof(value.c_str());
  150. }
  151. else if (name == "Scale9Width")
  152. {
  153. capInsets.size.width = atof(value.c_str());
  154. }
  155. else if (name == "Scale9Height")
  156. {
  157. capInsets.size.height = atof(value.c_str());
  158. }
  159. attribute = attribute->Next();
  160. }
  161. // child elements
  162. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  163. while (child)
  164. {
  165. std::string name = child->Name();
  166. if (name == "Size" && scale9Enabled)
  167. {
  168. attribute = child->FirstAttribute();
  169. while (attribute)
  170. {
  171. name = attribute->Name();
  172. std::string value = attribute->Value();
  173. if (name == "X")
  174. {
  175. scale9Size.width = atof(value.c_str());
  176. }
  177. else if (name == "Y")
  178. {
  179. scale9Size.height = atof(value.c_str());
  180. }
  181. attribute = attribute->Next();
  182. }
  183. }
  184. else if (name == "FileData")
  185. {
  186. std::string texture = "";
  187. std::string texturePng = "";
  188. attribute = child->FirstAttribute();
  189. while (attribute)
  190. {
  191. name = attribute->Name();
  192. std::string value = attribute->Value();
  193. if (name == "Path")
  194. {
  195. path = value;
  196. }
  197. else if (name == "Type")
  198. {
  199. resourceType = getResourceType(value);
  200. }
  201. else if (name == "Plist")
  202. {
  203. plistFile = value;
  204. texture = value;
  205. }
  206. attribute = attribute->Next();
  207. }
  208. if (resourceType == 1)
  209. {
  210. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  211. fbs->_textures.push_back(builder->CreateString(texture));
  212. }
  213. }
  214. child = child->NextSiblingElement();
  215. }
  216. CapInsets f_capInsets(capInsets.origin.x, capInsets.origin.y, capInsets.size.width, capInsets.size.height);
  217. FlatSize f_scale9Size(scale9Size.width, scale9Size.height);
  218. auto options = CreateImageViewOptions(*builder,
  219. widgetOptions,
  220. CreateResourceData(*builder,
  221. builder->CreateString(path),
  222. builder->CreateString(plistFile),
  223. resourceType),
  224. &f_capInsets,
  225. &f_scale9Size,
  226. scale9Enabled);
  227. return *(Offset<Table>*)(&options);
  228. }
  229. void ImageViewReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *imageViewOptions)
  230. {
  231. ImageView* imageView = static_cast<ImageView*>(node);
  232. auto options = (ImageViewOptions*)imageViewOptions;
  233. bool fileExist = false;
  234. std::string errorFilePath = "";
  235. auto imageFileNameDic = options->fileNameData();
  236. int imageFileNameType = imageFileNameDic->resourceType();
  237. std::string imageFileName = imageFileNameDic->path()->c_str();
  238. switch (imageFileNameType)
  239. {
  240. case 0:
  241. {
  242. if (FileUtils::getInstance()->isFileExist(imageFileName))
  243. {
  244. fileExist = true;
  245. }
  246. else if(SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName))
  247. {
  248. fileExist = true;
  249. imageFileNameType = 1;
  250. }
  251. else
  252. {
  253. errorFilePath = imageFileName;
  254. fileExist = false;
  255. }
  256. break;
  257. }
  258. case 1:
  259. {
  260. std::string plist = imageFileNameDic->plistFile()->c_str();
  261. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(imageFileName);
  262. if (spriteFrame)
  263. {
  264. fileExist = true;
  265. }
  266. else
  267. {
  268. if (FileUtils::getInstance()->isFileExist(plist))
  269. {
  270. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  271. ValueMap metadata = value["metadata"].asValueMap();
  272. std::string textureFileName = metadata["textureFileName"].asString();
  273. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  274. {
  275. errorFilePath = textureFileName;
  276. }
  277. }
  278. else
  279. {
  280. errorFilePath = plist;
  281. }
  282. fileExist = false;
  283. }
  284. break;
  285. }
  286. default:
  287. break;
  288. }
  289. if (fileExist)
  290. {
  291. imageView->loadTexture(imageFileName, (Widget::TextureResType)imageFileNameType);
  292. }
  293. bool scale9Enabled = options->scale9Enabled() != 0;
  294. imageView->setScale9Enabled(scale9Enabled);
  295. auto widgetReader = WidgetReader::getInstance();
  296. widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
  297. if (scale9Enabled)
  298. {
  299. imageView->setUnifySizeEnabled(false);
  300. imageView->ignoreContentAdaptWithSize(false);
  301. auto f_scale9Size = options->scale9Size();
  302. Size scale9Size(f_scale9Size->width(), f_scale9Size->height());
  303. imageView->setContentSize(scale9Size);
  304. auto f_capInset = options->capInsets();
  305. Rect capInsets(f_capInset->x(), f_capInset->y(), f_capInset->width(), f_capInset->height());
  306. imageView->setCapInsets(capInsets);
  307. }
  308. else
  309. {
  310. Size contentSize(options->widgetOptions()->size()->width(), options->widgetOptions()->size()->height());
  311. imageView->setContentSize(contentSize);
  312. }
  313. }
  314. Node* ImageViewReader::createNodeWithFlatBuffers(const flatbuffers::Table *imageViewOptions)
  315. {
  316. ImageView* imageView = ImageView::create();
  317. setPropsWithFlatBuffers(imageView, (Table*)imageViewOptions);
  318. return imageView;
  319. }
  320. int ImageViewReader::getResourceType(std::string key)
  321. {
  322. if(key == "Normal" || key == "Default")
  323. {
  324. return 0;
  325. }
  326. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  327. if(fbs->_isSimulator)
  328. {
  329. if(key == "MarkedSubImage")
  330. {
  331. return 0;
  332. }
  333. }
  334. return 1;
  335. }
  336. }