From cee4ca8067f6c69ceb993e4eeec1e5405e203285 Mon Sep 17 00:00:00 2001 From: greerdv Date: Fri, 30 Apr 2021 12:37:12 +0100 Subject: [PATCH] WIP adding button to transform component to add non-uniform scale component --- .../ToolsComponents/TransformComponent.cpp | 73 +++++++++++++++++++ .../ToolsComponents/TransformComponent.h | 13 ++++ 2 files changed, 86 insertions(+) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.cpp index dcced5b705..7d8b802622 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.cpp @@ -25,11 +25,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include @@ -37,10 +39,29 @@ #include +#pragma optimize("", off) + namespace AzToolsFramework { namespace Components { + void AddNonUniformScaleButton::Reflect(AZ::ReflectContext* context) + { + if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) + { + serializeContext->Class()-> + Version(1); + + if (AZ::EditContext* ptrEdit = serializeContext->GetEditContext()) + { + ptrEdit->Class("AddNonUniformScaleButton", "")-> + UIElement(AZ::Edit::UIHandlers::Button, "", "Add non-uniform scale component")-> + Attribute(AZ::Edit::Attributes::ButtonText, "Add non-uniform scale") + ; + } + } + } + namespace Internal { const AZ::u32 ParentEntityCRC = AZ_CRC("Parent Entity", 0x5b1b276c); @@ -1196,8 +1217,55 @@ namespace AzToolsFramework destinationComponent->SetWorldTM(const_cast(sourceComponent)->GetWorldTM()); } + AZ::Crc32 TransformComponent::OnAddNonUniformScaleButtonPressed() + { + // if there is already a non-uniform scale component, do nothing + if (GetEntity()->FindComponent()) + { + return AZ::Edit::PropertyRefreshLevels::None; + } + + const AZStd::vector entityList = { GetEntityId() }; + const AZ::ComponentTypeList componentsToAdd = { EditorNonUniformScaleComponent::TYPEINFO_Uuid() }; + + AzToolsFramework::EntityCompositionRequests::AddComponentsOutcome outcome; + AzToolsFramework::EntityCompositionRequestBus::BroadcastResult(outcome, + &AzToolsFramework::EntityCompositionRequests::AddComponentsToEntities, entityList, componentsToAdd); + + auto nonUniformScaleComponent = GetEntity()->FindComponent(); + + if (!outcome.IsSuccess() || nonUniformScaleComponent == nullptr) + { + AZ_Warning("Transform component", false, "Failed to add non-uniform scale component."); + return AZ::Edit::PropertyRefreshLevels::None; + } + + AzToolsFramework::ToolsApplicationEvents::Bus::Broadcast( + &AzToolsFramework::ToolsApplicationEvents::Bus::Events::InvalidatePropertyDisplay, AzToolsFramework::Refresh_EntireTree); + + ComponentOrderArray componentOrderArray; + EditorInspectorComponentRequestBus::EventResult(componentOrderArray, GetEntityId(), + &EditorInspectorComponentRequests::GetComponentOrderArray); + + // find the id for the non-uniform scale component and move it immediately after the transform component in the sort order + auto nonUniformScaleComponentIter = AZStd::find(componentOrderArray.begin(), componentOrderArray.end(), nonUniformScaleComponent->GetId()); + auto transformComponentIter = AZStd::find(componentOrderArray.begin(), componentOrderArray.end(), GetId()); + if (nonUniformScaleComponentIter != componentOrderArray.end() && transformComponentIter != componentOrderArray.end()) + { + componentOrderArray.erase(nonUniformScaleComponentIter); + transformComponentIter = AZStd::find(componentOrderArray.begin(), componentOrderArray.end(), GetId()); + componentOrderArray.insert(++transformComponentIter, nonUniformScaleComponent->GetId()); + EditorInspectorComponentRequestBus::Event(GetEntityId(), + &EditorInspectorComponentRequests::SetComponentOrderArray, componentOrderArray); + } + + return AZ::Edit::PropertyRefreshLevels::EntireTree; + } + void TransformComponent::Reflect(AZ::ReflectContext* context) { + AddNonUniformScaleButton::Reflect(context); + // reflect data for script, serialization, editing.. if (AZ::SerializeContext* serializeContext = azrtti_cast(context)) { @@ -1211,6 +1279,7 @@ namespace AzToolsFramework serializeContext->Class()-> Field("Parent Entity", &TransformComponent::m_parentEntityId)-> Field("Transform Data", &TransformComponent::m_editorTransform)-> + Field("AddNonUniformScaleButton", &TransformComponent::m_addNonUniformScaleButton)-> Field("Cached World Transform", &TransformComponent::m_cachedWorldTransform)-> Field("Cached World Transform Parent", &TransformComponent::m_cachedWorldTransformParent)-> Field("Parent Activation Transform Mode", &TransformComponent::m_parentActivationTransformMode)-> @@ -1234,6 +1303,10 @@ namespace AzToolsFramework DataElement(AZ::Edit::UIHandlers::Default, &TransformComponent::m_editorTransform, "Values", "")-> Attribute(AZ::Edit::Attributes::ChangeNotify, &TransformComponent::TransformChanged)-> Attribute(AZ::Edit::Attributes::AutoExpand, true)-> + DataElement(AZ::Edit::UIHandlers::Default, &TransformComponent::m_addNonUniformScaleButton, "", "")-> + Attribute(AZ::Edit::Attributes::AutoExpand, true)-> + Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)-> + Attribute(AZ::Edit::Attributes::ChangeNotify, &TransformComponent::OnAddNonUniformScaleButtonPressed)-> DataElement(AZ::Edit::UIHandlers::ComboBox, &TransformComponent::m_parentActivationTransformMode, "Parent activation", "Configures relative transform behavior when parent activates.")-> EnumAttribute(AZ::TransformConfig::ParentActivationTransformMode::MaintainOriginalRelativeTransform, "Original relative transform")-> diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.h b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.h index 8327c5f128..dd8c7b8693 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/TransformComponent.h @@ -23,6 +23,7 @@ #include #include #include +#include #include "EditorComponentBase.h" #include "TransformComponentBus.h" @@ -31,6 +32,15 @@ namespace AzToolsFramework { namespace Components { + class AddNonUniformScaleButton + { + public: + AZ_TYPE_INFO(AddNonUniformScaleButton, "{92ECB8B6-DD25-4FC0-A5EE-4CEBAF51A780}") + static void Reflect(AZ::ReflectContext* context); + private: + void OnAddNonUniformScaleButtonPressed() {}; + }; + /// Manages transform data as separate vector fields for editing purposes. /// The TransformComponent is referenced by other components in the same entity, it is not an asset. class TransformComponent @@ -228,6 +238,8 @@ namespace AzToolsFramework void CheckApplyCachedWorldTransform(const AZ::Transform& parentWorld); + AZ::Crc32 OnAddNonUniformScaleButtonPressed(); + // Drives transform behavior when parent activates. See AZ::TransformConfig::ParentActivationTransformMode for details. AZ::TransformConfig::ParentActivationTransformMode m_parentActivationTransformMode; @@ -259,6 +271,7 @@ namespace AzToolsFramework bool m_localTransformDirty = true; bool m_worldTransformDirty = true; bool m_isStatic = false; + AddNonUniformScaleButton m_addNonUniformScaleButton; // Deprecated AZ::InterpolationMode m_interpolatePosition;