tmxlite 1.0.0
lightweight parse for Tiled maps
Layer.hpp
1/*********************************************************************
2Matt Marchant 2016 - 2023
3http://trederia.blogspot.com
4
5tmxlite - Zlib license.
6
7This software is provided 'as-is', without any express or
8implied warranty. In no event will the authors be held
9liable for any damages arising from the use of this software.
10
11Permission is granted to anyone to use this software for any purpose,
12including commercial applications, and to alter it and redistribute
13it freely, subject to the following restrictions:
14
151. The origin of this software must not be misrepresented;
16you must not claim that you wrote the original software.
17If you use this software in a product, an acknowledgment
18in the product documentation would be appreciated but
19is not required.
20
212. Altered source versions must be plainly marked as such,
22and must not be misrepresented as being the original software.
23
243. This notice may not be removed or altered from any
25source distribution.
26*********************************************************************/
27
28#pragma once
29
30#include <tmxlite/Config.hpp>
31#include <tmxlite/Property.hpp>
32#include <tmxlite/Types.hpp>
33
34#include <string>
35#include <memory>
36#include <vector>
37
38namespace pugi
39{
40 class xml_node;
41}
42
43namespace tmx
44{
45 class Map;
46 class TileLayer;
47 class ObjectGroup;
48 class ImageLayer;
49 class LayerGroup;
55 class TMXLITE_EXPORT_API Layer
56 {
57 public:
58 using Ptr = std::unique_ptr<Layer>;
59
60 Layer() : m_opacity(1.f), m_visible(true) {};
61 virtual ~Layer() = default;
62
70 enum class Type
71 {
72 Tile,
73 Object,
74 Image,
75 Group
76 };
77
83 virtual Type getType() const = 0;
84
88 const std::string& getClass() const { return m_class; }
89
96 template <typename T>
98
99
100 template <typename T>
101 const T& getLayerAs() const;
102
106 virtual void parse(const pugi::xml_node&, Map* = nullptr) = 0;
107
111 const std::string& getName() const { return m_name; }
112
116 float getOpacity() const { return m_opacity; }
117
121 bool getVisible() const { return m_visible; }
122
127 const Vector2i& getOffset() const { return m_offset; }
128
132 const Vector2f& getParallaxFactor() const { return m_parallaxFactor; }
133
138 Colour getTintColour() const { return m_tintColour; }
139
144 const Vector2u& getSize() const { return m_size; }
145
149 const std::vector<Property>& getProperties() const { return m_properties; }
150
151 protected:
152
153 void setName(const std::string& name) { m_name = name; }
154 void setClass(const std::string& cls) { m_class = cls; }
155 void setOpacity(float opacity) { m_opacity = opacity; }
156 void setVisible(bool visible) { m_visible = visible; }
157 void setOffset(std::int32_t x, std::int32_t y) { m_offset = Vector2i(x, y); }
158 void setParallaxFactor(float x, float y) { m_parallaxFactor.x = x; m_parallaxFactor.y = y; }
159 void setTintColour(Colour c) { m_tintColour = c; }
160 void setSize(std::uint32_t width, std::uint32_t height) { m_size = Vector2u(width, height); }
161 void addProperty(const pugi::xml_node& node) { m_properties.emplace_back(); m_properties.back().parse(node); }
162
163 private:
164 std::string m_name;
165 std::string m_class;
166 float m_opacity;
167 bool m_visible;
168 Vector2i m_offset;
169 Vector2f m_parallaxFactor;
170 Colour m_tintColour = { 255,255,255,255 };
171 Vector2u m_size;
172
173 std::vector<Property> m_properties;
174 };
175}
Represents a layer of a tmx format tile map. This is an abstract base class from which all layer type...
Definition Layer.hpp:56
T & getLayerAs()
Use this to get a reference to the concrete layer type which this layer points to....
const Vector2u & getSize() const
Returns the size of the layer, in pixels. This will be the same as the map size for fixed size maps.
Definition Layer.hpp:144
float getOpacity() const
Returns the opacity value for the layer.
Definition Layer.hpp:116
Type
Layer type as returned by getType() Tile: this layer is a TileLayer type Object: This layer is an Obj...
Definition Layer.hpp:71
Colour getTintColour() const
Returns the tint colour of the layer. Defaults to 0xFFFFFFFF - pure white.
Definition Layer.hpp:138
const std::vector< Property > & getProperties() const
Returns the list of properties of this layer.
Definition Layer.hpp:149
virtual void parse(const pugi::xml_node &, Map *=nullptr)=0
Attempts to parse the specific node layer type.
bool getVisible() const
Returns whether this layer is visible or not.
Definition Layer.hpp:121
virtual Type getType() const =0
Returns a Type value representing the concrete type. Use this when deciding which conrete layer type ...
const Vector2f & getParallaxFactor() const
Returns the parallax factor.
Definition Layer.hpp:132
const std::string & getName() const
Returns the name of the layer.
Definition Layer.hpp:111
const Vector2i & getOffset() const
Returns the offset from the top left corner of the layer, in pixels.
Definition Layer.hpp:127
const std::string & getClass() const
Returns the class of the Layer, as defined in the editor Tiled 1.9+.
Definition Layer.hpp:88
Parser for TMX format tile maps. This class can be used to parse the XML format tile maps created wit...
Definition Map.hpp:94
Objects are stored in ObjectGroup layers. Objects may be rectangular, elliptical, polygonal or a poly...
Definition Object.hpp:84
Contains the red, green, blue and alpha values of a colour in the range 0 - 255.
Definition Types.hpp:111