CCBReader.h 9.3 KB

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