UserCameraReader.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "base/CCDirector.h"
  21. #include "2d/CCCamera.h"
  22. #include "platform/CCFileUtils.h"
  23. #include "editor-support/cocostudio/WidgetReader/UserCameraReader/UserCameraReader.h"
  24. #include "editor-support/cocostudio/CSParseBinary_generated.h"
  25. #include "editor-support/cocostudio/CSParse3DBinary_generated.h"
  26. #include "editor-support/cocostudio/FlatBuffersSerialize.h"
  27. #include "editor-support/cocostudio/WidgetReader/Node3DReader/Node3DReader.h"
  28. #include "editor-support/cocostudio/WidgetReader/GameNode3DReader/GameNode3DReader.h"
  29. #include "tinyxml2.h"
  30. #include "flatbuffers/flatbuffers.h"
  31. USING_NS_CC;
  32. using namespace flatbuffers;
  33. namespace cocostudio
  34. {
  35. IMPLEMENT_CLASS_NODE_READER_INFO(UserCameraReader)
  36. UserCameraReader::UserCameraReader()
  37. {
  38. }
  39. UserCameraReader::~UserCameraReader()
  40. {
  41. }
  42. static UserCameraReader* _instanceUserCameraReader = nullptr;
  43. UserCameraReader* UserCameraReader::getInstance()
  44. {
  45. if (!_instanceUserCameraReader)
  46. {
  47. _instanceUserCameraReader = new (std::nothrow) UserCameraReader();
  48. }
  49. return _instanceUserCameraReader;
  50. }
  51. void UserCameraReader::purge()
  52. {
  53. CC_SAFE_DELETE(_instanceUserCameraReader);
  54. }
  55. void UserCameraReader::destroyInstance()
  56. {
  57. CC_SAFE_DELETE(_instanceUserCameraReader);
  58. }
  59. Vec2 UserCameraReader::getVec2Attribute(const tinyxml2::XMLAttribute* attribute) const
  60. {
  61. if(!attribute)
  62. return Vec2::ZERO;
  63. Vec2 ret;
  64. std::string attriname;
  65. while (attribute)
  66. {
  67. attriname = attribute->Name();
  68. std::string value = attribute->Value();
  69. if (attriname == "X")
  70. {
  71. ret.x = atof(value.c_str());
  72. }
  73. else if (attriname == "Y")
  74. {
  75. ret.y = atof(value.c_str());
  76. }
  77. attribute = attribute->Next();
  78. }
  79. return ret;
  80. }
  81. Offset<Table> UserCameraReader::createOptionsWithFlatBuffers(const tinyxml2::XMLElement *objectData,
  82. flatbuffers::FlatBufferBuilder *builder)
  83. {
  84. auto temp = Node3DReader::getInstance()->createOptionsWithFlatBuffers(objectData, builder);
  85. auto node3DOptions = *(Offset<Node3DOption>*)(&temp);
  86. float fov = 60.f;
  87. unsigned int cameraFlag = 0;
  88. bool skyBoxEnabled = false;
  89. bool skyBoxValid = true;
  90. std::string attriname;
  91. const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute();
  92. while(attribute)
  93. {
  94. attriname = attribute->Name();
  95. std::string value = attribute->Value();
  96. if(attriname == "Fov")
  97. {
  98. fov = atof(value.c_str());
  99. }
  100. else if(attriname == "UserCameraFlagMode")
  101. {
  102. if (cameraFlag == 0)
  103. {
  104. if (value == "DEFAULT") cameraFlag = 1;
  105. else if (value == "USER1") cameraFlag = 1 << 1;
  106. else if (value == "USER2") cameraFlag = 1 << 2;
  107. else if (value == "USER3") cameraFlag = 1 << 3;
  108. else if (value == "USER4") cameraFlag = 1 << 4;
  109. }
  110. }
  111. else if (attriname == "CameraFlagData")
  112. {
  113. int flag = atoi(value.c_str());
  114. if (flag != 0)
  115. cameraFlag = flag;
  116. }
  117. else if (attriname == "SkyBoxEnabled")
  118. {
  119. skyBoxEnabled = (value == "True") ? true : false;
  120. }
  121. else if (attriname == "SkyBoxValid")
  122. {
  123. skyBoxValid = (value == "True") ? true : false;
  124. }
  125. attribute = attribute->Next();
  126. }
  127. if (!skyBoxValid)
  128. skyBoxEnabled = false;
  129. Vec2 clipPlane(1, 1000);
  130. std::string leftPath = "";
  131. std::string leftPlistFile = "";
  132. int leftResourceType = 0;
  133. std::string rightPath = "";
  134. std::string rightPlistFile = "";
  135. int rightResourceType = 0;
  136. std::string upPath = "";
  137. std::string upPlistFile = "";
  138. int upResourceType = 0;
  139. std::string downPath = "";
  140. std::string downPlistFile = "";
  141. int downResourceType = 0;
  142. std::string forwardPath = "";
  143. std::string forwardPlistFile = "";
  144. int forwardResourceType = 0;
  145. std::string backPath = "";
  146. std::string backPlistFile = "";
  147. int backResourceType = 0;
  148. // FileData
  149. const tinyxml2::XMLElement* child = objectData->FirstChildElement();
  150. while (child)
  151. {
  152. std::string name = child->Name();
  153. if (name == "ClipPlane")
  154. {
  155. attribute = child->FirstAttribute();
  156. clipPlane = getVec2Attribute(attribute);
  157. }
  158. else if (name == "LeftImage")
  159. {
  160. attribute = child->FirstAttribute();
  161. while (attribute)
  162. {
  163. name = attribute->Name();
  164. std::string value = attribute->Value();
  165. if (name == "Path")
  166. {
  167. leftPath = value;
  168. }
  169. else if (name == "Type")
  170. {
  171. leftResourceType = getResourceType(value);
  172. }
  173. else if (name == "Plist")
  174. {
  175. leftPlistFile = value;
  176. }
  177. attribute = attribute->Next();
  178. }
  179. if (leftResourceType == 1)
  180. {
  181. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  182. fbs->_textures.push_back(builder->CreateString(leftPlistFile));
  183. }
  184. }
  185. else if (name == "RightImage")
  186. {
  187. attribute = child->FirstAttribute();
  188. while (attribute)
  189. {
  190. name = attribute->Name();
  191. std::string value = attribute->Value();
  192. if (name == "Path")
  193. {
  194. rightPath = value;
  195. }
  196. else if (name == "Type")
  197. {
  198. rightResourceType = getResourceType(value);
  199. }
  200. else if (name == "Plist")
  201. {
  202. rightPlistFile = value;
  203. }
  204. attribute = attribute->Next();
  205. }
  206. if (rightResourceType == 1)
  207. {
  208. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  209. fbs->_textures.push_back(builder->CreateString(rightPlistFile));
  210. }
  211. }
  212. else if (name == "UpImage")
  213. {
  214. attribute = child->FirstAttribute();
  215. while (attribute)
  216. {
  217. name = attribute->Name();
  218. std::string value = attribute->Value();
  219. if (name == "Path")
  220. {
  221. upPath = value;
  222. }
  223. else if (name == "Type")
  224. {
  225. upResourceType = getResourceType(value);
  226. }
  227. else if (name == "Plist")
  228. {
  229. upPlistFile = value;
  230. }
  231. attribute = attribute->Next();
  232. }
  233. if (upResourceType == 1)
  234. {
  235. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  236. fbs->_textures.push_back(builder->CreateString(upPlistFile));
  237. }
  238. }
  239. else if (name == "DownImage")
  240. {
  241. attribute = child->FirstAttribute();
  242. while (attribute)
  243. {
  244. name = attribute->Name();
  245. std::string value = attribute->Value();
  246. if (name == "Path")
  247. {
  248. downPath = value;
  249. }
  250. else if (name == "Type")
  251. {
  252. downResourceType = getResourceType(value);
  253. }
  254. else if (name == "Plist")
  255. {
  256. downPlistFile = value;
  257. }
  258. attribute = attribute->Next();
  259. }
  260. if (downResourceType == 1)
  261. {
  262. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  263. fbs->_textures.push_back(builder->CreateString(downPlistFile));
  264. }
  265. }
  266. else if (name == "ForwardImage")
  267. {
  268. attribute = child->FirstAttribute();
  269. while (attribute)
  270. {
  271. name = attribute->Name();
  272. std::string value = attribute->Value();
  273. if (name == "Path")
  274. {
  275. forwardPath = value;
  276. }
  277. else if (name == "Type")
  278. {
  279. forwardResourceType = getResourceType(value);
  280. }
  281. else if (name == "Plist")
  282. {
  283. forwardPlistFile = value;
  284. }
  285. attribute = attribute->Next();
  286. }
  287. if (forwardResourceType == 1)
  288. {
  289. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  290. fbs->_textures.push_back(builder->CreateString(forwardPlistFile));
  291. }
  292. }
  293. else if (name == "BackImage")
  294. {
  295. attribute = child->FirstAttribute();
  296. while (attribute)
  297. {
  298. name = attribute->Name();
  299. std::string value = attribute->Value();
  300. if (name == "Path")
  301. {
  302. backPath = value;
  303. }
  304. else if (name == "Type")
  305. {
  306. backResourceType = getResourceType(value);
  307. }
  308. else if (name == "Plist")
  309. {
  310. backPlistFile = value;
  311. }
  312. attribute = attribute->Next();
  313. }
  314. if (backResourceType == 1)
  315. {
  316. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  317. fbs->_textures.push_back(builder->CreateString(backPlistFile));
  318. }
  319. }
  320. child = child->NextSiblingElement();
  321. }
  322. auto options = CreateUserCameraOptions(*builder,
  323. node3DOptions,
  324. fov,
  325. clipPlane.x,
  326. clipPlane.y,
  327. cameraFlag,
  328. skyBoxEnabled,
  329. CreateResourceData(*builder,
  330. builder->CreateString(leftPath),
  331. builder->CreateString(leftPlistFile),
  332. leftResourceType),
  333. CreateResourceData(*builder,
  334. builder->CreateString(rightPath),
  335. builder->CreateString(rightPlistFile),
  336. rightResourceType),
  337. CreateResourceData(*builder,
  338. builder->CreateString(upPath),
  339. builder->CreateString(upPlistFile),
  340. upResourceType),
  341. CreateResourceData(*builder,
  342. builder->CreateString(downPath),
  343. builder->CreateString(downPlistFile),
  344. downResourceType),
  345. CreateResourceData(*builder,
  346. builder->CreateString(forwardPath),
  347. builder->CreateString(forwardPlistFile),
  348. forwardResourceType),
  349. CreateResourceData(*builder,
  350. builder->CreateString(backPath),
  351. builder->CreateString(backPlistFile),
  352. backResourceType)
  353. );
  354. return *(Offset<Table>*)(&options);
  355. }
  356. void UserCameraReader::setPropsWithFlatBuffers(cocos2d::Node *node,
  357. const flatbuffers::Table* userCameraDOptions)
  358. {
  359. auto options = (UserCameraOptions*)userCameraDOptions;
  360. Camera* camera = static_cast<Camera*>(node);
  361. int cameraFlag = options->cameraFlag();
  362. camera->setCameraFlag((CameraFlag)cameraFlag);
  363. auto node3DReader = Node3DReader::getInstance();
  364. node3DReader->setPropsWithFlatBuffers(node, (Table*)(options->node3DOption()));
  365. bool skyBoxEnabled = options->skyBoxEnabled() != 0;
  366. if (skyBoxEnabled)
  367. {
  368. std::string leftFileData = options->leftFileData()->path()->c_str();
  369. std::string rightFileData = options->rightFileData()->path()->c_str();
  370. std::string upFileData = options->upFileData()->path()->c_str();
  371. std::string downFileData = options->downFileData()->path()->c_str();
  372. std::string forwardFileData = options->forwardFileData()->path()->c_str();
  373. std::string backFileData = options->backFileData()->path()->c_str();
  374. FileUtils *fileUtils = FileUtils::getInstance();
  375. if (fileUtils->isFileExist(leftFileData)
  376. && fileUtils->isFileExist(rightFileData)
  377. && fileUtils->isFileExist(upFileData)
  378. && fileUtils->isFileExist(downFileData)
  379. && fileUtils->isFileExist(forwardFileData)
  380. && fileUtils->isFileExist(backFileData))
  381. {
  382. CameraBackgroundSkyBoxBrush* brush = CameraBackgroundSkyBoxBrush::create(leftFileData, rightFileData, upFileData, downFileData, forwardFileData, backFileData);
  383. camera->setBackgroundBrush(brush);
  384. }
  385. else
  386. {
  387. if (GameNode3DReader::getSceneBrushInstance() != nullptr)
  388. camera->setBackgroundBrush(GameNode3DReader::getSceneBrushInstance());
  389. }
  390. }
  391. else
  392. {
  393. if (GameNode3DReader::getSceneBrushInstance() != nullptr)
  394. camera->setBackgroundBrush(GameNode3DReader::getSceneBrushInstance());
  395. }
  396. }
  397. Node* UserCameraReader::createNodeWithFlatBuffers(const flatbuffers::Table *userCameraDOptions)
  398. {
  399. auto options = (UserCameraOptions*)userCameraDOptions;
  400. float fov = options->fov();
  401. float nearClip = options->nearClip();
  402. float farClip = options->farClip();
  403. auto size = Director::getInstance()->getWinSize();
  404. Camera* camera = Camera::createPerspective(fov, size.width / size.height, nearClip, farClip);
  405. setPropsWithFlatBuffers(camera, userCameraDOptions);
  406. return camera;
  407. }
  408. int UserCameraReader::getResourceType(std::string key)
  409. {
  410. if (key == "Normal" || key == "Default")
  411. {
  412. return 0;
  413. }
  414. FlatBuffersSerialize* fbs = FlatBuffersSerialize::getInstance();
  415. if (fbs->_isSimulator)
  416. {
  417. if (key == "MarkedSubImage")
  418. {
  419. return 0;
  420. }
  421. }
  422. return 1;
  423. }
  424. }