From 96f5b29418eba41c360e2386350b64946ab036a2 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Wed, 19 Oct 2022 01:03:32 +0800 Subject: [PATCH] Serialization/Deserialization --- .../HierarchyPanel/SHHierarchyPanel.cpp | 6 +++ .../EditorWindow/MenuBar/SHEditorMenuBar.cpp | 11 ++++- .../src/Serialization/SHSerialization.cpp | 42 ++++++++++++++++--- .../src/Serialization/SHSerialization.h | 3 +- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp index 42c9da66..30af228b 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp @@ -21,6 +21,8 @@ //#==============================================================# #include +#include "Serialization/SHSerialization.h" + namespace SHADE { @@ -171,6 +173,10 @@ namespace SHADE editor->selectedEntities.clear(); 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())) { SHEntityManager::DestroyEntity(eid); diff --git a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp index 3cb6561d..c33f4fb6 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/MenuBar/SHEditorMenuBar.cpp @@ -17,6 +17,8 @@ #include #include +#include "Serialization/SHSerialization.h" + namespace SHADE { constexpr ImGuiWindowFlags editorMenuBarFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | @@ -73,7 +75,14 @@ namespace SHADE { 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(); } if(ImGui::BeginMenu("Edit")) diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index ee189f2c..e259fbbc 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -12,6 +12,7 @@ #include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Math/Transform/SHTransformComponent.h" +#include "Physics/Components/SHRigidBodyComponent.h" namespace SHADE { @@ -119,11 +120,27 @@ namespace SHADE } } - - - std::string SHSerialization::SerializeEntityToString() + void SHSerialization::EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out) { - 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 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(out.c_str()); } void SHSerialization::SerializeEntityToFile(std::filesystem::path const& path) @@ -153,6 +170,14 @@ namespace SHADE { components[rttr::type::get().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(transform); } + if (const auto renderable = SHComponentManager::GetComponent_s(eid)) + { + components[rttr::type::get().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(renderable); + } + if (const auto rigidbody = SHComponentManager::GetComponent_s(eid)) + { + components[rttr::type::get().get_name().data()] = SHSerializationHelper::SerializeComponentToNode(rigidbody); + } node[ComponentsNode] = components; return node; } @@ -173,17 +198,22 @@ namespace SHADE auto id = GetComponentID(componentsNode); if (id.has_value()) componentIDList.push_back(id.value()); + id = GetComponentID(componentsNode); if (id.has_value()) componentIDList.push_back(id.value()); + id = GetComponentID(componentsNode); + if (id.has_value()) + componentIDList.push_back(id.value()); + return componentIDList; } void SHSerialization::InitializeEntity(YAML::Node const& entityNode, EntityID const& eid) { - auto componentsNode = entityNode[ComponentsNode]; - if(!componentsNode) + auto const componentsNode = entityNode[ComponentsNode]; + if (!componentsNode) return; SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); } diff --git a/SHADE_Engine/src/Serialization/SHSerialization.h b/SHADE_Engine/src/Serialization/SHSerialization.h index 8a57f036..d247de7a 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.h +++ b/SHADE_Engine/src/Serialization/SHSerialization.h @@ -31,7 +31,8 @@ namespace SHADE 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 const& entities); static void SerializeEntityToFile(std::filesystem::path const& path); static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode);