CSLoader.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /****************************************************************************
  2. Copyright (c) 2013 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. #ifndef __cocos2d_libs__CSLoader__
  21. #define __cocos2d_libs__CSLoader__
  22. #include "editor-support/cocostudio/DictionaryHelper.h"
  23. #include "editor-support/cocostudio/CocosStudioExport.h"
  24. #include "base/ObjectFactory.h"
  25. #include "base/CCData.h"
  26. #include "ui/UIWidget.h"
  27. namespace flatbuffers
  28. {
  29. class FlatBufferBuilder;
  30. struct NodeTree;
  31. struct WidgetOptions;
  32. struct SingleNodeOptions;
  33. struct SpriteOptions;
  34. struct ParticleSystemOptions;
  35. struct TMXTiledMapOptions;
  36. struct ProjectNodeOptions;
  37. struct ComponentOptions;
  38. struct ComAudioOptions;
  39. }
  40. namespace cocostudio
  41. {
  42. class ComAudio;
  43. }
  44. namespace cocostudio
  45. {
  46. namespace timeline
  47. {
  48. class ActionTimeline;
  49. class ActionTimelineNode;
  50. }
  51. }
  52. NS_CC_BEGIN
  53. typedef std::function<void(Ref*)> ccNodeLoadCallback;
  54. class CC_STUDIO_DLL CSLoader
  55. {
  56. public:
  57. static CSLoader* getInstance();
  58. static void destroyInstance();
  59. CSLoader();
  60. /** @deprecated Use method destroyInstance() instead */
  61. CC_DEPRECATED_ATTRIBUTE void purge();
  62. void init();
  63. static cocos2d::Node* createNode(const std::string& filename);
  64. static cocos2d::Node* createNode(const std::string& filename, const ccNodeLoadCallback& callback);
  65. static cocos2d::Node* createNode(const Data& data);
  66. static cocos2d::Node* createNode(const Data& data, const ccNodeLoadCallback &callback);
  67. static cocos2d::Node* createNodeWithVisibleSize(const std::string& filename);
  68. static cocos2d::Node* createNodeWithVisibleSize(const std::string& filename, const ccNodeLoadCallback& callback);
  69. static cocostudio::timeline::ActionTimeline* createTimeline(const std::string& filename);
  70. static cocostudio::timeline::ActionTimeline* createTimeline(const Data& data, const std::string& filename);
  71. /*
  72. static cocostudio::timeline::ActionTimelineNode* createActionTimelineNode(const std::string& filename);
  73. static cocostudio::timeline::ActionTimelineNode* createActionTimelineNode(const std::string& filename, int startIndex, int endIndex, bool loop);
  74. */
  75. cocos2d::Node* createNodeFromJson(const std::string& filename);
  76. cocos2d::Node* loadNodeWithFile(const std::string& fileName);
  77. cocos2d::Node* loadNodeWithContent(const std::string& content);
  78. void setRecordJsonPath(bool record) { _recordJsonPath = record; }
  79. bool isRecordJsonPath() const { return _recordJsonPath; }
  80. void setJsonPath(std::string jsonPath) { _jsonPath = jsonPath; }
  81. std::string getJsonPath() const { return _jsonPath; }
  82. cocos2d::Node* createNodeWithFlatBuffersFile(const std::string& filename);
  83. cocos2d::Node* nodeWithFlatBuffersFile(const std::string& fileName);
  84. cocos2d::Node* nodeWithFlatBuffers(const flatbuffers::NodeTree* nodetree);
  85. bool bindCallback(const std::string& callbackName,
  86. const std::string& callbackType,
  87. cocos2d::ui::Widget* sender,
  88. cocos2d::Node* handler);
  89. void registReaderObject(const std::string& className,
  90. ObjectFactory::Instance ins);
  91. cocos2d::Node* createNodeWithFlatBuffersForSimulator(const std::string& filename);
  92. cocos2d::Node* nodeWithFlatBuffersForSimulator(const flatbuffers::NodeTree* nodetree);
  93. protected:
  94. cocos2d::Node* createNodeWithFlatBuffersFile(const std::string& filename, const ccNodeLoadCallback& callback);
  95. cocos2d::Node* nodeWithFlatBuffersFile(const std::string& fileName, const ccNodeLoadCallback& callback);
  96. cocos2d::Node* nodeWithFlatBuffers(const flatbuffers::NodeTree* nodetree, const ccNodeLoadCallback& callback);
  97. cocos2d::Node* loadNode(const rapidjson::Value& json);
  98. void locateNodeWithMulresPosition(cocos2d::Node* node, const rapidjson::Value& json);
  99. void initNode(cocos2d::Node* node, const rapidjson::Value& json);
  100. // load nodes
  101. cocos2d::Node* loadSimpleNode(const rapidjson::Value& json);
  102. cocos2d::Node* loadSubGraph (const rapidjson::Value& json);
  103. cocos2d::Node* loadSprite (const rapidjson::Value& json);
  104. cocos2d::Node* loadParticle (const rapidjson::Value& json);
  105. cocos2d::Node* loadTMXTiledMap(const rapidjson::Value& json);
  106. // load gui
  107. cocos2d::Node* loadWidget(const rapidjson::Value& json);
  108. // load component
  109. cocos2d::Component* loadComponent(const rapidjson::Value& json);
  110. cocos2d::Component* loadComAudio(const rapidjson::Value& json);
  111. bool isWidget(const std::string& type);
  112. bool isCustomWidget(const std::string& type);
  113. std::string getGUIClassName(const std::string &name);
  114. std::string getWidgetReaderClassName(cocos2d::ui::Widget *widget);
  115. inline void reconstructNestNode(cocos2d::Node * node);
  116. static inline std::string getExtentionName(const std::string& name);
  117. typedef std::function<cocos2d::Node*(const rapidjson::Value& json)> NodeCreateFunc;
  118. typedef std::pair<std::string, NodeCreateFunc> Pair;
  119. std::unordered_map<std::string, NodeCreateFunc> _funcs;
  120. typedef std::function<cocos2d::Component*(const rapidjson::Value& json)> ComponentCreateFunc;
  121. typedef std::pair<std::string, ComponentCreateFunc> ComponentPair;
  122. std::unordered_map<std::string, ComponentCreateFunc> _componentFuncs;
  123. bool _recordJsonPath;
  124. std::string _jsonPath;
  125. std::string _monoCocos2dxVersion;
  126. Node* _rootNode;
  127. cocos2d::Vector<cocos2d::Node*> _callbackHandlers;
  128. std::string _csBuildID;
  129. };
  130. NS_CC_END
  131. #endif /* defined(__cocos2d_libs__CSLoader__) */