le2d v0.4.7
2D game engine framework
 
Loading...
Searching...
No Matches
json_io.hpp
Go to the documentation of this file.
1#pragma once
6#include "le2d/uri.hpp"
7#include <djson/json.hpp>
8
9namespace le {
10template <typename Type>
11constexpr auto json_type_name_v = std::string_view{};
12
13template <>
14inline constexpr auto json_type_name_v<IShader> = std::string_view{"Shader"};
15template <>
16inline constexpr auto json_type_name_v<TileSet> = std::string_view{"TileSet"};
17template <>
18inline constexpr auto json_type_name_v<ITexture> = std::string_view{"Texture"};
19template <>
20inline constexpr auto json_type_name_v<ITileSheet> = std::string_view{"TileSheet"};
21template <>
22inline constexpr auto json_type_name_v<anim::Timeline<Transform>> = std::string_view{"anim::Timeline<Transform>"};
23template <>
24inline constexpr auto json_type_name_v<anim::Timeline<TileId>> = std::string_view{"anim::Timeline<TileId>"};
25template <>
26inline constexpr auto json_type_name_v<anim::Animation<Transform>> = std::string_view{"anim::Animation<Transform>"};
27template <>
28inline constexpr auto json_type_name_v<anim::Animation<TileId>> = std::string_view{"anim::Animation<TileId>"};
29
30[[nodiscard]] auto get_json_type_name(dj::Json const& json) -> std::string_view;
31void set_json_type_name(dj::Json& json, std::string_view type_name);
32[[nodiscard]] auto json_type_name_match(dj::Json const& json, std::string_view type_name) -> bool;
33
34template <typename Type>
35auto is_json_type(dj::Json const& json, Type const& /*unused*/) -> bool {
36 return json_type_name_match(json, json_type_name_v<Type>);
37}
38
39template <typename Type>
40auto is_json_type(dj::Json const& json) -> bool {
41 return json_type_name_match(json, json_type_name_v<Type>);
42}
43
44template <typename Type>
45void set_json_type(dj::Json& json, Type const& /*unused*/) {
46 set_json_type_name(json, json_type_name_v<Type>);
47}
48
49template <typename Type>
50void set_json_type(dj::Json& json) {
51 set_json_type_name(json, json_type_name_v<Type>);
52}
53
54template <typename Type, glm::length_t Length>
55void from_json(dj::Json const& json, glm::vec<Length, Type>& vec) {
56 if (!json.is_array()) { return; }
57 from_json(json[0], vec.x);
58 if constexpr (Length > 1) { from_json(json[1], vec.y); }
59 if constexpr (Length > 2) { from_json(json[2], vec.z); }
60}
61
62template <typename Type, glm::length_t Length>
63void to_json(dj::Json& json, glm::vec<Length, Type> const& vec) {
64 to_json(json[0], vec.x);
65 if constexpr (Length > 1) { to_json(json[1], vec.y); }
66 if constexpr (Length > 2) { to_json(json[2], vec.z); }
67}
68
69template <typename Type>
70void from_json(dj::Json const& json, kvf::Rect<Type>& rect) {
71 from_json(json["lt"], rect.lt);
72 from_json(json["rb"], rect.rb);
73}
74
75template <typename Type>
76void to_json(dj::Json& json, kvf::Rect<Type> const& rect) {
77 to_json(json["lt"], rect.lt);
78 to_json(json["rb"], rect.rb);
79}
80
81void from_json(dj::Json const& json, Uri& uri);
82void to_json(dj::Json& json, Uri const& uri);
83
84void from_json(dj::Json const& json, kvf::Seconds& seconds);
85void to_json(dj::Json& json, kvf::Seconds const& seconds);
86
87void from_json(dj::Json const& json, TileId& tile_id);
88void to_json(dj::Json& json, TileId const& tile_id);
89
90void from_json(dj::Json const& json, Tile& tile);
91void to_json(dj::Json& json, Tile const& tile);
92
93void from_json(dj::Json const& json, Transform& transform);
94void to_json(dj::Json& json, Transform const& transform);
95
96void from_json(dj::Json const& json, TileSet& tile_set);
97void to_json(dj::Json& json, TileSet const& tile_set);
98
99void from_json(dj::Json const& json, anim::Keyframe<Transform>& keyframe);
100void to_json(dj::Json& json, anim::Keyframe<Transform> const& keyframe);
101
102void from_json(dj::Json const& json, anim::Timeline<Transform>& timeline);
103void to_json(dj::Json& json, anim::Timeline<Transform> const& timeline);
104
105void from_json(dj::Json const& json, anim::Animation<Transform>& animation);
106void to_json(dj::Json& json, anim::Animation<Transform> const& animation);
107
108void from_json(dj::Json const& json, anim::Keyframe<TileId>& keyframe);
109void to_json(dj::Json& json, anim::Keyframe<TileId> const& keyframe);
110
111void from_json(dj::Json const& json, anim::Timeline<TileId>& timeline);
112void to_json(dj::Json& json, anim::Timeline<TileId> const& timeline);
113
114void from_json(dj::Json const& json, anim::Animation<TileId>& animation);
115void to_json(dj::Json& json, anim::Animation<TileId> const& animation);
116} // namespace le
Sorted set of Tiles.
Definition tile_set.hpp:9
Definition uri.hpp:5
Class template for Animation types.
Definition animation.hpp:12
Definition animation.hpp:8
void set_json_type(dj::Json &json, Type const &)
Definition json_io.hpp:45
auto json_type_name_match(dj::Json const &json, std::string_view type_name) -> bool
constexpr auto json_type_name_v
Definition json_io.hpp:11
constexpr auto json_type_name_v< ITileSheet >
Definition json_io.hpp:20
void from_json(dj::Json const &json, glm::vec< Length, Type > &vec)
Definition json_io.hpp:55
void set_json_type_name(dj::Json &json, std::string_view type_name)
auto get_json_type_name(dj::Json const &json) -> std::string_view
auto is_json_type(dj::Json const &json, Type const &) -> bool
Definition json_io.hpp:35
constexpr auto json_type_name_v< IShader >
Definition json_io.hpp:14
void to_json(dj::Json &json, glm::vec< Length, Type > const &vec)
Definition json_io.hpp:63
constexpr auto json_type_name_v< TileSet >
Definition json_io.hpp:16
TileId
Unique identifier for a Tile.
Definition tile_id.hpp:6
constexpr auto json_type_name_v< ITexture >
Definition json_io.hpp:18
Tile in a sprite sheet.
Definition tile.hpp:7
2D transformation.
Definition transform.hpp:8
Class template for an animation keyframe.
Definition keyframe.hpp:7
Class template for a list of animation keyframes.
Definition timeline.hpp:8