CCTMXXMLParser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /****************************************************************************
  2. Copyright (c) 2009-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CC_TM_XML_PARSER__
  24. #define __CC_TM_XML_PARSER__
  25. /// @cond DO_NOT_SHOW
  26. #include "math/CCGeometry.h"
  27. #include "platform/CCSAXParser.h"
  28. #include "base/CCVector.h"
  29. #include "base/CCValue.h"
  30. #include "2d/CCTMXObjectGroup.h" // needed for Vector<TMXObjectGroup*> for binding
  31. #include <string>
  32. NS_CC_BEGIN
  33. class TMXLayerInfo;
  34. class TMXTilesetInfo;
  35. /** @file
  36. * Internal TMX parser
  37. *
  38. * IMPORTANT: These classed should not be documented using doxygen strings
  39. * since the user should not use them.
  40. *
  41. */
  42. /**
  43. * @addtogroup tilemap_parallax_nodes
  44. * @{
  45. */
  46. enum {
  47. TMXLayerAttribNone = 1 << 0,
  48. TMXLayerAttribBase64 = 1 << 1,
  49. TMXLayerAttribGzip = 1 << 2,
  50. TMXLayerAttribZlib = 1 << 3,
  51. TMXLayerAttribCSV = 1 << 4,
  52. };
  53. enum {
  54. TMXPropertyNone,
  55. TMXPropertyMap,
  56. TMXPropertyLayer,
  57. TMXPropertyObjectGroup,
  58. TMXPropertyObject,
  59. TMXPropertyTile
  60. };
  61. typedef enum TMXTileFlags_ {
  62. kTMXTileHorizontalFlag = 0x80000000,
  63. kTMXTileVerticalFlag = 0x40000000,
  64. kTMXTileDiagonalFlag = 0x20000000,
  65. kTMXFlipedAll = (kTMXTileHorizontalFlag|kTMXTileVerticalFlag|kTMXTileDiagonalFlag),
  66. kTMXFlippedMask = ~(kTMXFlipedAll)
  67. } TMXTileFlags;
  68. // Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
  69. /** @brief TMXLayerInfo contains the information about the layers like:
  70. - Layer name
  71. - Layer size
  72. - Layer opacity at creation time (it can be modified at runtime)
  73. - Whether the layer is visible (if it's not visible, then the CocosNode won't be created)
  74. This information is obtained from the TMX file.
  75. */
  76. class CC_DLL TMXLayerInfo : public Ref
  77. {
  78. public:
  79. /**
  80. * @js ctor
  81. */
  82. TMXLayerInfo();
  83. /**
  84. * @js NA
  85. * @lua NA
  86. */
  87. virtual ~TMXLayerInfo();
  88. void setProperties(ValueMap properties);
  89. ValueMap& getProperties();
  90. ValueMap _properties;
  91. std::string _name;
  92. Size _layerSize;
  93. uint32_t *_tiles;
  94. bool _visible;
  95. unsigned char _opacity;
  96. bool _ownTiles;
  97. Vec2 _offset;
  98. };
  99. /** @brief TMXTilesetInfo contains the information about the tilesets like:
  100. - Tileset name
  101. - Tileset spacing
  102. - Tileset margin
  103. - size of the tiles
  104. - Image used for the tiles
  105. - Image size
  106. This information is obtained from the TMX file.
  107. */
  108. class CC_DLL TMXTilesetInfo : public Ref
  109. {
  110. public:
  111. std::string _name;
  112. int _firstGid;
  113. Size _tileSize;
  114. int _spacing;
  115. int _margin;
  116. Vec2 _tileOffset;
  117. //! filename containing the tiles (should be spritesheet / texture atlas)
  118. std::string _sourceImage;
  119. //! size in pixels of the image
  120. Size _imageSize;
  121. std::string _originSourceImage;
  122. public:
  123. /**
  124. * @js ctor
  125. */
  126. TMXTilesetInfo();
  127. /**
  128. * @js NA
  129. * @lua NA
  130. */
  131. virtual ~TMXTilesetInfo();
  132. Rect getRectForGID(uint32_t gid);
  133. };
  134. /** @brief TMXMapInfo contains the information about the map like:
  135. - Map orientation (hexagonal, isometric or orthogonal)
  136. - Tile size
  137. - Map size
  138. And it also contains:
  139. - Layers (an array of TMXLayerInfo objects)
  140. - Tilesets (an array of TMXTilesetInfo objects)
  141. - ObjectGroups (an array of TMXObjectGroupInfo objects)
  142. This information is obtained from the TMX file.
  143. */
  144. class CC_DLL TMXMapInfo : public Ref, public SAXDelegator
  145. {
  146. public:
  147. /** creates a TMX Format with a tmx file */
  148. static TMXMapInfo * create(const std::string& tmxFile);
  149. /** creates a TMX Format with an XML string and a TMX resource path */
  150. static TMXMapInfo * createWithXML(const std::string& tmxString, const std::string& resourcePath);
  151. /** creates a TMX Format with a tmx file */
  152. CC_DEPRECATED_ATTRIBUTE static TMXMapInfo * formatWithTMXFile(const char *tmxFile) { return TMXMapInfo::create(tmxFile); };
  153. /** creates a TMX Format with an XML string and a TMX resource path */
  154. CC_DEPRECATED_ATTRIBUTE static TMXMapInfo * formatWithXML(const char* tmxString, const char* resourcePath) { return TMXMapInfo::createWithXML(tmxString, resourcePath); };
  155. /**
  156. * @js ctor
  157. */
  158. TMXMapInfo();
  159. /**
  160. * @js NA
  161. * @lua NA
  162. */
  163. virtual ~TMXMapInfo();
  164. /** initializes a TMX format with a tmx file */
  165. bool initWithTMXFile(const std::string& tmxFile);
  166. /** initializes a TMX format with an XML string and a TMX resource path */
  167. bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
  168. /** initializes parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file */
  169. bool parseXMLFile(const std::string& xmlFilename);
  170. /* initializes parsing of an XML string, either a tmx (Map) string or tsx (Tileset) string */
  171. bool parseXMLString(const std::string& xmlString);
  172. ValueMapIntKey& getTileProperties() { return _tileProperties; };
  173. void setTileProperties(const ValueMapIntKey& tileProperties) {
  174. _tileProperties = tileProperties;
  175. }
  176. /// map orientation
  177. int getOrientation() const { return _orientation; }
  178. void setOrientation(int orientation) { _orientation = orientation; }
  179. /// map staggeraxis
  180. int getStaggerAxis() const { return _staggerAxis; }
  181. void setStaggerAxis(int staggerAxis) { _staggerAxis = staggerAxis; }
  182. /// map stagger index
  183. int getStaggerIndex() const { return _staggerIndex; }
  184. void setStaggerIndex(int staggerIndex) { _staggerIndex = staggerIndex; }
  185. /// map hexsidelength
  186. int getHexSideLength() const { return _hexSideLength; }
  187. void setHexSideLength(int hexSideLength) { _hexSideLength = hexSideLength; }
  188. /// map width & height
  189. const Size& getMapSize() const { return _mapSize; }
  190. void setMapSize(const Size& mapSize) { _mapSize = mapSize; }
  191. /// tiles width & height
  192. const Size& getTileSize() const { return _tileSize; }
  193. void setTileSize(const Size& tileSize) { _tileSize = tileSize; }
  194. /// Layers
  195. const Vector<TMXLayerInfo*>& getLayers() const { return _layers; }
  196. Vector<TMXLayerInfo*>& getLayers() { return _layers; }
  197. void setLayers(const Vector<TMXLayerInfo*>& layers) {
  198. _layers = layers;
  199. }
  200. /// tilesets
  201. const Vector<TMXTilesetInfo*>& getTilesets() const { return _tilesets; }
  202. Vector<TMXTilesetInfo*>& getTilesets() { return _tilesets; }
  203. void setTilesets(const Vector<TMXTilesetInfo*>& tilesets) {
  204. _tilesets = tilesets;
  205. }
  206. /// ObjectGroups
  207. const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }
  208. Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; }
  209. void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
  210. _objectGroups = groups;
  211. }
  212. /// parent element
  213. int getParentElement() const { return _parentElement; }
  214. void setParentElement(int element) { _parentElement = element; }
  215. /// parent GID
  216. int getParentGID() const { return _parentGID; }
  217. void setParentGID(int gid) { _parentGID = gid; }
  218. /// layer attribs
  219. int getLayerAttribs() const { return _layerAttribs; }
  220. void setLayerAttribs(int layerAttribs) { _layerAttribs = layerAttribs; }
  221. /// is storing characters?
  222. bool isStoringCharacters() const { return _storingCharacters; }
  223. CC_DEPRECATED_ATTRIBUTE bool getStoringCharacters() const { return isStoringCharacters(); }
  224. void setStoringCharacters(bool storingCharacters) { _storingCharacters = storingCharacters; }
  225. /// properties
  226. const ValueMap& getProperties() const { return _properties; }
  227. ValueMap& getProperties() { return _properties; }
  228. void setProperties(const ValueMap& properties) {
  229. _properties = properties;
  230. }
  231. // implement pure virtual methods of SAXDelegator
  232. /**
  233. * @js NA
  234. * @lua NA
  235. */
  236. void startElement(void *ctx, const char *name, const char **atts) override;
  237. /**
  238. * @js NA
  239. * @lua NA
  240. */
  241. void endElement(void *ctx, const char *name) override;
  242. /**
  243. * @js NA
  244. * @lua NA
  245. */
  246. void textHandler(void *ctx, const char *ch, size_t len) override;
  247. const std::string& getCurrentString() const { return _currentString; }
  248. void setCurrentString(const std::string& currentString){ _currentString = currentString; }
  249. const std::string& getTMXFileName() const { return _TMXFileName; }
  250. void setTMXFileName(const std::string& fileName){ _TMXFileName = fileName; }
  251. const std::string& getExternalTilesetFileName() const { return _externalTilesetFilename; }
  252. protected:
  253. void internalInit(const std::string& tmxFileName, const std::string& resourcePath);
  254. /// map orientation
  255. int _orientation;
  256. ///map staggerAxis
  257. int _staggerAxis;
  258. ///map staggerIndex
  259. int _staggerIndex;
  260. ///map hexsidelength
  261. int _hexSideLength;
  262. /// map width & height
  263. Size _mapSize;
  264. /// tiles width & height
  265. Size _tileSize;
  266. /// Layers
  267. Vector<TMXLayerInfo*> _layers;
  268. /// tilesets
  269. Vector<TMXTilesetInfo*> _tilesets;
  270. /// ObjectGroups
  271. Vector<TMXObjectGroup*> _objectGroups;
  272. /// parent element
  273. int _parentElement;
  274. /// parent GID
  275. int _parentGID;
  276. /// layer attribs
  277. int _layerAttribs;
  278. /// is storing characters?
  279. bool _storingCharacters;
  280. /// properties
  281. ValueMap _properties;
  282. //! xml format tile index
  283. int _xmlTileIndex;
  284. //! tmx filename
  285. std::string _TMXFileName;
  286. // tmx resource path
  287. std::string _resources;
  288. //! current string
  289. std::string _currentString;
  290. //! tile properties
  291. ValueMapIntKey _tileProperties;
  292. int _currentFirstGID;
  293. bool _recordFirstGID;
  294. std::string _externalTilesetFilename;
  295. };
  296. // end of tilemap_parallax_nodes group
  297. /// @}
  298. NS_CC_END
  299. /// @endcond
  300. #endif