Skip to content

Commit

Permalink
Add support for the creation of layers that are set to invisible in t…
Browse files Browse the repository at this point in the history
…ilemap (axmolengine#2321)

Add support for the creation of layers that are set to invisible in the TMX file
  • Loading branch information
rh101 authored Jan 10, 2025
1 parent 2cdfe88 commit 2212048
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
64 changes: 36 additions & 28 deletions core/2d/FastTMXTiledMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace ax

// implementation FastTMXTiledMap

FastTMXTiledMap* FastTMXTiledMap::create(std::string_view tmxFile)
FastTMXTiledMap* FastTMXTiledMap::create(std::string_view tmxFile, bool allowInvisibleLayers)
{
FastTMXTiledMap* ret = new FastTMXTiledMap();
if (ret->initWithTMXFile(tmxFile))
if (ret->initWithTMXFile(tmxFile, allowInvisibleLayers))
{
ret->autorelease();
return ret;
Expand All @@ -47,10 +47,12 @@ FastTMXTiledMap* FastTMXTiledMap::create(std::string_view tmxFile)
return nullptr;
}

FastTMXTiledMap* FastTMXTiledMap::createWithXML(std::string_view tmxString, std::string_view resourcePath)
FastTMXTiledMap* FastTMXTiledMap::createWithXML(std::string_view tmxString,
std::string_view resourcePath,
bool allowInvisibleLayers)
{
FastTMXTiledMap* ret = new FastTMXTiledMap();
if (ret->initWithXML(tmxString, resourcePath))
if (ret->initWithXML(tmxString, resourcePath, allowInvisibleLayers))
{
ret->autorelease();
return ret;
Expand All @@ -59,9 +61,9 @@ FastTMXTiledMap* FastTMXTiledMap::createWithXML(std::string_view tmxString, std:
return nullptr;
}

bool FastTMXTiledMap::initWithTMXFile(std::string_view tmxFile)
bool FastTMXTiledMap::initWithTMXFile(std::string_view tmxFile, bool allowInvisibleLayers)
{
AXASSERT(tmxFile.size() > 0, "FastTMXTiledMap: tmx file should not be empty");
AXASSERT(!tmxFile.empty(), "FastTMXTiledMap: tmx file should not be empty");

setContentSize(Vec2::ZERO);

Expand All @@ -72,26 +74,28 @@ bool FastTMXTiledMap::initWithTMXFile(std::string_view tmxFile)
return false;
}
AXASSERT(!mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
buildWithMapInfo(mapInfo);
buildWithMapInfo(mapInfo, allowInvisibleLayers);

_tmxFile = tmxFile;

return true;
}

bool FastTMXTiledMap::initWithXML(std::string_view tmxString, std::string_view resourcePath)
bool FastTMXTiledMap::initWithXML(std::string_view tmxString, std::string_view resourcePath, bool allowInvisibleLayers)
{
setContentSize(Vec2::ZERO);

TMXMapInfo* mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);

AXASSERT(!mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
buildWithMapInfo(mapInfo);
buildWithMapInfo(mapInfo, allowInvisibleLayers);

return true;
}

FastTMXTiledMap::FastTMXTiledMap() : _mapSize(Vec2::ZERO), _tileSize(Vec2::ZERO), _mapInfo(nullptr) {}
FastTMXTiledMap::FastTMXTiledMap() : _mapSize(Vec2::ZERO), _tileSize(Vec2::ZERO), _mapOrientation(0), _mapInfo(nullptr)
{
}

FastTMXTiledMap::~FastTMXTiledMap()
{
Expand Down Expand Up @@ -157,7 +161,7 @@ TMXTilesetInfo* FastTMXTiledMap::tilesetForLayer(TMXLayerInfo* layerInfo, TMXMap
return nullptr;
}

void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo, bool allowInvisibleLayers)
{
_mapSize = mapInfo->getMapSize();
_tileSize = mapInfo->getTileSize();
Expand All @@ -174,25 +178,29 @@ void FastTMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
auto& layers = mapInfo->getLayers();
for (const auto& layerInfo : layers)
{
if (layerInfo->_visible)
if (!layerInfo->_visible && !allowInvisibleLayers)
{
FastTMXLayer* child = parseLayer(layerInfo, mapInfo);
if (child == nullptr)
{
idx++;
continue;
}
addChild(child, idx, idx);

// update content size with the max size
const Vec2& childSize = child->getContentSize();
Vec2 currentSize = this->getContentSize();
currentSize.width = std::max(currentSize.width, childSize.width);
currentSize.height = std::max(currentSize.height, childSize.height);
this->setContentSize(currentSize);
continue;
}

FastTMXLayer* child = parseLayer(layerInfo, mapInfo);
if (child == nullptr)
{
idx++;
continue;
}
addChild(child, idx, idx);

child->setVisible(layerInfo->_visible);

// update content size with the max size
const Vec2& childSize = child->getContentSize();
Vec2 currentSize = this->getContentSize();
currentSize.width = std::max(currentSize.width, childSize.width);
currentSize.height = std::max(currentSize.height, childSize.height);
this->setContentSize(currentSize);

idx++;
}

_layerCount = idx;
Expand All @@ -211,7 +219,7 @@ FastTMXLayer* FastTMXTiledMap::getLayer(std::string_view layerName) const
FastTMXLayer* layer = dynamic_cast<FastTMXLayer*>(child);
if (layer)
{
if (layerName.compare(layer->getLayerName()) == 0)
if (layerName == layer->getLayerName())
{
return layer;
}
Expand All @@ -226,7 +234,7 @@ TMXObjectGroup* FastTMXTiledMap::getObjectGroup(std::string_view groupName) cons
{
AXASSERT(!groupName.empty(), "Invalid group name!");

if (_objectGroups.size() > 0)
if (!_objectGroups.empty())
{
for (const auto& objectGroup : _objectGroups)
{
Expand Down
15 changes: 9 additions & 6 deletions core/2d/FastTMXTiledMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,18 @@ class AX_DLL FastTMXTiledMap : public Node
*
* @return An autorelease object.
*/
static FastTMXTiledMap* create(std::string_view tmxFile);
static FastTMXTiledMap* create(std::string_view tmxFile, bool allowInvisibleLayers = false);

/** Initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources.
*
* @param tmxString A TMX formatted XML string.
* @param resourcePath A path to TMX resources.
* @param allowInvisibleLayers Allow creation of invisible layers. Default is false.
* @return An autorelease object.
*/
static FastTMXTiledMap* createWithXML(std::string_view tmxString, std::string_view resourcePath);
static FastTMXTiledMap* createWithXML(std::string_view tmxString,
std::string_view resourcePath,
bool allowInvisibleLayers = false);

/** Return the FastTMXLayer for the specific layer.
*
Expand Down Expand Up @@ -225,18 +228,18 @@ class AX_DLL FastTMXTiledMap : public Node
* @js NA
* @lua NA
*/
virtual ~FastTMXTiledMap();
virtual ~FastTMXTiledMap() override;

/** initializes a TMX Tiled Map with a TMX file */
bool initWithTMXFile(std::string_view tmxFile);
bool initWithTMXFile(std::string_view tmxFile, bool allowInvisibleLayers = false);

/** initializes a TMX Tiled Map with a TMX formatted XML string and a path to TMX resources */
bool initWithXML(std::string_view tmxString, std::string_view resourcePath);
bool initWithXML(std::string_view tmxString, std::string_view resourcePath, bool allowInvisibleLayers = false);

protected:
FastTMXLayer* parseLayer(TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo);
TMXTilesetInfo* tilesetForLayer(TMXLayerInfo* layerInfo, TMXMapInfo* mapInfo);
void buildWithMapInfo(TMXMapInfo* mapInfo);
void buildWithMapInfo(TMXMapInfo* mapInfo, bool ignoreInvisibleLayers);

/** the map's size property measured in tiles */
Vec2 _mapSize;
Expand Down

0 comments on commit 2212048

Please sign in to comment.