Tileson  1.3.0
A helpful json parser for Tiled maps
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 
11 namespace tson
12 {
13  class WangColor
14  {
15  public:
16  inline WangColor() = default;
17  inline explicit WangColor(IJson &json);
18  inline bool parse(IJson &json);
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 
31  private:
32  tson::Colori m_color;
33  std::string m_name;
34  float m_probability{};
35  int m_tile{};
37  //New in Tiled v1.5
38  tson::PropertyCollection m_properties;
39  };
40 }
41 
43 {
44  parse(json);
45 }
46 
48 {
49  bool allFound = true;
50 
51  if(json.count("color") > 0) m_color = tson::Colori(json["color"].get<std::string>()); else allFound = false;
52  if(json.count("name") > 0) m_name = json["name"].get<std::string>(); else allFound = false;
53  if(json.count("probability") > 0) m_probability = json["probability"].get<float>(); else allFound = false;
54  if(json.count("tile") > 0) m_tile = json["tile"].get<int>(); else allFound = false;
55 
56  if(json.count("properties") > 0 && json["properties"].isArray())
57  {
58  auto &properties = json.array("properties");
59  std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item); });
60  }
61 
62 
63  return allFound;
64 }
65 
71 {
72  return m_color;
73 }
74 
79 const std::string &tson::WangColor::getName() const
80 {
81  return m_name;
82 }
83 
89 {
90  return m_probability;
91 }
92 
98 {
99  return m_tile;
100 }
101 
108 {
109  return m_properties;
110 }
111 
118 template<typename T>
119 T tson::WangColor::get(const std::string &name)
120 {
121  return m_properties.getValue<T>(name);
122 }
123 
129 tson::Property *tson::WangColor::getProp(const std::string &name)
130 {
131  if(m_properties.hasProperty(name))
132  return m_properties.getProperty(name);
133 
134  return nullptr;
135 }
136 
137 #endif //TILESON_WANGCOLOR_HPP
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: PropertyCollection.hpp:15
Definition: Property.hpp:21
Definition: WangColor.hpp:14
tson::Property * getProp(const std::string &name)
Definition: WangColor.hpp:129
float getProbability() const
Definition: WangColor.hpp:88
T get(const std::string &name)
Definition: WangColor.hpp:119
const std::string & getName() const
Definition: WangColor.hpp:79
bool parse(IJson &json)
Definition: WangColor.hpp:47
WangColor()=default
PropertyCollection & getProperties()
Definition: WangColor.hpp:107
const Colori & getColor() const
Definition: WangColor.hpp:70
int getTile() const
Definition: WangColor.hpp:97
Definition: Base64.hpp:12
Color< uint8_t > Colori
Definition: Color.hpp:89