SpriteReader.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/WidgetReader/SpriteReader/SpriteReader.h"
  21. #include "2d/CCSprite.h"
  22. #include "2d/CCSpriteFrameCache.h"
  23. #include "platform/CCFileUtils.h"
  24. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  25. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  26. #include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
  27. #include "tinyxml2.h"
  28. #include "flatbuffers/flatbuffers.h"
  29. USING_NS_CC;
  30. using namespace flatbuffers;
  31. namespace cocostudio
  32. {
  33. IMPLEMENT_CLASS_NODE_READER_INFO(SpriteReader)
  34. SpriteReader::SpriteReader()
  35. {
  36. }
  37. SpriteReader::~SpriteReader()
  38. {
  39. }
  40. static SpriteReader* _instanceSpriteReader = nullptr;
  41. SpriteReader* SpriteReader::getInstance()
  42. {
  43. if (!_instanceSpriteReader)
  44. {
  45. _instanceSpriteReader = new (std::nothrow) SpriteReader();
  46. }
  47. return _instanceSpriteReader;
  48. }
  49. void SpriteReader::purge()
  50. {
  51. CC_SAFE_DELETE(_instanceSpriteReader);
  52. }
  53. void SpriteReader::destroyInstance()
  54. {
  55. CC_SAFE_DELETE(_instanceSpriteReader);
  56. }
  57. Offset<Table> SpriteReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  58. flatbuffers::FlatBufferBuilder *builder)
  59. {
  60. auto temp = NodeReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  61. auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
  62. std::string path = "";
  63. std::string plistFile = "";
  64. int resourceType = 0;
  65. cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
  66. // FileData
  67. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  68. while (child)
  69. {
  70. std::string name = child->Name();
  71. if (name == "FileData")
  72. {
  73. std::string texture = "";
  74. std::string texturePng = "";
  75. const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
  76. while (attribute)
  77. {
  78. name = attribute->Name();
  79. std::string value = attribute->Value();
  80. if (name == "Path")
  81. {
  82. path = value;
  83. }
  84. else if (name == "Type")
  85. {
  86. resourceType = getResourceType(value);
  87. }
  88. else if (name == "Plist")
  89. {
  90. plistFile = value;
  91. texture = value;
  92. }
  93. attribute = attribute->Next();
  94. }
  95. if (resourceType == 1)
  96. {
  97. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  98. fbs->_textures.push_back(builder->CreateString(texture));
  99. }
  100. }
  101. else if (name == "BlendFunc")
  102. {
  103. const tinyxml2::XMLAttribute* attribute = child->FirstAttribute();
  104. while (attribute)
  105. {
  106. name = attribute->Name();
  107. std::string value = attribute->Value();
  108. if (name == "Src")
  109. {
  110. blendFunc.src = atoi(value.c_str());
  111. }
  112. else if (name == "Dst")
  113. {
  114. blendFunc.dst = atoi(value.c_str());
  115. }
  116. attribute = attribute->Next();
  117. }
  118. }
  119. child = child->NextSiblingElement();
  120. }
  121. flatbuffers::BlendFunc f_blendFunc(blendFunc.src, blendFunc.dst);
  122. auto options = CreateSpriteOptions(*builder,
  123. nodeOptions,
  124. CreateResourceData(*builder,
  125. builder->CreateString(path),
  126. builder->CreateString(plistFile),
  127. resourceType),
  128. &f_blendFunc);
  129. return *(Offset<Table>*)(&options);
  130. }
  131. void SpriteReader::setPropsWithFlatBuffers(cocos2d::Node *node,
  132. const flatbuffers::Table* spriteOptions)
  133. {
  134. Sprite *sprite = static_cast<Sprite*>(node);
  135. auto options = (SpriteOptions*)spriteOptions;
  136. auto nodeReader = NodeReader::getInstance();
  137. nodeReader->setPropsWithFlatBuffers(node, (Table*)(options->nodeOptions()));
  138. auto fileNameData = options->fileNameData();
  139. int resourceType = fileNameData->resourceType();
  140. std::string path = fileNameData->path()->c_str();
  141. std::string errorFilePath = "";
  142. switch (resourceType)
  143. {
  144. case 0:
  145. {
  146. if (FileUtils::getInstance()->isFileExist(path))
  147. {
  148. sprite->setTexture(path);
  149. }
  150. else
  151. {
  152. errorFilePath = path;
  153. }
  154. break;
  155. }
  156. case 1:
  157. {
  158. std::string plist = fileNameData->plistFile()->c_str();
  159. SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(path);
  160. if (spriteFrame)
  161. {
  162. sprite->setSpriteFrame(spriteFrame);
  163. }
  164. else
  165. {
  166. if (FileUtils::getInstance()->isFileExist(plist))
  167. {
  168. ValueMap value = FileUtils::getInstance()->getValueMapFromFile(plist);
  169. ValueMap metadata = value["metadata"].asValueMap();
  170. std::string textureFileName = metadata["textureFileName"].asString();
  171. if (!FileUtils::getInstance()->isFileExist(textureFileName))
  172. {
  173. errorFilePath = textureFileName;
  174. }
  175. }
  176. else
  177. {
  178. errorFilePath = plist;
  179. }
  180. }
  181. break;
  182. }
  183. default:
  184. break;
  185. }
  186. auto f_blendFunc = options->blendFunc();
  187. if (f_blendFunc)
  188. {
  189. cocos2d::BlendFunc blendFunc = cocos2d::BlendFunc::ALPHA_PREMULTIPLIED;
  190. blendFunc.src = f_blendFunc->src();
  191. blendFunc.dst = f_blendFunc->dst();
  192. sprite->setBlendFunc(blendFunc);
  193. }
  194. auto nodeOptions = options->nodeOptions();
  195. GLubyte alpha = (GLubyte)nodeOptions->color()->a();
  196. GLubyte red = (GLubyte)nodeOptions->color()->r();
  197. GLubyte green = (GLubyte)nodeOptions->color()->g();
  198. GLubyte blue = (GLubyte)nodeOptions->color()->b();
  199. if (alpha != 255)
  200. {
  201. sprite->setOpacity(alpha);
  202. }
  203. if (red != 255 || green != 255 || blue != 255)
  204. {
  205. sprite->setColor(Color3B(red, green, blue));
  206. }
  207. bool flipX = nodeOptions->flipX() != 0;
  208. bool flipY = nodeOptions->flipY() != 0;
  209. if(flipX != false)
  210. sprite->setFlippedX(flipX);
  211. if(flipY != false)
  212. sprite->setFlippedY(flipY);
  213. }
  214. Node* SpriteReader::createNodeWithFlatBuffers(const flatbuffers::Table *spriteOptions)
  215. {
  216. Sprite* sprite = Sprite::create();
  217. setPropsWithFlatBuffers(sprite, (Table*)spriteOptions);
  218. return sprite;
  219. }
  220. int SpriteReader::getResourceType(std::string key)
  221. {
  222. if(key == "Normal" || key == "Default")
  223. {
  224. return 0;
  225. }
  226. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  227. if(fbs->_isSimulator)
  228. {
  229. if(key == "MarkedSubImage")
  230. {
  231. return 0;
  232. }
  233. }
  234. return 1;
  235. }
  236. }