Sprite3DReader.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "2d/CCLight.h"
  21. #include "3d/CCSprite3D.h"
  22. #include "3d/CCAnimate3D.h"
  23. #include "3d/CCAnimation3D.h"
  24. #include "platform/CCFileUtils.h"
  25. #include "editor-support/cocostudio/WidgetReader/Sprite3DReader/Sprite3DReader.h"
  26. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  27. #include "editor-support/cocostudio/CSParse3DBinary_generated.h"
  28. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  29. #include "editor-support/cocostudio/WidgetReader/Node3DReader/Node3DReader.h"
  30. #include "tinyxml2.h"
  31. #include "flatbuffers/flatbuffers.h"
  32. USING_NS_CC;
  33. using namespace flatbuffers;
  34. namespace cocostudio
  35. {
  36. IMPLEMENT_CLASS_NODE_READER_INFO(Sprite3DReader)
  37. Sprite3DReader::Sprite3DReader()
  38. {
  39. }
  40. Sprite3DReader::~Sprite3DReader()
  41. {
  42. }
  43. static Sprite3DReader* _instanceSprite3DReader = nullptr;
  44. Sprite3DReader* Sprite3DReader::getInstance()
  45. {
  46. if (!_instanceSprite3DReader)
  47. {
  48. _instanceSprite3DReader = new (std::nothrow) Sprite3DReader();
  49. }
  50. return _instanceSprite3DReader;
  51. }
  52. void Sprite3DReader::purge()
  53. {
  54. CC_SAFE_DELETE(_instanceSprite3DReader);
  55. }
  56. void Sprite3DReader::destroyInstance()
  57. {
  58. CC_SAFE_DELETE(_instanceSprite3DReader);
  59. }
  60. Vec2 Sprite3DReader::getVec2Attribute(const tinyxml2::XMLAttribute* attribute) const
  61. {
  62. if(!attribute)
  63. return Vec2::ZERO;
  64. Vec2 ret;
  65. std::string attriname;
  66. while (attribute)
  67. {
  68. attriname = attribute->Name();
  69. std::string value = attribute->Value();
  70. if (attriname == "X")
  71. {
  72. ret.x = atof(value.c_str());
  73. }
  74. else if (attriname == "Y")
  75. {
  76. ret.y = atof(value.c_str());
  77. }
  78. attribute = attribute->Next();
  79. }
  80. return ret;
  81. }
  82. Offset<Table> Sprite3DReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  83. flatbuffers::FlatBufferBuilder *builder)
  84. {
  85. auto temp = Node3DReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  86. auto node3DOptions = *(Offset<Node3DOption>*)(&temp);
  87. bool runAction = false;
  88. std::string path;
  89. int resourceType = 0;
  90. bool isFlipped = false;
  91. int lightFlag = 0;
  92. std::string attriname;
  93. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  94. while(attribute)
  95. {
  96. attriname = attribute->Name();
  97. std::string value = attribute->Value();
  98. if(attriname == "RunAction3D")
  99. {
  100. runAction = value == "True" ? true : false;
  101. }
  102. else if (attriname == "IsFlipped")
  103. {
  104. isFlipped = value == "True" ? true : false;
  105. }
  106. else if (attriname == "LightFlag")
  107. {
  108. if (value == "LIGHT0") lightFlag = (int)LightFlag::LIGHT0;
  109. else if (value == "LIGHT1") lightFlag = (int)LightFlag::LIGHT1;
  110. else if (value == "LIGHT2") lightFlag = (int)LightFlag::LIGHT2;
  111. else if (value == "LIGHT3") lightFlag = (int)LightFlag::LIGHT3;
  112. else if (value == "LIGHT4") lightFlag = (int)LightFlag::LIGHT4;
  113. else if (value == "LIGHT5") lightFlag = (int)LightFlag::LIGHT5;
  114. else if (value == "LIGHT6") lightFlag = (int)LightFlag::LIGHT6;
  115. else if (value == "LIGHT7") lightFlag = (int)LightFlag::LIGHT7;
  116. else if (value == "LIGHT8") lightFlag = (int)LightFlag::LIGHT8;
  117. else if (value == "LIGHT9") lightFlag = (int)LightFlag::LIGHT9;
  118. else if (value == "LIGHT10") lightFlag = (int)LightFlag::LIGHT10;
  119. else if (value == "LIGHT11") lightFlag = (int)LightFlag::LIGHT11;
  120. else if (value == "LIGHT12") lightFlag = (int)LightFlag::LIGHT12;
  121. }
  122. attribute = attribute->Next();
  123. }
  124. // FileData
  125. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  126. while (child)
  127. {
  128. std::string name = child->Name();
  129. if (name == "FileData")
  130. {
  131. attribute = child->FirstAttribute();
  132. while (attribute)
  133. {
  134. name = attribute->Name();
  135. std::string value = attribute->Value();
  136. if (name == "Path")
  137. {
  138. path = value;
  139. }
  140. else if (name == "Type")
  141. {
  142. if(value == "Normal")
  143. {
  144. resourceType = 1;
  145. }
  146. else if(value == "Default")
  147. {
  148. resourceType = 0;
  149. }
  150. }
  151. attribute = attribute->Next();
  152. }
  153. }
  154. child = child->NextSiblingElement();
  155. }
  156. auto options = CreateSprite3DOptions(*builder,
  157. node3DOptions,
  158. CreateResourceData(*builder,
  159. builder->CreateString(path),
  160. builder->CreateString(""),
  161. resourceType),
  162. runAction,
  163. isFlipped,
  164. lightFlag
  165. );
  166. return *(Offset<Table>*)(&options);
  167. }
  168. void Sprite3DReader::setPropsWithFlatBuffers(cocos2d::Node *node,
  169. const flatbuffers::Table* sprite3DOptions)
  170. {
  171. Sprite3D* sprite3D = static_cast<Sprite3D*>(node);
  172. auto options = (Sprite3DOptions*)sprite3DOptions;
  173. int lightFlag = options->lightFlag();
  174. bool runAction = options->runAction() != 0;
  175. bool isFlipped = options->isFlipped() != 0;
  176. auto fileData = options->fileData();
  177. std::string path = fileData->path()->c_str();
  178. if(runAction && FileUtils::getInstance()->isFileExist(path))
  179. {
  180. Animation3D* animation = Animation3D::create(path);
  181. if(animation)
  182. {
  183. Animate3D* animate = Animate3D::create(animation);
  184. Action* action = RepeatForever::create(animate);
  185. sprite3D->runAction(action);
  186. }
  187. }
  188. auto nodeOptions = options->node3DOption()->nodeOptions();
  189. GLubyte alpha = (GLubyte)nodeOptions->color()->a();
  190. GLubyte red = (GLubyte)nodeOptions->color()->r();
  191. GLubyte green = (GLubyte)nodeOptions->color()->g();
  192. GLubyte blue = (GLubyte)nodeOptions->color()->b();
  193. if (alpha != 255)
  194. {
  195. sprite3D->setOpacity(alpha);
  196. }
  197. if (red != 255 || green != 255 || blue != 255)
  198. {
  199. sprite3D->setColor(Color3B(red, green, blue));
  200. }
  201. if (isFlipped)
  202. {
  203. sprite3D->setCullFaceEnabled(true);
  204. sprite3D->setCullFace(GL_FRONT);
  205. }
  206. if (lightFlag <= 0)
  207. {
  208. lightFlag = 1;
  209. }
  210. sprite3D->setLightMask(lightFlag);
  211. auto node3DReader = Node3DReader::getInstance();
  212. node3DReader->setPropsWithFlatBuffers(sprite3D, (Table*)(options->node3DOption()));
  213. }
  214. Node* Sprite3DReader::createNodeWithFlatBuffers(const flatbuffers::Table *sprite3DOptions)
  215. {
  216. auto options = (Sprite3DOptions*)sprite3DOptions;
  217. auto fileData = options->fileData();
  218. std::string path = fileData->path()->c_str();
  219. Sprite3D* ret = Sprite3D::create();
  220. if(FileUtils::getInstance()->isFileExist(path))
  221. {
  222. ret->initWithFile(path);
  223. }
  224. setPropsWithFlatBuffers(ret, sprite3DOptions);
  225. return ret;
  226. }
  227. }