Tileson  1.3.0
A helpful json parser for Tiled maps
WangSet.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 22.03.2020.
3 //
4 
5 #ifndef TILESON_WANGSET_HPP
6 #define TILESON_WANGSET_HPP
7 
8 //#include "../external/json.hpp"
9 #include "WangColor.hpp"
10 #include "WangTile.hpp"
11 #include "../objects/PropertyCollection.hpp"
12 
13 namespace tson
14 {
15  class WangSet
16  {
17  public:
18  inline WangSet() = default;
19  inline explicit WangSet(IJson &json);
20  inline bool parse(IJson &json);
21 
22  [[nodiscard]] inline const std::string &getName() const;
23  [[nodiscard]] inline int getTile() const;
24 
25  [[nodiscard]] inline const std::vector<tson::WangTile> &getWangTiles() const;
26  [[nodiscard]] inline const std::vector<tson::WangColor> &getCornerColors() const;
27  [[nodiscard]] inline const std::vector<tson::WangColor> &getEdgeColors() const;
28 
29  inline tson::WangColor * getColor(const std::string &name);
30  inline const std::vector<tson::WangColor> &getColors() const;
32 
33  template <typename T>
34  inline T get(const std::string &name);
35  inline tson::Property * getProp(const std::string &name);
36 
37  private:
38 
39  inline bool parseTiled15Props(IJson &json);
40 
41  std::string m_name;
42  int m_tile{};
43  std::vector<tson::WangTile> m_wangTiles;
44  std::vector<tson::WangColor> m_cornerColors;
45  std::vector<tson::WangColor> m_edgeColors;
46  tson::PropertyCollection m_properties;
48  //Tiled v1.5
49  std::vector<tson::WangColor> m_colors;
51  };
52 
59  template<typename T>
60  T tson::WangSet::get(const std::string &name)
61  {
62  return m_properties.getValue<T>(name);
63  }
64 }
65 
67 {
68  parse(json);
69 }
70 
72 {
73  bool allFound = true;
74 
75  if(json.count("tile") > 0) m_tile = json["tile"].get<int>(); else allFound = false;
76  if(json.count("name") > 0) m_name = json["name"].get<std::string>(); else allFound = false;
77 
78  //More advanced data
79  if(json.count("wangtiles") > 0 && json["wangtiles"].isArray())
80  {
81  auto &wangtiles = json.array("wangtiles");
82  std::for_each(wangtiles.begin(), wangtiles.end(), [&](std::unique_ptr<IJson> &item) { m_wangTiles.emplace_back(*item); });
83  }
84  if(json.count("cornercolors") > 0 && json["cornercolors"].isArray())
85  {
86  auto &cornercolors = json.array("cornercolors");
87  std::for_each(cornercolors.begin(), cornercolors.end(), [&](std::unique_ptr<IJson> &item) { m_cornerColors.emplace_back(*item); });
88  }
89  if(json.count("edgecolors") > 0 && json["edgecolors"].isArray())
90  {
91  auto &edgecolors = json.array("edgecolors");
92  std::for_each(edgecolors.begin(), edgecolors.end(), [&](std::unique_ptr<IJson> &item) { m_edgeColors.emplace_back(*item); });
93  }
94  if(json.count("properties") > 0 && json["properties"].isArray())
95  {
96  auto &properties = json.array("properties");
97  std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item); });
98  }
99 
100  if(!parseTiled15Props(json))
101  allFound = false;
102 
103  return allFound;
104 }
105 
112 bool tson::WangSet::parseTiled15Props(tson::IJson &json)
113 {
114  if(json.count("colors") > 0 && json["colors"].isArray())
115  {
116  auto &colors = json.array("colors");
117  std::for_each(colors.begin(), colors.end(), [&](std::unique_ptr<IJson> &item) { m_colors.emplace_back(*item); });
118  }
119  return true;
120 }
121 
126 const std::string &tson::WangSet::getName() const
127 {
128  return m_name;
129 }
130 
136 {
137  return m_tile;
138 }
139 
144 const std::vector<tson::WangTile> &tson::WangSet::getWangTiles() const
145 {
146  return m_wangTiles;
147 }
148 
153 const std::vector<tson::WangColor> &tson::WangSet::getCornerColors() const
154 {
155  return m_cornerColors;
156 }
157 
162 const std::vector<tson::WangColor> &tson::WangSet::getEdgeColors() const
163 {
164  return m_edgeColors;
165 }
166 
172 {
173  return m_properties;
174 }
175 
181 tson::Property *tson::WangSet::getProp(const std::string &name)
182 {
183  if(m_properties.hasProperty(name))
184  return m_properties.getProperty(name);
185 
186  return nullptr;
187 }
188 
193 const std::vector<tson::WangColor> &tson::WangSet::getColors() const
194 {
195  return m_colors;
196 }
197 
205 tson::WangColor *tson::WangSet::getColor(const std::string &name)
206 {
207  auto color = std::find_if(m_colors.begin(), m_colors.end(), [&](const auto &c) { return c.getName() == name; });
208 
209  if(color != m_colors.end())
210  return &color.operator*();
211 
212  return nullptr;
213 }
214 
215 
216 #endif //TILESON_WANGSET_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
Definition: WangSet.hpp:16
const std::vector< tson::WangColor > & getColors() const
Definition: WangSet.hpp:193
int getTile() const
Definition: WangSet.hpp:135
bool parse(IJson &json)
Definition: WangSet.hpp:71
WangSet()=default
PropertyCollection & getProperties()
Definition: WangSet.hpp:171
const std::vector< tson::WangTile > & getWangTiles() const
Definition: WangSet.hpp:144
const std::string & getName() const
Definition: WangSet.hpp:126
const std::vector< tson::WangColor > & getEdgeColors() const
Definition: WangSet.hpp:162
tson::WangColor * getColor(const std::string &name)
Definition: WangSet.hpp:205
const std::vector< tson::WangColor > & getCornerColors() const
Definition: WangSet.hpp:153
tson::Property * getProp(const std::string &name)
Definition: WangSet.hpp:181
T get(const std::string &name)
Definition: WangSet.hpp:60
Definition: Base64.hpp:12