SHVec to yaml node convert

This commit is contained in:
Sri Sham Haran 2022-10-25 08:42:51 +08:00
parent 23b8b66297
commit f64f13521b
3 changed files with 115 additions and 75 deletions

View File

@ -1,54 +0,0 @@
[Window][MainStatusBar]
Pos=0,1060
Size=1920,20
Collapsed=0
[Window][SHEditorMenuBar]
Pos=0,48
Size=1920,1012
Collapsed=0
[Window][Hierarchy Panel]
Pos=0,142
Size=387,918
Collapsed=0
DockId=0x00000004,0
[Window][Debug##Default]
Pos=60,60
Size=400,400
Collapsed=0
[Window][Inspector]
Pos=1649,48
Size=271,1012
Collapsed=0
DockId=0x00000006,0
[Window][Profiler]
Pos=0,48
Size=387,92
Collapsed=0
DockId=0x00000003,0
[Window][Viewport]
Pos=648,48
Size=2519,1319
Collapsed=0
DockId=0x00000002,0
[Window][ Viewport]
Pos=389,48
Size=1258,1012
Collapsed=0
DockId=0x00000002,0
[Docking][Data]
DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=8,79 Size=1920,1012 Split=X
DockNode ID=0x00000005 Parent=0xC5C9B8AB SizeRef=1992,1036 Split=X
DockNode ID=0x00000001 Parent=0x00000005 SizeRef=387,1036 Split=Y Selected=0x1E6EB881
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=225,94 Selected=0x1E6EB881
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=225,940 Selected=0xE096E5AE
DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1258,1036 CentralNode=1 Selected=0xB41284E7
DockNode ID=0x00000006 Parent=0xC5C9B8AB SizeRef=271,1036 Selected=0xE7039252

View File

@ -192,6 +192,10 @@ namespace SHADE
{
components[rttr::type::get<SHRigidBodyComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(rigidbody);
}
if (const auto collisionComp = SHComponentManager::GetComponent_s<SHColliderComponent>(eid))
{
components[rttr::type::get<SHColliderComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(collisionComp);
}
node[ComponentsNode] = components;
YAML::Node scripts;

View File

@ -9,46 +9,125 @@
#include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec3.h"
#include "Math/Vector/SHVec4.h"
#include "Physics/Components/SHColliderComponent.h"
namespace YAML
{
using namespace SHADE;
template<>
struct convert<SHVec4>
{
static constexpr const char* x = "x";
static constexpr const char* y = "y";
static constexpr const char* z = "z";
static constexpr const char* w = "w";
static Node encode(SHVec4 const& rhs)
{
Node node;
node.SetStyle(EmitterStyle::Flow);
node[x] = rhs.x;
node[y] = rhs.y;
node[z] = rhs.z;
node[w] = rhs.w;
return node;
}
static bool decode(Node const& node, SHVec4& rhs)
{
if(node[x])
rhs.x = node[x].as<float>();
if(node[y])
rhs.y = node[y].as<float>();
if(node[z])
rhs.z = node[z].as<float>();
if(node[w])
rhs.w = node[w].as<float>();
return true;
}
};
template<>
struct convert<SHVec3>
{
static constexpr const char* x = "x";
static constexpr const char* y = "y";
static constexpr const char* z = "z";
static Node encode(SHVec3 const& rhs)
{
Node node;
node.SetStyle(EmitterStyle::Flow);
node[x] = rhs.x;
node[y] = rhs.y;
node[z] = rhs.z;
return node;
}
static bool decode(Node const& node, SHVec3& rhs)
{
if(node[x])
rhs.x = node[x].as<float>();
if(node[y])
rhs.y = node[y].as<float>();
if(node[z])
rhs.z = node[z].as<float>();
return true;
}
};
template<>
struct convert<SHVec2>
{
static constexpr const char* x = "x";
static constexpr const char* y = "y";
static Node encode(SHVec2 const& rhs)
{
Node node;
node.SetStyle(EmitterStyle::Flow);
node[x] = rhs.x;
node[y] = rhs.y;
return node;
}
static bool decode(Node const& node, SHVec2& rhs)
{
if(node[x])
rhs.x = node[x].as<float>();
if(node[y])
rhs.y = node[y].as<float>();
return true;
}
};
}
namespace SHADE
{
struct SHSerializationHelper
{
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static std::string SerializeComponentToString(ComponentType* component)
{
return std::string();
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static void SerializeComponentToFile(ComponentType* component, std::filesystem::path const& path)
{
}
static YAML::Node RTTRToNode(const rttr::variant& var)
{
YAML::Node node;
auto varType = var.get_type();
if(varType.is_sequential_container())
{
for(auto const& elem : var.create_sequential_view())
{
node.push_back(RTTRToNode(elem));
}
}
if (varType == rttr::type::get<SHVec4>())
{
node.SetStyle(YAML::EmitterStyle::Flow);
node["X"] = var.convert<SHVec4>().x;
node["Y"] = var.convert<SHVec4>().y;
node["Z"] = var.convert<SHVec4>().z;
node["W"] = var.convert<SHVec4>().w;
node = YAML::convert<SHVec4>::encode(var.convert<SHVec4>());
}
else if (varType == rttr::type::get<SHVec3>())
{
node.SetStyle(YAML::EmitterStyle::Flow);
node["X"] = var.convert<SHVec3>().x;
node["Y"] = var.convert<SHVec3>().y;
node["Z"] = var.convert<SHVec3>().z;
node = YAML::convert<SHVec3>::encode(var.convert<SHVec3>());
}
else if (varType == rttr::type::get<SHVec2>())
{
node.SetStyle(YAML::EmitterStyle::Flow);
node["X"] = var.convert<SHVec3>().x;
node["Y"] = var.convert<SHVec3>().y;
node = YAML::convert<SHVec2>::encode(var.convert<SHVec2>());
}
else if (varType.is_arithmetic())
{
@ -107,6 +186,17 @@ namespace SHADE
return node;
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static std::string SerializeComponentToString(ComponentType* component)
{
return std::string();
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static void SerializeComponentToFile(ComponentType* component, std::filesystem::path const& path)
{
}
template <typename ComponentType, std::enable_if_t<std::is_base_of_v<SHComponent, ComponentType>, bool> = true>
static YAML::Node SerializeComponentToNode(ComponentType* component)
{