CCSSceneReader.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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. #ifndef __CCSSCENEREADER_H__
  21. #define __CCSSCENEREADER_H__
  22. #include "editor-support/cocostudio/DictionaryHelper.h"
  23. #include "editor-support/cocostudio/CocosStudioExport.h"
  24. namespace cocostudio {
  25. class CC_STUDIO_DLL SceneReader
  26. {
  27. public:
  28. enum class AttachComponentType
  29. {
  30. ///parent: Empty Node
  31. /// ComRender(Sprite, Armature, TMXTiledMap, ParticleSystemQuad, GUIComponent)
  32. /// ComAttribute
  33. /// ComAudio
  34. /// ....
  35. EMPTY_NODE,
  36. ///parent: ComRender(Sprite, Armature, TMXTiledMap, ParticleSystemQuad, GUIComponent)
  37. /// ComAttribute
  38. /// ComAudio
  39. /// .....
  40. RENDER_NODE,
  41. /// Default AttachComponentType is _EmptyNode
  42. DEFAULT = EMPTY_NODE,
  43. };
  44. static SceneReader* getInstance();
  45. /**
  46. * @js purge
  47. * @lua destroySceneReader
  48. */
  49. static void destroyInstance();
  50. static const char* sceneReaderVersion();
  51. cocos2d::Node* createNodeWithSceneFile(const std::string &fileName, AttachComponentType attachComponent = AttachComponentType::EMPTY_NODE);
  52. void setTarget(const std::function<void(cocos2d::Ref* obj, void* doc)>& selector);
  53. cocos2d::Node* getNodeByTag(int nTag);
  54. inline AttachComponentType getAttachComponentType(){return _attachComponent;}
  55. CC_CONSTRUCTOR_ACCESS:
  56. SceneReader(void);
  57. virtual ~SceneReader(void);
  58. private:
  59. std::string getComponentClassName(const std::string& name);
  60. cocos2d::Component* createComponent(const std::string classname);
  61. cocos2d::Node* createObject(const rapidjson::Value& dict, cocos2d::Node* parent, AttachComponentType attachComponent);
  62. void setPropertyFromJsonDict(const rapidjson::Value& dict, cocos2d::Node *node);
  63. bool readJson(const std::string &fileName, rapidjson::Document& doc);
  64. cocos2d::Node* createObject(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node* parent, AttachComponentType attachComponent);
  65. void setPropertyFromJsonDict(CocoLoader *cocoLoader, stExpCocoNode *cocoNode, cocos2d::Node *node);
  66. cocos2d::Node* nodeByTag(cocos2d::Node *parent, int tag);
  67. private:
  68. static SceneReader* s_sharedReader;
  69. std::function<void(cocos2d::Ref* obj, void* doc)> _fnSelector;
  70. cocos2d::Node* _node;
  71. AttachComponentType _attachComponent;
  72. };
  73. }
  74. #endif