CCSGUIReader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 __CCSGUIREADER_H__
  21. #define __CCSGUIREADER_H__
  22. #include "ui/UILayout.h"
  23. #include "editor-support/cocostudio/DictionaryHelper.h"
  24. #include "editor-support/cocostudio/WidgetReader/WidgetReaderProtocol.h"
  25. #include "base/ObjectFactory.h"
  26. #include "base/CCValue.h"
  27. #include "editor-support/cocostudio/CocosStudioExport.h"
  28. namespace protocolbuffers
  29. {
  30. class NodeTree;
  31. }
  32. namespace tinyxml2
  33. {
  34. class XMLElement;
  35. }
  36. namespace cocostudio {
  37. class CocoLoader;
  38. struct stExpCocoNode;
  39. #define kCCSVersion 1.0
  40. typedef void (cocos2d::Ref::*SEL_ParseEvent)(const std::string&, cocos2d::Ref*, const rapidjson::Value&);
  41. #define parseselector(_SELECTOR) (SEL_ParseEvent)(&_SELECTOR)
  42. class CC_STUDIO_DLL GUIReader : public cocos2d::Ref
  43. {
  44. public:
  45. CC_DEPRECATED_ATTRIBUTE static GUIReader* shareReader() { return GUIReader::getInstance(); };
  46. CC_DEPRECATED_ATTRIBUTE static void purgeGUIReader() { GUIReader::destroyInstance(); };
  47. static GUIReader* getInstance();
  48. static void destroyInstance();
  49. cocos2d::ui::Widget* widgetFromJsonFile(const char* fileName);
  50. cocos2d::ui::Widget* widgetFromBinaryFile(const char* fileName);
  51. int getVersionInteger(const char* str);
  52. /**
  53. * @js NA
  54. */
  55. void storeFileDesignSize(const char* fileName, const cocos2d::Size &size);
  56. /**
  57. * @js NA
  58. */
  59. cocos2d::Size getFileDesignSize(const char* fileName) const;
  60. void setFilePath(const std::string& strFilePath) { m_strFilePath = strFilePath; }
  61. const std::string& getFilePath() const { return m_strFilePath; }
  62. void registerTypeAndCallBack(const std::string& classType,
  63. cocos2d::ObjectFactory::Instance ins,
  64. Ref* object,
  65. SEL_ParseEvent callBack);
  66. void registerTypeAndCallBack(const std::string& classType,
  67. cocos2d::ObjectFactory::InstanceFunc ins,
  68. Ref* object,
  69. SEL_ParseEvent callBack);
  70. protected:
  71. GUIReader();
  72. ~GUIReader();
  73. std::string m_strFilePath;
  74. cocos2d::ValueMap _fileDesignSizes;
  75. typedef std::map<std::string, SEL_ParseEvent> ParseCallBackMap;
  76. ParseCallBackMap _mapParseSelector;
  77. typedef std::map<std::string, Ref*> ParseObjectMap;
  78. ParseObjectMap _mapObject;
  79. public:
  80. ParseCallBackMap* getParseCallBackMap() { return &_mapParseSelector; };
  81. ParseObjectMap* getParseObjectMap() { return &_mapObject; };
  82. };
  83. class CC_STUDIO_DLL WidgetPropertiesReader : public cocos2d::Ref
  84. {
  85. public:
  86. virtual cocos2d::ui::Widget* createWidget(const rapidjson::Value& dic, const char* fullPath, const char* fileName)=0;
  87. virtual cocos2d::ui::Widget* widgetFromJsonDictionary(const rapidjson::Value& data) = 0;
  88. virtual void setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* reader, cocos2d::ui::Widget* widget, const rapidjson::Value& options) = 0;
  89. virtual void setPropsForAllCustomWidgetFromJsonDictionary(const std::string& classType,
  90. cocos2d::ui::Widget* widget,
  91. const rapidjson::Value& customOptions) = 0;
  92. //add binary parsing
  93. virtual cocos2d::ui::Widget* createWidgetFromBinary(CocoLoader* cocoLoader,stExpCocoNode* pCocoNode, const char* fileName)=0;
  94. virtual cocos2d::ui::Widget* widgetFromBinary(CocoLoader* cocoLoader, stExpCocoNode* pCocoNode) = 0;
  95. virtual void setPropsForAllWidgetFromBinary(WidgetReaderProtocol* reader,
  96. cocos2d::ui::Widget* widget,
  97. CocoLoader* cocoLoader,
  98. stExpCocoNode* pCocoNode) = 0;
  99. protected:
  100. void setAnchorPointForWidget(cocos2d::ui::Widget* widget, const rapidjson::Value&options);
  101. std::string getWidgetReaderClassName(const std::string& classname);
  102. std::string getWidgetReaderClassName(cocos2d::ui::Widget *widget);
  103. std::string getGUIClassName(const std::string& name);
  104. cocos2d::ui::Widget *createGUI(const std::string& classname);
  105. WidgetReaderProtocol* createWidgetReaderProtocol(const std::string& classname);
  106. protected:
  107. std::string m_strFilePath;
  108. };
  109. class CC_STUDIO_DLL WidgetPropertiesReader0250 : public WidgetPropertiesReader
  110. {
  111. public:
  112. WidgetPropertiesReader0250(){};
  113. virtual ~WidgetPropertiesReader0250(){};
  114. virtual cocos2d::ui::Widget* createWidget(const rapidjson::Value& dic, const char* fullPath, const char* fileName) override;
  115. virtual cocos2d::ui::Widget* widgetFromJsonDictionary(const rapidjson::Value& dic) override;
  116. //added for binary parsing
  117. virtual cocos2d::ui::Widget* createWidgetFromBinary(CocoLoader* cocoLoader,
  118. stExpCocoNode* pCocoNode,
  119. const char* fileName) override;
  120. virtual cocos2d::ui::Widget* widgetFromBinary(CocoLoader* cocoLoader,
  121. stExpCocoNode* pCocoNode) override;
  122. virtual void setPropsForAllWidgetFromBinary(WidgetReaderProtocol* reader,
  123. cocos2d::ui::Widget* widget,
  124. CocoLoader* cocoLoader,
  125. stExpCocoNode* pCocoNode) override;
  126. virtual void setPropsForWidgetFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  127. virtual void setColorPropsForWidgetFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  128. virtual void setPropsForButtonFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  129. virtual void setPropsForCheckBoxFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  130. virtual void setPropsForImageViewFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  131. virtual void setPropsForLabelFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  132. virtual void setPropsForLabelAtlasFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  133. virtual void setPropsForLabelBMFontFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  134. virtual void setPropsForLoadingBarFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  135. virtual void setPropsForSliderFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  136. virtual void setPropsForTextFieldFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  137. virtual void setPropsForLayoutFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  138. virtual void setPropsForScrollViewFromJsonDictionary(cocos2d::ui::Widget* widget,const rapidjson::Value& options);
  139. virtual void setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* reader, cocos2d::ui::Widget* widget, const rapidjson::Value& options) override;
  140. virtual void setPropsForAllCustomWidgetFromJsonDictionary(const std::string& classType,
  141. cocos2d::ui::Widget* widget,
  142. const rapidjson::Value& customOptions) override;
  143. };
  144. class CC_STUDIO_DLL WidgetPropertiesReader0300 : public WidgetPropertiesReader
  145. {
  146. public:
  147. WidgetPropertiesReader0300(){};
  148. virtual ~WidgetPropertiesReader0300(){};
  149. virtual cocos2d::ui::Widget* createWidget(const rapidjson::Value& dic,
  150. const char* fullPath,
  151. const char* fileName) override;
  152. //add bin parse support
  153. virtual cocos2d::ui::Widget* createWidgetFromBinary(CocoLoader* cocoLoader,
  154. stExpCocoNode* pCocoNode,
  155. const char* fileName)override;
  156. virtual cocos2d::ui::Widget* widgetFromBinary(CocoLoader* cocoLoader,
  157. stExpCocoNode* pCocoNode) override;
  158. virtual void setPropsForAllWidgetFromBinary(WidgetReaderProtocol* reader,
  159. cocos2d::ui::Widget* widget,
  160. CocoLoader* cocoLoader,
  161. stExpCocoNode* pCocoNode) override;
  162. virtual void setPropsForAllCustomWidgetFromBinary(const std::string& classType,
  163. cocos2d::ui::Widget* widget,
  164. CocoLoader* cocoLoader,
  165. stExpCocoNode* pCocoNode);
  166. virtual cocos2d::ui::Widget* widgetFromJsonDictionary(const rapidjson::Value& dic) override;
  167. virtual void setPropsForAllWidgetFromJsonDictionary(WidgetReaderProtocol* reader,
  168. cocos2d::ui::Widget* widget,
  169. const rapidjson::Value& options) override;
  170. virtual void setPropsForAllCustomWidgetFromJsonDictionary(const std::string& classType,
  171. cocos2d::ui::Widget* widget,
  172. const rapidjson::Value& customOptions) override;
  173. };
  174. }
  175. #endif /* defined(__CCSGUIReader__) */