CCBReader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /****************************************************************************
  2. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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 _CCB_CCBREADER_H_
  21. #define _CCB_CCBREADER_H_
  22. #include <set>
  23. #include <string>
  24. #include <vector>
  25. #include "2d/CCNode.h"
  26. #include "base/CCData.h"
  27. #include "base/CCMap.h"
  28. #include "editor-support/cocosbuilder/CCBSequence.h"
  29. #include "extensions/GUI/CCControlExtension/CCControl.h"
  30. #include "editor-support/cocosbuilder/CCBAnimationManager.h"
  31. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  32. #ifdef ABSOLUTE
  33. #undef ABSOLUTE
  34. #endif
  35. #endif
  36. #define CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(T, METHOD) static T * METHOD() { \
  37. T * ptr = new (std::nothrow) T(); \
  38. if(ptr != NULL) { \
  39. ptr->autorelease(); \
  40. return ptr; \
  41. } \
  42. CC_SAFE_DELETE(ptr); \
  43. return NULL; \
  44. }
  45. #define CCB_STATIC_NEW_AUTORELEASE_OBJECT_WITH_INIT_METHOD(T, METHOD) static T * METHOD() { \
  46. T * ptr = new (std::nothrow) T(); \
  47. if(ptr != NULL && ptr->init()) { \
  48. ptr->autorelease(); \
  49. return ptr; \
  50. } \
  51. CC_SAFE_DELETE(ptr); \
  52. return NULL; \
  53. }
  54. #define CCB_VERSION 5
  55. namespace cocosbuilder {
  56. /**
  57. * @addtogroup cocosbuilder
  58. * @{
  59. */
  60. class CCBFile : public cocos2d::Node
  61. {
  62. private:
  63. cocos2d::Node *_CCBFileNode;
  64. public:
  65. CCBFile();
  66. static CCBFile* create();
  67. cocos2d::Node* getCCBFileNode();
  68. void setCCBFileNode(Node *pNode); // retain
  69. };
  70. /* Forward declaration. */
  71. class NodeLoader;
  72. class NodeLoaderLibrary;
  73. class NodeLoaderListener;
  74. class CCBMemberVariableAssigner;
  75. class CCBSelectorResolver;
  76. class CCBAnimationManager;
  77. class CCBKeyframe;
  78. /**
  79. * @brief Parse CCBI file which is generated by CocosBuilder
  80. */
  81. class CC_DLL CCBReader : public cocos2d::Ref
  82. {
  83. public:
  84. enum class PropertyType {
  85. POSITION = 0,
  86. SIZE,
  87. POINT,
  88. POINT_LOCK,
  89. SCALE_LOCK,
  90. DEGREES,
  91. INTEGER,
  92. FLOAT,
  93. FLOAT_VAR,
  94. CHECK,
  95. SPRITEFRAME,
  96. TEXTURE,
  97. BYTE,
  98. COLOR3,
  99. COLOR4F_VAR,
  100. FLIP,
  101. BLEND_MODE,
  102. FNT_FILE,
  103. TEXT,
  104. FONT_TTF,
  105. INTEGER_LABELED,
  106. BLOCK,
  107. ANIMATION,
  108. CCB_FILE,
  109. STRING,
  110. BLOCK_CONTROL,
  111. FLOAT_SCALE,
  112. FLOAT_XY
  113. };
  114. enum class FloatType {
  115. _0 = 0,
  116. _1,
  117. MINUS1,
  118. _05,
  119. INTEGER,
  120. FULL
  121. };
  122. enum class PlatformType {
  123. ALL = 0,
  124. IOS,
  125. MAC
  126. };
  127. enum class TargetType {
  128. NONE = 0,
  129. DOCUMENT_ROOT = 1,
  130. OWNER = 2,
  131. };
  132. enum class PositionType
  133. {
  134. RELATIVE_BOTTOM_LEFT,
  135. RELATIVE_TOP_LEFT,
  136. RELATIVE_TOP_RIGHT,
  137. RELATIVE_BOTTOM_RIGHT,
  138. PERCENT,
  139. MULTIPLY_RESOLUTION,
  140. };
  141. enum class SizeType
  142. {
  143. ABSOLUTE,
  144. PERCENT,
  145. RELATIVE_CONTAINER,
  146. HORIZONTAL_PERCENT,
  147. VERTICAL_PERCENT,
  148. MULTIPLY_RESOLUTION,
  149. };
  150. enum class ScaleType
  151. {
  152. ABSOLUTE,
  153. MULTIPLY_RESOLUTION
  154. };
  155. /**
  156. * @js NA
  157. * @lua NA
  158. */
  159. CCBReader(NodeLoaderLibrary *pNodeLoaderLibrary, CCBMemberVariableAssigner *pCCBMemberVariableAssigner = NULL, CCBSelectorResolver *pCCBSelectorResolver = NULL, NodeLoaderListener *pNodeLoaderListener = NULL);
  160. /**
  161. * @js NA
  162. * @lua NA
  163. */
  164. CCBReader(CCBReader *ccbReader);
  165. /**
  166. * @js NA
  167. * @lua NA
  168. */
  169. virtual ~CCBReader();
  170. /**
  171. * @js NA
  172. * @lua NA
  173. */
  174. CCBReader();
  175. void setCCBRootPath(const char* ccbRootPath);
  176. const std::string& getCCBRootPath() const;
  177. cocos2d::Node* readNodeGraphFromFile(const char *pCCBFileName);
  178. cocos2d::Node* readNodeGraphFromFile(const char *pCCBFileName, cocos2d::Ref *pOwner);
  179. cocos2d::Node* readNodeGraphFromFile(const char *pCCBFileName, cocos2d::Ref *pOwner, const cocos2d::Size &parentSize);
  180. /**
  181. * @js NA
  182. * @lua NA
  183. */
  184. cocos2d::Node* readNodeGraphFromData(std::shared_ptr<cocos2d::Data> data, cocos2d::Ref *pOwner, const cocos2d::Size &parentSize);
  185. /**
  186. @lua NA
  187. */
  188. cocos2d::Scene* createSceneWithNodeGraphFromFile(const char *pCCBFileName);
  189. /**
  190. @lua NA
  191. */
  192. cocos2d::Scene* createSceneWithNodeGraphFromFile(const char *pCCBFileName, cocos2d::Ref *pOwner);
  193. /**
  194. @lua NA
  195. */
  196. cocos2d::Scene* createSceneWithNodeGraphFromFile(const char *pCCBFileName, cocos2d::Ref *pOwner, const cocos2d::Size &parentSize);
  197. /**
  198. * @js NA
  199. * @lua NA
  200. */
  201. CCBMemberVariableAssigner* getCCBMemberVariableAssigner();
  202. /**
  203. * @js NA
  204. * @lua NA
  205. */
  206. CCBSelectorResolver* getCCBSelectorResolver();
  207. /**
  208. * @js getActionManager
  209. * @lua getActionManager
  210. */
  211. CCBAnimationManager* getAnimationManager();
  212. /**
  213. * @js setActionManager
  214. * @lua setActionManager
  215. */
  216. void setAnimationManager(CCBAnimationManager *pAnimationManager);
  217. /** Used in NodeLoader::parseProperties()
  218. * @js NA
  219. * @lua NA
  220. */
  221. std::set<std::string>* getAnimatedProperties();
  222. /**
  223. * @js NA
  224. * @lua NA
  225. */
  226. std::set<std::string>& getLoadedSpriteSheet();
  227. /**
  228. * @js NA
  229. * @lua NA
  230. */
  231. cocos2d::Ref* getOwner();
  232. /* Utility methods.
  233. * @js NA
  234. * @lua NA
  235. */
  236. static std::string lastPathComponent(const char* pString);
  237. /**
  238. * @js NA
  239. * @lua NA
  240. */
  241. static std::string deletePathExtension(const char* pString);
  242. /**
  243. * @js NA
  244. * @lua NA
  245. */
  246. static std::string toLowerCase(const char* pString);
  247. /**
  248. * @js NA
  249. * @lua NA
  250. */
  251. static bool endsWith(const char* pString, const char* pEnding);
  252. /* Parse methods.
  253. * @js NA
  254. * @lua NA
  255. */
  256. int readInt(bool pSigned);
  257. /**
  258. * @js NA
  259. * @lua NA
  260. */
  261. unsigned char readByte();
  262. /**
  263. * @js NA
  264. * @lua NA
  265. */
  266. bool readBool();
  267. std::string readUTF8();
  268. /**
  269. * @js NA
  270. * @lua NA
  271. */
  272. float readFloat();
  273. /**
  274. * @js NA
  275. * @lua NA
  276. */
  277. std::string readCachedString();
  278. /**
  279. * @js NA
  280. * @lua NA
  281. */
  282. bool isJSControlled();
  283. bool readCallbackKeyframesForSeq(CCBSequence* seq);
  284. bool readSoundKeyframesForSeq(CCBSequence* seq);
  285. cocos2d::ValueVector getOwnerCallbackNames();
  286. cocos2d::Vector<cocos2d::Node*>& getOwnerCallbackNodes();
  287. cocos2d::ValueVector& getOwnerCallbackControlEvents();
  288. cocos2d::ValueVector getOwnerOutletNames();
  289. cocos2d::Vector<cocos2d::Node*>& getOwnerOutletNodes();
  290. cocos2d::Vector<cocos2d::Node*>& getNodesWithAnimationManagers();
  291. cocos2d::Vector<CCBAnimationManager*>& getAnimationManagersForNodes();
  292. typedef cocos2d::Map<cocos2d::Node*, CCBAnimationManager*> CCBAnimationManagerMap;
  293. typedef std::shared_ptr<CCBAnimationManagerMap> CCBAnimationManagerMapPtr;
  294. /**
  295. * @js NA
  296. * @lua NA
  297. */
  298. CCBAnimationManagerMapPtr getAnimationManagers();
  299. /**
  300. * @js NA
  301. * @lua NA
  302. */
  303. void setAnimationManagers(CCBAnimationManagerMapPtr x);
  304. /**
  305. * @js NA
  306. * @lua NA
  307. */
  308. void addOwnerCallbackName(const std::string& name);
  309. /**
  310. * @js NA
  311. * @lua NA
  312. */
  313. void addOwnerCallbackNode(cocos2d::Node *node);
  314. void addOwnerCallbackControlEvents(cocos2d::extension::Control::EventType type);
  315. /**
  316. * @js NA
  317. * @lua NA
  318. */
  319. void addDocumentCallbackName(const std::string& name);
  320. /**
  321. * @js NA
  322. * @lua NA
  323. */
  324. void addDocumentCallbackNode(cocos2d::Node *node);
  325. void addDocumentCallbackControlEvents(cocos2d::extension::Control::EventType eventType);
  326. /**
  327. * @js NA
  328. * @lua NA
  329. */
  330. static float getResolutionScale();
  331. static void setResolutionScale(float scale);
  332. /**
  333. * @js NA
  334. * @lua NA
  335. */
  336. cocos2d::Node* readFileWithCleanUp(bool bCleanUp, CCBAnimationManagerMapPtr am);
  337. void addOwnerOutletName(std::string name);
  338. void addOwnerOutletNode(cocos2d::Node *node);
  339. private:
  340. void cleanUpNodeGraph(cocos2d::Node *pNode);
  341. bool readSequences();
  342. CCBKeyframe* readKeyframe(PropertyType type);
  343. bool readHeader();
  344. bool readStringCache();
  345. //void readStringCacheEntry();
  346. cocos2d::Node* readNodeGraph();
  347. cocos2d::Node* readNodeGraph(cocos2d::Node * pParent);
  348. bool getBit();
  349. void alignBits();
  350. bool init();
  351. friend class NodeLoader;
  352. private:
  353. std::shared_ptr<cocos2d::Data> _data;
  354. unsigned char *_bytes;
  355. int _currentByte;
  356. int _currentBit;
  357. std::vector<std::string> _stringCache;
  358. std::set<std::string> _loadedSpriteSheets;
  359. cocos2d::Ref *_owner;
  360. CCBAnimationManager* _animationManager; //retain
  361. CCBAnimationManagerMapPtr _animationManagers;
  362. std::set<std::string> *_animatedProps;
  363. NodeLoaderLibrary *_nodeLoaderLibrary;
  364. NodeLoaderListener *_nodeLoaderListener;
  365. CCBMemberVariableAssigner *_CCBMemberVariableAssigner;
  366. CCBSelectorResolver *_CCBSelectorResolver;
  367. std::vector<std::string> _ownerOutletNames;
  368. cocos2d::Vector<cocos2d::Node*> _ownerOutletNodes;
  369. cocos2d::Vector<cocos2d::Node*> _nodesWithAnimationManagers;
  370. cocos2d::Vector<CCBAnimationManager*> _animationManagersForNodes;
  371. std::vector<std::string> _ownerCallbackNames;
  372. cocos2d::Vector<cocos2d::Node*> _ownerCallbackNodes;
  373. cocos2d::ValueVector _ownerOwnerCallbackControlEvents;
  374. std::string _CCBRootPath;
  375. bool _jsControlled;
  376. };
  377. // end of effects group
  378. /// @}
  379. }
  380. #endif