5#ifndef TILESON_TILESON_FORWARD_HPP
6#define TILESON_TILESON_FORWARD_HPP
22 if(m_class ==
nullptr)
24 TiledClass* baseClass = (m_project !=
nullptr) ? m_project->
getClass(m_classType) :
nullptr;
25 if(baseClass !=
nullptr)
27 m_class = std::make_shared<TiledClass>(*baseClass);
28 m_class->update(m_properties);
31 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
47 if(json.
count(
"image") > 0) m_image = fs::path(json[
"image"].get<std::string>());
49 bool allFound = parseId(json);
51 if(json.
count(
"type") > 0) m_type = json[
"type"].
get<std::string>();
52 else if(json.
count(
"class") > 0) m_type = json[
"class"].get<std::string>();
54 if(json.
count(
"objectgroup") > 0) m_objectgroup =
tson::Layer(json[
"objectgroup"], m_map);
56 if(json.
count(
"imagewidth") > 0 && json.
count(
"imageheight") > 0)
57 m_imageSize = {json[
"imagewidth"].
get<
int>(), json[
"imageheight"].get<int>()};
59 m_subRect = {0,0, m_imageSize.x, m_imageSize.y};
60 if(json.
count(
"x") > 0) m_subRect.x = json[
"x"].
get<
int>();
61 if(json.
count(
"y") > 0) m_subRect.y = json[
"y"].
get<
int>();
62 if(json.
count(
"width") > 0) m_subRect.width = json[
"width"].
get<
int>();
63 if(json.
count(
"height") > 0) m_subRect.height = json[
"height"].
get<
int>();
66 if(json.
count(
"animation") > 0 && json[
"animation"].
isArray())
68 auto &animation = json.
array(
"animation");
69 std::vector<tson::Frame> frames;
70 std::for_each(animation.begin(), animation.end(), [&](std::unique_ptr<IJson> &item) { frames.emplace_back(*item); });
73 m_animation.setFrames(frames);
76 if(json.
count(
"terrain") > 0 && json[
"terrain"].
isArray())
78 auto &terrain = json.
array(
"terrain");
79 std::for_each(terrain.begin(), terrain.end(), [&](std::unique_ptr<IJson> &item) { m_terrain.emplace_back(item->get<int>()); });
82 if(json.
count(
"properties") > 0 && json[
"properties"].
isArray())
84 auto &properties = json.
array(
"properties");
85 tson::Project *project = (m_map !=
nullptr) ? m_map->getProject() :
nullptr;
86 std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item, project); });
89 performDataCalculations();
101 return m_map->getTileSize();
108 if(json.
count(
"id") > 0)
110 m_id = json[
"id"].
get<uint32_t>() + 1;
111 if (m_tileset !=
nullptr)
112 m_gid = m_tileset->getFirstgid() + m_id - 1;
115 manageFlipFlagsByIdThenRemoveFlags(m_gid);
125void tson::Tile::performDataCalculations()
127 if(m_tileset ==
nullptr || m_map ==
nullptr)
130 int firstId = m_tileset->getFirstgid();
131 int columns = m_tileset->getColumns();
132 int rows = m_tileset->getTileCount() / columns;
133 int lastId = (m_tileset->getFirstgid() + m_tileset->getTileCount()) - 1;
135 int const gid =
static_cast<int>(getGid());
136 if (gid >= firstId && gid <= lastId)
138 int const baseTilePosition = (gid - firstId);
140 int const tileModX = (baseTilePosition % columns);
141 int const currentRow = (baseTilePosition / columns);
142 int const offsetX = (tileModX != 0) ? ((tileModX) * m_map->getTileSize().x) : (0 * m_map->getTileSize().x);
143 int const offsetY = (currentRow < rows-1) ? (currentRow * m_map->getTileSize().y) : ((rows-1) * m_map->getTileSize().y);
145 tson::Vector2i spacing = m_tileset->getMarginSpacingOffset({tileModX, currentRow});
146 m_drawingRect = { offsetX + spacing.
x, offsetY + spacing.
y, m_map->getTileSize().x, m_map->getTileSize().y };
149 m_drawingRect = {0, 0, 0, 0};
158 return {((float) std::get<0>(tileDataPos)) * m_drawingRect.width, ((
float) std::get<1>(tileDataPos)) * m_drawingRect.height};
168 if(m_class ==
nullptr)
170 TiledClass* baseClass = (m_map !=
nullptr && m_map->getProject() !=
nullptr) ? m_map->getProject()->getClass(m_type) :
nullptr;
171 if(baseClass !=
nullptr)
173 m_class = std::make_shared<TiledClass>(*baseClass);
174 m_class->update(m_properties);
177 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
185 if(m_class ==
nullptr)
187 TiledClass* baseClass = (m_map !=
nullptr && m_map->getProject() !=
nullptr) ? m_map->getProject()->getClass(m_classType) :
nullptr;
188 if(baseClass !=
nullptr)
190 m_class = std::make_shared<TiledClass>(*baseClass);
191 m_class->update(m_properties);
194 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
215 return m_tile->getDrawingRect();
224void tson::Layer::decompressData()
228 if(container->
empty())
231 if(m_encoding.empty() && m_compression.empty())
234 std::string data = m_base64Data;
235 bool hasBeenDecoded =
false;
236 if(!m_encoding.empty() && container->
contains(m_encoding))
239 hasBeenDecoded =
true;
242 if(!m_compression.empty() && container->
contains(m_compression))
263 bool allFound =
true;
264 if(json.
count(
"tintcolor") > 0) m_tintColor =
tson::Colori(json[
"tintcolor"].get<std::string>());
265 if(json.
count(
"compression") > 0) m_compression = json[
"compression"].
get<std::string>();
266 if(json.
count(
"draworder") > 0) m_drawOrder = json[
"draworder"].get<std::string>();
267 if(json.
count(
"encoding") > 0) m_encoding = json[
"encoding"].get<std::string>();
268 if(json.
count(
"id") > 0) m_id = json[
"id"].get<int>();
269 if(json.
count(
"image") > 0) m_image = json[
"image"].get<std::string>();
270 if(json.
count(
"name") > 0) m_name = json[
"name"].get<std::string>();
else allFound =
false;
271 if(json.
count(
"offsetx") > 0 && json.
count(
"offsety") > 0)
272 m_offset = {json[
"offsetx"].
get<
float>(), json[
"offsety"].get<float>()};
273 if(json.
count(
"opacity") > 0) m_opacity = json[
"opacity"].get<float>();
else allFound =
false;
274 if(json.
count(
"width") > 0 && json.
count(
"height") > 0)
275 m_size = {json[
"width"].
get<
int>(), json[
"height"].get<int>()};
276 if(json.
count(
"transparentcolor") > 0) m_transparentColor =
tson::Colori(json[
"transparentcolor"].get<std::string>());
277 if(json.
count(
"type") > 0) m_typeStr = json[
"type"].get<std::string>();
else allFound =
false;
278 if(json.
count(
"class") > 0) m_classType = json[
"class"].get<std::string>();
279 if(json.
count(
"visible") > 0) m_visible = json[
"visible"].get<bool>();
else allFound =
false;
280 if(json.
count(
"x") > 0) m_x = json[
"x"].get<int>();
else allFound =
false;
281 if(json.
count(
"y") > 0) m_y = json[
"y"].get<int>();
else allFound =
false;
282 if(json.
count(
"repeatx") > 0) m_repeatX = json[
"repeatx"].get<bool>();
283 if(json.
count(
"repeaty") > 0) m_repeatY = json[
"repeaty"].get<bool>();
286 if(json.
count(
"parallaxx") > 0)
287 parallax.
x = json[
"parallaxx"].
get<
float>();
288 if(json.
count(
"parallaxy") > 0)
289 parallax.y = json[
"parallaxy"].
get<
float>();
291 m_parallax = parallax;
294 if(json.
count(
"data") > 0)
296 if(json[
"data"].isArray())
298 auto &array = json.
array(
"data");
299 std::for_each(array.begin(), array.end(), [&](std::unique_ptr<IJson> &item) { m_data.push_back(item->get<uint32_t>()); });
303 m_base64Data = json[
"data"].
get<std::string>();
309 if(json.
count(
"chunks") > 0 && json[
"chunks"].
isArray())
311 auto &chunks = json.
array(
"chunks");
312 std::for_each(chunks.begin(), chunks.end(), [&](std::unique_ptr<IJson> &item) { m_chunks.emplace_back(*item); });
314 if(json.
count(
"layers") > 0 && json[
"layers"].
isArray())
316 auto &layers = json.
array(
"layers");
317 std::for_each(layers.begin(), layers.end(), [&](std::unique_ptr<IJson> &item) { m_layers.emplace_back(*item, m_map); });
319 if(json.
count(
"objects") > 0 && json[
"objects"].
isArray())
321 auto &objects = json.
array(
"objects");
322 std::for_each(objects.begin(), objects.end(), [&](std::unique_ptr<IJson> &item) { m_objects.emplace_back(*item, m_map); });
324 if(json.
count(
"properties") > 0 && json[
"properties"].
isArray())
326 auto &properties = json.
array(
"properties");
327 tson::Project *project = (m_map !=
nullptr) ? m_map->getProject() :
nullptr;
328 std::for_each(properties.begin(), properties.end(), [&](std::unique_ptr<IJson> &item) { m_properties.add(*item, project); });
338 if(m_class ==
nullptr)
340 TiledClass* baseClass = (m_map !=
nullptr && m_map->getProject() !=
nullptr) ? m_map->getProject()->getClass(m_classType) :
nullptr;
341 if(baseClass !=
nullptr)
343 m_class = std::make_shared<TiledClass>(*baseClass);
344 m_class->update(m_properties);
347 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
363 if(main.
count(fieldName) > 0)
365 return &main[fieldName];
366 }
else if (templ && templ->
count(fieldName) > 0)
368 return &(*templ)[fieldName];
402 if(fieldJson && fieldJson->
isArray())
404 auto polyline = fieldJson->
array();
405 std::for_each(polyline.begin(), polyline.end(),[&field](std::unique_ptr<IJson> &item)
408 field.emplace_back(j[
"x"].get<int>(), j[
"y"].get<int>());
443 if(json.
count(
"properties") > 0 && json[
"properties"].
isArray())
445 auto &props = json.
array(
"properties");
447 std::for_each(props.begin(), props.end(), [&](std::unique_ptr<IJson> &item)
449 properties.add(*item, project);
471 g &= ~(FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG);
487 bool allFound =
true;
488 IJson* templateJson =
nullptr;
490 if(
readField(m_template,
"template", json) && map !=
nullptr)
492 IJson* tobjJsonFile = map->parseLinkedFile(m_template);
494 templateJson =
readField(
"object", *tobjJsonFile);
497 readField(m_ellipse,
"ellipse", json, templateJson);
498 readField(m_point,
"point", json, templateJson);
499 readField(m_text,
"text", json, templateJson);
500 readGid(m_gid, m_flipFlags, json, templateJson);
502 allFound &=
readField(m_id,
"id", json, templateJson);
503 allFound &=
readField(m_name,
"name", json, templateJson);
504 allFound &=
readField(m_rotation,
"rotation", json, templateJson);
505 allFound &=
readField(m_type,
"type", json, templateJson) ||
readField(m_type,
"class", json, templateJson);
506 allFound &=
readField(m_visible,
"visible", json, templateJson);
507 allFound &=
readVector(m_size,
"width",
"height", json, templateJson);
508 allFound &=
readVector(m_position,
"x",
"y", json, templateJson);
510 setObjectTypeByJson(json, templateJson);
516 readField(m_polygon,
"polygon", json, templateJson);
517 readField(m_polyline,
"polyline", json, templateJson);
520 if(templateJson)
readProperties(m_properties, *templateJson, m_map);
530 if(m_class ==
nullptr)
532 TiledClass* baseClass = (m_map !=
nullptr && m_map->getProject() !=
nullptr) ? m_map->getProject()->getClass(m_classType) :
nullptr;
533 if(baseClass !=
nullptr)
535 m_class = std::make_shared<TiledClass>(*baseClass);
536 m_class->update(m_properties);
539 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
546 if(m_class ==
nullptr)
548 TiledClass* baseClass = (m_map !=
nullptr && m_map->getProject() !=
nullptr) ? m_map->getProject()->getClass(m_classType) :
nullptr;
549 if(baseClass !=
nullptr)
551 m_class = std::make_shared<TiledClass>(*baseClass);
552 m_class->update(m_properties);
555 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
565 if(m_class ==
nullptr)
567 TiledClass* baseClass = (m_map !=
nullptr && m_map->getProject() !=
nullptr) ? m_map->getProject()->getClass(m_type) :
nullptr;
568 if(baseClass !=
nullptr)
570 m_class = std::make_shared<TiledClass>(*baseClass);
571 m_class->update(m_properties);
574 return (m_class !=
nullptr) ? m_class.get() :
nullptr;
591 if(fs::exists(data.path))
593 std::unique_ptr<tson::Map> map = parser->parse(data.path);
594 m_maps.push_back(std::move(map));
598 return m_maps.size();
608 m_value =
Colori(json.
get<std::string>());
612 m_value = fs::path(json.
get<std::string>());
616 if(!m_propertyType.empty())
619 tson::EnumDefinition *def = (m_project !=
nullptr) ? m_project->getEnumDefinition(m_propertyType) :
nullptr;
622 uint32_t v = json.
get<uint32_t>();
629 m_value = json.
get<
int>();
634 m_value = json.
get<
bool>();
638 m_value = json.
get<
float>();
642 if(!m_propertyType.empty())
645 tson::EnumDefinition *def = (m_project !=
nullptr) ? m_project->getEnumDefinition(m_propertyType) :
nullptr;
648 std::string v = json.
get<std::string>();
655 setStrValue(json.
get<std::string>());
661 tson::TiledClass *baseClass = (m_project !=
nullptr) ? m_project->getClass(m_propertyType) :
nullptr;
662 if (baseClass !=
nullptr)
672 m_value = json.
get<uint32_t>();
675 setStrValue(json.
get<std::string>());
Definition DecompressorContainer.hpp:16
bool contains(std::string_view name) const
Definition DecompressorContainer.hpp:44
bool empty() const
Definition DecompressorContainer.hpp:91
IDecompressor< std::string_view, std::string > * get(std::string_view name)
Definition DecompressorContainer.hpp:77
Definition TiledEnum.hpp:11
Definition TiledEnum.hpp:160
virtual TOut decompress(const TIn &input)=0
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
bool parse(IJson &json, tson::Map *map)
Definition tileson_forward.hpp:259
tson::TiledClass * getClass()
Definition tileson_forward.hpp:336
tson::TiledClass * getClass()
Definition tileson_forward.hpp:20
Project * getProject()
Definition Map.hpp:590
bool parse(IJson &json, tson::Map *map)
Definition tileson_forward.hpp:484
tson::TiledClass * getClass()
Definition tileson_forward.hpp:563
Definition Project.hpp:20
tson::TiledClass * getClass(std::string_view name)
Definition Project.hpp:136
Definition PropertyCollection.hpp:15
void setValueByType(IJson &json)
Definition tileson_forward.hpp:603
const tson::Rect & getDrawingRect() const
Definition tileson_forward.hpp:213
void initialize(const std::tuple< int, int > &posInTileUnits, tson::Tile *tile)
Definition tileson_forward.hpp:206
const tson::Vector2f getPosition(const std::tuple< int, int > &tileDataPos)
Definition tileson_forward.hpp:156
const tson::Vector2i getTileSize() const
Definition tileson_forward.hpp:98
bool parseId(IJson &json)
Definition tileson_forward.hpp:106
bool parse(IJson &json, tson::Tileset *tileset, tson::Map *map)
Definition tileson_forward.hpp:42
const tson::Vector2i getPositionInTileUnits(const std::tuple< int, int > &tileDataPos)
Definition Tile.hpp:276
tson::TiledClass * getClass()
Definition tileson_forward.hpp:166
Definition TiledClass.hpp:11
void update(IJson &json)
Definition TiledClass.hpp:92
Definition Tileset.hpp:24
tson::TiledClass * getClass()
Definition tileson_forward.hpp:183
Definition tileson_parser.hpp:56
T y
Definition Vector2.hpp:22
T x
Definition Vector2.hpp:21
tson::TiledClass * getClass()
Definition tileson_forward.hpp:544
tson::TiledClass * getClass()
Definition tileson_forward.hpp:528
Definition WorldMapData.hpp:11
std::size_t loadMaps(tson::Tileson *parser)
Definition tileson_forward.hpp:586
bool readVector(Vector2i &field, const std::string &fieldNameX, const std::string &fieldNameY, IJson &main, IJson *templ=nullptr)
Definition tileson_forward.hpp:424
IJson * readField(const std::string &fieldName, IJson &main, IJson *templ=nullptr)
Definition tileson_forward.hpp:361
void readGid(uint32_t &gid, TileFlipFlags &flags, IJson &main, IJson *templ=nullptr)
Definition tileson_forward.hpp:461
TileFlipFlags
Definition Enums.hpp:77
Color< uint8_t > Colori
Definition Color.hpp:89
void readProperties(tson::PropertyCollection &properties, IJson &json, tson::Map *map)
Definition tileson_forward.hpp:441