From c5ef8805db200cf2b78e6e7ba4af9c38a3b07091 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 13 Oct 2022 15:43:22 -0400 Subject: [PATCH] Delete entity hierarchy working --- src/run_modes/editor/panels/world_tree.cpp | 56 +++++++++++++++++++++- src/run_modes/editor/panels/world_tree.h | 2 + 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/run_modes/editor/panels/world_tree.cpp b/src/run_modes/editor/panels/world_tree.cpp index 5c46983..f32db86 100644 --- a/src/run_modes/editor/panels/world_tree.cpp +++ b/src/run_modes/editor/panels/world_tree.cpp @@ -23,7 +23,7 @@ namespace lunarium { namespace editor mpEditor(editor), // TODO: Temp world created here mpWorld(nullptr), - mpSelectedEntity(nullptr), mNewChild(false), mParentEnt(nullptr) + mpSelectedEntity(nullptr), mNewChild(false), mParentEnt(nullptr), mCMenuOpen(false) { AddPopup(PopUp::NEW_ENTITY, "New Entity", [](Panel *p) { @@ -196,6 +196,7 @@ namespace lunarium { namespace editor if (mNewChild) { + mCMenuOpen = true; if (ImGui::Button("Delete Entity")) { // mParentEnt is the entity that was right-clicked on @@ -238,13 +239,64 @@ namespace lunarium { namespace editor mpWorld->RemoveEntity(ToRemove->GetUUID()); } - if (ImGui::Button("Delete Hierarchy")) + if (ImGui::Button("Delete Entity Hierarchy")) { + // mParentEnt is the entity that was right-clicked on + // Lets give it a less confusing name for this context + Entity *ToRemove = mParentEnt; + + // Give the editor a chance to clean up any refs to this entity + mpEditor->OnEntityDelete(ToRemove); + + // If this entity has a parent we need to tell it we don't exist anymore + LUUID parent_id = 0; + if (ToRemove->HasComponent()) + { + parent_id = ToRemove->GetComponent().Parent; + mpWorld->GetEntity(parent_id)->RemoveChild(ToRemove->GetUUID()); + } + + if (ToRemove->HasChildren()) + { + // Recursively remove all children + auto children = ToRemove->GetChildren(); + for (LUUID child : children) + { + RecursiveEntDelete(mpWorld->GetEntity(child)); + } + } + // All children are removed so remove the entity + mpWorld->RemoveEntity(ToRemove->GetUUID()); } } ImGui::EndPopup(); } + + else if (mNewChild && mCMenuOpen) + { + mNewChild = false; + mCMenuOpen = false; + mParentEnt = nullptr; + } + + } + + + void WorldTree::RecursiveEntDelete(Entity* pe) + { + mpEditor->OnEntityDelete(pe); + + if (pe->HasChildren()) + { + auto children = pe->GetChildren(); + for (LUUID child : children) + { + RecursiveEntDelete(mpWorld->GetEntity(child)); + } + } + + mpWorld->RemoveEntity(pe->GetUUID()); } }} \ No newline at end of file diff --git a/src/run_modes/editor/panels/world_tree.h b/src/run_modes/editor/panels/world_tree.h index 0083b2d..d8ff99d 100644 --- a/src/run_modes/editor/panels/world_tree.h +++ b/src/run_modes/editor/panels/world_tree.h @@ -34,6 +34,7 @@ namespace editor lunarium::World* mpWorld; Editor* mpEditor; Entity* mpSelectedEntity; + bool mCMenuOpen; // Helpers void ShowEntity(Entity* pEnt); @@ -43,6 +44,7 @@ namespace editor void HandlePopupEvents(); void PopupNewEntity(); + void RecursiveEntDelete(Entity* pe); private: // POPUP ENUMS