From ff2b9b4af38d6918beca1fa838191f2d20614ae1 Mon Sep 17 00:00:00 2001 From: Sri Sham Haran Date: Sun, 26 Feb 2023 22:44:39 +0800 Subject: [PATCH] Rudimentary Prefab (no resolution) Warning Fixes --- Assets/Editor/Editor.SHConfig | 4 - Assets/Prefabs/====Raccoon====.shprefab | 147 ++++++++++++++++++ .../Prefabs/====Raccoon====.shprefab.shmeta | 3 + .../AssetBrowser/SHAssetBrowser.cpp | 34 ++++ .../HierarchyPanel/SHHierarchyPanel.cpp | 10 +- .../Serialization/Prefab/SHPrefabManager.cpp | 27 +++- .../Serialization/Prefab/SHPrefabManager.h | 3 +- .../src/Serialization/SHSerialization.cpp | 35 +++-- .../src/Serialization/SHSerialization.h | 5 +- .../Serialization/SHSerializationHelper.hpp | 1 + 10 files changed, 243 insertions(+), 26 deletions(-) delete mode 100644 Assets/Editor/Editor.SHConfig create mode 100644 Assets/Prefabs/====Raccoon====.shprefab create mode 100644 Assets/Prefabs/====Raccoon====.shprefab.shmeta diff --git a/Assets/Editor/Editor.SHConfig b/Assets/Editor/Editor.SHConfig deleted file mode 100644 index 37edf50c..00000000 --- a/Assets/Editor/Editor.SHConfig +++ /dev/null @@ -1,4 +0,0 @@ -Start Maximized: true -Working Scene ID: 97158628 -Window Size: {x: 1920, y: 1013} -Style: 0 \ No newline at end of file diff --git a/Assets/Prefabs/====Raccoon====.shprefab b/Assets/Prefabs/====Raccoon====.shprefab new file mode 100644 index 00000000..59b42ef2 --- /dev/null +++ b/Assets/Prefabs/====Raccoon====.shprefab @@ -0,0 +1,147 @@ +- EID: 0 + PrefabID: 117058283 + Name: ====Raccoon==== + IsActive: true + NumberOfChildren: 2 + Components: ~ + Scripts: ~ +- EID: 1 + Name: Player + IsActive: true + NumberOfChildren: 3 + Components: + Transform Component: + Translate: {x: 2.35245037, y: 0.38365531, z: 7.10571432} + Rotate: {x: -0, y: 0, z: -0} + Scale: {x: 0.999999881, y: 1, z: 0.999999881} + IsActive: true + Renderable Component: + Mesh: 149697411 + Material: 126974645 + IsActive: true + RigidBody Component: + Type: Dynamic + Drag: 0.00999999978 + Angular Drag: 0.100000001 + Use Gravity: false + Interpolate: false + Sleeping Enabled: true + Freeze Position X: false + Freeze Position Y: true + Freeze Position Z: false + Freeze Rotation X: true + Freeze Rotation Y: true + Freeze Rotation Z: true + IsActive: true + Collider Component: + Colliders: + - Is Trigger: false + Collision Tag: 0 + Type: Box + Half Extents: {x: 0.400000006, y: 0.5, z: 0.300000012} + Friction: 0.400000006 + Bounciness: 0 + Density: 1 + Position Offset: {x: 0, y: 0.25, z: 0} + Rotation Offset: {x: 0, y: 0, z: 0} + IsActive: true + Scripts: + - Type: PlayerController + Enabled: true + respawnPoint: 239 + currentState: 0 + maxMoveVel: 3 + moveForce: 50 + sprintMultiplier: 1.5 + rotationFactorPerFrame: 5 + maxJumpHeight: 2 + maxJumpTime: 0.75 + fallMultipler: 3 + lightMultiper: 0.899999976 + mediumMultiper: 0.699999988 + heavyMultiper: 0.5 + - Type: PickAndThrow + Enabled: true + throwForce: [10, 8, 10] + cameraArmOffSet: [0.25, 0.600000024, 0.200000003] + delayTimer: 1 + aimingLength: 1 + throwItem: false + rayDistance: 0.75 + rayHeight: 0.100000001 +- EID: 2 + Name: HoldingPoint + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: 0.899999976, z: 0.200000286} + Rotate: {x: 0, y: 0, z: -0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Scripts: ~ +- EID: 3 + Name: PlayerCamera + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: -5.96046448e-08, z: 0} + Rotate: {x: 0, y: 6.28318548, z: 2.23517329e-08} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Camera Component: + Position: {x: 2.12735963, y: 0.362327784, z: 7.98933029} + Pitch: 0 + Yaw: 360 + Roll: 1.28065994e-06 + Width: 2560 + Near: 0.00999999978 + Far: 10000 + Perspective: true + FOV: 45 + IsActive: true + Camera Arm Component: + Arm Pitch: 0 + Arm Yaw: 0 + Arm Length: 3 + Look At Camera Origin: true + Target Offset: {x: 0, y: 0.75, z: 0} + Camera Collision: true + IsActive: true + Scripts: + - Type: SHADE_Scripting.ThirdPersonCamera + Enabled: true + armLength: 3 + turnSpeedPitch: 0.200000003 + turnSpeedYaw: 0.400000006 + pitchClamp: 45 + inverseXControls: false + inverseYControls: false + lowerClamp: 5 +- EID: 4 + Name: PlayerBag + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 0, y: -2.98023224e-08, z: 4.76837158e-07} + Rotate: {x: 0, y: 0, z: -0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Renderable Component: + Mesh: 144838771 + Material: 123745521 + IsActive: true + Scripts: ~ +- EID: 5 + Name: RespawnPoint + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 2.5, y: 0.660660267, z: 7} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Scripts: ~ \ No newline at end of file diff --git a/Assets/Prefabs/====Raccoon====.shprefab.shmeta b/Assets/Prefabs/====Raccoon====.shprefab.shmeta new file mode 100644 index 00000000..8fc77e7e --- /dev/null +++ b/Assets/Prefabs/====Raccoon====.shprefab.shmeta @@ -0,0 +1,3 @@ +Name: ====Raccoon==== +ID: 117058283 +Type: 6 diff --git a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp index 791396df..dce3ffbd 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/AssetBrowser/SHAssetBrowser.cpp @@ -19,6 +19,10 @@ #include "Tools/Utilities/SHStringUtilities.h" #include #include +#include "Assets/Asset Types/SHPrefabAsset.h" +#include "Serialization/SHSerialization.h" +#include +#include "Serialization/Prefab/SHPrefabManager.h" namespace SHADE { @@ -247,6 +251,7 @@ namespace SHADE const ImRect childRect = DrawAsset(file.assetMeta, file.ext); return childRect; } + return ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax()); } ImRect SHAssetBrowser::DrawAsset(SHAsset const* const asset, FileExt const& ext /*= ""*/) noexcept @@ -365,6 +370,35 @@ namespace SHADE } } + if(ImGui::BeginPopupContextItem()) + { + switch (asset->type) + { + case AssetType::INVALID: break; + case AssetType::SHADER: break; + case AssetType::SHADER_BUILT_IN: break; + case AssetType::TEXTURE: break; + case AssetType::MESH: break; + case AssetType::SCENE: + break; + case AssetType::PREFAB: + { + if(ImGui::Selectable("Insert into scene")) + { + auto createdEntitiesList = SHSerialization::DeserializeEntitiesFromString(SHAssetManager::GetData(asset->id)->data); + SHEditorWindowManager::GetEditorWindow()->SetScrollTo(createdEntitiesList.begin()->second); + SHPrefabManager::AddEntity(asset->id, createdEntitiesList.begin()->second); + } + } + break; + case AssetType::MATERIAL: break; + case AssetType::SCRIPT: break; + case AssetType::MAX_COUNT: break; + default:; + } + ImGui::EndPopup(); + } + if (ImGui::IsItemHovered()) { ImGui::BeginTooltip(); diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp index a7e85ad4..a24d46e2 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp @@ -26,6 +26,9 @@ #include "Serialization/SHSerialization.h" #include "Tools/Utilities/SHClipboardUtilities.h" #include "Tools/Utilities/SHStringUtilities.h" +#include "Serialization/Prefab/SHPrefabManager.h" +#include "../SHEditorWindowManager.h" +#include "../AssetBrowser/SHAssetBrowser.h" namespace SHADE @@ -315,7 +318,7 @@ namespace SHADE const ImGuiTreeNodeFlags nodeFlags = ((isSelected) ? ImGuiTreeNodeFlags_Selected : 0) | ((children.empty()) ? ImGuiTreeNodeFlags_Leaf : ImGuiTreeNodeFlags_OpenOnArrow); //Draw Node - bool isNodeOpen = ImGui::TreeNodeEx(reinterpret_cast(eid), nodeFlags, "%u: %s", SHEntityManager::GetEntityIndex(eid), entity->name.c_str()); + bool isNodeOpen = ImGui::TreeNodeEx((void*)(eid), nodeFlags, "%u: %s", SHEntityManager::GetEntityIndex(eid), entity->name.c_str()); if(!filter.empty()) { @@ -388,6 +391,11 @@ namespace SHADE //SHEntityManager::DestroyEntity(eid); DeleteSelectedEntities(); } + if(ImGui::Selectable("Save entity as Prefab")) + { + SHPrefabManager::SaveEntityAsPrefab(eid); + SHEditorWindowManager::GetEditorWindow()->QueueRefresh(); + } if ((currentNode->GetParent() != sceneGraph.GetRoot()) && ImGui::Selectable(std::format("{} Unparent Selected", ICON_MD_NORTH_WEST).data())) { diff --git a/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.cpp b/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.cpp index 048298ed..b19a20df 100644 --- a/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.cpp +++ b/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.cpp @@ -9,9 +9,21 @@ namespace SHADE { SHPrefabManager::PrefabMap SHPrefabManager::prefabMap{}; + AssetID SHPrefabManager::GetPrefabAssetID(EntityID eid) noexcept + { + for(auto const& [assetId, entityList] : prefabMap) + { + if(std::ranges::find(entityList, eid) != entityList.end()) + { + return assetId; + } + } + return 0; + } + void SHPrefabManager::AddPrefab(AssetID const& prefabAssetID) noexcept { - prefabMap.insert({ prefabAssetID, {} }); + prefabMap[prefabAssetID] = {}; } void SHPrefabManager::RemovePrefab(AssetID const& prefabAssetID) noexcept @@ -34,17 +46,16 @@ namespace SHADE void SHPrefabManager::AddEntity(AssetID const& prefabAssetID, EntityID const& eid) noexcept { - if (prefabMap.contains(prefabAssetID)) - { - prefabMap[prefabAssetID].push_back(eid); - } + + prefabMap[prefabAssetID].push_back(eid); + } void SHPrefabManager::RemoveEntity(AssetID const& prefabAssetID, EntityID const& eid) noexcept { if (prefabMap.contains(prefabAssetID)) { - std::ranges::remove(prefabMap[prefabAssetID], eid); + (void)std::ranges::remove(prefabMap[prefabAssetID], eid); } } @@ -52,9 +63,9 @@ namespace SHADE { SHEntity* const entity = SHEntityManager::GetEntityByID(eid); AssetID const assetID = SHAssetManager::CreateNewAsset(AssetType::PREFAB, entity->name); - + AddEntity(assetID, eid); auto assetData = SHAssetManager::GetData(assetID); - assetData->data = SHSerialization::SerializeEntitiesToString({eid}); + assetData->data = SHSerialization::SerializeEntityToString(eid); SHAssetManager::SaveAsset(assetID); } diff --git a/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.h b/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.h index 29d47c5c..64e8f24f 100644 --- a/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.h +++ b/SHADE_Engine/src/Serialization/Prefab/SHPrefabManager.h @@ -24,6 +24,7 @@ namespace SHADE using PrefabMap = std::unordered_map>; using PrefabEntitiesComponentStatusData = std::unordered_map,PrefabEntityComponentStatus>>; + static AssetID GetPrefabAssetID(EntityID eid) noexcept; static void AddPrefab(AssetID const& prefabAssetID) noexcept; static void RemovePrefab(AssetID const& prefabAssetID) noexcept; static void ClearPrefab(AssetID const& prefabAssetID) noexcept; @@ -39,6 +40,6 @@ namespace SHADE static PrefabEntitiesComponentStatusData prefabEntitiesComponentStatusData; friend class SHSerialization; - friend class SHSerializationHelper; + friend struct SHSerializationHelper; }; } diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index c291e4ea..464b1d9d 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -15,6 +15,7 @@ #include "ECS_Base/Managers/SHSystemManager.h" #include "Scripting/SHScriptEngine.h" #include "Tools/FileIO/SHFileIO.h" +#include "Prefab/SHPrefabManager.h" namespace SHADE { @@ -62,6 +63,7 @@ namespace SHADE EntityID eid{MAX_EID}, oldEID{MAX_EID}; if (!node) return eid; + if (node[EIDNode]) oldEID = eid = node[EIDNode].as(); std::string name = "UnnamedEntitiy"; @@ -141,18 +143,18 @@ namespace SHADE void SHSerialization::EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out, bool isPrefab /*= false*/, EntityID* entityIndex /*= nullptr*/) { - out << SerializeEntityToNode(entityNode); + out << SerializeEntityToNode(entityNode, isPrefab, entityIndex); if(isPrefab) ++(*entityIndex); auto const& children = entityNode->GetChildren(); for (auto const& child : children) { - EmitEntity(child, out, isPrefab); + EmitEntity(child, out, isPrefab, entityIndex); } } - std::string SHSerialization::SerializeEntitiesToString(std::vector const& entities, bool isPrefab) noexcept + std::string SHSerialization::SerializeEntitiesToString(std::vector const& entities) noexcept { YAML::Emitter out; YAML::Node node; @@ -162,15 +164,27 @@ namespace SHADE for (auto const& eid : entities) { auto entityNode = sceneGraph.GetNode(eid); - if(isPrefab) - EmitEntity(entityNode, out, isPrefab); - else - EmitEntity(entityNode, out); + EmitEntity(entityNode, out); } out << YAML::EndSeq; return std::string(out.c_str()); } + std::string SHSerialization::SerializeEntityToString(EntityID eid) noexcept + { + YAML::Emitter out; + YAML::Node node; + auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + out << YAML::BeginSeq; + EntityID entityIndex = 0; + + auto entityNode = sceneGraph.GetNode(eid); + EmitEntity(entityNode, out, true, &entityIndex); + + out << YAML::EndSeq; + return std::string(out.c_str()); + } + //void SHSerialization::SerializeEntityToFile(std::filesystem::path const& path) //{ //} @@ -201,11 +215,12 @@ namespace SHADE } node.SetStyle(YAML::EmitterStyle::Block); - node[EIDNode] = eid; + node[EIDNode] = (entityIndex) ? *entityIndex : eid; - if(isPrefab) //TODO: Set asset id for top level prefab entity only + AssetID prefabAssetID = SHPrefabManager::GetPrefabAssetID(eid); + if(prefabAssetID != 0) { - node[PrefabID] = 0; + node[PrefabID] = prefabAssetID; } node[EntityNameNode] = entity->name; diff --git a/SHADE_Engine/src/Serialization/SHSerialization.h b/SHADE_Engine/src/Serialization/SHSerialization.h index 9d8362ed..c8971f84 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.h +++ b/SHADE_Engine/src/Serialization/SHSerialization.h @@ -38,10 +38,11 @@ namespace SHADE static std::string DeserializeSceneFromFile(AssetID const& sceneAssetID) noexcept; - static void EmitEntity(SHSceneNode* entityNode, YAML::Emitter& out, bool isPrefab = false, EntityID* entityIndex = nullptr); - static std::string SerializeEntitiesToString(std::vector const& entities, bool isPrefab = false) noexcept; + static std::string SerializeEntitiesToString(std::vector const& entities) noexcept; + + static std::string SerializeEntityToString(EntityID eid) noexcept; //static void SerializeEntityToFile(std::filesystem::path const& path); static YAML::Node SerializeEntityToNode(SHSceneNode* sceneNode, bool isPrefab = false, EntityID* entityIndex = nullptr); diff --git a/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp b/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp index 08f1262f..a4936528 100644 --- a/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp +++ b/SHADE_Engine/src/Serialization/SHSerializationHelper.hpp @@ -135,6 +135,7 @@ namespace SHADE } } + return YAML::Node(); } template