CCTMXXMLParser.h 11 KB

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