Entity remove working

master
Joey Pollack 3 years ago
parent 82dc6b000a
commit 7e41b4d259

@ -494,5 +494,11 @@ namespace editor
if (mpMapEditor) mpMapEditor->ReloadTileSets(); if (mpMapEditor) mpMapEditor->ReloadTileSets();
} }
} }
void Editor::OnEntityDelete(Entity* pEnt)
{
((PropertiesView*)mPanelManager.GetPanel(mPanels.PropertiesView))->SetSelection((Entity*)nullptr);
}
} }
} }

@ -61,6 +61,7 @@ namespace lunarium { namespace editor
void OnAssetSelected(EditorAsset* pAsset); void OnAssetSelected(EditorAsset* pAsset);
void OnNewAsset(EditorAsset* pAsset); void OnNewAsset(EditorAsset* pAsset);
void OnAssetUpdate(EditorAsset* pAsset); void OnAssetUpdate(EditorAsset* pAsset);
void OnEntityDelete(Entity* pEnt);
private: private:
Editor(const Editor&) = delete; Editor(const Editor&) = delete;

@ -87,8 +87,6 @@ namespace lunarium { namespace editor
if (ImGui::TreeNode("World Root")) if (ImGui::TreeNode("World Root"))
{ {
// List all world entities // List all world entities
// This will fail if we end up implementing child entities
// In that case this needs to become recursive
int idx = 0; int idx = 0;
for (auto iter = mpWorld->EntitiesBegin(); !mpWorld->EntitiesIsEnd(iter); iter++, idx++) for (auto iter = mpWorld->EntitiesBegin(); !mpWorld->EntitiesIsEnd(iter); iter++, idx++)
{ {
@ -117,6 +115,35 @@ namespace lunarium { namespace editor
bool was_right_clicked = false; bool was_right_clicked = false;
if (ImGui::TreeNode(pEnt->GetName().c_str())) if (ImGui::TreeNode(pEnt->GetName().c_str()))
{ {
if (ImGui::BeginDragDropSource())
{
// Need to pass a pointer to the payload data. This means we need to pass a
// pointer to the Entity pointer (Entity**) which &(*pEnt) becomes
ImGui::SetDragDropPayload("Entity", (void *)&(*pEnt), sizeof(Entity *));
ImGui::Text("%s", pEnt->GetName().c_str());
ImGui::EndDragDropSource();
}
if (ImGui::BeginDragDropTarget())
{
if (const ImGuiPayload *payload = ImGui::AcceptDragDropPayload("Entity"))
{
Entity* pDroppedEnt = *((Entity**) payload->Data);
// Can't drop and entity on itself
if (pDroppedEnt != pEnt)
{
// If the dropped ent is the parent of pEnt then we need to swap the relationship
// If the dropped ent is already a child of pEnt then we do nothing
// Else add dropped ent as a child of pEnt
}
}
ImGui::EndDragDropTarget();
}
was_clicked = ImGui::IsItemClicked(ImGuiMouseButton_Left); was_clicked = ImGui::IsItemClicked(ImGuiMouseButton_Left);
was_right_clicked = ImGui::IsItemClicked(ImGuiMouseButton_Right); was_right_clicked = ImGui::IsItemClicked(ImGuiMouseButton_Right);
@ -166,6 +193,56 @@ namespace lunarium { namespace editor
OpenPopup(PopUp::NEW_ENTITY).LogIfFailed(Editor::LogCat); OpenPopup(PopUp::NEW_ENTITY).LogIfFailed(Editor::LogCat);
} }
} }
if (mNewChild)
{
if (ImGui::Button("Delete Entity"))
{
// 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 patch some relationships
bool has_parent = false;
LUUID parent_id = 0;
if (ToRemove->HasComponent<ParentEntityComponent>())
{
// These will be used to patch the children to the grandparent - now the parent
parent_id = ToRemove->GetComponent<ParentEntityComponent>().Parent;
has_parent = true;
// Tell the parent this child does not exist anymore
mpWorld->GetEntity(parent_id)->RemoveChild(ToRemove->GetUUID());
}
if (ToRemove->HasChildren())
{
auto children = ToRemove->GetChildren();
for (LUUID child : children)
{
// Clear the parent for each child
mpWorld->GetEntity(child)->ClearParent();
if (has_parent)
{
// Make the grandparent the new parent (if there is a grandparent)
mpWorld->GetEntity(parent_id)->AddChild(child);
}
}
}
// Relationships are all patched so finally remove the entity
mpWorld->RemoveEntity(ToRemove->GetUUID());
}
if (ImGui::Button("Delete Hierarchy"))
{
}
}
ImGui::EndPopup(); ImGui::EndPopup();
} }
} }

@ -151,6 +151,25 @@ namespace lunarium
} }
void World::RemoveEntity(LUUID id)
{
Entity* temp = mEntitiesByUUID[id];
mEntitiesByUUID.erase(id);
for (auto iter = mEntities.begin(); iter != mEntities.end(); iter++)
{
if ((*iter)->GetUUID() == id)
{
mEntities.erase(iter);
break;
}
}
mECSRegistry.destroy(temp->GetEnttHandle());
delete temp;
}
Entity* World::GetEntity(LUUID id) Entity* World::GetEntity(LUUID id)
{ {
if (mEntitiesByUUID.find(id) == mEntitiesByUUID.end()) if (mEntitiesByUUID.find(id) == mEntitiesByUUID.end())

@ -77,6 +77,7 @@ namespace lunarium
entt::registry* GetEntityRegistry(); entt::registry* GetEntityRegistry();
LUUID CreateEntity(); LUUID CreateEntity();
void RemoveEntity(LUUID id);
Entity* GetEntity(LUUID id); Entity* GetEntity(LUUID id);
unsigned int GetNumEntities() const; unsigned int GetNumEntities() const;
std::vector<Entity*>::iterator EntitiesBegin(); std::vector<Entity*>::iterator EntitiesBegin();

Loading…
Cancel
Save