Serialization/Deserialization

This commit is contained in:
Sri Sham Haran 2022-10-19 01:03:32 +08:00
parent 2f429f33f8
commit 96f5b29418
4 changed files with 54 additions and 8 deletions

View File

@ -21,6 +21,8 @@
//#==============================================================# //#==============================================================#
#include <imgui.h> #include <imgui.h>
#include "Serialization/SHSerialization.h"
namespace SHADE namespace SHADE
{ {
@ -171,6 +173,10 @@ namespace SHADE
editor->selectedEntities.clear(); editor->selectedEntities.clear();
editor->selectedEntities.push_back(eid); editor->selectedEntities.push_back(eid);
} }
if(ImGui::Selectable("Copy"))
{
SHLOG_INFO(SHSerialization::SerializeEntitiesToString(editor->selectedEntities))
}
if(ImGui::Selectable(std::format("{} Delete", ICON_MD_DELETE).data())) if(ImGui::Selectable(std::format("{} Delete", ICON_MD_DELETE).data()))
{ {
SHEntityManager::DestroyEntity(eid); SHEntityManager::DestroyEntity(eid);

View File

@ -17,6 +17,8 @@
#include <imgui_internal.h> #include <imgui_internal.h>
#include <rttr/type> #include <rttr/type>
#include "Serialization/SHSerialization.h"
namespace SHADE namespace SHADE
{ {
constexpr ImGuiWindowFlags editorMenuBarFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | constexpr ImGuiWindowFlags editorMenuBarFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse |
@ -73,7 +75,14 @@ namespace SHADE
{ {
if (ImGui::BeginMenu("File")) if (ImGui::BeginMenu("File"))
{ {
if(ImGui::Selectable("Save"))
{
SHSerialization::SerializeSceneToFile("../../Assets/Scenes/Test.SHADE");
}
if(ImGui::Selectable("Load"))
{
SHSerialization::DeserializeSceneFromFile("../../Assets/Scenes/Test.SHADE");
}
ImGui::EndMenu(); ImGui::EndMenu();
} }
if(ImGui::BeginMenu("Edit")) if(ImGui::BeginMenu("Edit"))

View File

@ -12,6 +12,7 @@
#include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Math/Transform/SHTransformComponent.h" #include "Math/Transform/SHTransformComponent.h"
#include "Physics/Components/SHRigidBodyComponent.h"
namespace SHADE namespace SHADE
{ {
@ -119,11 +120,27 @@ namespace SHADE
} }
} }
void SHSerialization::EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out)
std::string SHSerialization::SerializeEntityToString()
{ {
return std::string(); out << SerializeEntityToNode(entityNode);
auto const& children = entityNode->GetChildren();
for(auto const& child : children)
{
EmitEntity(child, out);
}
}
std::string SHSerialization::SerializeEntitiesToString(std::vector<EntityID> const& entities)
{
YAML::Emitter out;
YAML::Node node;
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
for (auto const& eid : entities)
{
auto entityNode = sceneGraph.GetNode(eid);
EmitEntity(entityNode, out);
}
return std::basic_string<char>(out.c_str());
} }
void SHSerialization::SerializeEntityToFile(std::filesystem::path const& path) void SHSerialization::SerializeEntityToFile(std::filesystem::path const& path)
@ -153,6 +170,14 @@ namespace SHADE
{ {
components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform); components[rttr::type::get<SHTransformComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform);
} }
if (const auto renderable = SHComponentManager::GetComponent_s<SHRenderable>(eid))
{
components[rttr::type::get<SHRenderable>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(renderable);
}
if (const auto rigidbody = SHComponentManager::GetComponent_s<SHRigidBodyComponent>(eid))
{
components[rttr::type::get<SHRigidBodyComponent>().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(rigidbody);
}
node[ComponentsNode] = components; node[ComponentsNode] = components;
return node; return node;
} }
@ -173,17 +198,22 @@ namespace SHADE
auto id = GetComponentID<SHTransformComponent>(componentsNode); auto id = GetComponentID<SHTransformComponent>(componentsNode);
if (id.has_value()) if (id.has_value())
componentIDList.push_back(id.value()); componentIDList.push_back(id.value());
id = GetComponentID<SHRenderable>(componentsNode); id = GetComponentID<SHRenderable>(componentsNode);
if (id.has_value()) if (id.has_value())
componentIDList.push_back(id.value()); componentIDList.push_back(id.value());
id = GetComponentID<SHRigidBodyComponent>(componentsNode);
if (id.has_value())
componentIDList.push_back(id.value());
return componentIDList; return componentIDList;
} }
void SHSerialization::InitializeEntity(YAML::Node const& entityNode, EntityID const& eid) void SHSerialization::InitializeEntity(YAML::Node const& entityNode, EntityID const& eid)
{ {
auto componentsNode = entityNode[ComponentsNode]; auto const componentsNode = entityNode[ComponentsNode];
if(!componentsNode) if (!componentsNode)
return; return;
SHSerializationHelper::InitializeComponentFromNode<SHTransformComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHTransformComponent>(componentsNode, eid);
} }

View File

@ -31,7 +31,8 @@ namespace SHADE
static void DeserializeSceneFromFile(std::filesystem::path const& path); static void DeserializeSceneFromFile(std::filesystem::path const& path);
static std::string SerializeEntityToString(); static void EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out);
static std::string SerializeEntitiesToString(std::vector<EntityID> const& entities);
static void SerializeEntityToFile(std::filesystem::path const& path); static void SerializeEntityToFile(std::filesystem::path const& path);
static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode); static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode);