Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
Object.hpp
Go to the documentation of this file.
1//
2// Created by robin on 22.03.2020.
3//
4
5#ifndef TILESON_OBJECT_HPP
6#define TILESON_OBJECT_HPP
7
8//#include "../external/json.hpp"
9#include "../objects/Vector2.hpp"
10#include "../objects/PropertyCollection.hpp"
11#include "Text.hpp"
12
13#include "../common/Enums.hpp"
14#include <optional>
15
16namespace tson
17{
18 class TiledClass;
19 class Map;
20 class Object
21 {
22 public:
23 //enum class Type : uint8_t
24 //{
25 // Undefined = 0,
26 // Object = 1,
27 // Ellipse = 2,
28 // Rectangle = 3,
29 // Point = 4,
30 // Polygon = 5,
31 // Polyline = 6,
32 // Text = 7,
33 // Template = 8
34 //};
35
36
37 inline Object() = default;
38 inline explicit Object(IJson &json, tson::Map *map);
39 inline bool parse(IJson &json, tson::Map *map);
40
41 [[nodiscard]] inline ObjectType getObjectType() const;
42 [[nodiscard]] inline bool isEllipse() const;
43 [[nodiscard]] inline uint32_t getGid() const;
44 [[nodiscard]] inline const Vector2i &getSize() const;
45 [[nodiscard]] inline int getId() const;
46 [[nodiscard]] inline const std::string &getName() const;
47 [[nodiscard]] inline bool isPoint() const;
48 [[nodiscard]] inline float getRotation() const;
49 [[nodiscard]] inline const std::string &getTemplate() const;
50 [[nodiscard]] inline const std::string &getType() const;
51 [[nodiscard]] inline const std::string &getClassType() const;
52 [[nodiscard]] inline tson::TiledClass *getClass();
53 [[nodiscard]] inline bool isVisible() const;
54 [[nodiscard]] inline const Vector2i &getPosition() const;
55
56 [[nodiscard]] inline const std::vector<tson::Vector2i> &getPolygons() const;
57 [[nodiscard]] inline const std::vector<tson::Vector2i> &getPolylines() const;
58 [[nodiscard]] inline PropertyCollection &getProperties();
59 [[nodiscard]] inline const Text &getText() const;
60
61 template <typename T>
62 inline T get(const std::string &name);
63 inline tson::Property * getProp(const std::string &name);
64
65 //v1.2.0-stuff
66 [[nodiscard]] inline TileFlipFlags getFlipFlags() const;
67 inline bool hasFlipFlags(TileFlipFlags flags);
68
69
70 private:
71 inline void setObjectTypeByJson(IJson &json, IJson* templ);
72
73 ObjectType m_objectType = ObjectType::Undefined;
74 bool m_ellipse {};
75 uint32_t m_gid {};
76 tson::Vector2i m_size;
77 int m_id{};
78 std::string m_name;
79 bool m_point {};
80 std::vector<tson::Vector2i> m_polygon;
81 std::vector<tson::Vector2i> m_polyline;
82 tson::PropertyCollection m_properties;
83 float m_rotation {};
84 std::string m_template;
85 tson::Text m_text;
86 std::string m_type;
87 bool m_visible {};
88 tson::Vector2i m_position;
90 //v1.2.0-stuff
93 tson::Map *m_map {nullptr};
94 std::shared_ptr<tson::TiledClass> m_class {};
95 };
96
103 template<typename T>
104 T tson::Object::get(const std::string &name)
105 {
106 return m_properties.getValue<T>(name);
107 }
108
116 inline IJson* readField(const std::string& fieldName, IJson& main, IJson* templ = nullptr);
117
118
126 inline bool readField(Text& field, const std::string& fieldName, IJson& main, IJson* templ = nullptr);
127
135 inline bool readField(std::vector<Vector2i>& field, const std::string& fieldName, IJson& main, IJson* templ = nullptr);
136
144 template <typename T> bool readField(T& field, const std::string& fieldName, IJson& main, IJson* templ = nullptr)
145 {
146 IJson* fieldJson = readField(fieldName, main, templ);
147 if(fieldJson){
148 field = fieldJson->get<T>();
149 return true;
150 }
151 return false;
152 }
153
163 inline bool readVector(Vector2i& field, const std::string& fieldNameX, const std::string& fieldNameY, IJson& main, IJson* templ = nullptr);
164
171 inline void readProperties(tson::PropertyCollection& properties, IJson& json, tson::Map* map);
172
179 inline void readGid(uint32_t& gid, TileFlipFlags& flags, IJson& main, IJson* templ = nullptr);
180}
181
187{
188 parse(json, map);
189}
190
191
196void tson::Object::setObjectTypeByJson(IJson &json, IJson* templ)
197{
198 m_objectType = ObjectType::Undefined;
199 if(m_ellipse)
200 m_objectType = ObjectType::Ellipse;
201 else if(m_point)
202 m_objectType = ObjectType::Point;
203 else if(readField("polygon", json, templ))
204 m_objectType = ObjectType::Polygon;
205 else if(readField("polyline", json, templ))
206 m_objectType = ObjectType::Polyline;
207 else if(readField("text", json, templ))
208 m_objectType = ObjectType::Text;
209 else if(readField("gid", json, templ))
210 m_objectType = ObjectType::Object;
211 else if(json.count("template") > 0)
212 m_objectType = ObjectType::Template;
213 else
214 m_objectType = ObjectType::Rectangle;
215}
216
223{
224 return m_objectType;
225}
226
232{
233 return m_ellipse;
234}
235
240uint32_t tson::Object::getGid() const
241{
242 return m_gid;
243}
244
250{
251 return m_size;
252}
253
259{
260 return m_id;
261}
262
267const std::string &tson::Object::getName() const
268{
269 return m_name;
270}
271
277{
278 return m_point;
279}
280
286{
287 return m_rotation;
288}
289
294const std::string &tson::Object::getTemplate() const
295{
296 return m_template;
297}
298
304const std::string &tson::Object::getType() const
305{
306 return m_type;
307}
308
314const std::string &tson::Object::getClassType() const
315{
316 return m_type;
317}
318
324{
325 return m_visible;
326}
327
333{
334 return m_position;
335}
336
342const std::vector<tson::Vector2i> &tson::Object::getPolygons() const
343{
344 return m_polygon;
345}
346
352const std::vector<tson::Vector2i> &tson::Object::getPolylines() const
353{
354 return m_polyline;
355}
356
362{
363 return m_properties;
364}
365
371{
372 return m_text;
373}
374
380tson::Property *tson::Object::getProp(const std::string &name)
381{
382 if(m_properties.hasProperty(name))
383 return m_properties.getProperty(name);
384 return nullptr;
385}
386
392{
393 return m_flipFlags;
394}
395
405{
406 return ((m_flipFlags & flags) == flags) ? true : false;
407}
408
409#endif //TILESON_OBJECT_HPP
Definition IJson.hpp:11
T get(std::string_view key)
Definition IJson.hpp:82
virtual size_t count(std::string_view key) const =0
Definition Map.hpp:29
Definition Object.hpp:21
const std::string & getTemplate() const
Definition Object.hpp:294
const std::string & getClassType() const
Definition Object.hpp:314
Object()=default
bool hasFlipFlags(TileFlipFlags flags)
Definition Object.hpp:404
T get(const std::string &name)
Definition Object.hpp:104
const std::string & getName() const
Definition Object.hpp:267
TileFlipFlags getFlipFlags() const
Definition Object.hpp:391
const std::vector< tson::Vector2i > & getPolygons() const
Definition Object.hpp:342
tson::Property * getProp(const std::string &name)
Definition Object.hpp:380
const std::string & getType() const
Definition Object.hpp:304
float getRotation() const
Definition Object.hpp:285
const std::vector< tson::Vector2i > & getPolylines() const
Definition Object.hpp:352
const Vector2i & getPosition() const
Definition Object.hpp:332
bool parse(IJson &json, tson::Map *map)
Definition tileson_forward.hpp:484
const Vector2i & getSize() const
Definition Object.hpp:249
ObjectType getObjectType() const
Definition Object.hpp:222
bool isVisible() const
Definition Object.hpp:323
uint32_t getGid() const
Definition Object.hpp:240
tson::TiledClass * getClass()
Definition tileson_forward.hpp:563
const Text & getText() const
Definition Object.hpp:370
bool isEllipse() const
Definition Object.hpp:231
int getId() const
Definition Object.hpp:258
PropertyCollection & getProperties()
Definition Object.hpp:361
bool isPoint() const
Definition Object.hpp:276
Definition PropertyCollection.hpp:15
Definition Property.hpp:23
Definition Text.hpp:13
Definition TiledClass.hpp:11
Definition Base64.hpp:12
bool readVector(Vector2i &field, const std::string &fieldNameX, const std::string &fieldNameY, IJson &main, IJson *templ=nullptr)
Definition tileson_forward.hpp:424
ObjectType
Definition Enums.hpp:58
IJson * readField(const std::string &fieldName, IJson &main, IJson *templ=nullptr)
Definition tileson_forward.hpp:361
void readGid(uint32_t &gid, TileFlipFlags &flags, IJson &main, IJson *templ=nullptr)
Definition tileson_forward.hpp:461
TileFlipFlags
Definition Enums.hpp:77
Vector2< int > Vector2i
Definition Vector2.hpp:51
void readProperties(tson::PropertyCollection &properties, IJson &json, tson::Map *map)
Definition tileson_forward.hpp:441