From d365d00abd8555490ae6278876b016c439f3c7cf Mon Sep 17 00:00:00 2001 From: greerdv Date: Fri, 14 May 2021 11:52:29 +0100 Subject: [PATCH] hook up non-uniform scale manipulator callbacks --- .../Manipulators/LinearManipulator.h | 1 + .../EditorNonUniformScaleComponent.cpp | 6 +- .../EditorNonUniformScaleComponentMode.cpp | 68 +++++++++++++++---- .../EditorNonUniformScaleComponentMode.h | 13 +++- 4 files changed, 73 insertions(+), 15 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Manipulators/LinearManipulator.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Manipulators/LinearManipulator.h index 9de9f0dee3..576c543dce 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Manipulators/LinearManipulator.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Manipulators/LinearManipulator.h @@ -42,6 +42,7 @@ namespace AzToolsFramework ~LinearManipulator() = default; /// A Manipulator must only be created and managed through a shared_ptr. + /// @note worldFromLocal should not contain scale. static AZStd::shared_ptr MakeShared(const AZ::Transform& worldFromLocal); /// Unchanging data set once for the linear manipulator. diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponent.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponent.cpp index 0ff91f24f4..276fe0b3ab 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponent.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponent.cpp @@ -82,14 +82,16 @@ namespace AzToolsFramework AZ::NonUniformScaleRequestBus::Handler::BusConnect(GetEntityId()); // ComponentMode + AZ::EntityComponentIdPair entityComponentIdPair(GetEntityId(), GetId()); + NonUniformScaleManipulatorRequestBus::Handler::BusConnect(entityComponentIdPair); m_componentModeDelegate.ConnectWithSingleComponentMode< - EditorNonUniformScaleComponent, NonUniformScaleComponentMode>( - AZ::EntityComponentIdPair(GetEntityId(), GetId()), this); + EditorNonUniformScaleComponent, NonUniformScaleComponentMode>(entityComponentIdPair, this); } void EditorNonUniformScaleComponent::Deactivate() { m_componentModeDelegate.Disconnect(); + NonUniformScaleManipulatorRequestBus::Handler::BusDisconnect(); AZ::NonUniformScaleRequestBus::Handler::BusDisconnect(); } diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.cpp index 4be0e834dd..0521a1f2d6 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.cpp @@ -1,29 +1,73 @@ /* -* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or -* its licensors. -* -* For complete copyright and license terms please see the LICENSE at the root of this -* distribution (the "License"). All use of this software is governed by the License, -* or, if provided, by the license below or the license accompanying this file. Do not -* remove or modify any license notices. This file is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* -*/ + * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or + * its licensors. + * + * For complete copyright and license terms please see the LICENSE at the root of this + * distribution (the "License"). All use of this software is governed by the License, + * or, if provided, by the license below or the license accompanying this file. Do not + * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * + */ +#include +#include +#include #include namespace AzToolsFramework { namespace Components { - NonUniformScaleComponentMode::NonUniformScaleComponentMode(const AZ::EntityComponentIdPair& entityComponentIdPair, - AZ::Uuid componentType) + NonUniformScaleComponentMode::NonUniformScaleComponentMode( + const AZ::EntityComponentIdPair& entityComponentIdPair, AZ::Uuid componentType) : EditorBaseComponentMode(entityComponentIdPair, componentType) { + m_entityComponentIdPair = entityComponentIdPair; + + AZ::Transform worldFromLocal = AZ::Transform::CreateIdentity(); + AZ::TransformBus::EventResult(worldFromLocal, m_entityComponentIdPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldTM); + + worldFromLocal.ExtractScale(); + + m_manipulators = AZStd::make_unique(worldFromLocal); + + m_manipulators->Register(g_mainManipulatorManagerId); + + m_manipulators->SetAxes(AZ::Vector3::CreateAxisX(), AZ::Vector3::CreateAxisY(), AZ::Vector3::CreateAxisZ()); + + m_manipulators->ConfigureView( + 2.0f, AzFramework::ViewportColors::XAxisColor, AzFramework::ViewportColors::YAxisColor, + AzFramework::ViewportColors::ZAxisColor); + + m_manipulators->InstallAxisLeftMouseDownCallback([this](const LinearManipulator::Action& action) { + AZ::Vector3 nonUniformScale = AZ::Vector3::CreateOne(); + NonUniformScaleManipulatorRequestBus::EventResult( + nonUniformScale, m_entityComponentIdPair, &NonUniformScaleManipulatorRequests::GetScale); + + m_initialScale = nonUniformScale + action.m_start.m_scaleSnapOffset; + + NonUniformScaleManipulatorRequestBus::Event( + m_entityComponentIdPair, &NonUniformScaleManipulatorRequests::SetScale, m_initialScale); + }); + + m_manipulators->InstallAxisMouseMoveCallback([this](const LinearManipulator::Action& action) { + const AZ::Vector3 scale = + (AZ::Vector3::CreateOne() + ((action.LocalScaleOffset() * action.m_start.m_sign) / m_initialScale)) + .GetMax(AZ::Vector3(AZ::MinTransformScale)); + + NonUniformScaleManipulatorRequestBus::Event( + m_entityComponentIdPair, &NonUniformScaleManipulatorRequests::SetScale, scale * m_initialScale); + }); } NonUniformScaleComponentMode::~NonUniformScaleComponentMode() { + if (m_manipulators) + { + m_manipulators->Unregister(); + } + m_manipulators.reset(); } void NonUniformScaleComponentMode::Refresh() diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.h b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.h index d4cd0125db..8b40438d38 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ToolsComponents/EditorNonUniformScaleComponentMode.h @@ -13,6 +13,7 @@ #pragma once #include +#include namespace AzToolsFramework { @@ -24,12 +25,17 @@ namespace AzToolsFramework : public AZ::EntityComponentBus { public: + //! Gets the non-uniform scale. + virtual AZ::Vector3 GetScale() const = 0; + + //! Sets the non-uniform scale. + virtual void SetScale(const AZ::Vector3& scale) = 0; protected: ~NonUniformScaleManipulatorRequests() = default; }; - //! Type to inherit to implement NonUniformScaleManipulatorRequests + //! Type to inherit to implement NonUniformScaleManipulatorRequests. using NonUniformScaleManipulatorRequestBus = AZ::EBus; class NonUniformScaleComponentMode @@ -47,6 +53,11 @@ namespace AzToolsFramework // EditorBaseComponentMode void Refresh() override; + + private: + AZ::EntityComponentIdPair m_entityComponentIdPair; + AZStd::unique_ptr m_manipulators; + AZ::Vector3 m_initialScale; }; } // namespace Components } // namespace AzToolsFramework