diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp index 080cbf2c..889abc79 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorComponentView.hpp @@ -44,7 +44,7 @@ namespace SHADE { if (!component) return; - auto componentType = rttr::type::get(*component); + auto componentType = rttr::type::get(); CheckBox("##IsActive", [component]() {return component->isActive; }, [component](bool const& active) {component->isActive = active; }); ImGui::SameLine(); if (ImGui::CollapsingHeader(componentType.get_name().data())) diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp new file mode 100644 index 00000000..ee681a85 --- /dev/null +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -0,0 +1,79 @@ +#include "SHpch.h" +#include "SHSerialization.h" + +#include + +#include "Scene/SHSceneManager.h" +#include "Tools/SHException.h" + +#include "SHSerializationHelper.hpp" +#include "Math/Transform/SHTransformComponent.h" + +namespace SHADE +{ + namespace SHSerialization + { + void SerializeSceneToFile(std::filesystem::path const& path) + { + + } + + std::string SerializeSceneToString() + { + YAML::Emitter out; + SerializeSceneToEmitter(out); + return std::string(out.c_str()); + } + + void SerializeSceneToEmitter(YAML::Emitter& out) + { + auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + auto root = sceneGraph.GetRoot(); + + SHASSERT(root != nullptr, "Root is null. Failed to serialize scene to node."); + + auto const& children = root->GetChildren(); + out << YAML::BeginDoc; + for (auto child : children) + { + out << YAML::BeginSeq; + out << SerializeEntityToNode(child); + out << YAML::EndSeq; + } + out << YAML::EndDoc; + } + + std::string SerializeEntityToString() + { + return std::string(); + } + + void SerializeEntityToFile(std::filesystem::path const& path) + { + } + + YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode) + { + YAML::Node node; + if(!sceneNode) + { + node = YAML::Null; + return node; + } + auto eid = sceneNode->GetEntityID(); + node["EID"] = eid; + node["isActive"] = sceneNode->IsActive(); + auto const& children = sceneNode->GetChildren(); + node["NumberOfChildren"] = children.size(); + + YAML::Node components; + + if(const auto transform = SHComponentManager::GetComponent_s(eid)) + { + components[rttr::type::get().get_name().data()] = SerializeComponentToNode(transform); + } + node["Components"] = components; + return node; + } + } +} diff --git a/SHADE_Engine/src/Serialization/SHSerialization.h b/SHADE_Engine/src/Serialization/SHSerialization.h new file mode 100644 index 00000000..aaccd065 --- /dev/null +++ b/SHADE_Engine/src/Serialization/SHSerialization.h @@ -0,0 +1,28 @@ +#pragma once + +#include "SH_API.h" +#include +#include + +#include + +namespace YAML +{ + class Emitter; + class Node; +} + +namespace SHADE +{ + class SHSceneNode; + namespace SHSerialization + { + static void SerializeSceneToFile(std::filesystem::path const& path); + static std::string SerializeSceneToString(); + static void SerializeSceneToEmitter(YAML::Emitter& out); + + static std::string SerializeEntityToString(); + static void SerializeEntityToFile(std::filesystem::path const& path); + static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode); + } +} \ No newline at end of file diff --git a/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp b/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp new file mode 100644 index 00000000..560c58dd --- /dev/null +++ b/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp @@ -0,0 +1,97 @@ +#pragma once + +#include "ECS_Base/Components/SHComponent.h" +#include + +#include + +namespace SHADE +{ + namespace SHSerialization + { + template , bool> = true> + static std::string SerializeComponentToString(ComponentType* component) + { + return std::string(); + } + + template , 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_arithmetic()) + { + bool ok = false; + if (varType == rttr::type::get()) + node = var.to_bool(); + else if (varType == rttr::type::get()) + node = var.to_int8(&ok); + else if (varType == rttr::type::get()) + node = var.to_int16(&ok); + else if (varType == rttr::type::get()) + node = var.to_int32(&ok); + else if (varType == rttr::type::get()) + node = var.to_int64(&ok); + else if (varType == rttr::type::get()) + node = var.to_uint8(&ok); + else if (varType == rttr::type::get()) + node = var.to_uint16(&ok); + else if (varType == rttr::type::get()) + node = var.to_uint32(&ok); + else if (varType == rttr::type::get()) + node = var.to_uint64(&ok); + else if (varType == rttr::type::get()) + node = var.to_float(&ok); + else if (varType == rttr::type::get()) + node = var.to_double(&ok); + //else if (varType == rttr::type::get()) //same as uint8_t + // node = var.to_uint8(); + } + else if (varType.is_enumeration()) + { + bool ok = false; + auto result = var.to_string(&ok); + if (ok) + { + node = var.to_string(); + } + else + { + ok = false; + auto value = var.to_uint64(&ok); + if (ok) + node = value; + else + node = YAML::Null; + } + } + else + { + auto properties = var.get_type().get_properties(); + for(auto property : properties) + { + node[property.get_name().data()] = RTTRToNode(property.get_value(var)); + } + } + return node; + } + + template , bool> = true> + static YAML::Node SerializeComponentToNode(ComponentType* component) + { + YAML::Node node{}; + if(!component) + return node; + + auto componentType = rttr::type::get(); + node[componentType.get_name().data()] = RTTRToNode(*component); + + return node; + } + } +} \ No newline at end of file