Tileson  1.3.0
A helpful json parser for Tiled maps
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 
15 namespace tson
16 {
17  class Object
18  {
19  public:
20  //enum class Type : uint8_t
21  //{
22  // Undefined = 0,
23  // Object = 1,
24  // Ellipse = 2,
25  // Rectangle = 3,
26  // Point = 4,
27  // Polygon = 5,
28  // Polyline = 6,
29  // Text = 7,
30  // Template = 8
31  //};
32 
33 
34  inline Object() = default;
35  inline explicit Object(IJson &json);
36  inline bool parse(IJson &json);
37 
38  [[nodiscard]] inline ObjectType getObjectType() const;
39  [[nodiscard]] inline bool isEllipse() const;
40  [[nodiscard]] inline uint32_t getGid() const;
41  [[nodiscard]] inline const Vector2i &getSize() const;
42  [[nodiscard]] inline int getId() const;
43  [[nodiscard]] inline const std::string &getName() const;
44  [[nodiscard]] inline bool isPoint() const;
45  [[nodiscard]] inline float getRotation() const;
46  [[nodiscard]] inline const std::string &getTemplate() const;
47  [[nodiscard]] inline const std::string &getType() const;
48  [[nodiscard]] inline bool isVisible() const;
49  [[nodiscard]] inline const Vector2i &getPosition() const;
50 
51  [[nodiscard]] inline const std::vector<tson::Vector2i> &getPolygons() const;
52  [[nodiscard]] inline const std::vector<tson::Vector2i> &getPolylines() const;
53  [[nodiscard]] inline PropertyCollection &getProperties();
54  [[nodiscard]] inline const Text &getText() const;
55 
56  template <typename T>
57  inline T get(const std::string &name);
58  inline tson::Property * getProp(const std::string &name);
59 
60  //v1.2.0-stuff
61  [[nodiscard]] inline TileFlipFlags getFlipFlags() const;
62  inline bool hasFlipFlags(TileFlipFlags flags);
63 
64 
65  private:
66  inline void setObjectTypeByJson(IJson &json);
67 
68  ObjectType m_objectType = ObjectType::Undefined;
69  bool m_ellipse {};
70  uint32_t m_gid {};
71  tson::Vector2i m_size;
72  int m_id{};
73  std::string m_name;
74  bool m_point {};
75  std::vector<tson::Vector2i> m_polygon;
76  std::vector<tson::Vector2i> m_polyline;
77  tson::PropertyCollection m_properties;
78  float m_rotation {};
79  std::string m_template;
80  tson::Text m_text;
81  std::string m_type;
82  bool m_visible {};
83  tson::Vector2i m_position;
85  //v1.2.0-stuff
87  };
88 
95  template<typename T>
96  T tson::Object::get(const std::string &name)
97  {
98  return m_properties.getValue<T>(name);
99  }
100 }
101 
107 {
108  parse(json);
109 }
110 
118 {
119  bool allFound = true;
120 
121  if(json.count("ellipse") > 0) m_ellipse = json["ellipse"].get<bool>(); //Optional
122  if(json.count("gid") > 0)
123  {
124  uint32_t gid = json["gid"].get<uint32_t>(); //Optional
125  if (gid & FLIPPED_HORIZONTALLY_FLAG) m_flipFlags |= TileFlipFlags::Horizontally;
126  if (gid & FLIPPED_VERTICALLY_FLAG) m_flipFlags |= TileFlipFlags::Vertically;
127  if (gid & FLIPPED_DIAGONALLY_FLAG) m_flipFlags |= TileFlipFlags::Diagonally;
128 
129  // Clear flags
130  gid &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG);
131 
132  m_gid = gid;
133  }
134  if(json.count("id") > 0) m_id = json["id"].get<int>(); else allFound = false;
135  if(json.count("name") > 0) m_name = json["name"].get<std::string>(); else allFound = false;
136  if(json.count("point") > 0) m_point = json["point"].get<bool>(); //Optional
137  if(json.count("rotation") > 0) m_rotation = json["rotation"].get<float>(); else allFound = false;
138  if(json.count("template") > 0) m_template = json["template"].get<std::string>(); //Optional
139  if(json.count("type") > 0) m_type = json["type"].get<std::string>(); else allFound = false;
140  if(json.count("visible") > 0) m_visible = json["visible"].get<bool>(); else allFound = false;
141 
142  if(json.count("width") > 0 && json.count("height") > 0)
143  m_size = {json["width"].get<int>(), json["height"].get<int>()}; else allFound = false;
144  if(json.count("x") > 0 && json.count("y") > 0)
145  m_position = {json["x"].get<int>(), json["y"].get<int>()}; else allFound = false;
146 
147  if(json.count("text") > 0)
148  {
149  bool hasColor = json["text"].count("color") > 0;
150  tson::Color c = (hasColor) ? tson::Colori(json["text"]["color"].get<std::string>()) : tson::Colori();
151  m_text = {json["text"]["text"].get<std::string>(), json["text"]["wrap"].get<bool>(), c}; //Optional
152  }
153 
154  setObjectTypeByJson(json);
155 
156  if(m_objectType == ObjectType::Template)
157  allFound = true; //Just accept anything with this type
158 
159  //More advanced data
160  if(json.count("polygon") > 0 && json["polygon"].isArray())
161  {
162  auto &polygon = json.array("polygon");
163  std::for_each(polygon.begin(), polygon.end(),[&](std::unique_ptr<IJson> &item)
164  {
165  IJson &j = *item;
166  m_polygon.emplace_back(j["x"].get<int>(), j["y"].get<int>());
167  });
168 
169  }
170 
171  if(json.count("polyline") > 0 && json["polyline"].isArray())
172  {
173  auto &polyline = json.array("polyline");
174  std::for_each(polyline.begin(), polyline.end(),[&](std::unique_ptr<IJson> &item)
175  {
176  IJson &j = *item;
177  m_polyline.emplace_back(j["x"].get<int>(), j["y"].get<int>());
178  });
179  }
180 
181  if(json.count("properties") > 0 && json["properties"].isArray())
182  {
183  auto &properties = json.array("properties");
184  std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item)
185  {
186  m_properties.add(*item);
187  });
188  }
189 
190  return allFound;
191 }
192 
193 
198 void tson::Object::setObjectTypeByJson(IJson &json)
199 {
200  m_objectType = ObjectType::Undefined;
201  if(m_ellipse)
202  m_objectType = ObjectType::Ellipse;
203  else if(m_point)
204  m_objectType = ObjectType::Point;
205  else if(json.count("polygon") > 0)
206  m_objectType = ObjectType::Polygon;
207  else if(json.count("polyline") > 0)
208  m_objectType = ObjectType::Polyline;
209  else if(json.count("text") > 0)
210  m_objectType = ObjectType::Text;
211  else if(json.count("gid") > 0)
212  m_objectType = ObjectType::Object;
213  else if(json.count("template") > 0)
214  m_objectType = ObjectType::Template;
215  else
216  m_objectType = ObjectType::Rectangle;
217 }
218 
225 {
226  return m_objectType;
227 }
228 
234 {
235  return m_ellipse;
236 }
237 
242 uint32_t tson::Object::getGid() const
243 {
244  return m_gid;
245 }
246 
252 {
253  return m_size;
254 }
255 
261 {
262  return m_id;
263 }
264 
269 const std::string &tson::Object::getName() const
270 {
271  return m_name;
272 }
273 
279 {
280  return m_point;
281 }
282 
288 {
289  return m_rotation;
290 }
291 
296 const std::string &tson::Object::getTemplate() const
297 {
298  return m_template;
299 }
300 
305 const std::string &tson::Object::getType() const
306 {
307  return m_type;
308 }
309 
315 {
316  return m_visible;
317 }
318 
324 {
325  return m_position;
326 }
327 
333 const std::vector<tson::Vector2i> &tson::Object::getPolygons() const
334 {
335  return m_polygon;
336 }
337 
343 const std::vector<tson::Vector2i> &tson::Object::getPolylines() const
344 {
345  return m_polyline;
346 }
347 
353 {
354  return m_properties;
355 }
356 
362 {
363  return m_text;
364 }
365 
371 tson::Property *tson::Object::getProp(const std::string &name)
372 {
373  if(m_properties.hasProperty(name))
374  return m_properties.getProperty(name);
375  return nullptr;
376 }
377 
383 {
384  return m_flipFlags;
385 }
386 
396 {
397  return ((m_flipFlags & flags) == flags) ? true : false;
398 }
399 
400 #endif //TILESON_OBJECT_HPP
Definition: Color.hpp:17
Definition: IJson.hpp:11
T get(std::string_view key)
Definition: IJson.hpp:72
virtual bool isArray() const =0
virtual size_t count(std::string_view key) const =0
virtual std::vector< std::unique_ptr< IJson > > array()=0
Definition: Object.hpp:18
const std::string & getTemplate() const
Definition: Object.hpp:296
Object()=default
bool hasFlipFlags(TileFlipFlags flags)
Definition: Object.hpp:395
T get(const std::string &name)
Definition: Object.hpp:96
const std::string & getName() const
Definition: Object.hpp:269
TileFlipFlags getFlipFlags() const
Definition: Object.hpp:382
const std::vector< tson::Vector2i > & getPolygons() const
Definition: Object.hpp:333
tson::Property * getProp(const std::string &name)
Definition: Object.hpp:371
const std::string & getType() const
Definition: Object.hpp:305
float getRotation() const
Definition: Object.hpp:287
const std::vector< tson::Vector2i > & getPolylines() const
Definition: Object.hpp:343
const Vector2i & getPosition() const
Definition: Object.hpp:323
const Vector2i & getSize() const
Definition: Object.hpp:251
ObjectType getObjectType() const
Definition: Object.hpp:224
bool isVisible() const
Definition: Object.hpp:314
uint32_t getGid() const
Definition: Object.hpp:242
const Text & getText() const
Definition: Object.hpp:361
bool isEllipse() const
Definition: Object.hpp:233
int getId() const
Definition: Object.hpp:260
bool parse(IJson &json)
Definition: Object.hpp:117
PropertyCollection & getProperties()
Definition: Object.hpp:352
bool isPoint() const
Definition: Object.hpp:278
Definition: PropertyCollection.hpp:15
Definition: Property.hpp:21
Definition: Text.hpp:13
Definition: Base64.hpp:12
ObjectType
Definition: Enums.hpp:55
TileFlipFlags
Definition: Enums.hpp:74