diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp index 07446115..6be89a8b 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.cpp @@ -106,6 +106,10 @@ namespace SHADE PasteEntities(editor->selectedEntities.back()); } } + if(ImGui::IsKeyReleased(ImGuiKey_Delete)) + { + DeleteSelectedEntities(); + } } } @@ -259,9 +263,10 @@ namespace SHADE PasteEntities(eid); skipFrame = true; } - if (ImGui::Selectable(std::format("{} Delete", ICON_MD_DELETE).data())) + if (ImGui::Selectable(std::format("{} Delete selected", ICON_MD_DELETE).data())) { - SHEntityManager::DestroyEntity(eid); + //SHEntityManager::DestroyEntity(eid); + DeleteSelectedEntities(); } if ((currentNode->GetParent() != sceneGraph.GetRoot()) && ImGui::Selectable(std::format("{} Unparent Selected", ICON_MD_NORTH_WEST).data())) @@ -420,6 +425,25 @@ namespace SHADE void SHHierarchyPanel::PasteEntities(EntityID parentEID) { //SetScrollTo(SHSerialization::DeserializeEntitiesFromString(SHClipboardUtilities::GetDataFromClipboard(), parentEID).front()); - SHCommandManager::PerformCommand(std::make_shared(SHClipboardUtilities::GetDataFromClipboard(), parentEID)); + SHCommandManager::PerformCommand(std::make_shared(SHClipboardUtilities::GetDataFromClipboard(), parentEID)); } + + void SHHierarchyPanel::DeleteSelectedEntities() + { + const auto editor = SHSystemManager::GetSystem(); + auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph(); + + std::vector entitiesToDelete{}; + std::ranges::copy_if(editor->selectedEntities, std::back_inserter(entitiesToDelete), [&sceneGraph, &selectedEntities = editor->selectedEntities](EntityID const& eid) + { + EntityID parentEID = sceneGraph.GetParent(eid)->GetEntityID(); + if (parentEID == MAX_EID) + return true; + else if(std::ranges::find(selectedEntities, parentEID) == selectedEntities.end()) + return true; + return false; + }); + SHCommandManager::PerformCommand(std::make_shared(entitiesToDelete)); + } + }//namespace SHADE diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h index b667bae7..66b9ca2f 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanel.h @@ -32,6 +32,7 @@ namespace SHADE void SelectAllEntities(); void CopySelectedEntities(); void PasteEntities(EntityID parentEID = MAX_EID); + void DeleteSelectedEntities(); bool skipFrame = false; std::string filter; bool isAnyNodeSelected = false; diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.cpp b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.cpp index dfae969e..78545829 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.cpp @@ -44,7 +44,7 @@ namespace SHADE } } - void SHPasteEntityCommand::Execute() + void SHPasteEntitiesCommand::Execute() { data.createdEntities.clear(); data.createdEntities = SHSerialization::DeserializeEntitiesFromString(data.entityData, data.parentEID); @@ -52,7 +52,7 @@ namespace SHADE SHEditorWindowManager::GetEditorWindow()->SetScrollTo(data.createdEntities.begin()->second); } - void SHPasteEntityCommand::Undo() + void SHPasteEntitiesCommand::Undo() { for (auto const& [oldEID, newEID] : data.createdEntities) { @@ -60,4 +60,25 @@ namespace SHADE } } + void SHDeleteEntitiesCommand::Execute() + { + if(!data.createdEntities.empty()) + { + for(auto& eid : data.entitiesToDelete) + { + eid = data.createdEntities[eid]; + } + } + data.entityData = SHSerialization::SerializeEntitiesToString(data.entitiesToDelete); + for (auto const& eid : data.entitiesToDelete) + { + SHEntityManager::DestroyEntity(eid); + } + } + + void SHDeleteEntitiesCommand::Undo() + { + data.createdEntities = SHSerialization::DeserializeEntitiesFromString(data.entityData); + data.entityData = SHSerialization::ResolveSerializedEntityIndices(data.entityData, data.createdEntities); + } } diff --git a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.h b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.h index 8bad9df2..fccd9489 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.h +++ b/SHADE_Engine/src/Editor/EditorWindow/HierarchyPanel/SHHierarchyPanelCommands.h @@ -35,21 +35,38 @@ namespace SHADE std::unordered_map entityParentData{}; }; - class SHPasteEntityCommand final : public SHBaseCommand + class SHPasteEntitiesCommand final : public SHBaseCommand { public: struct Data { - SHSerialization::CreatedEntitiesList createdEntities{}; EntityID parentEID{MAX_EID}; std::string entityData{}; + SHSerialization::CreatedEntitiesList createdEntities{}; }; - SHPasteEntityCommand() = delete; - SHPasteEntityCommand(std::string const& serializedEntityData, EntityID parentEid = MAX_EID):data({{}, parentEid, serializedEntityData}){} + SHPasteEntitiesCommand() = delete; + SHPasteEntitiesCommand(std::string const& serializedEntityData, EntityID parentEid = MAX_EID):data({parentEid, serializedEntityData, {}}){} void Execute() override; void Undo() override; private: Data data; }; + + class SHDeleteEntitiesCommand final : public SHBaseCommand + { + public: + struct Data + { + std::vector entitiesToDelete{}; + SHSerialization::CreatedEntitiesList createdEntities{}; + std::string entityData{}; + }; + SHDeleteEntitiesCommand() = delete; + SHDeleteEntitiesCommand(std::vector entitiesToBeDeleted): data{entitiesToBeDeleted}{} + void Execute() override; + void Undo() override; + private: + Data data; + }; }