Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
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
13namespace tson
14{
15 class WangSet
16 {
17 public:
18 inline WangSet() = default;
19 inline explicit WangSet(IJson &json, tson::Map *map);
20 inline bool parse(IJson &json, tson::Map *map);
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 [[nodiscard]] inline const std::string &getClassType() const;
38 [[nodiscard]] inline tson::TiledClass *getClass();
40 private:
41
42 inline bool parseTiled15Props(IJson &json);
43
44 std::string m_name;
45 int m_tile{};
46 std::vector<tson::WangTile> m_wangTiles;
47 std::vector<tson::WangColor> m_cornerColors;
48 std::vector<tson::WangColor> m_edgeColors;
49 tson::PropertyCollection m_properties;
51 //Tiled v1.5
52 std::vector<tson::WangColor> m_colors;
54 tson::Map * m_map;
55 std::string m_classType {};
56 std::shared_ptr<tson::TiledClass> m_class {};
57
58 };
59
66 template<typename T>
67 T tson::WangSet::get(const std::string &name)
68 {
69 return m_properties.getValue<T>(name);
70 }
71}
72
74{
75 parse(json, map);
76}
77
79{
80 m_map = map;
81 bool allFound = true;
82
83 if(json.count("tile") > 0) m_tile = json["tile"].get<int>(); else allFound = false;
84 if(json.count("name") > 0) m_name = json["name"].get<std::string>(); else allFound = false;
85
86 //More advanced data
87 if(json.count("wangtiles") > 0 && json["wangtiles"].isArray())
88 {
89 auto &wangtiles = json.array("wangtiles");
90 std::for_each(wangtiles.begin(), wangtiles.end(), [&](std::unique_ptr<IJson> &item) { m_wangTiles.emplace_back(*item); });
91 }
92 if(json.count("cornercolors") > 0 && json["cornercolors"].isArray())
93 {
94 auto &cornercolors = json.array("cornercolors");
95 std::for_each(cornercolors.begin(), cornercolors.end(), [&](std::unique_ptr<IJson> &item) { m_cornerColors.emplace_back(*item, m_map); });
96 }
97 if(json.count("edgecolors") > 0 && json["edgecolors"].isArray())
98 {
99 auto &edgecolors = json.array("edgecolors");
100 std::for_each(edgecolors.begin(), edgecolors.end(), [&](std::unique_ptr<IJson> &item) { m_edgeColors.emplace_back(*item, m_map); });
101 }
102 if(json.count("properties") > 0 && json["properties"].isArray())
103 {
104 auto &properties = json.array("properties");
105 std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item); });
106 }
107
108 if(json.count("class") > 0) m_classType = json["class"].get<std::string>(); //Optional
109
110 if(!parseTiled15Props(json))
111 allFound = false;
112
113 return allFound;
114}
115
122bool tson::WangSet::parseTiled15Props(tson::IJson &json)
123{
124 if(json.count("colors") > 0 && json["colors"].isArray())
125 {
126 auto &colors = json.array("colors");
127 std::for_each(colors.begin(), colors.end(), [&](std::unique_ptr<IJson> &item) { m_colors.emplace_back(*item, m_map); });
128 }
129 return true;
130}
131
136const std::string &tson::WangSet::getName() const
137{
138 return m_name;
139}
140
146{
147 return m_tile;
148}
149
154const std::vector<tson::WangTile> &tson::WangSet::getWangTiles() const
155{
156 return m_wangTiles;
157}
158
163const std::vector<tson::WangColor> &tson::WangSet::getCornerColors() const
164{
165 return m_cornerColors;
166}
167
172const std::vector<tson::WangColor> &tson::WangSet::getEdgeColors() const
173{
174 return m_edgeColors;
175}
176
182{
183 return m_properties;
184}
185
191tson::Property *tson::WangSet::getProp(const std::string &name)
192{
193 if(m_properties.hasProperty(name))
194 return m_properties.getProperty(name);
195
196 return nullptr;
197}
198
203const std::vector<tson::WangColor> &tson::WangSet::getColors() const
204{
205 return m_colors;
206}
207
216{
217 auto color = std::find_if(m_colors.begin(), m_colors.end(), [&](const auto &c) { return c.getName() == name; });
218
219 if(color != m_colors.end())
220 return &color.operator*();
221
222 return nullptr;
223}
224
225const std::string &tson::WangSet::getClassType() const
226{
227 return m_classType;
228}
229
230
231
232
233#endif //TILESON_WANGSET_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
Definition Property.hpp:23
Definition TiledClass.hpp:11
Definition WangColor.hpp:14
Definition WangSet.hpp:16
const std::string & getClassType() const
Definition WangSet.hpp:225
const std::vector< tson::WangColor > & getColors() const
Definition WangSet.hpp:203
int getTile() const
Definition WangSet.hpp:145
WangSet()=default
PropertyCollection & getProperties()
Definition WangSet.hpp:181
const std::vector< tson::WangTile > & getWangTiles() const
Definition WangSet.hpp:154
tson::TiledClass * getClass()
Definition tileson_forward.hpp:528
bool parse(IJson &json, tson::Map *map)
Definition WangSet.hpp:78
const std::string & getName() const
Definition WangSet.hpp:136
const std::vector< tson::WangColor > & getEdgeColors() const
Definition WangSet.hpp:172
tson::WangColor * getColor(const std::string &name)
Definition WangSet.hpp:215
const std::vector< tson::WangColor > & getCornerColors() const
Definition WangSet.hpp:163
tson::Property * getProp(const std::string &name)
Definition WangSet.hpp:191
T get(const std::string &name)
Definition WangSet.hpp:67
Definition Base64.hpp:12