Node3DReader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/Node3DReader/Node3DReader.h"
  21. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  22. #include "editor-support/cocostudio/CSParse3DBinary_generated.h"
  23. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  24. #include "editor-support/cocostudio/WidgetReader/NodeReader/NodeReader.h"
  25. #include "tinyxml2.h"
  26. #include "flatbuffers/flatbuffers.h"
  27. USING_NS_CC;
  28. using namespace flatbuffers;
  29. namespace cocostudio
  30. {
  31. IMPLEMENT_CLASS_NODE_READER_INFO(Node3DReader)
  32. Node3DReader::Node3DReader()
  33. {
  34. }
  35. Node3DReader::~Node3DReader()
  36. {
  37. }
  38. static Node3DReader* _instanceNode3DReader = nullptr;
  39. Node3DReader* Node3DReader::getInstance()
  40. {
  41. if (!_instanceNode3DReader)
  42. {
  43. _instanceNode3DReader = new (std::nothrow) Node3DReader();
  44. }
  45. return _instanceNode3DReader;
  46. }
  47. void Node3DReader::purge()
  48. {
  49. CC_SAFE_DELETE(_instanceNode3DReader);
  50. }
  51. void Node3DReader::destroyInstance()
  52. {
  53. CC_SAFE_DELETE(_instanceNode3DReader);
  54. }
  55. Vec3 Node3DReader::getVec3Attribute(const tinyxml2::XMLAttribute* attribute) const
  56. {
  57. if(!attribute)
  58. return Vec3::ZERO;
  59. Vec3 ret;
  60. std::string attriname;
  61. while (attribute)
  62. {
  63. attriname = attribute->Name();
  64. std::string value = attribute->Value();
  65. if (attriname == "X")
  66. {
  67. ret.x = atof(value.c_str());
  68. }
  69. else if (attriname == "Y")
  70. {
  71. ret.y = atof(value.c_str());
  72. }
  73. else if(attriname == "Z")
  74. {
  75. ret.z = atof(value.c_str());
  76. }
  77. attribute = attribute->Next();
  78. }
  79. return ret;
  80. }
  81. Offset<Table> Node3DReader::createOptionsWithFlatBuffersForNode(const tinyxml2::XMLElement *objectData,
  82. flatbuffers::FlatBufferBuilder *builder)
  83. {
  84. std::string name = "";
  85. long actionTag = 0;
  86. Vec2 rotationSkew = Vec2::ZERO;
  87. int zOrder = 0;
  88. bool visible = true;
  89. GLubyte alpha = 255;
  90. int tag = 0;
  91. Vec2 position = Vec2::ZERO;
  92. Vec2 scale = Vec2(1.0f, 1.0f);
  93. Vec2 anchorPoint = Vec2::ZERO;
  94. Color4B color(255, 255, 255, 255);
  95. Vec2 size = Vec2::ZERO;
  96. bool flipX = false;
  97. bool flipY = false;
  98. bool ignoreSize = false;
  99. bool touchEnabled = false;
  100. std::string frameEvent = "";
  101. std::string customProperty = "";
  102. bool positionXPercentEnabled = false;
  103. bool positionYPercentEnabled = false;
  104. float positionXPercent = 0;
  105. float positionYPercent = 0;
  106. bool sizeXPercentEnable = false;
  107. bool sizeYPercentEnable = false;
  108. float sizeXPercent = 0;
  109. float sizeYPercent = 0;
  110. bool stretchHorizontalEnabled = false;
  111. bool stretchVerticalEnabled = false;
  112. std::string horizontalEdge;
  113. std::string verticalEdge;
  114. float leftMargin = 0;
  115. float rightMargin = 0;
  116. float topMargin = 0;
  117. float bottomMargin = 0;
  118. // attributes
  119. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  120. while (attribute)
  121. {
  122. std::string attriname = attribute->Name();
  123. std::string value = attribute->Value();
  124. if (attriname == "Name")
  125. {
  126. name = value;
  127. }
  128. else if (attriname == "ActionTag")
  129. {
  130. actionTag = atol(value.c_str());
  131. }
  132. else if (attriname == "VisibleForFrame")
  133. {
  134. visible = (value == "True") ? true : false;
  135. }
  136. else if (attriname == "Alpha")
  137. {
  138. alpha = atoi(value.c_str());
  139. }
  140. else if (attriname == "Tag")
  141. {
  142. tag = atoi(value.c_str());
  143. }
  144. else if (attriname == "UserData")
  145. {
  146. customProperty = value;
  147. }
  148. else if (attriname == "FrameEvent")
  149. {
  150. frameEvent = value;
  151. }
  152. attribute = attribute->Next();
  153. }
  154. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  155. while (child)
  156. {
  157. std::string attriname = child->Name();
  158. if (attriname == "Position3D")
  159. {
  160. attribute = child->FirstAttribute();
  161. while (attribute)
  162. {
  163. attriname = attribute->Name();
  164. std::string value = attribute->Value();
  165. if (attriname == "X")
  166. {
  167. position.x = atof(value.c_str());
  168. }
  169. else if (attriname == "Y")
  170. {
  171. position.y = atof(value.c_str());
  172. }
  173. attribute = attribute->Next();
  174. }
  175. }
  176. else if (attriname == "Scale3D")
  177. {
  178. attribute = child->FirstAttribute();
  179. while (attribute)
  180. {
  181. attriname = attribute->Name();
  182. std::string value = attribute->Value();
  183. if (attriname == "X")
  184. {
  185. scale.x = atof(value.c_str());
  186. }
  187. else if (attriname == "Y")
  188. {
  189. scale.y = atof(value.c_str());
  190. }
  191. attribute = attribute->Next();
  192. }
  193. }
  194. else if (attriname == "CColor")
  195. {
  196. attribute = child->FirstAttribute();
  197. while (attribute)
  198. {
  199. attriname = attribute->Name();
  200. std::string value = attribute->Value();
  201. if (attriname == "A")
  202. {
  203. color.a = atoi(value.c_str());
  204. }
  205. else if (attriname == "R")
  206. {
  207. color.r = atoi(value.c_str());
  208. }
  209. else if (attriname == "G")
  210. {
  211. color.g = atoi(value.c_str());
  212. }
  213. else if (attriname == "B")
  214. {
  215. color.b = atoi(value.c_str());
  216. }
  217. attribute = attribute->Next();
  218. }
  219. }
  220. child = child->NextSiblingElement();
  221. }
  222. RotationSkew f_rotationskew(rotationSkew.x, rotationSkew.y);
  223. Position f_position(position.x, position.y);
  224. Scale f_scale(scale.x, scale.y);
  225. AnchorPoint f_anchortpoint(anchorPoint.x, anchorPoint.y);
  226. Color f_color(color.a, color.r, color.g, color.b);
  227. FlatSize f_size(size.x, size.y);
  228. auto f_layoutComponent = CreateLayoutComponentTable(*builder,
  229. positionXPercentEnabled,
  230. positionYPercentEnabled,
  231. positionXPercent,
  232. positionYPercent,
  233. sizeXPercentEnable,
  234. sizeYPercentEnable,
  235. sizeXPercent,
  236. sizeYPercent,
  237. stretchHorizontalEnabled,
  238. stretchVerticalEnabled,
  239. builder->CreateString(horizontalEdge),
  240. builder->CreateString(verticalEdge),
  241. leftMargin,
  242. rightMargin,
  243. topMargin,
  244. bottomMargin);
  245. auto options = CreateWidgetOptions(*builder,
  246. builder->CreateString(name),
  247. (int)actionTag,
  248. &f_rotationskew,
  249. zOrder,
  250. visible,
  251. alpha,
  252. tag,
  253. &f_position,
  254. &f_scale,
  255. &f_anchortpoint,
  256. &f_color,
  257. &f_size,
  258. flipX,
  259. flipY,
  260. ignoreSize,
  261. touchEnabled,
  262. builder->CreateString(frameEvent),
  263. builder->CreateString(customProperty),
  264. 0,
  265. 0,
  266. f_layoutComponent);
  267. return *(Offset<Table>*)(&options);
  268. }
  269. Offset<Table> Node3DReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  270. flatbuffers::FlatBufferBuilder *builder)
  271. {
  272. auto temp = createOptionsWithFlatBuffersForNode(objectData, builder);
  273. auto nodeOptions = *(Offset<WidgetOptions>*)(&temp);
  274. Vec3 position;
  275. Vec3 rotation;
  276. Vec3 scale;
  277. int cameraMask = 0;
  278. std::string attriname;
  279. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  280. while(attribute)
  281. {
  282. attriname = attribute->Name();
  283. std::string value = attribute->Value();
  284. if(attriname == "CameraFlagMode")
  285. {
  286. cameraMask = atoi(value.c_str());
  287. }
  288. attribute = attribute->Next();
  289. }
  290. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  291. while (child)
  292. {
  293. std::string name = child->Name();
  294. if (name == "Position3D")
  295. {
  296. position = getVec3Attribute(child->FirstAttribute());
  297. }
  298. else if(name == "Rotation3D")
  299. {
  300. rotation = getVec3Attribute(child->FirstAttribute());
  301. }
  302. else if(name == "Scale3D")
  303. {
  304. scale = getVec3Attribute(child->FirstAttribute());
  305. }
  306. child = child->NextSiblingElement();
  307. }
  308. Vector3 position3D(position.x, position.y, position.z);
  309. Vector3 rotation3D(rotation.x, rotation.y, rotation.z);
  310. Vector3 scale3D(scale.x, scale.y, scale.z);
  311. auto options = CreateNode3DOption(*builder,
  312. nodeOptions,
  313. &position3D,
  314. &rotation3D,
  315. &scale3D,
  316. cameraMask
  317. );
  318. return *(Offset<Table>*)(&options);
  319. }
  320. void Node3DReader::setPropsWithFlatBuffers(cocos2d::Node *node,
  321. const flatbuffers::Table* node3DOptions)
  322. {
  323. auto options = (Node3DOption*)node3DOptions;
  324. const Vector3* position = options->position3D();
  325. const Vector3* rotation = options->rotation3D();
  326. const Vector3* scale = options->scale3D();
  327. int cameraMask = options->cameramask();
  328. if(position)
  329. {
  330. node->setPosition3D(Vec3(position->x(), position->y(), position->z()));
  331. }
  332. if(rotation)
  333. {
  334. node->setRotation3D(Vec3(rotation->x(), rotation->y(), rotation->z()));
  335. }
  336. if(scale)
  337. {
  338. node->setScaleX(scale->x());
  339. node->setScaleY(scale->y());
  340. node->setScaleZ(scale->z());
  341. }
  342. node->setCameraMask(cameraMask, true);
  343. auto nodeReader = NodeReader::getInstance();
  344. nodeReader->setPropsWithFlatBuffers(node, (Table*)(options->nodeOptions()));
  345. }
  346. Node* Node3DReader::createNodeWithFlatBuffers(const flatbuffers::Table *node3DOptions)
  347. {
  348. Node* node = Node::create();
  349. setPropsWithFlatBuffers(node, (Table*)node3DOptions);
  350. return node;
  351. }
  352. }