sculpto
scene_serializer.h
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file scene_serializer.h
3 * \brief Scene serializer (also could perform deserialization) class definition module.
4 *
5 * \author Sabitov Kirill
6 * \date 17 July 2022
7 *********************************************************************/
8
9#pragma once
10
11#include <json.h>
12namespace scl { using json = nlohmann::json; };
13
14#include "scene.h"
15#include "scene_object.h"
17
18namespace scl
19{
20 /*!*********************************************************************
21 * Common types serialization/deserialization to json format functions.
22 **********************************************************************/
23
24 namespace math
25 {
26 template <typename T> void to_json(json &Json, const vec2<T> &Vector) { Json = { Vector.X, Vector.Y }; }
27 template <typename T> void to_json(json &Json, const vec3<T> &Vector) { Json = { Vector.X, Vector.Y, Vector.Z }; }
28 template <typename T> void to_json(json &Json, const vec4<T> &Vector) { Json = { Vector.X, Vector.Y, Vector.Z, Vector.W }; }
29 template <typename T> void to_json(json &Json, const matr3<T> &Matrix) { Json = { Matrix.A }; }
30 template <typename T> void to_json(json &Json, const matr4<T> &Matrix) { Json = { Matrix.A }; }
31
32 template <typename T> void from_json(const json &Json, vec2<T> &Vector)
33 {
34 if (!Json.is_array()) return;
35 for (int i = 0; i < 2 && i < Json.size(); i++) Vector[i] = Json[i];
36 }
37 template <typename T> void from_json(const json &Json, vec3<T> &Vector)
38 {
39 if (!Json.is_array()) return;
40 for (int i = 0; i < 3 && i < Json.size(); i++) Vector[i] = Json[i];
41 }
42 template <typename T> void from_json(const json &Json, vec4<T> &Vector)
43 {
44 if (!Json.is_array()) return;
45 for (int i = 0; i < 4 && i < Json.size(); i++) Vector[i] = Json[i];
46 }
47 template <typename T> void from_json(const json &Json, matr3<T> &Matrix)
48 {
49 if (!Json.is_array()) return;
50 for (int i = 0; i < 9 && i < Json.size(); i++) Matrix[i] = Json[i];
51 }
52 template <typename T> void from_json(const json &Json, matr4<T> &Matrix)
53 {
54 if (!Json.is_array()) return;
55 for (int i = 0; i < 16 && i < Json.size(); i++) Matrix[i] = Json[i];
56 }
57 }
58
59 /*!*******************************************************************
60 * Components serialization/deserialization to json format functions.
61 ********************************************************************/
62
63 void to_json(json &Json, const name_component &NameComponent);
64 void to_json(json &Json, const transform_component &TransformComponent);
65 void to_json(json &Json, const camera_component &CameraComponent);
66 void to_json(json &Json, const point_light_component &PointLightComponent);
67 void to_json(json &Json, const directional_light_component &DirectionalLightComponent);
68 void to_json(json &Json, const spot_light_component &SpotLightComponent);
69
70 bool from_json(const json &Json, name_component &NameComponent);
71 bool from_json(const json &Json, transform_component &TransformComponent);
72 bool from_json(const json &Json, camera_component &CameraComponent);
73 bool from_json(const json &Json, point_light_component &PointLightComponent);
74 bool from_json(const json &Json, directional_light_component &DirectionalLightComponent);
75 bool from_json(const json &Json, spot_light_component &SpotLightComponent);
76
79 {
80 private:
81 weak<scene> Scene;
82
83 private:
84 /*!*
85 * Serialize specific component of scene object function.
86 *
87 * \tparam Tcomponent - type of serializeing scene object component.
88 * \param Json - json to serialize in.
89 * \param SceneObject - scene object - scene object whose component is beeing serialized.
90 * \return None.
91 */
92 template <typename Tcomponent>
93 static void SerializeComponent(json &Json, const scene_object &SceneObject)
94 {
95 if (SceneObject.HasComponent<Tcomponent>())
96 Json[typeid(Tcomponent).name()] = SceneObject.GetComponent<Tcomponent>();
97 }
98
99 template <typename Tcomponent>
100 static void DeserializeComponent(const json &Json, scene_object &SceneObject)
101 {
102 const auto &component_json = Json.find(typeid(Tcomponent).name());
103 if (component_json == Json.end()) return;
104
105 Tcomponent component {};
106 if (from_json(*component_json, component))
107 SceneObject.AddComponent<Tcomponent>(component);
108 }
109
110 /*!*
111 * Serialize scene object function.
112 *
113 * \param SceneObject - scene object to serialize.
114 * \return None.
115 */
116 static void SerializeObject(json &Json, const scene_object &SceneObject);
117
118 /*!*
119 * Deserialize object data from json and put in object function.
120 *
121 * \param Json
122 * \param Object
123 */
124 static void DeserializeObject(const json &Json, scene_object &SceneObject);
125
126 public:
127 /*!*
128 * Serialize scene data to file function.
129 *
130 * \param OutFileName - output file name.
131 * \return None.
132 */
133 static void Serialize(const shared<scene> &Scene, const std::filesystem::path &OutFileName);
134
135 /*!*
136 * Deserialize scene data from specified file function.
137 *
138 * \param Scene - scene to put deserialized date in.
139 * \param InFileName
140 */
141 static void Deserialize(shared<scene> &Scene, const std::filesystem::path &InFileName);
142 };
143}
decltype(auto) AddComponent(Targs &&... Args)
Definition: scene_object.h:84
const bool HasComponent() const
Definition: scene_object.h:71
static void Serialize(const shared< scene > &Scene, const std::filesystem::path &OutFileName)
static void Deserialize(shared< scene > &Scene, const std::filesystem::path &InFileName)
Application scene objects system components include module.
void to_json(json &Json, const vec2< T > &Vector)
void from_json(const json &Json, vec2< T > &Vector)
Definition: base.h:33
std::shared_ptr< T > shared
Definition: smart_ptr.h:15
void to_json(json &Json, const name_component &NameComponent)
nlohmann::json json
std::weak_ptr< T > weak
Definition: smart_ptr.h:35
bool from_json(const json &Json, name_component &NameComponent)
Scene class defintion module.
Application scene object class defintion module.