From bb458254a2645ca75b4b1be216d82bc54c889fe6 Mon Sep 17 00:00:00 2001 From: daimini Date: Tue, 18 May 2021 14:52:30 -0700 Subject: [PATCH] Polish pass - rename arguments to be more generic, add comments, restore patches to links during instantiation that were mistakenly removed in previous changes. --- .../Prefab/PrefabPublicHandler.cpp | 34 +++++++++++++++---- .../Prefab/PrefabPublicHandler.h | 14 ++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp index 53bf515860..7ddd9f16e1 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp @@ -180,9 +180,10 @@ namespace AzToolsFramework return AZ::Success(); } - PrefabDom PrefabPublicHandler::ApplyContainerTransformAndGeneratePatch(AZ::EntityId containerEntityId, AZ::EntityId commonRootEntityId, const EntityList& topLevelEntities) + PrefabDom PrefabPublicHandler::ApplyContainerTransformAndGeneratePatch(AZ::EntityId containerEntityId, AZ::EntityId parentEntityId, const EntityList& childEntities) { AZ::Entity* containerEntity = GetEntityById(containerEntityId); + AZ_Assert(containerEntity, "Invalid container entity passed to ApplyContainerTransformAndGeneratePatch."); // Generate the transform for the container entity out of the top level entities, and set it // This step needs to be done before anything is parented to the container, else children position will be wrong @@ -193,10 +194,10 @@ namespace AzToolsFramework AZ::Quaternion containerEntityRotation(AZ::Quaternion::CreateZero()); // Set container entity to be child of common root - AZ::TransformBus::Event(containerEntityId, &AZ::TransformBus::Events::SetParent, commonRootEntityId); + AZ::TransformBus::Event(containerEntityId, &AZ::TransformBus::Events::SetParent, parentEntityId); // Set the transform (translation, rotation) of the container entity - GenerateContainerEntityTransform(topLevelEntities, containerEntityTranslation, containerEntityRotation); + GenerateContainerEntityTransform(childEntities, containerEntityTranslation, containerEntityRotation); AZ::TransformBus::Event(containerEntityId, &AZ::TransformBus::Events::SetLocalTranslation, containerEntityTranslation); AZ::TransformBus::Event(containerEntityId, &AZ::TransformBus::Events::SetLocalRotationQuaternion, containerEntityRotation); @@ -263,10 +264,10 @@ namespace AzToolsFramework // Initialize Undo Batch object ScopedUndoBatch undoBatch("Instantiate Prefab"); + // Instantiate the Prefab PrefabDom instanceToParentUnderDomBeforeCreate; m_instanceToTemplateInterface->GenerateDomForInstance(instanceToParentUnderDomBeforeCreate, instanceToParentUnder->get()); - // Instantiate the Prefab auto instanceToCreate = prefabEditorEntityOwnershipInterface->InstantiatePrefab(relativePath, instanceToParentUnder); if (!instanceToCreate) @@ -278,11 +279,32 @@ namespace AzToolsFramework PrefabUndoHelpers::UpdatePrefabInstance( instanceToParentUnder->get(), "Update prefab instance", instanceToParentUnderDomBeforeCreate, undoBatch.GetUndoBatch()); - CreateLink(instanceToCreate->get(), instanceToParentUnder->get().GetTemplateId(), undoBatch.GetUndoBatch(), {}); + // Create Link with correct container patches AZ::EntityId containerEntityId = instanceToCreate->get().GetContainerEntityId(); + AZ::Entity* containerEntity = GetEntityById(containerEntityId); + AZ_Assert(containerEntity, "Invalid container entity detected in InstantiatePrefab."); - // Apply position + Prefab::PrefabDom containerEntityDomBefore; + m_instanceToTemplateInterface->GenerateDomForEntity(containerEntityDomBefore, *containerEntity); + + // Set container entity's parent + AZ::TransformBus::Event(containerEntityId, &AZ::TransformBus::Events::SetParent, parent); + + // Set the position of the container entity AZ::TransformBus::Event(containerEntityId, &AZ::TransformBus::Events::SetWorldTranslation, position); + + PrefabDom containerEntityDomAfter; + m_instanceToTemplateInterface->GenerateDomForEntity(containerEntityDomAfter, *containerEntity); + + // Generate patch to be stored in the link + PrefabDom patch; + m_instanceToTemplateInterface->GeneratePatch(patch, containerEntityDomBefore, containerEntityDomAfter); + m_instanceToTemplateInterface->AppendEntityAliasToPatchPaths(patch, containerEntityId); + + CreateLink(instanceToCreate->get(), instanceToParentUnder->get().GetTemplateId(), undoBatch.GetUndoBatch(), AZStd::move(patch)); + + // Update the cache - this prevents these changes from being stored in the regular undo/redo nodes + m_prefabUndoCache.Store(containerEntityId, AZStd::move(containerEntityDomAfter)); } return AZ::Success(); diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.h index d6763cab40..5ad5b4a9cf 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.h @@ -68,9 +68,19 @@ namespace AzToolsFramework InstanceOptionalReference GetOwnerInstanceByEntityId(AZ::EntityId entityId) const; bool EntitiesBelongToSameInstance(const EntityIdList& entityIds) const; - + + /** + * Applies the correct transform changes to the container entity based on the parent and child entities provided, and returns an appropriate patch. + * The container will be parented to parentId, moved to the average transform of the future direct children and its cache will be updated. + * This helper function won't support undo/redo, update the templates or create any links. All that needs to be done by the caller. + * + * \param containerEntityId The container to apply the changes to. + * \param parentEntityId The id of the entity the container should be parented to. + * \param childEntities A list of entities that will subsequently be parented to this container. + * \return The PrefabDom containing the patches that should be stored in the parent link. + */ PrefabDom ApplyContainerTransformAndGeneratePatch( - AZ::EntityId containerEntityId, AZ::EntityId commonRootEntityId, const EntityList& topLevelEntities); + AZ::EntityId containerEntityId, AZ::EntityId parentEntityId, const EntityList& childEntities); /** * Creates a link between the templates of an instance and its parent.