From cd4be3a708b6645fbcc6cc2f711e6cd9ae851048 Mon Sep 17 00:00:00 2001 From: srikappa Date: Thu, 13 May 2021 14:37:22 -0700 Subject: [PATCH] Found a better way to find common owning instance --- .../Prefab/PrefabPublicHandler.cpp | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp index d1a3e189b1..eec0128419 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp @@ -639,20 +639,19 @@ namespace AzToolsFramework return AZ::Failure(AZStd::string("Cannot delete multiple entities belonging to different instances with one operation.")); } + AZ::EntityId firstEntityIdToDelete = entityIds[0]; + InstanceOptionalReference commonOwningInstance = GetOwnerInstanceByEntityId(firstEntityIdToDelete); + + // If the first entity id is that of an instance, we need to delete that instance from it's parent. + if (commonOwningInstance->get().GetContainerEntityId() == firstEntityIdToDelete && + !IsLevelInstanceContainerEntity(firstEntityIdToDelete)) + { + commonOwningInstance = commonOwningInstance->get().GetParentInstance(); + } + // Retrieve entityList from entityIds EntityList inputEntityList = EntityIdListToEntityList(entityIds); - EntityList topLevelEntities; - AZ::EntityId commonRootEntityId; - InstanceOptionalReference commonRootEntityOwningInstance; - PrefabOperationResult findCommonRootOutcome = FindCommonRootOwningInstance( - entityIds, inputEntityList, topLevelEntities, commonRootEntityId, commonRootEntityOwningInstance); - - if (!findCommonRootOutcome.IsSuccess()) - { - return findCommonRootOutcome; - } - AZ_PROFILE_FUNCTION(AZ::Debug::ProfileCategory::AzToolsFramework); UndoSystem::URSequencePoint* currentUndoBatch = nullptr; @@ -688,15 +687,14 @@ namespace AzToolsFramework AZ_PROFILE_SCOPE(AZ::Debug::ProfileCategory::AzToolsFramework, "Internal::DeleteEntities:UndoCaptureAndPurgeEntities"); Prefab::PrefabDom instanceDomBefore; - m_instanceToTemplateInterface->GenerateDomForInstance(instanceDomBefore, commonRootEntityOwningInstance->get()); + m_instanceToTemplateInterface->GenerateDomForInstance(instanceDomBefore, commonOwningInstance->get()); if (deleteDescendants) { AZStd::vector entities; AZStd::vector> instances; - bool success = RetrieveAndSortPrefabEntitiesAndInstances( - inputEntityList, commonRootEntityOwningInstance->get(), entities, instances); + bool success = RetrieveAndSortPrefabEntitiesAndInstances(inputEntityList, commonOwningInstance->get(), entities, instances); if (!success) { @@ -721,23 +719,22 @@ namespace AzToolsFramework // If this is the container entity, it actually represents the instance so get its owner if (owningInstance->get().GetContainerEntityId() == entityId) { - auto instancePtr = - commonRootEntityOwningInstance->get().DetachNestedInstance(owningInstance->get().GetInstanceAlias()); + auto instancePtr = commonOwningInstance->get().DetachNestedInstance(owningInstance->get().GetInstanceAlias()); instancePtr.reset(); } else { - commonRootEntityOwningInstance->get().DetachEntity(entityId); + commonOwningInstance->get().DetachEntity(entityId); AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationRequests::DeleteEntity, entityId); } } } Prefab::PrefabDom instanceDomAfter; - m_instanceToTemplateInterface->GenerateDomForInstance(instanceDomAfter, commonRootEntityOwningInstance->get()); + m_instanceToTemplateInterface->GenerateDomForInstance(instanceDomAfter, commonOwningInstance->get()); PrefabUndoInstance* command = aznew PrefabUndoInstance("Instance deletion"); - command->Capture(instanceDomBefore, instanceDomAfter, commonRootEntityOwningInstance->get().GetTemplateId()); + command->Capture(instanceDomBefore, instanceDomAfter, commonOwningInstance->get().GetTemplateId()); command->SetParent(selCommand); }