Skip to content

Commit

Permalink
Merge pull request #127 from mpartel/more-classes
Browse files Browse the repository at this point in the history
Support for `class` attribute on `Map`, `Tileset` and any `Layer`
  • Loading branch information
fallahn committed Jun 18, 2023
2 parents c3f9984 + 92b3691 commit 56c62c2
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ ReleaseStatic
/tmxlite/bin
Debug
Release
build

*.swp
/.vscode
/.vs/tmxlite
/tmxlite/tmxlite.vcxproj.user
6 changes: 3 additions & 3 deletions ParseTest/maps/platform.tmx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="left-down" width="200" height="40" tilewidth="64" tileheight="64" nextobjectid="46">
<map version="1.0" class="TestMapClass" orientation="orthogonal" renderorder="left-down" width="200" height="40" tilewidth="64" tileheight="64" nextobjectid="46">
<properties>
<property name="test map property" type="float" value="12.43"/>
</properties>
<tileset firstgid="1" source="../images/tilemap/platform.tsx"/>
<tileset firstgid="43" name="tileset02" tilewidth="32" tileheight="32" tilecount="24" columns="6">
<tileset firstgid="43" name="tileset02" class="TestTilesetClass" tilewidth="32" tileheight="32" tilecount="24" columns="6">
<image source="../images/tilemap/tileset02.png" width="192" height="128"/>
</tileset>
<group name="TestGroup">
<properties>
<property name="layer group prop" value="hello"/>
<property name="another property" type="bool" value="false"/>
</properties>
<layer name="Far" width="200" height="40">
<layer name="Far" class="TestLayerClass" width="200" height="40">
<data encoding="base64" compression="zlib">
eJzt2VsKwyAQBdCsoo8VNN3/BvvbQC1Em9iZOQeE/AQcyFXjLAuc69YxIKuePMgLVckCtNkz4LvrjgGVXHYMqGZPPuSFSuQCPrt3Dshudt/jOfAuZLfOngD8MfmANvkY43ya22P2BIKzvkCbfECbfECb8ykAAABnqNb/qlYvY6rdX0avV77PVe3+Mnq90fMNv/a+Z8gHbK2NZ2B7Jox+PqQO/8rQZi0HAAAAiCf7nWf2+jhW9v5u9vo41sj3E2Ftlg9GjPTMIvTbIswRYKYXswgSvA==
</data>
Expand Down
9 changes: 9 additions & 0 deletions ParseTest/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ int main()
}

const auto& mapProperties = map.getProperties();
std::cout << "Map class: " << map.getClass() << std::endl;

std::cout << "Map tileset has " << map.getTilesets().size() << " tilesets" << std::endl;
for (const auto& tileset : map.getTilesets()) {
std::cout << "Tileset: " << tileset.getName() << std::endl;
std::cout << "Tileset class: " << tileset.getClass() << std::endl;
}

std::cout << "Map has " << mapProperties.size() << " properties" << std::endl;
for (const auto& prop : mapProperties)
{
Expand Down Expand Up @@ -89,6 +97,7 @@ int main()
{
std::cout << "Found Layer: " << sublayer->getName() << std::endl;
std::cout << "Sub-layer Type: " << LayerStrings[static_cast<std::int32_t>(sublayer->getType())] << std::endl;
std::cout << "Sub-layer Class: " << sublayer->getClass() << std::endl;
std::cout << "Sub-layer Dimensions: " << sublayer->getSize() << std::endl;
std::cout << "Sub-layer Tint: " << sublayer->getTintColour() << std::endl;

Expand Down
7 changes: 7 additions & 0 deletions tmxlite/include/tmxlite/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ namespace tmx
*/
virtual Type getType() const = 0;

/*!
\brief Returns the class of the Layer, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }

/*!
\brief Use this to get a reference to the concrete layer type
which this layer points to.
Expand Down Expand Up @@ -146,6 +151,7 @@ namespace tmx
protected:

void setName(const std::string& name) { m_name = name; }
void setClass(const std::string& cls) { m_class = cls; }
void setOpacity(float opacity) { m_opacity = opacity; }
void setVisible(bool visible) { m_visible = visible; }
void setOffset(std::int32_t x, std::int32_t y) { m_offset = Vector2i(x, y); }
Expand All @@ -156,6 +162,7 @@ namespace tmx

private:
std::string m_name;
std::string m_class;
float m_opacity;
bool m_visible;
Vector2i m_offset;
Expand Down
6 changes: 6 additions & 0 deletions tmxlite/include/tmxlite/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ namespace tmx
*/
const std::vector<Layer::Ptr>& getLayers() const { return m_layers; }

/*!
\brief Returns the class of the Map, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }

/*!
\brief Returns a vector of Property objects loaded by the map
*/
Expand Down Expand Up @@ -242,6 +247,7 @@ namespace tmx

private:
Version m_version;
std::string m_class;
Orientation m_orientation;
RenderOrder m_renderOrder;
bool m_infinite;
Expand Down
4 changes: 2 additions & 2 deletions tmxlite/include/tmxlite/Object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ namespace tmx
const std::string& getType() const { return m_class; }

/*!
\brief Returns the class (equal to type) of the Object, as defined in the editor Tiled 1.9
*/
\brief Returns the class (equal to type) of the Object, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }

/*!
Expand Down
6 changes: 6 additions & 0 deletions tmxlite/include/tmxlite/Tileset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ namespace tmx
*/
const std::string& getName() const { return m_name; }

/*!
\brief Returns the class of the Tileset, as defined in the editor Tiled 1.9+
*/
const std::string& getClass() const { return m_class; }

/*!
\brief Returns the width and height of a tile in the
tile set, in pixels.
Expand Down Expand Up @@ -260,6 +265,7 @@ namespace tmx
std::uint32_t m_firstGID;
std::string m_source;
std::string m_name;
std::string m_class;
Vector2u m_tileSize;
std::uint32_t m_spacing;
std::uint32_t m_margin;
Expand Down
1 change: 1 addition & 0 deletions tmxlite/src/ImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void ImageLayer::parse(const pugi::xml_node& node, Map*)

//TODO this gets repeated foreach layer type and could all be moved to base class...
setName(node.attribute("name").as_string());
setClass(node.attribute("class").as_string());
setOpacity(node.attribute("opacity").as_float(1.f));
setVisible(node.attribute("visible").as_bool(true));
setOffset(node.attribute("offsetx").as_int(0), node.attribute("offsety").as_int(0));
Expand Down
1 change: 1 addition & 0 deletions tmxlite/src/LayerGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void LayerGroup::parse(const pugi::xml_node& node, Map* map)
}

setName(node.attribute("name").as_string());
setClass(node.attribute("class").as_string());
setOpacity(node.attribute("opacity").as_float(1.f));
setVisible(node.attribute("visible").as_bool(true));
setOffset(node.attribute("offsetx").as_int(0), node.attribute("offsety").as_int(0));
Expand Down
2 changes: 2 additions & 0 deletions tmxlite/src/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ bool Map::parseMapNode(const pugi::xml_node& mapNode)
m_version.upper = STOI(attribString.substr(0, pointPos));
m_version.lower = STOI(attribString.substr(pointPos + 1));

m_class = mapNode.attribute("class").as_string();

attribString = mapNode.attribute("orientation").as_string();
if (attribString.empty())
{
Expand Down
1 change: 1 addition & 0 deletions tmxlite/src/ObjectGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void ObjectGroup::parse(const pugi::xml_node& node, Map* map)
}

setName(node.attribute("name").as_string());
setClass(node.attribute("class").as_string());

attribString = node.attribute("color").as_string();
if (!attribString.empty())
Expand Down
1 change: 1 addition & 0 deletions tmxlite/src/TileLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void TileLayer::parse(const pugi::xml_node& node, Map*)
}

setName(node.attribute("name").as_string());
setClass(node.attribute("class").as_string());
setOpacity(node.attribute("opacity").as_float(1.f));
setVisible(node.attribute("visible").as_bool(true));
setOffset(node.attribute("offsetx").as_int(0), node.attribute("offsety").as_int(0));
Expand Down
2 changes: 2 additions & 0 deletions tmxlite/src/Tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ void Tileset::parse(pugi::xml_node node, Map* map)

m_name = node.attribute("name").as_string();
LOG("found tile set " + m_name, Logger::Type::Info);
m_class = node.attribute("class").as_string();

m_tileSize.x = node.attribute("tilewidth").as_int();
m_tileSize.y = node.attribute("tileheight").as_int();
Expand Down Expand Up @@ -253,6 +254,7 @@ void Tileset::reset()
m_firstGID = 0;
m_source = "";
m_name = "";
m_class = "";
m_tileSize = { 0,0 };
m_spacing = 0;
m_margin = 0;
Expand Down

0 comments on commit 56c62c2

Please sign in to comment.