axmol/cocos/2d/CCTMXXMLParser.h

424 lines
12 KiB
C
Raw Normal View History

2010-11-13 11:34:49 +08:00
/****************************************************************************
Copyright (c) 2009-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2010-11-13 11:34:49 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2010-11-13 11:34:49 +08:00
#ifndef __CC_TM_XML_PARSER__
#define __CC_TM_XML_PARSER__
2015-03-24 20:23:51 +08:00
/// @cond DO_NOT_SHOW
2021-10-23 23:27:14 +08:00
#include "math/CCMath.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCSAXParser.h"
2014-04-27 01:35:57 +08:00
#include "base/CCVector.h"
#include "base/CCMap.h"
2014-04-27 01:35:57 +08:00
#include "base/CCValue.h"
2014-08-28 17:32:23 +08:00
#include "2d/CCTMXObjectGroup.h" // needed for Vector<TMXObjectGroup*> for binding
#include <string>
NS_CC_BEGIN
class TMXLayerInfo;
class TMXTilesetInfo;
/** @file
* Internal TMX parser
*
* IMPORTANT: These classed should not be documented using doxygen strings
* since the user should not use them.
*
*/
2012-06-20 18:09:11 +08:00
/**
* @addtogroup tilemap_parallax_nodes
* @{
*/
enum {
TMXLayerAttribNone = 1 << 0,
TMXLayerAttribBase64 = 1 << 1,
TMXLayerAttribGzip = 1 << 2,
TMXLayerAttribZlib = 1 << 3,
2016-04-13 03:51:09 +08:00
TMXLayerAttribCSV = 1 << 4,
};
enum {
TMXPropertyNone,
TMXPropertyMap,
TMXPropertyLayer,
TMXPropertyObjectGroup,
TMXPropertyObject,
TMXPropertyTile,
TMXPropertyAnimation
};
typedef enum TMXTileFlags_ {
kTMXTileHorizontalFlag = 0x80000000,
kTMXTileVerticalFlag = 0x40000000,
kTMXTileDiagonalFlag = 0x20000000,
2014-02-08 10:33:30 +08:00
kTMXFlipedAll = (kTMXTileHorizontalFlag|kTMXTileVerticalFlag|kTMXTileDiagonalFlag),
kTMXFlippedMask = ~(kTMXFlipedAll)
} TMXTileFlags;
/**
* @addtogroup _2d
* @{
*/
/** Possible orientations of the TMX map. */
enum
{
/** Orthogonal orientation. */
TMXOrientationOrtho,
/** Hexagonal orientation. */
TMXOrientationHex,
/** Isometric orientation. */
TMXOrientationIso,
/** Isometric staggered orientation. */
TMXOrientationStaggered,
};
/** Possible stagger axis of the TMX map. */
enum
{
/** Stagger Axis x. */
TMXStaggerAxis_X,
/** Stagger Axis y. */
TMXStaggerAxis_Y,
};
/** Possible stagger index of the TMX map. */
enum
{
/** Stagger Index: Odd */
TMXStaggerIndex_Odd,
/** Stagger Index: Even */
TMXStaggerIndex_Even,
};
/** @brief TMXTileAnimFrame contains the information about the frame of a animated tile like:
- Frame gid
- duration of this frame
This information is obtained from the TMX file.
*/
struct CC_DLL TMXTileAnimFrame
{
TMXTileAnimFrame(uint32_t tileID, float duration);
/** gid of the frame */
uint32_t _tileID = 0;
/** duration of the frame */
float _duration = 0.0f;
};
/** @brief TMXTileAnimInfo contains the information about the animated tile like:
- Animated Tile gid
- frames the animated tile contains
This information is obtained from the TMX file.
*/
struct CC_DLL TMXTileAnimInfo : public Ref
{
static TMXTileAnimInfo* create(uint32_t tileID);
explicit TMXTileAnimInfo(uint32_t tileID);
uint32_t _tileID = 0;
std::vector<TMXTileAnimFrame> _frames;
};
// Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
/** @brief TMXLayerInfo contains the information about the layers like:
- Layer name
- Layer size
- Layer opacity at creation time (it can be modified at runtime)
- Whether the layer is visible (if it's not visible, then the CocosNode won't be created)
This information is obtained from the TMX file.
*/
class CC_DLL TMXLayerInfo : public Ref
{
public:
/**
* @js ctor
*/
TMXLayerInfo();
/**
* @js NA
* @lua NA
*/
virtual ~TMXLayerInfo();
void setProperties(ValueMap properties);
ValueMap& getProperties();
ValueMap _properties;
std::string _name;
2021-10-23 23:27:14 +08:00
Vec2 _layerSize;
uint32_t *_tiles;
bool _visible;
unsigned char _opacity;
bool _ownTiles;
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
Vec2 _offset;
};
/** @brief TMXTilesetInfo contains the information about the tilesets like:
- Tileset name
- Tileset spacing
- Tileset margin
- size of the tiles
- Image used for the tiles
- Image size
This information is obtained from the TMX file.
*/
class CC_DLL TMXTilesetInfo : public Ref
{
public:
std::string _name;
2013-12-20 09:50:53 +08:00
int _firstGid;
2021-10-23 23:27:14 +08:00
Vec2 _tileSize;
2013-12-20 09:50:53 +08:00
int _spacing;
int _margin;
Vec2 _tileOffset;
//! filename containing the tiles (should be spritesheet / texture atlas)
std::string _sourceImage;
//! size in pixels of the image
2021-10-23 23:27:14 +08:00
Vec2 _imageSize;
std::string _originSourceImage;
//! map from gid of animated tile to its animation info
Map<uint32_t, TMXTileAnimInfo*> _animationInfo;
public:
/**
* @js ctor
*/
TMXTilesetInfo();
/**
* @js NA
* @lua NA
*/
virtual ~TMXTilesetInfo();
2014-02-08 10:33:30 +08:00
Rect getRectForGID(uint32_t gid);
};
/** @brief TMXMapInfo contains the information about the map like:
- Map orientation (hexagonal, isometric or orthogonal)
- Tile size
- Map size
And it also contains:
- Layers (an array of TMXLayerInfo objects)
- Tilesets (an array of TMXTilesetInfo objects)
- ObjectGroups (an array of TMXObjectGroupInfo objects)
This information is obtained from the TMX file.
*/
class CC_DLL TMXMapInfo : public Ref, public SAXDelegator
{
public:
/** creates a TMX Format with a tmx file */
static TMXMapInfo * create(const std::string& tmxFile);
/** creates a TMX Format with an XML string and a TMX resource path */
static TMXMapInfo * createWithXML(const std::string& tmxString, const std::string& resourcePath);
/**
* @js ctor
*/
TMXMapInfo();
/**
* @js NA
* @lua NA
*/
virtual ~TMXMapInfo();
/** initializes a TMX format with a tmx file */
bool initWithTMXFile(const std::string& tmxFile);
/** initializes a TMX format with an XML string and a TMX resource path */
bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
/** initializes parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file */
bool parseXMLFile(const std::string& xmlFilename);
/* initializes parsing of an XML string, either a tmx (Map) string or tsx (Tileset) string */
bool parseXMLString(const std::string& xmlString);
ValueMapIntKey& getTileProperties() { return _tileProperties; };
void setTileProperties(const ValueMapIntKey& tileProperties) {
_tileProperties = tileProperties;
}
/// map orientation
int getOrientation() const { return _orientation; }
void setOrientation(int orientation) { _orientation = orientation; }
/// map staggeraxis
int getStaggerAxis() const { return _staggerAxis; }
void setStaggerAxis(int staggerAxis) { _staggerAxis = staggerAxis; }
/// map stagger index
int getStaggerIndex() const { return _staggerIndex; }
void setStaggerIndex(int staggerIndex) { _staggerIndex = staggerIndex; }
/// map hexsidelength
int getHexSideLength() const { return _hexSideLength; }
void setHexSideLength(int hexSideLength) { _hexSideLength = hexSideLength; }
/// map width & height
2021-10-23 23:27:14 +08:00
const Vec2& getMapSize() const { return _mapSize; }
void setMapSize(const Vec2& mapSize) { _mapSize = mapSize; }
/// tiles width & height
2021-10-23 23:27:14 +08:00
const Vec2& getTileSize() const { return _tileSize; }
void setTileSize(const Vec2& tileSize) { _tileSize = tileSize; }
/// Layers
const Vector<TMXLayerInfo*>& getLayers() const { return _layers; }
Vector<TMXLayerInfo*>& getLayers() { return _layers; }
void setLayers(const Vector<TMXLayerInfo*>& layers) {
_layers = layers;
}
/// tilesets
const Vector<TMXTilesetInfo*>& getTilesets() const { return _tilesets; }
Vector<TMXTilesetInfo*>& getTilesets() { return _tilesets; }
void setTilesets(const Vector<TMXTilesetInfo*>& tilesets) {
_tilesets = tilesets;
}
/// ObjectGroups
const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }
Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; }
void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
_objectGroups = groups;
}
/// parent element
int getParentElement() const { return _parentElement; }
void setParentElement(int element) { _parentElement = element; }
/// parent GID
int getParentGID() const { return _parentGID; }
void setParentGID(int gid) { _parentGID = gid; }
/// layer attribs
int getLayerAttribs() const { return _layerAttribs; }
void setLayerAttribs(int layerAttribs) { _layerAttribs = layerAttribs; }
/// is storing characters?
bool isStoringCharacters() const { return _storingCharacters; }
void setStoringCharacters(bool storingCharacters) { _storingCharacters = storingCharacters; }
/// properties
const ValueMap& getProperties() const { return _properties; }
ValueMap& getProperties() { return _properties; }
void setProperties(const ValueMap& properties) {
_properties = properties;
}
// implement pure virtual methods of SAXDelegator
/**
* @js NA
* @lua NA
*/
void startElement(void *ctx, const char *name, const char **atts) override;
/**
* @js NA
* @lua NA
*/
void endElement(void *ctx, const char *name) override;
/**
* @js NA
* @lua NA
*/
void textHandler(void *ctx, const char *ch, size_t len) override;
const std::string& getCurrentString() const { return _currentString; }
void setCurrentString(const std::string& currentString){ _currentString = currentString; }
const std::string& getTMXFileName() const { return _TMXFileName; }
void setTMXFileName(const std::string& fileName){ _TMXFileName = fileName; }
const std::string& getExternalTilesetFileName() const { return _externalTilesetFilename; }
protected:
void internalInit(const std::string& tmxFileName, const std::string& resourcePath);
/// map orientation
int _orientation;
///map staggerAxis
int _staggerAxis;
///map staggerIndex
int _staggerIndex;
///map hexsidelength
int _hexSideLength;
/// map width & height
2021-10-23 23:27:14 +08:00
Vec2 _mapSize;
/// tiles width & height
2021-10-23 23:27:14 +08:00
Vec2 _tileSize;
/// Layers
Vector<TMXLayerInfo*> _layers;
/// tilesets
Vector<TMXTilesetInfo*> _tilesets;
/// ObjectGroups
Vector<TMXObjectGroup*> _objectGroups;
/// parent element
int _parentElement;
/// parent GID
2013-12-20 09:50:53 +08:00
int _parentGID;
/// layer attribs
int _layerAttribs;
/// is storing characters?
bool _storingCharacters;
/// properties
ValueMap _properties;
//! xml format tile index
int _xmlTileIndex;
//! tmx filename
std::string _TMXFileName;
// tmx resource path
std::string _resources;
//! current string
std::string _currentString;
//! tile properties
ValueMapIntKey _tileProperties;
2013-12-20 09:50:53 +08:00
int _currentFirstGID;
bool _recordFirstGID;
std::string _externalTilesetFilename;
};
2012-06-20 18:09:11 +08:00
// end of tilemap_parallax_nodes group
/// @}
NS_CC_END
2010-11-13 11:34:49 +08:00
2015-03-24 20:23:51 +08:00
/// @endcond
#endif