From 7f603c59ad99eece18a62dddd6e83dc1ee1130e9 Mon Sep 17 00:00:00 2001 From: hultonha <82228511+hultonha@users.noreply.github.com> Date: Tue, 17 Aug 2021 17:09:17 +0100 Subject: [PATCH] Fix for events that should have been consumed by manipulators (#3108) * fix for events that should have been consumed by manipulators making their way to the main viewport handler Signed-off-by: hultonha * add missing include for SANDBOX_API macro Signed-off-by: hultonha * add dependency on Qt::Test for AzToolsFrameworkTestCommon Signed-off-by: hultonha * fix order of buttons passed to QMouseEvent Signed-off-by: hultonha * potential fix for vtable error on linux Signed-off-by: hultonha * potential fix for vtable error on linux again Signed-off-by: hultonha --- Code/Editor/CMakeLists.txt | 4 + .../test_ViewportManipulatorController.cpp | 152 ++++++++++++++++++ Code/Editor/ViewportManipulatorController.cpp | 23 ++- Code/Editor/ViewportManipulatorController.h | 20 ++- Code/Editor/editor_lib_test_files.cmake | 1 + .../Input/QtEventToAzInputManager.cpp | 49 +++--- .../Input/QtEventToAzInputManager.h | 4 - .../UnitTest/AzToolsFrameworkTestHelpers.cpp | 29 ++++ .../UnitTest/AzToolsFrameworkTestHelpers.h | 15 ++ .../Viewport/ViewportMessages.h | 2 +- .../Framework/AzToolsFramework/CMakeLists.txt | 4 +- .../AzToolsFramework/Tests/SpinBoxTests.cpp | 25 --- 12 files changed, 253 insertions(+), 75 deletions(-) create mode 100644 Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp diff --git a/Code/Editor/CMakeLists.txt b/Code/Editor/CMakeLists.txt index fca16a2093..9baa83179b 100644 --- a/Code/Editor/CMakeLists.txt +++ b/Code/Editor/CMakeLists.txt @@ -238,9 +238,13 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) 3rdParty::Qt::Core 3rdParty::Qt::Gui 3rdParty::Qt::Widgets + 3rdParty::Qt::Test Legacy::CryCommon AZ::AzToolsFramework + AZ::AzToolsFramework.Tests + AZ::AzToolsFrameworkTestCommon Legacy::EditorLib + Gem::AtomToolsFramework.Static RUNTIME_DEPENDENCIES Gem::LmbrCentral ) diff --git a/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp b/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp new file mode 100644 index 0000000000..a2a7617083 --- /dev/null +++ b/Code/Editor/Lib/Tests/test_ViewportManipulatorController.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include +#include +#include +#include +#include + +namespace UnitTest +{ + using AzToolsFramework::ViewportInteraction::MouseInteractionEvent; + + class EditorInteractionViewportSelectionFake : public AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Handler + { + public: + void Connect(); + void Disconnect(); + + // EditorInteractionSystemViewportSelectionRequestBus overrides ... + void SetHandler(const AzToolsFramework::ViewportSelectionRequestsBuilderFn& interactionRequestsBuilder); + void SetDefaultHandler(); + bool InternalHandleMouseViewportInteraction(const MouseInteractionEvent& mouseInteraction); + bool InternalHandleMouseManipulatorInteraction(const MouseInteractionEvent& mouseInteraction); + + AZStd::function m_internalHandleMouseViewportInteraction; + AZStd::function m_internalHandleMouseManipulatorInteraction; + }; + + void EditorInteractionViewportSelectionFake::Connect() + { + AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId()); + } + + void EditorInteractionViewportSelectionFake::Disconnect() + { + AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Handler::BusDisconnect(); + } + + void EditorInteractionViewportSelectionFake::SetHandler( + [[maybe_unused]] const AzToolsFramework::ViewportSelectionRequestsBuilderFn& interactionRequestsBuilder) + { + // noop + } + + void EditorInteractionViewportSelectionFake::SetDefaultHandler() + { + // noop + } + + bool EditorInteractionViewportSelectionFake::InternalHandleMouseViewportInteraction(const MouseInteractionEvent& mouseInteraction) + { + if (m_internalHandleMouseViewportInteraction) + { + return m_internalHandleMouseViewportInteraction(mouseInteraction); + } + + return false; + } + + bool EditorInteractionViewportSelectionFake::InternalHandleMouseManipulatorInteraction(const MouseInteractionEvent& mouseInteraction) + { + if (m_internalHandleMouseManipulatorInteraction) + { + return m_internalHandleMouseManipulatorInteraction(mouseInteraction); + } + + return false; + } + + class ViewportManipulatorControllerFixture : public AllocatorsTestFixture + { + public: + static const AzFramework::ViewportId TestViewportId = AzFramework::ViewportId(0); + + void SetUp() override + { + AllocatorsTestFixture::SetUp(); + + m_rootWidget = AZStd::make_unique(); + m_rootWidget->setFixedSize(QSize(100, 100)); + + m_controllerList = AZStd::make_shared(); + m_controllerList->RegisterViewportContext(TestViewportId); + + m_inputChannelMapper = AZStd::make_unique(m_rootWidget.get(), TestViewportId); + } + + void TearDown() + { + m_inputChannelMapper.reset(); + + m_controllerList->UnregisterViewportContext(TestViewportId); + m_controllerList.reset(); + m_rootWidget.reset(); + + AllocatorsTestFixture::TearDown(); + } + + AZStd::unique_ptr m_rootWidget; + AzFramework::ViewportControllerListPtr m_controllerList; + AZStd::unique_ptr m_inputChannelMapper; + }; + + TEST_F(ViewportManipulatorControllerFixture, An_event_is_not_propagated_to_the_viewport_when_a_manipulator_handles_it_first) + { + // forward input events to our controller list + QObject::connect( + m_inputChannelMapper.get(), &AzToolsFramework::QtEventToAzInputMapper::InputChannelUpdated, m_rootWidget.get(), + [this](const AzFramework::InputChannel* inputChannel, [[maybe_unused]] QEvent* event) + { + m_controllerList->HandleInputChannelEvent( + AzFramework::ViewportControllerInputEvent{ TestViewportId, nullptr, *inputChannel }); + }); + + EditorInteractionViewportSelectionFake editorInteractionViewportFake; + editorInteractionViewportFake.m_internalHandleMouseManipulatorInteraction = [](const MouseInteractionEvent&) + { + // report the event was handled (manipulator was interacted with) + return true; + }; + + bool viewportInteractionCalled = false; + editorInteractionViewportFake.m_internalHandleMouseViewportInteraction = [&viewportInteractionCalled](const MouseInteractionEvent&) + { + // we should not call this as the manipulator will have consumed this event + viewportInteractionCalled = true; + return true; + }; + + editorInteractionViewportFake.Connect(); + + m_controllerList->Add(AZStd::make_shared()); + + // simulate a press and move + MousePressAndMove(m_rootWidget.get(), QPoint(10, 10), QPoint(10, 10), Qt::MouseButton::LeftButton); + MouseMove(m_rootWidget.get(), QPoint(20, 20), QPoint(10, 10), Qt::MouseButton::LeftButton); + MouseMove(m_rootWidget.get(), QPoint(30, 30), QPoint(0, 0), Qt::MouseButton::LeftButton); + QTest::mouseRelease(m_rootWidget.get(), Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier, QPoint(30, 30)); + + // ensure the viewport did not receive the event when it was intercepted first by the manipulator + EXPECT_FALSE(viewportInteractionCalled); + + editorInteractionViewportFake.Disconnect(); + } +} // namespace UnitTest diff --git a/Code/Editor/ViewportManipulatorController.cpp b/Code/Editor/ViewportManipulatorController.cpp index 0b519f0787..5282af009f 100644 --- a/Code/Editor/ViewportManipulatorController.cpp +++ b/Code/Editor/ViewportManipulatorController.cpp @@ -28,6 +28,8 @@ namespace SandboxEditor { } + ViewportManipulatorControllerInstance::~ViewportManipulatorControllerInstance() = default; + AzToolsFramework::ViewportInteraction::MouseButton ViewportManipulatorControllerInstance::GetMouseButton( const AzFramework::InputChannel& inputChannel) { @@ -103,14 +105,21 @@ namespace SandboxEditor // Cache the ray trace results when doing manipulator interaction checks, no need to recalculate after if (event.m_priority == ManipulatorPriority) { - AzFramework::ScreenPoint screenPosition = AzFramework::ScreenPoint(0, 0); - ViewportMouseCursorRequestBus::EventResult( - screenPosition, GetViewportId(), &ViewportMouseCursorRequestBus::Events::ViewportCursorScreenPosition); + const auto* position = event.m_inputChannel.GetCustomData(); + AZ_Assert(position, "Expected PositionData2D but found nullptr"); - m_mouseInteraction.m_mousePick.m_screenCoordinates = screenPosition; + AzFramework::WindowSize windowSize; + AzFramework::WindowRequestBus::EventResult( + windowSize, event.m_windowHandle, &AzFramework::WindowRequestBus::Events::GetClientAreaSize); + + auto screenPoint = AzFramework::ScreenPoint( + position->m_normalizedPosition.GetX() * windowSize.m_width, + position->m_normalizedPosition.GetY() * windowSize.m_height); + + m_mouseInteraction.m_mousePick.m_screenCoordinates = screenPoint; AZStd::optional ray; ViewportInteractionRequestBus::EventResult( - ray, GetViewportId(), &ViewportInteractionRequestBus::Events::ViewportScreenToWorldRay, screenPosition); + ray, GetViewportId(), &ViewportInteractionRequestBus::Events::ViewportScreenToWorldRay, screenPoint); if (ray.has_value()) { @@ -118,6 +127,7 @@ namespace SandboxEditor m_mouseInteraction.m_mousePick.m_rayDirection = ray.value().direction; } } + eventType = MouseEvent::Move; } else if (auto mouseButton = GetMouseButton(event.m_inputChannel); mouseButton != MouseButton::None) @@ -217,8 +227,7 @@ namespace SandboxEditor interactionHandled, AzToolsFramework::GetEntityContextId(), targetInteractionEvent, mouseInteractionEvent); } - // Only filter button/key press events, not release events - return interactionHandled && event.m_inputChannel.IsActive(); + return interactionHandled; } void ViewportManipulatorControllerInstance::ResetInputChannels() diff --git a/Code/Editor/ViewportManipulatorController.h b/Code/Editor/ViewportManipulatorController.h index 968b6745c1..d551eb3647 100644 --- a/Code/Editor/ViewportManipulatorController.h +++ b/Code/Editor/ViewportManipulatorController.h @@ -8,25 +8,29 @@ #pragma once -#include -#include #include +#include +#include #include +#include + namespace SandboxEditor { class ViewportManipulatorControllerInstance; - using ViewportManipulatorController = AzFramework::MultiViewportController; + using ViewportManipulatorController = AzFramework:: + MultiViewportController; class ViewportManipulatorControllerInstance final : public AzFramework::MultiViewportControllerInstanceInterface { public: - explicit ViewportManipulatorControllerInstance(AzFramework::ViewportId viewport, ViewportManipulatorController* controller); + SANDBOX_API ViewportManipulatorControllerInstance(AzFramework::ViewportId viewport, ViewportManipulatorController* controller); + SANDBOX_API ~ViewportManipulatorControllerInstance(); - bool HandleInputChannelEvent(const AzFramework::ViewportControllerInputEvent& event) override; - void ResetInputChannels() override; - void UpdateViewport(const AzFramework::ViewportControllerUpdateEvent& event) override; + SANDBOX_API bool HandleInputChannelEvent(const AzFramework::ViewportControllerInputEvent& event) override; + SANDBOX_API void ResetInputChannels() override; + SANDBOX_API void UpdateViewport(const AzFramework::ViewportControllerUpdateEvent& event) override; private: bool IsDoubleClick(AzToolsFramework::ViewportInteraction::MouseButton) const; @@ -39,4 +43,4 @@ namespace SandboxEditor AZStd::unordered_map m_pendingDoubleClicks; AZ::ScriptTimePoint m_curTime; }; -} //namespace SandboxEditor +} // namespace SandboxEditor diff --git a/Code/Editor/editor_lib_test_files.cmake b/Code/Editor/editor_lib_test_files.cmake index c67e70ddbd..49f707b1f6 100644 --- a/Code/Editor/editor_lib_test_files.cmake +++ b/Code/Editor/editor_lib_test_files.cmake @@ -20,6 +20,7 @@ set(FILES Lib/Tests/test_ViewPanePythonBindings.cpp Lib/Tests/test_ViewportTitleDlgPythonBindings.cpp Lib/Tests/test_DisplaySettingsPythonBindings.cpp + Lib/Tests/test_ViewportManipulatorController.cpp DisplaySettingsPythonFuncs.cpp DisplaySettingsPythonFuncs.h ) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.cpp index f8665d583e..b7776238ba 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.cpp @@ -162,7 +162,6 @@ namespace AzToolsFramework : QObject(sourceWidget) , m_sourceWidget(sourceWidget) , m_keyboardModifiers(AZStd::make_shared()) - , m_cursorPosition(AZStd::make_shared()) { InitializeKeyMappings(); InitializeMouseButtonMappings(); @@ -230,24 +229,17 @@ namespace AzToolsFramework return false; } - // Because there's no "end" to mouse movement and wheel events, we reset mouse movement channels that have been opened - // during the next processed non-mouse event. - if (m_mouseChannelsNeedUpdate && event->type() != QEvent::Type::MouseMove && event->type() != QEvent::Type::Wheel) - { - m_cursorPosition->m_normalizedPositionDelta = AZ::Vector2::CreateZero(); - ProcessPendingMouseEvents(); - m_mouseChannelsNeedUpdate = false; - } + const auto eventType = event->type(); // Only accept mouse & key release events that originate from an object that is not our target widget, // as we don't want to erroneously intercept user input meant for another component. - if (object != m_sourceWidget && event->type() != QEvent::Type::KeyRelease && event->type() != QEvent::Type::MouseButtonRelease) + if (object != m_sourceWidget && eventType != QEvent::Type::KeyRelease && eventType != QEvent::Type::MouseButtonRelease) { return false; } // If our focus changes, go ahead and reset all input devices. - if (event->type() == QEvent::FocusIn || event->type() == QEvent::FocusOut) + if (eventType == QEvent::FocusIn || eventType == QEvent::FocusOut) { HandleFocusChange(event); } @@ -255,27 +247,28 @@ namespace AzToolsFramework // ShortcutOverride is used in lieu of KeyPress for high priority input channels like Alt // that need to be accepted and stopped before they bubble up and cause unintended behavior. else if ( - event->type() == QEvent::Type::KeyPress || event->type() == QEvent::Type::KeyRelease || - event->type() == QEvent::Type::ShortcutOverride) + eventType == QEvent::Type::KeyPress || eventType == QEvent::Type::KeyRelease || eventType == QEvent::Type::ShortcutOverride) { QKeyEvent* keyEvent = static_cast(event); HandleKeyEvent(keyEvent); } // Map mouse events to input channels. - else if (event->type() == QEvent::Type::MouseButtonPress || event->type() == QEvent::Type::MouseButtonRelease || event->type() == QEvent::Type::MouseButtonDblClick) + else if ( + eventType == QEvent::Type::MouseButtonPress || eventType == QEvent::Type::MouseButtonRelease || + eventType == QEvent::Type::MouseButtonDblClick) { QMouseEvent* mouseEvent = static_cast(event); HandleMouseButtonEvent(mouseEvent); } // Map mouse movement to the movement input channels. // This includes SystemCursorPosition alongside Movement::X and Movement::Y. - else if (event->type() == QEvent::Type::MouseMove) + else if (eventType == QEvent::Type::MouseMove) { QMouseEvent* mouseEvent = static_cast(event); HandleMouseMoveEvent(mouseEvent); } // Map wheel events to the mouse Z movement channel. - else if (event->type() == QEvent::Type::Wheel) + else if (eventType == QEvent::Type::Wheel) { QWheelEvent* wheelEvent = static_cast(event); HandleWheelEvent(wheelEvent); @@ -303,14 +296,16 @@ namespace AzToolsFramework auto mouseWheelChannel = GetInputChannel(AzFramework::InputDeviceMouse::Movement::Z); - systemCursorChannel->ProcessRawInputEvent(m_cursorPosition->m_normalizedPositionDelta.GetLength()); + systemCursorChannel->ProcessRawInputEvent(m_mouseDevice->m_cursorPositionData2D->m_normalizedPositionDelta.GetLength()); // Generate movement events based on the pixel delta divided by the DPI scaling factor, to calculate a rough approximation // of cursor movement velocity. movementXChannel->ProcessRawInputEvent( - m_cursorPosition->m_normalizedPositionDelta.GetX() * aznumeric_cast(m_sourceWidget->width()) / m_sourceWidget->devicePixelRatioF()); + m_mouseDevice->m_cursorPositionData2D->m_normalizedPositionDelta.GetX() * aznumeric_cast(m_sourceWidget->width()) / + m_sourceWidget->devicePixelRatioF()); movementYChannel->ProcessRawInputEvent( - m_cursorPosition->m_normalizedPositionDelta.GetY() * aznumeric_cast(m_sourceWidget->height()) / m_sourceWidget->devicePixelRatioF()); - mouseWheelChannel->ProcessRawInputEvent(0.f); + m_mouseDevice->m_cursorPositionData2D->m_normalizedPositionDelta.GetY() * aznumeric_cast(m_sourceWidget->height()) / + m_sourceWidget->devicePixelRatioF()); + mouseWheelChannel->ProcessRawInputEvent(0.0f); NotifyUpdateChannelIfNotIdle(systemCursorChannel, nullptr); NotifyUpdateChannelIfNotIdle(movementXChannel, nullptr); @@ -358,14 +353,13 @@ namespace AzToolsFramework void QtEventToAzInputMapper::HandleMouseMoveEvent(QMouseEvent* mouseEvent) { - AZ::Vector2 lastCursorPosition = m_cursorPosition->m_normalizedPosition; + AZ::Vector2 lastCursorPosition = m_mouseDevice->m_cursorPositionData2D->m_normalizedPosition; const QPoint mousePos = mouseEvent->pos(); const AZ::Vector2 normalizedPosition = WidgetPositionToNormalizedPosition(mousePos); - m_cursorPosition->m_normalizedPositionDelta = normalizedPosition - m_cursorPosition->m_normalizedPosition; - m_cursorPosition->m_normalizedPosition = normalizedPosition; + m_mouseDevice->m_cursorPositionData2D->m_normalizedPositionDelta = normalizedPosition - m_mouseDevice->m_cursorPositionData2D->m_normalizedPosition; + m_mouseDevice->m_cursorPositionData2D->m_normalizedPosition = normalizedPosition; ProcessPendingMouseEvents(); - m_mouseChannelsNeedUpdate = true; if (m_capturingCursor) { @@ -376,7 +370,7 @@ namespace AzToolsFramework // Even though we just set the cursor position, there are edge cases such as remote desktop that will leave // the cursor position unchanged. For safety, we re-cache our last cursor position for delta generation. QPoint actualWidgetPosition = m_sourceWidget->mapFromGlobal(QCursor::pos()); - m_cursorPosition->m_normalizedPosition = WidgetPositionToNormalizedPosition(actualWidgetPosition); + m_mouseDevice->m_cursorPositionData2D->m_normalizedPosition = WidgetPositionToNormalizedPosition(actualWidgetPosition); } } @@ -427,21 +421,18 @@ namespace AzToolsFramework } cursorZChannel->ProcessRawInputEvent(aznumeric_cast(wheelAngle)); NotifyUpdateChannelIfNotIdle(cursorZChannel, wheelEvent); - m_mouseChannelsNeedUpdate = true; } void QtEventToAzInputMapper::HandleFocusChange(QEvent* event) { for (auto& channelData : m_channels) { - // If resetting the input device changed the channel state, submit it to the mapped channel list - // for processing. + // If resetting the input device changed the channel state, submit it to the mapped channel list for processing. if (channelData.second->IsActive()) { channelData.second->UpdateState(false); NotifyUpdateChannelIfNotIdle(channelData.second, event); } } - m_mouseChannelsNeedUpdate = false; } } // namespace AzToolsFramework diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.h index 919d8fcc2a..0187cb2e5b 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Input/QtEventToAzInputManager.h @@ -138,8 +138,6 @@ namespace AzToolsFramework // The current keyboard modifier state used by our synthetic key input channels. AZStd::shared_ptr m_keyboardModifiers; - // The current normalized cursor position used by our synthetic system cursor event. - AZStd::shared_ptr m_cursorPosition; // A lookup table for Qt key -> AZ input channel. AZStd::unordered_map m_keyMappings; // A lookup table for Qt mouse button -> AZ input channel. @@ -152,8 +150,6 @@ namespace AzToolsFramework AZStd::unordered_map m_channels; // The source widget to map events from, used to calculate the relative mouse position within the widget bounds. QWidget* m_sourceWidget; - // Flags when mouse movement channels have been opened and may need to be closed (as there are no movement ended events). - bool m_mouseChannelsNeedUpdate = false; // Flags whether or not Qt events should currently be processed. bool m_enabled = true; // Flags whether or not the cursor is being constrained to the source widget (for invisible mouse movement). diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.cpp index 1bec929223..6608e87784 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.cpp @@ -27,6 +27,35 @@ using namespace AzToolsFramework; namespace UnitTest { + void MousePressAndMove( + QWidget* widget, const QPoint& initialPositionWidget, const QPoint& mouseDelta, const Qt::MouseButton mouseButton) + { + QPoint position = widget->mapToGlobal(initialPositionWidget); + QTest::mousePress(widget, mouseButton, Qt::NoModifier, position); + + MouseMove(widget, initialPositionWidget, mouseDelta, mouseButton); + } + + // Note: There are a series of bugs in Qt that appear to be preventing mouseMove events + // firing when sent through the QTest framework. This is a work around for our version + // of Qt. In future this can hopefully be simplified. See ^1 for workaround. + // More info: Issues with mouse move in Qt + // - https://bugreports.qt.io/browse/QTBUG-5232 + // - https://bugreports.qt.io/browse/QTBUG-69414 + // - https://lists.qt-project.org/pipermail/development/2019-July/036873.html + void MouseMove(QWidget* widget, const QPoint& initialPositionWidget, const QPoint& mouseDelta, const Qt::MouseButton mouseButton) + { + QPoint nextPosition = widget->mapToGlobal(initialPositionWidget + mouseDelta); + + // ^1 To ensure a mouse move event is fired we must call the test mouse move function + // and also send a mouse move event that matches. Each on their own do not appear to + // work - please see the links above for more context. + QTest::mouseMove(widget, nextPosition); + QMouseEvent mouseMoveEvent( + QEvent::MouseMove, QPointF(nextPosition), QPointF(nextPosition), Qt::NoButton, mouseButton, Qt::NoModifier); + QApplication::sendEvent(widget, &mouseMoveEvent); + } + bool TestWidget::eventFilter(QObject* watched, QEvent* event) { AZ_UNUSED(watched); diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h b/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h index 3c413fd21e..b3a660d0f2 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h @@ -59,6 +59,21 @@ namespace UnitTest { constexpr AZStd::string_view prefabSystemSetting = "/Amazon/Preferences/EnablePrefabSystem"; + /// Performs a mouse press and move event on the provided widget. + /// @param widget The widget to perform the mouse press and move on. + /// @param initialPositionWidget The position of the mouse relative to the widget (will be remapped to a global position internally). + /// @param mouseDelta How far to move the mouse. + /// @param mouseButton The button to be used during the press and move. + void MousePressAndMove( + QWidget* widget, const QPoint& initialPositionWidget, const QPoint& mouseDelta, Qt::MouseButton mouseButton = Qt::LeftButton); + + /// Performs a mouse move event on the provided widget. + /// @param widget The widget to perform the mouse move on. + /// @param initialPositionWidget The position of the mouse relative to the widget (will be remapped to a global position internally). + /// @param mouseDelta How far to move the mouse (note: mouseDelta may be zero and the mouse will only be moved to initialPosition). + /// @param mouseButton The button to be held during the move. + void MouseMove(QWidget* widget, const QPoint& initialPosition, const QPoint& mouseDelta, Qt::MouseButton mouseButton = Qt::NoButton); + /// Test widget to store QActions generated by EditorTransformComponentSelection. class TestWidget : public QWidget { diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h b/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h index ef1dfb0414..543d5fb3a5 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/Viewport/ViewportMessages.h @@ -313,7 +313,7 @@ namespace AzToolsFramework //! Utility function to return EntityContextId. inline AzFramework::EntityContextId GetEntityContextId() { - AzFramework::EntityContextId entityContextId; + auto entityContextId = AzFramework::EntityContextId::CreateNull(); EditorEntityContextRequestBus::BroadcastResult(entityContextId, &EditorEntityContextRequests::GetEditorEntityContextId); return entityContextId; diff --git a/Code/Framework/AzToolsFramework/CMakeLists.txt b/Code/Framework/AzToolsFramework/CMakeLists.txt index 62f4f43d93..4ee329bd93 100644 --- a/Code/Framework/AzToolsFramework/CMakeLists.txt +++ b/Code/Framework/AzToolsFramework/CMakeLists.txt @@ -60,6 +60,7 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) PUBLIC AZ::AzTestShared PRIVATE + 3rdParty::Qt::Test 3rdParty::googletest::GMock 3rdParty::GoogleBenchmark AZ::AzToolsFramework @@ -76,8 +77,9 @@ if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) PRIVATE Tests BUILD_DEPENDENCIES - PRIVATE + PUBLIC AZ::AzTestShared + PRIVATE 3rdParty::Qt::Test AZ::AzFrameworkTestShared AZ::AzToolsFramework diff --git a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp index a88cb68638..2d524cbcf4 100644 --- a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp +++ b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp @@ -107,31 +107,6 @@ namespace UnitTest EXPECT_THAT(m_doubleSpinBoxWithLineEdit, Ne(nullptr)); } - // Note: There are a series of bugs in Qt that appear to be preventing mouseMove events - // firing when sent through the QTest framework. This is a work around for our version - // of Qt. In future this can hopefully be simplified. See ^1 for workaround. - // More info: Issues with mouse move in Qt - // - https://bugreports.qt.io/browse/QTBUG-5232 - // - https://bugreports.qt.io/browse/QTBUG-69414 - // - https://lists.qt-project.org/pipermail/development/2019-July/036873.html - void MousePressAndMove( - QWidget* widget, const QPoint& widgetScreenPosition, const QPoint& mouseDelta) - { - QPoint position = widget->mapToGlobal(widgetScreenPosition); - QPoint nextPosition = widget->mapToGlobal(widgetScreenPosition + mouseDelta); - - QTest::mousePress(widget, Qt::LeftButton, Qt::NoModifier, position); - - // ^1 To ensure a mouse move event is fired we must call the test mouse move function - // and also send a mouse move event that matches. Each on their own do not appear to - // work - please see the links above for more context. - QTest::mouseMove(widget, nextPosition); - QMouseEvent mouseMoveEvent( - QEvent::MouseMove, QPointF(nextPosition), QPointF(nextPosition), - Qt::NoButton, Qt::LeftButton, Qt::NoModifier); - QApplication::sendEvent(widget, &mouseMoveEvent); - } - TEST_F(SpinBoxFixture, SpinBoxMousePressAndMoveRightScrollsValue) { m_doubleSpinBox->setValue(10.0);