Merge pull request #321 from natural-law/support-objectgroup

Support show objectgroup in TiledMap again.
This commit is contained in:
Wang Nan
2016-09-12 09:09:38 +08:00
committed by GitHub
8 changed files with 753 additions and 109 deletions
+23 -9
View File
@@ -160,8 +160,6 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
_tileSize = mapInfo->getTileSize(); _tileSize = mapInfo->getTileSize();
_mapOrientation = mapInfo->getOrientation(); _mapOrientation = mapInfo->getOrientation();
_objectGroups = mapInfo->getObjectGroups();
_properties = mapInfo->getProperties(); _properties = mapInfo->getProperties();
_tileProperties = mapInfo->getTileProperties(); _tileProperties = mapInfo->getTileProperties();
@@ -217,17 +215,20 @@ TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
{ {
CCASSERT(groupName.size() > 0, "Invalid group name!"); CCASSERT(!groupName.empty(), "Invalid group name!");
if (_objectGroups.size()>0) if (groupName.empty()) {
return nullptr;
}
for (auto& child : _children)
{ {
TMXObjectGroup* objectGroup = nullptr; TMXObjectGroup* group = dynamic_cast<TMXObjectGroup*>(child);
for (auto iter = _objectGroups.cbegin(); iter != _objectGroups.cend(); ++iter) if(group)
{ {
objectGroup = *iter; if(groupName.compare( group->getGroupName()) == 0)
if (objectGroup && objectGroup->getGroupName() == groupName)
{ {
return objectGroup; return group;
} }
} }
} }
@@ -236,6 +237,19 @@ TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
return nullptr; return nullptr;
} }
Vector<TMXObjectGroup*> TMXTiledMap::getObjectGroups()
{
Vector<TMXObjectGroup*> groups;
for (auto& child : _children)
{
TMXObjectGroup* group = dynamic_cast<TMXObjectGroup*>(child);
if(group)
{
groups.pushBack(group);
}
}
return groups;
}
Value TMXTiledMap::getProperty(const std::string& propertyName) const Value TMXTiledMap::getProperty(const std::string& propertyName) const
{ {
if (_properties.find(propertyName) != _properties.end()) if (_properties.find(propertyName) != _properties.end())
+1 -11
View File
@@ -173,16 +173,7 @@ public:
* *
* @return Object groups. * @return Object groups.
*/ */
inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }; Vector<TMXObjectGroup*> getObjectGroups();
inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
/** Set object groups.
*
* @param groups An object groups.
*/
inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
_objectGroups = groups;
};
/** Get properties. /** Get properties.
* *
@@ -229,7 +220,6 @@ protected:
/** map orientation */ /** map orientation */
int _mapOrientation; int _mapOrientation;
/** object groups */ /** object groups */
Vector<TMXObjectGroup*> _objectGroups;
/** properties */ /** properties */
ValueMap _properties; ValueMap _properties;
+448 -9
View File
@@ -27,14 +27,383 @@ THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#include "2d/CCTMXObjectGroup.h" #include "2d/CCTMXObjectGroup.h"
#include "base/ccMacros.h" #include "base/ccMacros.h"
#include "2d/CCTMXXMLParser.h"
#include "base/CCDirector.h"
#include "renderer/CCTextureCache.h"
#include "2d/CCTMXTiledMap.h"
#include "base/ccTypes.h"
NS_CC_BEGIN NS_CC_BEGIN
//implementation TMXObject
TMXObject::TMXObject(ValueMap objectInfo)
{
_objectName = objectInfo["name"].asString();
_type = static_cast<TMXObjectType>(objectInfo["type"].asInt());
_gid = objectInfo["gid"].isNull() ? 0 : objectInfo["gid"].asInt();
_id = objectInfo["id"].asInt();
_offset = Vec2(objectInfo["x"].asFloat(), objectInfo["y"].asFloat());
_objectSize = Size(objectInfo["width"].asFloat(), objectInfo["height"].asFloat());
_objectVisible = objectInfo["visible"].asBool();
_objectRotation = objectInfo["rotation"].asFloat();
setProperties(objectInfo);
}
TMXObject::~TMXObject()
{
CCLOGINFO("deallocing TMXObject: %p", this);
}
Value TMXObject::getProperty(const std::string& propertyName) const
{
if (_properties.find(propertyName) != _properties.end())
return _properties.at(propertyName);
return Value();
}
//implementation TMXObjectImage
TMXObjectImage::TMXObjectImage(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize)
: TMXObject(objectInfo)
, _groupSize(groupSize)
{
_initWithMapInfo(mapInfo);
}
TMXObjectImage::~TMXObjectImage()
{
CCLOGINFO("deallocing TMXObjectImage: %p", this);
}
bool TMXObjectImage::_initWithMapInfo(TMXMapInfo* mapInfo)
{
if (_gid == 0) {
return false;
}
TMXTilesetInfo* useTileset = nullptr;
auto& tilesets = mapInfo->getTilesets();
if (tilesets.size()>0)
{
TMXTilesetInfo* tileset = nullptr;
for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
{
tileset = *iter;
if( (_gid & kTMXFlippedMask) >= tileset->_firstGid ) {
useTileset = tileset;
break;
}
}
}
if (useTileset == nullptr) {
return false;
}
setVisible(_objectVisible);
auto texture = Director::getInstance()->getTextureCache()->addImage(useTileset->_sourceImage);
if (texture == nullptr)
{
return false;
}
auto rect = useTileset->getRectForGID(_gid);
initWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(rect));
setScale(_objectSize.width / rect.size.width, _objectSize.height / rect.size.height);
setRotation(_objectRotation);
setFlippedX(false);
if (_gid & kTMXTileHorizontalFlag) {
setFlippedX(true);
}
setFlippedY(false);
if (_gid & kTMXTileVerticalFlag) {
setFlippedY(true);
}
_initPosWithMapInfo(mapInfo);
return true;
}
void TMXObjectImage::_initPosWithMapInfo(TMXMapInfo* mapInfo)
{
auto mapOri = mapInfo->getOrientation();
auto mapSize = mapInfo->getMapSize();
auto tileSize = mapInfo->getTileSize();
switch (mapOri) {
case TMXOrientationOrtho:
case TMXOrientationHex:
case TMXOrientationStaggered:
{
Vec2 offsetInPoints = CC_POINT_PIXELS_TO_POINTS(_offset);
setAnchorPoint(Vec2(0, 0));
setPosition(Vec2(offsetInPoints.x, _groupSize.height - offsetInPoints.y));
}
break;
case TMXOrientationIso:
{
setAnchorPoint(Vec2(0.5, 0));
auto posIdx = Vec2(_offset.x / tileSize.width * 2, _offset.y / tileSize.height);
auto pos = Vec2(tileSize.width / 2 * (mapSize.width + posIdx.x - posIdx.y),
tileSize.height / 2 * (mapSize.height * 2 - posIdx.x - posIdx.y));
setPosition(CC_POINT_PIXELS_TO_POINTS(pos));
}
break;
default:
break;
}
}
//implementation TMXObjectShape
TMXObjectShape::TMXObjectShape(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize, const Color3B& color)
: TMXObject(objectInfo)
, DrawNode(1)
{
_mapInfo = mapInfo;
_mapOrientation = mapInfo->getOrientation();
_groupSize = groupSize;
_groupColor = Color4F((float) color.r / 255, (float) color.g / 255, (float) color.b / 255, 1.0);
_initShape(objectInfo);
}
TMXObjectShape::~TMXObjectShape() {
CCLOGINFO("deallocing TMXObjectShape: %p", this);
}
void TMXObjectShape::_initShape(const ValueMap& objectInfo)
{
DrawNode::init();
Vec2 originPos;
if (TMXOrientationIso != _mapOrientation) {
Vec2 startPos = Vec2(0, _groupSize.height);
Vec2 offsetInPoints = CC_POINT_PIXELS_TO_POINTS(_offset);
originPos = Vec2(startPos.x + offsetInPoints.x, startPos.y - offsetInPoints.y);
} else {
originPos = _getPosByOffset(Vec2(0, 0));
}
setPosition(originPos);
setRotation(_objectRotation);
switch (_type) {
case TMXObjectType::RECT:
_drawRect();
break;
case TMXObjectType::ELLIPSE:
_drawEllipse();
break;
case TMXObjectType::POLYGON:
_drawPoly(objectInfo, originPos, true);
break;
case TMXObjectType::POLYLINE:
_drawPoly(objectInfo, originPos, false);
break;
default:
break;
}
setVisible(_objectVisible);
}
Vec2 TMXObjectShape::_getPosByOffset(const Vec2& offset)
{
auto mapSize = _mapInfo->getMapSize();
auto tileSize = _mapInfo->getTileSize();
auto posIdx = Vec2((_offset.x + offset.x) / tileSize.width * 2, (_offset.y + offset.y) / tileSize.height);
return CC_POINT_PIXELS_TO_POINTS(Vec2(tileSize.width / 2 * (mapSize.width + posIdx.x - posIdx.y),
tileSize.height / 2 * (mapSize.height * 2 - posIdx.x - posIdx.y)));
}
void TMXObjectShape::_drawRect()
{
if (TMXOrientationIso != _mapOrientation) {
auto objSize = _objectSize;
if (objSize.equals(Size::ZERO)) {
objSize = Size(20, 20);
setAnchorPoint(Vec2(0.5, 0.5));
} else {
setAnchorPoint(Vec2(0, 1));
}
Size objSizeInPoints = CC_SIZE_PIXELS_TO_POINTS(objSize);
Vec2 bl = Vec2(0, 0);
Vec2 tr = Vec2(objSizeInPoints.width, objSizeInPoints.height);
drawRect(bl, tr, _groupColor);
setContentSize(objSizeInPoints);
} else {
if (_objectSize.equals(Size::ZERO)) {
return;
}
auto pos1 = _getPosByOffset(Vec2(0, 0));
auto pos2 = _getPosByOffset(Vec2(_objectSize.width, 0));
auto pos3 = _getPosByOffset(Vec2(_objectSize.width, _objectSize.height));
auto pos4 = _getPosByOffset(Vec2(0, _objectSize.height));
float width = pos2.x - pos4.x, height = pos1.y - pos3.y;
setContentSize(Size(width, height));
setAnchorPoint(Vec2((pos1.x - pos4.x) / width, 1));
auto origin = Vec2(pos4.x, pos3.y);
pos1.subtract(origin);
pos2.subtract(origin);
pos3.subtract(origin);
pos4.subtract(origin);
if (_objectSize.width > 0) {
drawLine(pos1, pos2, _groupColor);
drawLine(pos3, pos4, _groupColor);
}
if (_objectSize.height > 0) {
drawLine(pos1, pos4, _groupColor);
drawLine(pos3, pos2, _groupColor);
}
}
}
void TMXObjectShape::_drawEllipse()
{
if (TMXOrientationIso != _mapOrientation) {
auto objSize = _objectSize;
if (objSize.equals(Size::ZERO)) {
objSize = Size(20, 20);
setAnchorPoint(Vec2(0.5, 0.5));
} else {
setAnchorPoint(Vec2(0, 1));
}
Size objSizeInPoints = CC_SIZE_PIXELS_TO_POINTS(objSize);
float scaleX = 1.0, scaleY = 1.0, radius = 0.0;
Vec2 center = Vec2(objSizeInPoints.width / 2, objSizeInPoints.height / 2);
if (objSizeInPoints.width > objSizeInPoints.height) {
scaleX = objSizeInPoints.width / objSizeInPoints.height;
radius = objSizeInPoints.height / 2;
} else {
scaleY = objSizeInPoints.height / objSizeInPoints.width;
radius = objSizeInPoints.width / 2;
}
drawCircle(center, radius, 0, 50, false, scaleX, scaleY, _groupColor);
setContentSize(objSizeInPoints);
} else {
if (_objectSize.equals(Size::ZERO)) {
return;
}
// draw the rect
auto pos1 = _getPosByOffset(Vec2(0, 0));
auto pos2 = _getPosByOffset(Vec2(_objectSize.width, 0));
auto pos3 = _getPosByOffset(Vec2(_objectSize.width, _objectSize.height));
auto pos4 = _getPosByOffset(Vec2(0, _objectSize.height));
float width = pos2.x - pos4.x, height = pos1.y - pos3.y;
setContentSize(Size(width, height));
setAnchorPoint(Vec2((pos1.x - pos4.x) / width, 1));
auto origin = Vec2(pos4.x, pos3.y);
pos1.subtract(origin);
pos2.subtract(origin);
pos3.subtract(origin);
pos4.subtract(origin);
if (_objectSize.width > 0) {
drawLine(pos1, pos2, _groupColor);
drawLine(pos3, pos4, _groupColor);
}
if (_objectSize.height > 0) {
drawLine(pos1, pos4, _groupColor);
drawLine(pos3, pos2, _groupColor);
}
// add a drawnode to draw the ellipse
Size objSizeInPoints = CC_SIZE_PIXELS_TO_POINTS(_objectSize);
Vec2 center = _getPosByOffset(Vec2(_objectSize.width / 2, _objectSize.height / 2));
center.subtract(origin);
auto ellipseNode = DrawNode::create(_lineWidth);
ellipseNode->setContentSize(Size(width, height));
ellipseNode->setAnchorPoint(Vec2(0.5, 0.5));
ellipseNode->setPosition(center);
addChild(ellipseNode);
float scaleX = 1.0, scaleY = 1.0, radius = 0.0;
if (objSizeInPoints.width > objSizeInPoints.height) {
scaleX = objSizeInPoints.width / objSizeInPoints.height;
radius = objSizeInPoints.height / 2;
} else {
scaleY = objSizeInPoints.height / objSizeInPoints.width;
radius = objSizeInPoints.width / 2;
}
auto tileSize = _mapInfo->getTileSize();
float rotateDegree = atan(tileSize.width / tileSize.height);
radius /= sin(rotateDegree);
ellipseNode->drawCircle(center, radius, 0, 50, false, scaleX, scaleY, _groupColor);
// should rotate the ellipse
ellipseNode->setRotationSkewX(CC_RADIANS_TO_DEGREES(rotateDegree));
ellipseNode->setRotationSkewY(90 - CC_RADIANS_TO_DEGREES(rotateDegree));
}
}
void TMXObjectShape::_drawPoly(const ValueMap& objectInfo, const Vec2& originPos, bool isPolygon)
{
// parse the data
ValueVector pointsData;
if (isPolygon)
pointsData = objectInfo.at("points").asValueVector();
else
pointsData = objectInfo.at("polylinePoints").asValueVector();
Vec2 * points = new Vec2[pointsData.size()];
int idx = 0;
float minX = 0, minY = 0, maxX = 0, maxY = 0;
for (auto& pointData : pointsData) {
auto pointDict = pointData.asValueMap();
float x = pointDict["x"].asFloat();
float y = pointDict["y"].asFloat();
points[idx] = Vec2(x, y);
minX = MIN(minX, points[idx].x);
minY = MIN(minY, points[idx].y);
maxX = MAX(maxX, points[idx].x);
maxY = MAX(maxY, points[idx].y);
idx++;
}
if (TMXOrientationIso != _mapOrientation) {
// set the content size & anchor point
float width = maxX - minX, height = maxY - minY;
setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(width, height)));
setAnchorPoint(Vec2(-minX / width, maxY / height));
// correct the points data
for (int i = 0; i < idx; i++) {
points[i] = CC_POINT_PIXELS_TO_POINTS(Vec2(points[i].x - minX, -points[i].y + maxY));
}
drawPoly(points, idx, isPolygon, _groupColor);
} else {
Vec2 bl = _getPosByOffset(Vec2(minX, maxY));
Vec2 tr = _getPosByOffset(Vec2(maxX, minY));
Vec2 origin = _getPosByOffset(Vec2(0 ,0));
float width = tr.x - bl.x, height = tr.y - bl.y;
setContentSize(Size(width, height));
setAnchorPoint(Vec2((origin.x - bl.x) / width, (origin.y - bl.y) / height));
// correct the points data
for (int i = 0; i < idx; i++) {
Vec2 tempPoint = _getPosByOffset(points[i]);
points[i] = Vec2(tempPoint.x - bl.x, tempPoint.y - bl.y);
}
drawPoly(points, idx, isPolygon, _groupColor);
}
delete [] points;
}
//implementation TMXObjectGroup //implementation TMXObjectGroup
TMXObjectGroup::TMXObjectGroup() TMXObjectGroup::TMXObjectGroup(TMXObjectGroupInfo* groupInfo, TMXMapInfo* mapInfo)
: _groupName("") : _groupName("")
{ {
_initGroup(groupInfo, mapInfo);
} }
TMXObjectGroup::~TMXObjectGroup() TMXObjectGroup::~TMXObjectGroup()
@@ -42,23 +411,38 @@ TMXObjectGroup::~TMXObjectGroup()
CCLOGINFO("deallocing TMXObjectGroup: %p", this); CCLOGINFO("deallocing TMXObjectGroup: %p", this);
} }
ValueMap TMXObjectGroup::getObject(const std::string& objectName) const TMXObject* TMXObjectGroup::getObject(const std::string& objectName)
{ {
if (!_objects.empty()) CCASSERT(objectName.size() > 0, "Invalid object name!");
for (auto& child : _children)
{ {
for (const auto& v : _objects) TMXObject* object = dynamic_cast<TMXObject*>(child);
if(object)
{ {
const ValueMap& dict = v.asValueMap(); if(objectName.compare( object->getObjectName()) == 0)
if (dict.find("name") != dict.end())
{ {
if (dict.at("name").asString() == objectName) return object;
return dict;
} }
} }
} }
// object not found // object not found
return ValueMap(); return nullptr;
}
Vector<TMXObject*> TMXObjectGroup::getObjects()
{
Vector<TMXObject*> objects;
for (auto& child : _children)
{
TMXObject* object = dynamic_cast<TMXObject*>(child);
if(object)
{
objects.pushBack(object);
}
}
return objects;
} }
Value TMXObjectGroup::getProperty(const std::string& propertyName) const Value TMXObjectGroup::getProperty(const std::string& propertyName) const
@@ -69,5 +453,60 @@ Value TMXObjectGroup::getProperty(const std::string& propertyName) const
return Value(); return Value();
} }
void TMXObjectGroup::_initGroup(TMXObjectGroupInfo* groupInfo, TMXMapInfo* mapInfo)
{
_groupName = groupInfo->_groupName;
_positionOffset = groupInfo->_positionOffset;
_properties = groupInfo->getProperties();
auto mapSize = mapInfo->getMapSize();
auto tileSize = mapInfo->getTileSize();
if (mapInfo->getOrientation() == TMXOrientationHex) {
float width, height = 0;
if (mapInfo->getStaggerAxis() == TMXStaggerAxis_X) {
height = tileSize.height * (mapSize.height + 0.5);
width = (tileSize.width + mapInfo->getHexSideLength()) * (int)(mapSize.width / 2) + tileSize.width * ((int)mapSize.width % 2);
} else {
width = tileSize.width * (mapSize.width + 0.5);
height = (tileSize.height + mapInfo->getHexSideLength()) * (int)(mapSize.height / 2) + tileSize.height * ((int)mapSize.height % 2);
}
setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(width, height)));
}
else if (mapInfo->getOrientation() == TMXOrientationStaggered)
{
float width, height = 0;
if (mapInfo->getStaggerAxis() == TMXStaggerAxis_X) {
height = tileSize.height * (mapSize.height + 0.5);
width = (tileSize.width / 2) * (mapSize.width + 1);
} else {
width = tileSize.width * (mapSize.width + 0.5);
height = (tileSize.height / 2) * (mapSize.height + 1);
}
setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(width, height)));
}
else {
setContentSize(CC_SIZE_PIXELS_TO_POINTS(Size(mapSize.width * tileSize.width, mapSize.height * tileSize.height)));
}
setAnchorPoint(Vec2(0, 0));
setPosition(Vec2(_positionOffset.x, -_positionOffset.y));
setVisible(groupInfo->_visible);
// create objects children
auto objects = groupInfo->_objects;
int idx = 0;
for (auto& object : objects) {
auto objectInfo = object.asValueMap();
Node* obj = nullptr;
if (objectInfo["type"].asInt() == static_cast<int>(TMXObjectType::IMAGE)) {
obj = new TMXObjectImage(objectInfo, mapInfo, getContentSize());
} else {
obj = new TMXObjectShape(objectInfo, mapInfo, getContentSize(), groupInfo->_color);
}
obj->autorelease();
obj->setOpacity(groupInfo->_opacity);
addChild(obj, idx, idx);
idx++;
}
}
NS_CC_END NS_CC_END
+99 -15
View File
@@ -31,24 +31,117 @@ THE SOFTWARE.
#include "math/CCGeometry.h" #include "math/CCGeometry.h"
#include "base/CCValue.h" #include "base/CCValue.h"
#include "base/CCRef.h" #include "base/CCRef.h"
#include "2d/CCSprite.h"
#include "2d/CCDrawNode.h"
NS_CC_BEGIN NS_CC_BEGIN
enum class TMXObjectType {
RECT,
ELLIPSE,
POLYGON,
POLYLINE,
IMAGE
};
class TMXMapInfo;
class TMXObjectGroupInfo;
class Color4F;
/** /**
* @addtogroup _2d * @addtogroup _2d
* @{ * @{
*/ */
/** @brief TMXObject represents the TMX object.
* @since v3.12
*/
class CC_DLL TMXObject: public Ref
{
public:
TMXObject(ValueMap objectInfo);
virtual ~TMXObject();
inline const std::string& getObjectName() { return _objectName; };
inline void setObjectName(std::string& name) { _objectName = name; };
Value getProperty(const std::string& propertyName) const;
inline const ValueMap& getProperties() const { return _properties; };
inline void setProperties(const ValueMap& properties) { _properties = properties; };
inline TMXObjectType getObjectType() { return _type; };
inline int getId() { return _id; };
inline uint32_t getGid() { return _gid; };
inline Vec2 getOffset() { return _offset; };
inline Size getObjectSize() { return _objectSize; };
inline bool getObjectVisible() { return _objectVisible; };
inline float getObjectRotation() { return _objectRotation; };
protected:
TMXObjectType _type;
std::string _objectName;
Vec2 _offset;
Size _objectSize;
uint32_t _gid;
int _id;
ValueMap _properties;
bool _objectVisible;
float _objectRotation;
};
/** @brief TMXObjectImage represents the TMX object image.
* @since v3.12
*/
class CC_DLL TMXObjectImage : public Sprite, public TMXObject
{
public:
TMXObjectImage(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize);
virtual ~TMXObjectImage();
protected:
bool _initWithMapInfo(TMXMapInfo* mapInfo);
void _initPosWithMapInfo(TMXMapInfo* mapInfo);
Size _groupSize;
};
/** @brief TMXObjectShape represents the TMX object shapes.
* @since v3.12
*/
class CC_DLL TMXObjectShape : public DrawNode, public TMXObject
{
public:
TMXObjectShape(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize, const Color3B& color);
virtual ~TMXObjectShape();
protected:
void _initShape(const ValueMap& objectInfo);
void _drawRect();
void _drawEllipse();
void _drawPoly(const ValueMap& objectInfo, const Vec2& originPos, bool isPolygon);
// internal methods for iso map
Vec2 _getPosByOffset(const Vec2& offset);
int _mapOrientation;
Size _groupSize;
Color4F _groupColor;
private:
TMXMapInfo* _mapInfo;
};
/** @brief TMXObjectGroup represents the TMX object group. /** @brief TMXObjectGroup represents the TMX object group.
* @since v0.99.0 * @since v0.99.0
*/ */
class CC_DLL TMXObjectGroup : public Ref class CC_DLL TMXObjectGroup : public Node
{ {
public: public:
/** /**
* @js ctor * @js ctor
*/ */
TMXObjectGroup(); TMXObjectGroup(TMXObjectGroupInfo* groupInfo, TMXMapInfo* mapInfo);
/** /**
* @js NA * @js NA
* @lua NA * @lua NA
@@ -80,7 +173,7 @@ public:
* *
* @return Return the dictionary for the specific object name. * @return Return the dictionary for the specific object name.
*/ */
ValueMap getObject(const std::string& objectName) const; TMXObject* getObject(const std::string& objectName);
/** Gets the offset position of child objects. /** Gets the offset position of child objects.
* *
@@ -113,26 +206,17 @@ public:
* *
* @return The array of the objects. * @return The array of the objects.
*/ */
inline const ValueVector& getObjects() const { return _objects; }; Vector<TMXObject*> getObjects();
inline ValueVector& getObjects() { return _objects; };
/** Sets the array of the objects.
*
* @param objects The array of the objects.
*/
inline void setObjects(const ValueVector& objects) {
_objects = objects;
};
protected: protected:
void _initGroup(TMXObjectGroupInfo* groupInfo, TMXMapInfo* mapInfo);
/** name of the group */ /** name of the group */
std::string _groupName; std::string _groupName;
/** offset position of child objects */ /** offset position of child objects */
Vec2 _positionOffset; Vec2 _positionOffset;
/** list of properties stored in a dictionary */ /** list of properties stored in a dictionary */
ValueMap _properties; ValueMap _properties;
/** array of the objects */
ValueVector _objects;
}; };
// end of tilemap_parallax_nodes group // end of tilemap_parallax_nodes group
+39 -13
View File
@@ -173,17 +173,17 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
_tileSize = mapInfo->getTileSize(); _tileSize = mapInfo->getTileSize();
_mapOrientation = mapInfo->getOrientation(); _mapOrientation = mapInfo->getOrientation();
_objectGroups = mapInfo->getObjectGroups();
_properties = mapInfo->getProperties(); _properties = mapInfo->getProperties();
_tileProperties = mapInfo->getTileProperties(); _tileProperties = mapInfo->getTileProperties();
int idx = 0; int idx = 0;
int layerCount = 0;
auto& layers = mapInfo->getLayers(); auto& children = mapInfo->getAllChildren();
for (const auto &layerInfo : layers) { for (const auto &childInfo : children) {
if (layerInfo->_visible) { TMXLayerInfo* layerInfo = dynamic_cast<TMXLayerInfo*>(childInfo);
if (layerInfo && layerInfo->_visible) {
TMXLayer *child = parseLayer(layerInfo, mapInfo); TMXLayer *child = parseLayer(layerInfo, mapInfo);
if (child == nullptr) { if (child == nullptr) {
idx++; idx++;
@@ -198,9 +198,17 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
this->setContentSize(currentSize); this->setContentSize(currentSize);
idx++; idx++;
layerCount++;
}
TMXObjectGroupInfo* groupInfo = dynamic_cast<TMXObjectGroupInfo*>(childInfo);
if (groupInfo) {
TMXObjectGroup* group = new TMXObjectGroup(groupInfo, mapInfo);
group->autorelease();
addChild(group, idx, idx);
idx++;
} }
} }
_tmxLayerNum = idx; _tmxLayerNum = layerCount;
} }
// public // public
@@ -230,17 +238,20 @@ TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
{ {
CCASSERT(groupName.size() > 0, "Invalid group name!"); CCASSERT(!groupName.empty(), "Invalid group name!");
if (_objectGroups.size()>0) if (groupName.empty()) {
return nullptr;
}
for (auto& child : _children)
{ {
TMXObjectGroup* objectGroup = nullptr; TMXObjectGroup* group = dynamic_cast<TMXObjectGroup*>(child);
for (auto iter = _objectGroups.cbegin(); iter != _objectGroups.cend(); ++iter) if(group)
{ {
objectGroup = *iter; if(groupName.compare( group->getGroupName()) == 0)
if (objectGroup && objectGroup->getGroupName() == groupName)
{ {
return objectGroup; return group;
} }
} }
} }
@@ -249,6 +260,21 @@ TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
return nullptr; return nullptr;
} }
Vector<TMXObjectGroup*> TMXTiledMap::getObjectGroups()
{
Vector<TMXObjectGroup*> groups;
for (auto& child : _children)
{
TMXObjectGroup* group = dynamic_cast<TMXObjectGroup*>(child);
if(group)
{
groups.pushBack(group);
}
}
return groups;
}
Value TMXTiledMap::getProperty(const std::string& propertyName) const Value TMXTiledMap::getProperty(const std::string& propertyName) const
{ {
if (_properties.find(propertyName) != _properties.end()) if (_properties.find(propertyName) != _properties.end())
+1 -12
View File
@@ -242,16 +242,7 @@ public:
* *
* @return The object groups. * @return The object groups.
*/ */
inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }; Vector<TMXObjectGroup*> getObjectGroups();
inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
/** Set the object groups.
*
* @param groups The object groups.
*/
inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
_objectGroups = groups;
};
/** Properties. /** Properties.
* *
@@ -303,8 +294,6 @@ protected:
Size _tileSize; Size _tileSize;
/** map orientation */ /** map orientation */
int _mapOrientation; int _mapOrientation;
/** object groups */
Vector<TMXObjectGroup*> _objectGroups;
/** properties */ /** properties */
ValueMap _properties; ValueMap _properties;
+86 -35
View File
@@ -67,6 +67,14 @@ void TMXLayerInfo::setProperties(ValueMap var)
_properties = var; _properties = var;
} }
TMXObjectGroupInfo::TMXObjectGroupInfo()
: _groupName("")
{
}
TMXObjectGroupInfo::~TMXObjectGroupInfo()
{
CCLOGINFO("deallocing TMXObjectGroup: %p", this);
}
// implementation TMXTilesetInfo // implementation TMXTilesetInfo
TMXTilesetInfo::TMXTilesetInfo() TMXTilesetInfo::TMXTilesetInfo()
:_firstGid(0) :_firstGid(0)
@@ -384,6 +392,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
float y = attributeDict["y"].asFloat(); float y = attributeDict["y"].asFloat();
layer->_offset.set(x, y); layer->_offset.set(x, y);
tmxMapInfo->getAllChildren().pushBack(layer);
tmxMapInfo->getLayers().pushBack(layer); tmxMapInfo->getLayers().pushBack(layer);
layer->release(); layer->release();
@@ -392,13 +401,41 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
} }
else if (elementName == "objectgroup") else if (elementName == "objectgroup")
{ {
TMXObjectGroup *objectGroup = new (std::nothrow) TMXObjectGroup(); TMXObjectGroupInfo *objectGroup = new (std::nothrow) TMXObjectGroupInfo();
objectGroup->setGroupName(attributeDict["name"].asString()); objectGroup->_groupName = attributeDict["name"].asString();
Vec2 positionOffset; Vec2 positionOffset;
positionOffset.x = attributeDict["x"].asFloat() * tmxMapInfo->getTileSize().width; positionOffset.x = attributeDict["offsetx"].asFloat();
positionOffset.y = attributeDict["y"].asFloat() * tmxMapInfo->getTileSize().height; positionOffset.y = attributeDict["offsety"].asFloat();
objectGroup->setPositionOffset(positionOffset); objectGroup->_positionOffset = CC_POINT_PIXELS_TO_POINTS(positionOffset);
// object group color
Value& colorValue = attributeDict["color"];
if (colorValue.isNull()) {
objectGroup->_color = Color3B(255, 255, 255);
} else {
std::string colorStr = colorValue.asString();
auto startPos = colorStr.find("#");
if (startPos != string::npos) {
colorStr.replace(startPos, 1, "");
}
int num = stoi(colorStr.c_str(), 0, 16);
int r = num / 0x10000;
int g = (num / 0x100) % 0x100;
int b = num % 0x100;
objectGroup->_color = Color3B(r, g, b);
}
// object group opacity
Value& opacityValue = attributeDict["opacity"];
float percent = opacityValue.isNull() ? 1.0 : opacityValue.asFloat();
objectGroup->_opacity = 255 * percent;
// object group visible
Value& visibleValue = attributeDict["visible"];
objectGroup->_visible = visibleValue.isNull() ? true : visibleValue.asBool();
tmxMapInfo->getAllChildren().pushBack(objectGroup);
tmxMapInfo->getObjectGroups().pushBack(objectGroup); tmxMapInfo->getObjectGroups().pushBack(objectGroup);
objectGroup->release(); objectGroup->release();
@@ -476,7 +513,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
} }
else if (elementName == "object") else if (elementName == "object")
{ {
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back(); TMXObjectGroupInfo* objectGroup = tmxMapInfo->getObjectGroups().back();
// The value for "type" was blank or not a valid class name // The value for "type" was blank or not a valid class name
// Create an instance of TMXObjectInfo to store the object and its properties // Create an instance of TMXObjectInfo to store the object and its properties
@@ -492,24 +529,36 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// But X and Y since they need special treatment // But X and Y since they need special treatment
// X // X
int x = attributeDict["x"].asInt(); int x = attributeDict["x"].asFloat();
// Y // Y
int y = attributeDict["y"].asInt(); int y = attributeDict["y"].asFloat();
Vec2 p(x + objectGroup->getPositionOffset().x, _mapSize.height * _tileSize.height - y - objectGroup->getPositionOffset().y - attributeDict["height"].asInt()); dict["x"] = Value(x);
p = CC_POINT_PIXELS_TO_POINTS(p); dict["y"] = Value(y);
dict["x"] = Value(p.x);
dict["y"] = Value(p.y);
int width = attributeDict["width"].asInt(); int width = attributeDict["width"].asFloat();
int height = attributeDict["height"].asInt(); int height = attributeDict["height"].asFloat();
Size s(width, height); dict["width"] = Value(width);
s = CC_SIZE_PIXELS_TO_POINTS(s); dict["height"] = Value(height);
dict["width"] = Value(s.width);
dict["height"] = Value(s.height); // visible
Value& visibleValue = attributeDict["visible"];
dict["visible"] = Value(visibleValue.isNull() ? true : visibleValue.asBool());
// rotation
Value& rotationValue = attributeDict["rotation"];
dict["rotation"] = Value(rotationValue.isNull() ? 0.0 : rotationValue.asFloat());
// default type is rect
dict["type"] = Value(static_cast<int>(TMXObjectType::RECT));
// if has gid, the type is image
if (!dict["gid"].isNull()) {
dict["type"] = Value(static_cast<int>(TMXObjectType::IMAGE));
}
// Add the object to the objectGroup // Add the object to the objectGroup
objectGroup->getObjects().push_back(Value(dict)); objectGroup->_objects.push_back(Value(dict));
// The parent element is now "object" // The parent element is now "object"
tmxMapInfo->setParentElement(TMXPropertyObject); tmxMapInfo->setParentElement(TMXPropertyObject);
@@ -540,7 +589,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if ( tmxMapInfo->getParentElement() == TMXPropertyObjectGroup ) else if ( tmxMapInfo->getParentElement() == TMXPropertyObjectGroup )
{ {
// The parent element is the last object group // The parent element is the last object group
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back(); TMXObjectGroupInfo* objectGroup = tmxMapInfo->getObjectGroups().back();
Value value = attributeDict["value"]; Value value = attributeDict["value"];
std::string key = attributeDict["name"].asString(); std::string key = attributeDict["name"].asString();
objectGroup->getProperties().insert(std::make_pair(key, value)); objectGroup->getProperties().insert(std::make_pair(key, value));
@@ -548,8 +597,8 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if ( tmxMapInfo->getParentElement() == TMXPropertyObject ) else if ( tmxMapInfo->getParentElement() == TMXPropertyObject )
{ {
// The parent element is the last object // The parent element is the last object
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back(); TMXObjectGroupInfo* objectGroup = tmxMapInfo->getObjectGroups().back();
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap(); ValueMap& dict = objectGroup->_objects.rbegin()->asValueMap();
std::string propertyName = attributeDict["name"].asString(); std::string propertyName = attributeDict["name"].asString();
dict[propertyName] = attributeDict["value"]; dict[propertyName] = attributeDict["value"];
@@ -565,8 +614,9 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if (elementName == "polygon") else if (elementName == "polygon")
{ {
// find parent object's dict and add polygon-points to it // find parent object's dict and add polygon-points to it
TMXObjectGroup* objectGroup = _objectGroups.back(); TMXObjectGroupInfo* objectGroup = _objectGroups.back();
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap(); ValueMap& dict = objectGroup->_objects.rbegin()->asValueMap();
dict["type"] = Value(static_cast<int>(TMXObjectType::POLYGON));
// get points value string // get points value string
std::string value = attributeDict["points"].asString(); std::string value = attributeDict["points"].asString();
@@ -589,15 +639,13 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// set x // set x
if (std::getline(pointStream, xStr, ',')) if (std::getline(pointStream, xStr, ','))
{ {
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x; pointDict["x"] = Value(atof(xStr.c_str()));
pointDict["x"] = Value(x);
} }
// set y // set y
if (std::getline(pointStream, yStr, ',')) if (std::getline(pointStream, yStr, ','))
{ {
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y; pointDict["y"] = Value(atof(yStr.c_str()));
pointDict["y"] = Value(y);
} }
// add to points array // add to points array
@@ -610,9 +658,9 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if (elementName == "polyline") else if (elementName == "polyline")
{ {
// find parent object's dict and add polyline-points to it // find parent object's dict and add polyline-points to it
TMXObjectGroup* objectGroup = _objectGroups.back(); TMXObjectGroupInfo* objectGroup = _objectGroups.back();
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap(); ValueMap& dict = objectGroup->_objects.rbegin()->asValueMap();
dict["type"] = Value(static_cast<int>(TMXObjectType::POLYLINE));
// get points value string // get points value string
std::string value = attributeDict["points"].asString(); std::string value = attributeDict["points"].asString();
if (!value.empty()) if (!value.empty())
@@ -634,15 +682,13 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// set x // set x
if (std::getline(pointStream, xStr, ',')) if (std::getline(pointStream, xStr, ','))
{ {
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x; pointDict["x"] = Value(atof(xStr.c_str()));
pointDict["x"] = Value(x);
} }
// set y // set y
if (std::getline(pointStream, yStr, ',')) if (std::getline(pointStream, yStr, ','))
{ {
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y; pointDict["y"] = Value(atof(yStr.c_str()));
pointDict["y"] = Value(y);
} }
// add to points array // add to points array
@@ -652,6 +698,11 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
dict["polylinePoints"] = Value(pointsArray); dict["polylinePoints"] = Value(pointsArray);
} }
} }
else if (elementName == "ellipse") {
TMXObjectGroupInfo* objectGroup = _objectGroups.back();
ValueMap& dict = objectGroup->_objects.rbegin()->asValueMap();
dict["type"] = Value(static_cast<int>(TMXObjectType::ELLIPSE));
}
} }
void TMXMapInfo::endElement(void *ctx, const char *name) void TMXMapInfo::endElement(void *ctx, const char *name)
+55 -4
View File
@@ -118,6 +118,48 @@ public:
Vec2 _offset; Vec2 _offset;
}; };
class CC_DLL TMXObjectGroupInfo : public Ref
{
public:
/**
* @js ctor
*/
TMXObjectGroupInfo();
/**
* @js NA
* @lua NA
*/
virtual ~TMXObjectGroupInfo();
/** Gets the list of properties stored in a dictionary.
*
* @return The list of properties stored in a dictionary.
*/
inline ValueMap& getProperties() { return _properties; };
/** Sets the list of properties.
*
* @param properties The list of properties.
*/
inline void setProperties(const ValueMap& properties) {
_properties = properties;
};
public:
/** name of the group */
std::string _groupName;
/** offset position of child objects */
Vec2 _positionOffset;
/** list of properties stored in a dictionary */
ValueMap _properties;
/** array of the objects */
ValueVector _objects;
bool _visible;
Color3B _color;
unsigned char _opacity;
};
/** @brief TMXTilesetInfo contains the information about the tilesets like: /** @brief TMXTilesetInfo contains the information about the tilesets like:
- Tileset name - Tileset name
- Tileset spacing - Tileset spacing
@@ -238,12 +280,19 @@ public:
}; };
/// ObjectGroups /// ObjectGroups
inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; }; inline const Vector<TMXObjectGroupInfo*>& getObjectGroups() const { return _objectGroups; };
inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; }; inline Vector<TMXObjectGroupInfo*>& getObjectGroups() { return _objectGroups; };
inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) { inline void setObjectGroups(const Vector<TMXObjectGroupInfo*>& groups) {
_objectGroups = groups; _objectGroups = groups;
}; };
/// all children
inline const Vector<Ref*>& getAllChildren() const { return _allChildren; };
inline Vector<Ref*>& getAllChildren() { return _allChildren; };
inline void setAllChildren(const Vector<Ref*>& children) {
_allChildren = children;
};
/// parent element /// parent element
inline int getParentElement() const { return _parentElement; }; inline int getParentElement() const { return _parentElement; };
inline void setParentElement(int element) { _parentElement = element; }; inline void setParentElement(int element) { _parentElement = element; };
@@ -311,7 +360,9 @@ protected:
/// tilesets /// tilesets
Vector<TMXTilesetInfo*> _tilesets; Vector<TMXTilesetInfo*> _tilesets;
/// ObjectGroups /// ObjectGroups
Vector<TMXObjectGroup*> _objectGroups; Vector<TMXObjectGroupInfo*> _objectGroups;
/// all children
Vector<Ref*> _allChildren;
/// parent element /// parent element
int _parentElement; int _parentElement;
/// parent GID /// parent GID