Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
WangColor.hpp
Go to the documentation of this file.
1//
2// Created by robin on 22.03.2020.
3//
4
5#ifndef TILESON_WANGCOLOR_HPP
6#define TILESON_WANGCOLOR_HPP
7
8//#include "../external/json.hpp"
9#include "../objects/Color.hpp"
10
11namespace tson
12{
14 {
15 public:
16 inline WangColor() = default;
17 inline explicit WangColor(IJson &json, tson::Map *map);
18 inline bool parse(IJson &json, tson::Map *map);
19
20 [[nodiscard]] inline const Colori &getColor() const;
21 [[nodiscard]] inline const std::string &getName() const;
22 [[nodiscard]] inline float getProbability() const;
23 [[nodiscard]] inline int getTile() const;
24
26 template <typename T>
27 inline T get(const std::string &name);
28 inline tson::Property * getProp(const std::string &name);
29
30 [[nodiscard]] inline const std::string &getClassType() const;
31 [[nodiscard]] inline tson::TiledClass *getClass();
34 private:
35 tson::Colori m_color;
36 std::string m_name;
37 float m_probability{};
38 int m_tile{};
40 //New in Tiled v1.5
41 tson::PropertyCollection m_properties;
42 tson::Map * m_map;
43 std::string m_classType {};
44 std::shared_ptr<tson::TiledClass> m_class {};
45
46 };
47}
48
50{
51 parse(json, map);
52}
53
55{
56 m_map = map;
57 bool allFound = true;
58
59 if(json.count("color") > 0) m_color = tson::Colori(json["color"].get<std::string>()); else allFound = false;
60 if(json.count("name") > 0) m_name = json["name"].get<std::string>(); else allFound = false;
61 if(json.count("probability") > 0) m_probability = json["probability"].get<float>(); else allFound = false;
62 if(json.count("tile") > 0) m_tile = json["tile"].get<int>(); else allFound = false;
63 if(json.count("class") > 0) m_classType = json["class"].get<std::string>(); //Optional
64
65 if(json.count("properties") > 0 && json["properties"].isArray())
66 {
67 auto &properties = json.array("properties");
68 std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item); });
69 }
70
71
72 return allFound;
73}
74
80{
81 return m_color;
82}
83
88const std::string &tson::WangColor::getName() const
89{
90 return m_name;
91}
92
98{
99 return m_probability;
100}
101
107{
108 return m_tile;
109}
110
117{
118 return m_properties;
119}
120
127template<typename T>
128T tson::WangColor::get(const std::string &name)
129{
130 return m_properties.getValue<T>(name);
131}
132
139{
140 if(m_properties.hasProperty(name))
141 return m_properties.getProperty(name);
142
143 return nullptr;
144}
145
146const std::string &tson::WangColor::getClassType() const
147{
148 return m_classType;
149}
150
151#endif //TILESON_WANGCOLOR_HPP
Definition IJson.hpp:11
T get(std::string_view key)
Definition IJson.hpp:82
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 Map.hpp:29
Definition PropertyCollection.hpp:15
T getValue(const std::string &name)
Definition PropertyCollection.hpp:47
Definition Property.hpp:23
Definition TiledClass.hpp:11
Definition WangColor.hpp:14
tson::Property * getProp(const std::string &name)
Definition WangColor.hpp:138
float getProbability() const
Definition WangColor.hpp:97
T get(const std::string &name)
Definition WangColor.hpp:128
const std::string & getClassType() const
Definition WangColor.hpp:146
bool parse(IJson &json, tson::Map *map)
Definition WangColor.hpp:54
const std::string & getName() const
Definition WangColor.hpp:88
WangColor()=default
PropertyCollection & getProperties()
Definition WangColor.hpp:116
const Colori & getColor() const
Definition WangColor.hpp:79
int getTile() const
Definition WangColor.hpp:106
tson::TiledClass * getClass()
Definition tileson_forward.hpp:544
Definition Base64.hpp:12
Color< uint8_t > Colori
Definition Color.hpp:89