From cabcc205e9b37f5ec52f99fce19bbd2978fc50ce Mon Sep 17 00:00:00 2001 From: zhangbin Date: Tue, 13 Sep 2016 16:11:19 +0800 Subject: [PATCH] Improve the implementation of showing Object Group in TiledMap. --- native/cocos/2d/CCTMXObjectGroup.cpp | 159 +++++++++++++-------------- native/cocos/2d/CCTMXObjectGroup.h | 71 +++++++----- native/cocos/2d/CCTMXXMLParser.cpp | 2 +- native/tools/tojs/cocos2dx.ini | 5 +- 4 files changed, 126 insertions(+), 111 deletions(-) diff --git a/native/cocos/2d/CCTMXObjectGroup.cpp b/native/cocos/2d/CCTMXObjectGroup.cpp index 3e42f5a538..11cfc240d8 100644 --- a/native/cocos/2d/CCTMXObjectGroup.cpp +++ b/native/cocos/2d/CCTMXObjectGroup.cpp @@ -36,7 +36,9 @@ THE SOFTWARE. NS_CC_BEGIN //implementation TMXObject -TMXObject::TMXObject(ValueMap objectInfo) +TMXObject::TMXObject(ValueMap objectInfo, TMXMapInfo* mapInfo, const Size& groupSize, const Color3B& color) +: _node(nullptr) +, _groupSize(groupSize) { _objectName = objectInfo["name"].asString(); _type = static_cast(objectInfo["type"].asInt()); @@ -47,11 +49,21 @@ TMXObject::TMXObject(ValueMap objectInfo) _objectVisible = objectInfo["visible"].asBool(); _objectRotation = objectInfo["rotation"].asFloat(); setProperties(objectInfo); + + if (TMXObjectType::IMAGE == _type) + { + _node = new TMXObjectImage(this, mapInfo); + } + else + { + _node = new TMXObjectShape(this, mapInfo, color); + } } TMXObject::~TMXObject() { CCLOGINFO("deallocing TMXObject: %p", this); + CC_SAFE_RELEASE(_node); } Value TMXObject::getProperty(const std::string& propertyName) const @@ -63,9 +75,8 @@ Value TMXObject::getProperty(const std::string& propertyName) const } //implementation TMXObjectImage -TMXObjectImage::TMXObjectImage(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize) -: TMXObject(objectInfo) -, _groupSize(groupSize) +TMXObjectImage::TMXObjectImage(TMXObject* container, TMXMapInfo* mapInfo) +: _container(container) { _initWithMapInfo(mapInfo); } @@ -77,7 +88,7 @@ TMXObjectImage::~TMXObjectImage() bool TMXObjectImage::_initWithMapInfo(TMXMapInfo* mapInfo) { - if (_gid == 0) { + if (_container->getGid() == 0) { return false; } @@ -89,7 +100,7 @@ bool TMXObjectImage::_initWithMapInfo(TMXMapInfo* mapInfo) for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter) { tileset = *iter; - if( (_gid & kTMXFlippedMask) >= tileset->_firstGid ) { + if( (_container->getGid() & kTMXFlippedMask) >= tileset->_firstGid ) { useTileset = tileset; break; } @@ -100,24 +111,24 @@ bool TMXObjectImage::_initWithMapInfo(TMXMapInfo* mapInfo) return false; } - setVisible(_objectVisible); + setVisible(_container->getObjectVisible()); auto texture = Director::getInstance()->getTextureCache()->addImage(useTileset->_sourceImage); if (texture == nullptr) { return false; } - auto rect = useTileset->getRectForGID(_gid); + auto rect = useTileset->getRectForGID(_container->getGid()); initWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(rect)); - setScale(_objectSize.width / rect.size.width, _objectSize.height / rect.size.height); - setRotation(_objectRotation); + setScale(_container->getObjectSize().width / rect.size.width, _container->getObjectSize().height / rect.size.height); + setRotation(_container->getObjectRotation()); setFlippedX(false); - if (_gid & kTMXTileHorizontalFlag) { + if (_container->getGid() & kTMXTileHorizontalFlag) { setFlippedX(true); } setFlippedY(false); - if (_gid & kTMXTileVerticalFlag) { + if (_container->getGid() & kTMXTileVerticalFlag) { setFlippedY(true); } @@ -136,15 +147,15 @@ void TMXObjectImage::_initPosWithMapInfo(TMXMapInfo* mapInfo) case TMXOrientationHex: case TMXOrientationStaggered: { - Vec2 offsetInPoints = CC_POINT_PIXELS_TO_POINTS(_offset); + Vec2 offsetInPoints = CC_POINT_PIXELS_TO_POINTS(_container->getOffset()); setAnchorPoint(Vec2(0, 0)); - setPosition(Vec2(offsetInPoints.x, _groupSize.height - offsetInPoints.y)); + setPosition(Vec2(offsetInPoints.x, _container->getGroupSize().height - offsetInPoints.y)); } break; case TMXOrientationIso: { setAnchorPoint(Vec2(0.5, 0)); - auto posIdx = Vec2(_offset.x / tileSize.width * 2, _offset.y / tileSize.height); + auto posIdx = Vec2(_container->getOffset().x / tileSize.width * 2, _container->getOffset().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)); @@ -156,37 +167,36 @@ void TMXObjectImage::_initPosWithMapInfo(TMXMapInfo* mapInfo) } //implementation TMXObjectShape -TMXObjectShape::TMXObjectShape(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize, const Color3B& color) -: TMXObject(objectInfo) -, DrawNode(1) +TMXObjectShape::TMXObjectShape(TMXObject* container, TMXMapInfo* mapInfo, const Color3B& color) +: DrawNode(1) +, _container(container) { _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); + _initShape(); } TMXObjectShape::~TMXObjectShape() { CCLOGINFO("deallocing TMXObjectShape: %p", this); } -void TMXObjectShape::_initShape(const ValueMap& objectInfo) +void TMXObjectShape::_initShape() { DrawNode::init(); - + Vec2 originPos; if (TMXOrientationIso != _mapOrientation) { - Vec2 startPos = Vec2(0, _groupSize.height); - Vec2 offsetInPoints = CC_POINT_PIXELS_TO_POINTS(_offset); + Vec2 startPos = Vec2(0, _container->getGroupSize().height); + Vec2 offsetInPoints = CC_POINT_PIXELS_TO_POINTS(_container->getOffset()); originPos = Vec2(startPos.x + offsetInPoints.x, startPos.y - offsetInPoints.y); } else { originPos = _getPosByOffset(Vec2(0, 0)); } setPosition(originPos); - setRotation(_objectRotation); + setRotation(_container->getObjectRotation()); - switch (_type) { + switch (_container->getType()) { case TMXObjectType::RECT: _drawRect(); break; @@ -194,22 +204,22 @@ void TMXObjectShape::_initShape(const ValueMap& objectInfo) _drawEllipse(); break; case TMXObjectType::POLYGON: - _drawPoly(objectInfo, originPos, true); + _drawPoly(originPos, true); break; case TMXObjectType::POLYLINE: - _drawPoly(objectInfo, originPos, false); + _drawPoly(originPos, false); break; default: break; } - setVisible(_objectVisible); + setVisible(_container->getObjectVisible()); } 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); + auto posIdx = Vec2((_container->getOffset().x + offset.x) / tileSize.width * 2, (_container->getOffset().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))); } @@ -217,7 +227,7 @@ Vec2 TMXObjectShape::_getPosByOffset(const Vec2& offset) void TMXObjectShape::_drawRect() { if (TMXOrientationIso != _mapOrientation) { - auto objSize = _objectSize; + auto objSize = _container->getObjectSize(); if (objSize.equals(Size::ZERO)) { objSize = Size(20, 20); setAnchorPoint(Vec2(0.5, 0.5)); @@ -231,14 +241,15 @@ void TMXObjectShape::_drawRect() setContentSize(objSizeInPoints); } else { - if (_objectSize.equals(Size::ZERO)) { + auto objSize = _container->getObjectSize(); + if (objSize.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)); + auto pos2 = _getPosByOffset(Vec2(objSize.width, 0)); + auto pos3 = _getPosByOffset(Vec2(objSize.width, objSize.height)); + auto pos4 = _getPosByOffset(Vec2(0, objSize.height)); float width = pos2.x - pos4.x, height = pos1.y - pos3.y; setContentSize(Size(width, height)); @@ -249,12 +260,12 @@ void TMXObjectShape::_drawRect() pos2.subtract(origin); pos3.subtract(origin); pos4.subtract(origin); - if (_objectSize.width > 0) { + if (objSize.width > 0) { drawLine(pos1, pos2, _groupColor); drawLine(pos3, pos4, _groupColor); } - if (_objectSize.height > 0) { + if (objSize.height > 0) { drawLine(pos1, pos4, _groupColor); drawLine(pos3, pos2, _groupColor); } @@ -264,7 +275,7 @@ void TMXObjectShape::_drawRect() void TMXObjectShape::_drawEllipse() { if (TMXOrientationIso != _mapOrientation) { - auto objSize = _objectSize; + auto objSize = _container->getObjectSize(); if (objSize.equals(Size::ZERO)) { objSize = Size(20, 20); setAnchorPoint(Vec2(0.5, 0.5)); @@ -286,15 +297,16 @@ void TMXObjectShape::_drawEllipse() setContentSize(objSizeInPoints); } else { - if (_objectSize.equals(Size::ZERO)) { + auto objSize = _container->getObjectSize(); + if (objSize.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)); + auto pos2 = _getPosByOffset(Vec2(objSize.width, 0)); + auto pos3 = _getPosByOffset(Vec2(objSize.width, objSize.height)); + auto pos4 = _getPosByOffset(Vec2(0, objSize.height)); float width = pos2.x - pos4.x, height = pos1.y - pos3.y; setContentSize(Size(width, height)); @@ -305,19 +317,19 @@ void TMXObjectShape::_drawEllipse() pos2.subtract(origin); pos3.subtract(origin); pos4.subtract(origin); - if (_objectSize.width > 0) { + if (objSize.width > 0) { drawLine(pos1, pos2, _groupColor); drawLine(pos3, pos4, _groupColor); } - if (_objectSize.height > 0) { + if (objSize.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)); + Size objSizeInPoints = CC_SIZE_PIXELS_TO_POINTS(objSize); + Vec2 center = _getPosByOffset(Vec2(objSize.width / 2, objSize.height / 2)); center.subtract(origin); auto ellipseNode = DrawNode::create(_lineWidth); @@ -345,9 +357,10 @@ void TMXObjectShape::_drawEllipse() } } -void TMXObjectShape::_drawPoly(const ValueMap& objectInfo, const Vec2& originPos, bool isPolygon) +void TMXObjectShape::_drawPoly(const Vec2& originPos, bool isPolygon) { // parse the data + auto objectInfo = _container->getProperties(); ValueVector pointsData; if (isPolygon) pointsData = objectInfo.at("points").asValueVector(); @@ -413,38 +426,20 @@ TMXObjectGroup::~TMXObjectGroup() TMXObject* TMXObjectGroup::getObject(const std::string& objectName) { - CCASSERT(objectName.size() > 0, "Invalid object name!"); - for (auto& child : _children) + CCASSERT(!objectName.empty(), "Invalid object name!"); + + for (auto& obj : _objects) { - TMXObject* object = dynamic_cast(child); - if(object) + if(obj && objectName.compare(obj->getObjectName()) == 0) { - if(objectName.compare( object->getObjectName()) == 0) - { - return object; - } + return obj; } } - + // object not found return nullptr; } -Vector TMXObjectGroup::getObjects() -{ - Vector objects; - for (auto& child : _children) - { - TMXObject* object = dynamic_cast(child); - if(object) - { - objects.pushBack(object); - } - } - - return objects; -} - Value TMXObjectGroup::getProperty(const std::string& propertyName) const { if (_properties.find(propertyName) != _properties.end()) @@ -495,18 +490,20 @@ void TMXObjectGroup::_initGroup(TMXObjectGroupInfo* groupInfo, TMXMapInfo* mapIn int idx = 0; for (auto& object : objects) { auto objectInfo = object.asValueMap(); - Node* obj = nullptr; - if (objectInfo["type"].asInt() == static_cast(TMXObjectType::IMAGE)) { - obj = new TMXObjectImage(objectInfo, mapInfo, getContentSize()); - } else { - obj = new TMXObjectShape(objectInfo, mapInfo, getContentSize(), groupInfo->_color); + auto obj = new TMXObject(objectInfo, mapInfo, getContentSize(), groupInfo->_color); + + // record the object + this->_objects.pushBack(obj); + obj->release(); + auto objNode = obj->getNode(); + if (objNode) + { + // add the node of object if existed + addChild(objNode, idx, idx); + objNode->setOpacity(groupInfo->_opacity); + idx++; } - obj->autorelease(); - obj->setOpacity(groupInfo->_opacity); - addChild(obj, idx, idx); - idx++; } } NS_CC_END - diff --git a/native/cocos/2d/CCTMXObjectGroup.h b/native/cocos/2d/CCTMXObjectGroup.h index d284df1268..b6aa4a9442 100644 --- a/native/cocos/2d/CCTMXObjectGroup.h +++ b/native/cocos/2d/CCTMXObjectGroup.h @@ -47,6 +47,7 @@ enum class TMXObjectType { class TMXMapInfo; class TMXObjectGroupInfo; class Color4F; + /** * @addtogroup _2d * @{ @@ -58,7 +59,7 @@ class Color4F; class CC_DLL TMXObject: public Ref { public: - TMXObject(ValueMap objectInfo); + TMXObject(ValueMap objectInfo, TMXMapInfo* mapInfo, const Size& groupSize, const Color3B& color); virtual ~TMXObject(); inline const std::string& getObjectName() { return _objectName; }; @@ -69,13 +70,16 @@ public: inline const ValueMap& getProperties() const { return _properties; }; inline void setProperties(const ValueMap& properties) { _properties = properties; }; - inline TMXObjectType getObjectType() { return _type; }; + inline TMXObjectType getType() { 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; }; + + inline Node* getNode() { return _node; }; + inline const Size& getGroupSize() { return _groupSize; }; protected: TMXObjectType _type; @@ -87,51 +91,59 @@ protected: ValueMap _properties; bool _objectVisible; float _objectRotation; + + Node* _node; + Size _groupSize; }; /** @brief TMXObjectImage represents the TMX object image. * @since v3.12 */ -class CC_DLL TMXObjectImage : public Sprite, public TMXObject +class CC_DLL TMXObjectImage : public Sprite { public: - TMXObjectImage(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize); + TMXObjectImage(TMXObject* container, TMXMapInfo* mapInfo); virtual ~TMXObjectImage(); protected: bool _initWithMapInfo(TMXMapInfo* mapInfo); void _initPosWithMapInfo(TMXMapInfo* mapInfo); - Size _groupSize; + TMXObject* _container; }; /** @brief TMXObjectShape represents the TMX object shapes. * @since v3.12 */ -class CC_DLL TMXObjectShape : public DrawNode, public TMXObject +class CC_DLL TMXObjectShape : public DrawNode { public: - TMXObjectShape(const ValueMap& objectInfo, TMXMapInfo* mapInfo, const Size& groupSize, const Color3B& color); + TMXObjectShape(TMXObject* container, TMXMapInfo* mapInfo, const Color3B& color); virtual ~TMXObjectShape(); protected: - void _initShape(const ValueMap& objectInfo); + void _initShape(); void _drawRect(); void _drawEllipse(); - void _drawPoly(const ValueMap& objectInfo, const Vec2& originPos, bool isPolygon); + void _drawPoly(const Vec2& originPos, bool isPolygon); // internal methods for iso map Vec2 _getPosByOffset(const Vec2& offset); int _mapOrientation; - Size _groupSize; Color4F _groupColor; + TMXObject* _container; private: TMXMapInfo* _mapInfo; }; +/** + * @addtogroup _2d + * @{ + */ + /** @brief TMXObjectGroup represents the TMX object group. * @since v0.99.0 */ @@ -147,26 +159,28 @@ public: * @lua NA */ virtual ~TMXObjectGroup(); - - /** Get the group name. + + /** Get the group name. * * @return The group name. */ inline const std::string& getGroupName() const { return _groupName; } - - /** Set the group name. + + /** Set the group name. * * @param groupName A string,it is used to set the group name. */ inline void setGroupName(const std::string& groupName){ _groupName = groupName; } - /** Return the value for the specific property name. + /** Return the value for the specific property name. * * @param propertyName The specific property name. * @return Return the value for the specific property name. * @js NA */ Value getProperty(const std::string& propertyName) const; + + CC_DEPRECATED_ATTRIBUTE Value propertyNamed(const std::string& propertyName) const { return getProperty(propertyName); }; /** Return the dictionary for the specific object name. * It will return the 1st object found on the array for the given name. @@ -174,26 +188,28 @@ public: * @return Return the dictionary for the specific object name. */ TMXObject* getObject(const std::string& objectName); - - /** Gets the offset position of child objects. + + CC_DEPRECATED_ATTRIBUTE TMXObject* objectNamed(const std::string& objectName) { return getObject(objectName); }; + + /** Gets the offset position of child objects. * * @return The offset position of child objects. */ inline const Vec2& getPositionOffset() const { return _positionOffset; }; - - /** Sets the offset position of child objects. + + /** Sets the offset position of child objects. * * @param offset The offset position of child objects. */ inline void setPositionOffset(const Vec2& offset) { _positionOffset = offset; }; - - /** Gets the list of properties stored in a dictionary. + + /** Gets the list of properties stored in a dictionary. * * @return The list of properties stored in a dictionary. */ inline const ValueMap& getProperties() const { return _properties; }; inline ValueMap& getProperties() { return _properties; }; - + /** Sets the list of properties. * * @param properties The list of properties. @@ -201,13 +217,13 @@ public: inline void setProperties(const ValueMap& properties) { _properties = properties; }; - - /** Gets the array of the objects. + + /** Gets the array of the objects. * * @return The array of the objects. */ - Vector getObjects(); - + inline const Vector getObjects() { return _objects; }; + protected: void _initGroup(TMXObjectGroupInfo* groupInfo, TMXMapInfo* mapInfo); @@ -217,6 +233,8 @@ protected: Vec2 _positionOffset; /** list of properties stored in a dictionary */ ValueMap _properties; + + Vector _objects; }; // end of tilemap_parallax_nodes group @@ -225,4 +243,3 @@ protected: NS_CC_END #endif //__CCTMX_OBJECT_GROUP_H__ - diff --git a/native/cocos/2d/CCTMXXMLParser.cpp b/native/cocos/2d/CCTMXXMLParser.cpp index bd28548956..353d14683b 100644 --- a/native/cocos/2d/CCTMXXMLParser.cpp +++ b/native/cocos/2d/CCTMXXMLParser.cpp @@ -420,7 +420,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts) } // Android NDK 10 doesn't support std::stoi a/ std::stoul #if CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID - int num = std::stoi(colorStr); + int num = std::stoi(colorStr, nullptr, 16); #else int num = atoi(colorStr.c_str()); #endif diff --git a/native/tools/tojs/cocos2dx.ini b/native/tools/tojs/cocos2dx.ini index 9b1be4d5e2..7b21ff7197 100644 --- a/native/tools/tojs/cocos2dx.ini +++ b/native/tools/tojs/cocos2dx.ini @@ -127,7 +127,8 @@ skip = Node::[^setPosition$ setGLServerState description getUserObject .*UserDat ShuffleTiles::[shuffle placeTile], TurnOffTiles::[shuffle], ParallaxNode::[getParallaxArray setParallaxArray], - TileMapAtlas::[getTGAInfo setTGAInfo] + TileMapAtlas::[getTGAInfo setTGAInfo], + TMXObject::[getGroupSize] rename_functions = SpriteFrameCache::[addSpriteFramesWithFile=addSpriteFrames getSpriteFrameByName=getSpriteFrame], MenuItemFont::[setFontNameObj=setFontName setFontSizeObj=setFontSize getFontSizeObj=getFontSize getFontNameObj=getFontName], @@ -171,7 +172,7 @@ base_classes_to_skip = Ref Clonable # classes that create no constructor # Set is special and we will use a hand-written constructor -abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application GLViewProtocol GLView ComponentContainer SAXParser Configuration EventListener AsyncTaskPool TMXObjectImage TMXObjectShape +abstract_classes = Action FiniteTimeAction ActionInterval ActionEase EaseRateAction EaseElastic EaseBounce ActionInstant GridAction Grid3DAction TiledGrid3DAction Director SpriteFrameCache TransitionEaseScene Set SimpleAudioEngine FileUtils Application GLViewProtocol GLView ComponentContainer SAXParser Configuration EventListener AsyncTaskPool # Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'. script_control_cpp = yes