Split editor entity activation from PrefabSystemComponent (#6787)

* Split editor entity activation from PrefabSystemComponent

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Fixed a small typo

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>

* Pass entity activation callback during prefab instantiation for failing tests

Signed-off-by: srikappa-amzn <82230713+srikappa-amzn@users.noreply.github.com>
This commit is contained in:
srikappa-amzn
2022-01-11 16:53:46 -08:00
committed by GitHub
parent 73419387c5
commit 13bc91aa77
6 changed files with 81 additions and 18 deletions
@@ -349,8 +349,12 @@ namespace AzToolsFramework
instanceToParentUnder = *m_rootInstance;
}
AZStd::unique_ptr<Prefab::Instance> instantiatedPrefabInstance =
m_prefabSystemComponent->InstantiatePrefab(filePath, instanceToParentUnder);
AZStd::unique_ptr<Prefab::Instance> instantiatedPrefabInstance = m_prefabSystemComponent->InstantiatePrefab(
filePath, instanceToParentUnder,
[this](const EntityList& entities)
{
HandleEntitiesAdded(entities);
});
if (instantiatedPrefabInstance)
{
@@ -277,7 +277,7 @@ namespace AzToolsFramework
}
AZStd::unique_ptr<Instance> PrefabSystemComponent::InstantiatePrefab(
AZ::IO::PathView filePath, InstanceOptionalReference parent)
AZ::IO::PathView filePath, InstanceOptionalReference parent, const InstantiatedEntitiesCallback& instantiatedEntitiesCallback)
{
// Retrieve the template id for the source prefab filepath
Prefab::TemplateId templateId = GetTemplateIdFromFilePath(filePath);
@@ -297,11 +297,11 @@ namespace AzToolsFramework
return nullptr;
}
return InstantiatePrefab(templateId, parent);
return InstantiatePrefab(templateId, parent, instantiatedEntitiesCallback);
}
AZStd::unique_ptr<Instance> PrefabSystemComponent::InstantiatePrefab(
TemplateId templateId, InstanceOptionalReference parent)
TemplateId templateId, InstanceOptionalReference parent, const InstantiatedEntitiesCallback& instantiatedEntitiesCallback)
{
TemplateReference instantiatingTemplate = FindTemplate(templateId);
@@ -324,8 +324,10 @@ namespace AzToolsFramework
return nullptr;
}
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, newEntities);
if (instantiatedEntitiesCallback)
{
instantiatedEntitiesCallback(newEntities);
}
return newInstance;
}
@@ -124,19 +124,25 @@ namespace AzToolsFramework
* Generates a new Prefab Instance based on the Template whose source is stored in filepath.
* @param filePath The path to the prefab source file containing the template being instantiated.
* @param parent Reference of the target instance the instantiated instance will be placed under.
* @param instantiatedEntitiesCallback An optional callback that can be used to modify the instantiated entities.
* @return A unique_ptr to the newly instantiated instance. Null if operation failed.
*/
AZStd::unique_ptr<Instance> InstantiatePrefab(
AZ::IO::PathView filePath, InstanceOptionalReference parent = AZStd::nullopt) override;
AZ::IO::PathView filePath,
InstanceOptionalReference parent = AZStd::nullopt,
const InstantiatedEntitiesCallback& instantiatedEntitiesCallback = {}) override;
/**
* Generates a new Prefab Instance based on the Template referenced by templateId.
* @param templateId The id of the template being instantiated.
* @param parent Reference of the target instance the instantiated instance will be placed under.
* @param instantiatedEntitiesCallback An optional callback that can be used to modify the instantiated entities.
* @return A unique_ptr to the newly instantiated instance. Null if operation failed.
*/
AZStd::unique_ptr<Instance> InstantiatePrefab(
TemplateId templateId, InstanceOptionalReference parent = AZStd::nullopt) override;
TemplateId templateId,
InstanceOptionalReference parent = AZStd::nullopt,
const InstantiatedEntitiesCallback& instantiatedEntitiesCallback = {}) override;
/**
* Add a new Link into Prefab System Component and create a unique id for it.
@@ -27,6 +27,9 @@ namespace AzToolsFramework
class PrefabSystemComponentInterface
{
public:
using InstantiatedEntitiesCallback = AZStd::function<void(AZStd::vector<AZ::Entity*>&)>;
AZ_RTTI(PrefabSystemComponentInterface, "{8E95A029-67F9-4F74-895F-DDBFE29516A0}");
virtual TemplateReference FindTemplate(TemplateId id) = 0;
@@ -70,9 +73,13 @@ namespace AzToolsFramework
virtual void PropagateTemplateChanges(TemplateId templateId, InstanceOptionalConstReference instanceToExclude = AZStd::nullopt) = 0;
virtual AZStd::unique_ptr<Instance> InstantiatePrefab(
AZ::IO::PathView filePath, InstanceOptionalReference parent = AZStd::nullopt) = 0;
AZ::IO::PathView filePath,
InstanceOptionalReference parent = AZStd::nullopt,
const InstantiatedEntitiesCallback& instantiatedEntitiesCallback = {}) = 0;
virtual AZStd::unique_ptr<Instance> InstantiatePrefab(
TemplateId templateId, InstanceOptionalReference parent = AZStd::nullopt) = 0;
TemplateId templateId,
InstanceOptionalReference parent = AZStd::nullopt,
const InstantiatedEntitiesCallback& instantiatedEntitiesCallback = {}) = 0;
virtual AZStd::unique_ptr<Instance> CreatePrefab(const AZStd::vector<AZ::Entity*>& entities,
AZStd::vector<AZStd::unique_ptr<Instance>>&& instancesToConsume, AZ::IO::PathView filePath,
AZStd::unique_ptr<AZ::Entity> containerEntity = nullptr, InstanceOptionalReference parent = AZStd::nullopt,
@@ -170,7 +170,14 @@ namespace UnitTest
m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
//instantiate a new nested instance
nestedInstance = m_prefabSystemComponent->InstantiatePrefab(nestedTemplateId);
nestedInstance = m_prefabSystemComponent->InstantiatePrefab(
nestedTemplateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
nestedContainerEntityId = nestedInstance->GetContainerEntityId();
AZ::ComponentApplicationBus::BroadcastResult(nestedContainerEntity, &AZ::ComponentApplicationBus::Events::FindEntity, nestedContainerEntityId);
ASSERT_TRUE(nestedContainerEntity);
@@ -198,7 +205,13 @@ namespace UnitTest
LinkId linkId = undoInstanceLinkNode.GetLinkId();
rootInstance = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
rootInstance = m_prefabSystemComponent->InstantiatePrefab(
rootTemplateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
aliases = rootInstance->GetNestedInstanceAliases(nestedTemplateId);
//verify the link was created
@@ -228,7 +241,13 @@ namespace UnitTest
m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
//verify the update worked
rootInstance = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
rootInstance = m_prefabSystemComponent->InstantiatePrefab(
rootTemplateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
aliases = rootInstance->GetNestedInstanceAliases(nestedTemplateId);
nestedInstanceRef = rootInstance->FindNestedInstance(aliases[0]);
nestedContainerEntityId = nestedInstanceRef->get().GetContainerEntityId();
@@ -244,7 +263,13 @@ namespace UnitTest
m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
//verify the undo update worked
rootInstance = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
rootInstance = m_prefabSystemComponent->InstantiatePrefab(
rootTemplateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
aliases = rootInstance->GetNestedInstanceAliases(nestedTemplateId);
nestedInstanceRef = rootInstance->FindNestedInstance(aliases[0]);
nestedContainerEntityId = nestedInstanceRef->get().GetContainerEntityId();
@@ -259,7 +284,13 @@ namespace UnitTest
undoLinkUpdateNode.Redo();
m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
rootInstance = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
rootInstance = m_prefabSystemComponent->InstantiatePrefab(
rootTemplateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
aliases = rootInstance->GetNestedInstanceAliases(nestedTemplateId);
nestedInstanceRef = rootInstance->FindNestedInstance(aliases[0]);
nestedContainerEntityId = nestedInstanceRef->get().GetContainerEntityId();
@@ -287,7 +318,13 @@ namespace UnitTest
m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
//verify the update worked
rootInstance = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
rootInstance = m_prefabSystemComponent->InstantiatePrefab(
rootTemplateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
aliases = rootInstance->GetNestedInstanceAliases(nestedTemplateId);
nestedInstanceRef = rootInstance->FindNestedInstance(aliases[0]);
nestedContainerEntityId = nestedInstanceRef->get().GetContainerEntityId();
@@ -79,7 +79,14 @@ namespace UnitTest
// verify template updated correctly
//instantiate second instance for checking if propogation works
AZStd::unique_ptr<Instance> secondInstance = m_prefabSystemComponent->InstantiatePrefab(templateId);
AZStd::unique_ptr<Instance> secondInstance = m_prefabSystemComponent->InstantiatePrefab(
templateId, AZStd::nullopt,
[](const AzToolsFramework::EntityList& entities)
{
AzToolsFramework::EditorEntityContextRequestBus::Broadcast(
&AzToolsFramework::EditorEntityContextRequests::HandleEntitiesAdded, entities);
});
ASSERT_TRUE(secondInstance);
ValidateInstanceEntitiesActive(*secondInstance);