Merge branch 'main' of https://github.com/aws-lumberyard/o3de into Prefab/Create/PositionFix

# Conflicts:
#	Code/Framework/AzToolsFramework/AzToolsFramework/Prefab/PrefabPublicHandler.cpp
This commit is contained in:
daimini
2021-05-18 15:04:34 -07:00
428 changed files with 3167 additions and 29057 deletions
@@ -80,6 +80,12 @@ namespace AzToolsFramework
void Instance::SetTemplateId(const TemplateId& templateId)
{
// If we aren't changing the template Id, there's no need to unregister / re-register
if (templateId == m_templateId)
{
return;
}
// If this instance's templateId is valid, we should be able to unregister this instance from
// Template to Instance mapping successfully.
if (m_templateId != InvalidTemplateId &&
@@ -72,10 +72,18 @@ namespace AzToolsFramework
for (auto instance : findInstancesResult->get())
{
m_instancesUpdateQueue.emplace(instance);
m_instancesUpdateQueue.emplace_back(instance);
}
}
void InstanceUpdateExecutor::RemoveTemplateInstanceFromQueue(const Instance* instance)
{
AZStd::erase_if(m_instancesUpdateQueue, [instance](Instance* entry)
{
return entry == instance;
});
}
bool InstanceUpdateExecutor::UpdateTemplateInstancesInQueue()
{
bool isUpdateSuccessful = true;
@@ -97,9 +105,16 @@ namespace AzToolsFramework
ToolsApplicationRequestBus::BroadcastResult(selectedEntityIds, &ToolsApplicationRequests::GetSelectedEntities);
ToolsApplicationRequestBus::Broadcast(&ToolsApplicationRequests::SetSelectedEntities, EntityIdList());
for (int i = 0; i < instanceCountToUpdateInBatch; ++i)
// Process all instances in the queue, capped to the batch size.
// Even though we potentially initialized the batch size to the queue, it's possible for the queue size to shrink
// during instance processing if the instance gets deleted and it was queued multiple times. To handle this, we
// make sure to end the loop once the queue is empty, regardless of what the initial size was.
for (int i = 0; (i < instanceCountToUpdateInBatch) && !m_instancesUpdateQueue.empty(); ++i)
{
Instance* instanceToUpdate = m_instancesUpdateQueue.front();
m_instancesUpdateQueue.pop_front();
AZ_Assert(instanceToUpdate != nullptr, "Invalid instance on update queue.");
TemplateId instanceTemplateId = instanceToUpdate->GetTemplateId();
if (currentTemplateId != instanceTemplateId)
{
@@ -115,19 +130,21 @@ namespace AzToolsFramework
// Remove the instance from update queue if its corresponding template couldn't be found
isUpdateSuccessful = false;
m_instancesUpdateQueue.pop();
continue;
}
}
auto findInstancesResult = m_templateInstanceMapperInterface->FindInstancesOwnedByTemplate(instanceTemplateId)->get();
auto findInstancesResult = m_templateInstanceMapperInterface->FindInstancesOwnedByTemplate(instanceTemplateId);
AZ_Assert(
findInstancesResult.has_value(), "Prefab Instances corresponding to template with id %llu couldn't be found.",
instanceTemplateId);
if (findInstancesResult.find(instanceToUpdate) == findInstancesResult.end())
if (findInstancesResult == AZStd::nullopt ||
findInstancesResult->get().find(instanceToUpdate) == findInstancesResult->get().end())
{
// Since nested instances get reconstructed during propagation, remove any nested instance that no longer
// maps to a template.
isUpdateSuccessful = false;
m_instancesUpdateQueue.pop();
continue;
}
@@ -148,8 +165,6 @@ namespace AzToolsFramework
isUpdateSuccessful = false;
}
m_instancesUpdateQueue.pop();
}
for (auto entityIdIterator = selectedEntityIds.begin(); entityIdIterator != selectedEntityIds.end(); entityIdIterator++)
@@ -14,7 +14,7 @@
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Serialization/Json/JsonSerialization.h>
#include <AzCore/std/containers/queue.h>
#include <AzCore/std/containers/deque.h>
#include <AzToolsFramework/Prefab/Instance/InstanceUpdateExecutorInterface.h>
#include <AzToolsFramework/Prefab/PrefabIdTypes.h>
@@ -37,6 +37,7 @@ namespace AzToolsFramework
void AddTemplateInstancesToQueue(TemplateId instanceTemplateId) override;
bool UpdateTemplateInstancesInQueue() override;
virtual void RemoveTemplateInstanceFromQueue(const Instance* instance) override;
void RegisterInstanceUpdateExecutorInterface();
void UnregisterInstanceUpdateExecutorInterface();
@@ -45,7 +46,7 @@ namespace AzToolsFramework
PrefabSystemComponentInterface* m_prefabSystemComponentInterface = nullptr;
TemplateInstanceMapperInterface* m_templateInstanceMapperInterface = nullptr;
int m_instanceCountToUpdateInBatch = 0;
AZStd::queue<Instance*> m_instancesUpdateQueue;
AZStd::deque<Instance*> m_instancesUpdateQueue;
bool m_updatingTemplateInstancesInQueue { false };
};
}
@@ -31,6 +31,9 @@ namespace AzToolsFramework
// Update Instances in the waiting queue.
virtual bool UpdateTemplateInstancesInQueue() = 0;
// Remove an Instance from the waiting queue.
virtual void RemoveTemplateInstanceFromQueue(const Instance* instance) = 0;
};
}
}
@@ -14,6 +14,7 @@
#include <AzCore/Interface/Interface.h>
#include <AzToolsFramework/Prefab/Instance/Instance.h>
#include <AzToolsFramework/Prefab/Instance/InstanceUpdateExecutorInterface.h>
namespace AzToolsFramework
{
@@ -71,6 +72,12 @@ namespace AzToolsFramework
bool TemplateInstanceMapper::UnregisterInstance(Instance& instance)
{
// The InstanceUpdateExecutor queries the TemplateInstanceMapper for a list of instances related to a template.
// Consequently, if an instance gets unregistered for a template, we need to notify the InstanceUpdateExecutor as well
// so that it clears any internal associations that it might have in its queue.
AZ_Assert(AZ::Interface<InstanceUpdateExecutorInterface>::Get() != nullptr, "InstanceUpdateExecutor doesn't exist");
AZ::Interface<InstanceUpdateExecutorInterface>::Get()->RemoveTemplateInstanceFromQueue(&instance);
auto found = m_templateIdToInstancesMap.find(instance.GetTemplateId());
return found != m_templateIdToInstancesMap.end() &&
found->second.erase(&instance) != 0;
@@ -182,16 +182,16 @@ namespace AzToolsFramework
else
{
AZ::JsonSerializationResult::ResultCode applyPatchResult = AZ::JsonSerialization::ApplyPatch(
linkedInstanceDom,
sourceTemplateDomCopy,
targetTemplatePrefabDom.GetAllocator(),
sourceTemplatePrefabDom,
patchesReference->get(),
AZ::JsonMergeApproach::JsonPatch);
linkedInstanceDom.CopyFrom(sourceTemplateDomCopy, targetTemplatePrefabDom.GetAllocator());
if (applyPatchResult.GetProcessing() != AZ::JsonSerializationResult::Processing::Completed)
{
AZ_Error("Prefab", false,
"Link::UpdateTarget - "
"ApplyPatches failed for Prefab DOM from source Template '%u' and target Template '%u'.",
AZ_Error(
"Prefab", false,
"Link::UpdateTarget - ApplyPatches failed for Prefab DOM from source Template '%u' and target Template '%u'.",
m_sourceTemplateId, m_targetTemplateId);
return false;
}
@@ -125,11 +125,14 @@ namespace AzToolsFramework
// Apply the correct transform to the container for the new instance, and store the patch for use when creating the link.
PrefabDom patch = ApplyContainerTransformAndGeneratePatch(containerEntityId, commonRootEntityId, topLevelEntities);
// Parent the entities to the container entity. Parenting the container entities of the instances passed to createPrefab
// will be done during the creation of links below.
for (AZ::Entity* topLevelEntity : entities)
// Parent the non-container top level entities to the container entity.
// Parenting the top level container entities will be done during the creation of links.
for (AZ::Entity* topLevelEntity : topLevelEntities)
{
AZ::TransformBus::Event(topLevelEntity->GetId(), &AZ::TransformBus::Events::SetParent, containerEntityId);
if (!IsInstanceContainerEntity(topLevelEntity->GetId()))
{
AZ::TransformBus::Event(topLevelEntity->GetId(), &AZ::TransformBus::Events::SetParent, containerEntityId);
}
}
// Update the template of the instance since the entities are modified since the template creation.
@@ -142,6 +145,20 @@ namespace AzToolsFramework
instanceToCreate->get().GetNestedInstances([&](AZStd::unique_ptr<Instance>& nestedInstance) {
AZ_Assert(nestedInstance, "Invalid nested instance found in the new prefab created.");
AZ::EntityId parentId;
AZ::TransformBus::EventResult(
parentId, nestedInstanceContainerEntity->get().GetId(), &AZ::TransformBus::Events::GetParentId);
auto entityIterator = AZStd::find_if(
entities.begin(), entities.end(), [parentId](AZ::Entity* entity) { return entity->GetId() == parentId; });
// If the previous parent entity of the nested instance is not part of the entities of the newly created prefab,
// then set the parent of the nested prefab as the container entity of the newly created prefab.
if (entityIterator == entities.end())
{
parentId = containerEntityId;
}
// These link creations shouldn't be undone because that would put the template in a non-usable state if a user
// chooses to instantiate the template after undoing the creation.
CreateLink(*nestedInstance, instanceToCreate->get().GetTemplateId(), undoBatch.GetUndoBatch(), {}, false);
@@ -162,6 +162,12 @@ namespace AzToolsFramework
classElement.RemoveElementByName(AZ_CRC("InterpolateScale", 0x9d00b831));
}
if (classElement.GetVersion() < 10)
{
// The "Sync Enabled" flag is no longer needed.
classElement.RemoveElementByName(AZ_CRC_CE("Sync Enabled"));
}
return true;
}
} // namespace Internal
@@ -1305,7 +1311,7 @@ namespace AzToolsFramework
Field("IsStatic", &TransformComponent::m_isStatic)->
Field("InterpolatePosition", &TransformComponent::m_interpolatePosition)->
Field("InterpolateRotation", &TransformComponent::m_interpolateRotation)->
Version(9, &Internal::TransformComponentDataConverter);
Version(10, &Internal::TransformComponentDataConverter);
if (AZ::EditContext* ptrEdit = serializeContext->GetEditContext())
{