ImmediateModeActionDispatcher.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
  9. #include <AzManipulatorTestFramework/ImmediateModeActionDispatcher.h>
  10. #include <AzToolsFramework/ComponentMode/EditorComponentModeBus.h>
  11. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  12. namespace AzManipulatorTestFramework
  13. {
  14. template<typename FieldT, typename FlagT>
  15. void ToggleOn(FieldT& field, FlagT flag)
  16. {
  17. field |= static_cast<FieldT>(flag);
  18. }
  19. template<typename FieldT, typename FlagT>
  20. void ToggleOff(FieldT& field, FlagT flag)
  21. {
  22. field &= ~static_cast<FieldT>(flag);
  23. }
  24. using MouseButton = AzToolsFramework::ViewportInteraction::MouseButton;
  25. using KeyboardModifier = AzToolsFramework::ViewportInteraction::KeyboardModifier;
  26. using MouseInteractionEvent = AzToolsFramework::ViewportInteraction::MouseInteractionEvent;
  27. ImmediateModeActionDispatcher::ImmediateModeActionDispatcher(ManipulatorViewportInteraction& viewportManipulatorInteraction)
  28. : m_viewportManipulatorInteraction(viewportManipulatorInteraction)
  29. {
  30. AzToolsFramework::ViewportInteraction::EditorModifierKeyRequestBus::Handler::BusConnect();
  31. AzToolsFramework::ViewportInteraction::EditorViewportInputTimeNowRequestBus::Handler::BusConnect();
  32. }
  33. ImmediateModeActionDispatcher::~ImmediateModeActionDispatcher()
  34. {
  35. AzToolsFramework::ViewportInteraction::EditorViewportInputTimeNowRequestBus::Handler::BusDisconnect();
  36. AzToolsFramework::ViewportInteraction::EditorModifierKeyRequestBus::Handler::BusDisconnect();
  37. }
  38. void ImmediateModeActionDispatcher::MouseMoveAfterButton()
  39. {
  40. // the editor application generates a mouse move event with a zero delta after every
  41. // mouse down and mouse up event, to match the editor behavior we insert this event
  42. // to ensure the tests are simulating the same environment as the editor
  43. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Move;
  44. m_viewportManipulatorInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  45. }
  46. void ImmediateModeActionDispatcher::SetSnapToGridImpl(const bool enabled)
  47. {
  48. m_viewportManipulatorInteraction.GetViewportInteraction().SetGridSnapping(enabled);
  49. }
  50. void ImmediateModeActionDispatcher::SetStickySelectImpl(const bool enabled)
  51. {
  52. m_viewportManipulatorInteraction.GetViewportInteraction().SetStickySelect(enabled);
  53. }
  54. void ImmediateModeActionDispatcher::GridSizeImpl(const float size)
  55. {
  56. m_viewportManipulatorInteraction.GetViewportInteraction().SetGridSize(size);
  57. }
  58. void ImmediateModeActionDispatcher::CameraStateImpl(const AzFramework::CameraState& cameraState)
  59. {
  60. m_viewportManipulatorInteraction.GetViewportInteraction().SetCameraState(cameraState);
  61. }
  62. void ImmediateModeActionDispatcher::MouseLButtonDownImpl()
  63. {
  64. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  65. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Down;
  66. m_viewportManipulatorInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  67. // the mouse position will be the same as the previous event, thus the delta will be 0
  68. MouseMoveAfterButton();
  69. }
  70. void ImmediateModeActionDispatcher::MouseLButtonUpImpl()
  71. {
  72. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  73. m_viewportManipulatorInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  74. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  75. // the mouse position will be the same as the previous event, thus the delta will be 0
  76. MouseMoveAfterButton();
  77. }
  78. void ImmediateModeActionDispatcher::MouseLButtonDoubleClickImpl()
  79. {
  80. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::DoubleClick;
  81. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  82. m_viewportManipulatorInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  83. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  84. // the mouse position will be the same as the previous event, thus the delta will be 0
  85. MouseMoveAfterButton();
  86. }
  87. void ImmediateModeActionDispatcher::MousePositionImpl(const AzFramework::ScreenPoint& position)
  88. {
  89. const auto cameraState = m_viewportManipulatorInteraction.GetViewportInteraction().GetCameraState();
  90. GetMouseInteractionEvent()->m_mouseInteraction.m_mousePick = BuildMousePick(position, cameraState);
  91. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Move;
  92. m_viewportManipulatorInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  93. }
  94. void ImmediateModeActionDispatcher::KeyboardModifierDownImpl(const KeyboardModifier& keyModifier)
  95. {
  96. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_keyboardModifiers.m_keyModifiers, keyModifier);
  97. }
  98. void ImmediateModeActionDispatcher::KeyboardModifierUpImpl(const KeyboardModifier& keyModifier)
  99. {
  100. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_keyboardModifiers.m_keyModifiers, keyModifier);
  101. }
  102. void ImmediateModeActionDispatcher::SetEntityWorldTransformImpl(AZ::EntityId entityId, const AZ::Transform& transform)
  103. {
  104. AzToolsFramework::SetWorldTransform(entityId, transform);
  105. }
  106. void ImmediateModeActionDispatcher::SetSelectedEntityImpl(AZ::EntityId entity)
  107. {
  108. AzToolsFramework::SelectEntity(entity);
  109. }
  110. void ImmediateModeActionDispatcher::SetSelectedEntitiesImpl(const AzToolsFramework::EntityIdList& entities)
  111. {
  112. AzToolsFramework::SelectEntities(entities);
  113. }
  114. void ImmediateModeActionDispatcher::EnterComponentModeImpl(const AZ::Uuid& uuid)
  115. {
  116. using AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus;
  117. ComponentModeSystemRequestBus::Broadcast(&ComponentModeSystemRequestBus::Events::AddSelectedComponentModesOfType, uuid);
  118. }
  119. const AzToolsFramework::ViewportInteraction::MouseInteractionEvent* ImmediateModeActionDispatcher::GetMouseInteractionEvent() const
  120. {
  121. if (!m_event)
  122. {
  123. m_event = AZStd::unique_ptr<MouseInteractionEvent>(AZStd::make_unique<MouseInteractionEvent>());
  124. m_event->m_mouseInteraction.m_interactionId.m_viewportId =
  125. m_viewportManipulatorInteraction.GetViewportInteraction().GetViewportId();
  126. }
  127. return m_event.get();
  128. }
  129. AzToolsFramework::ViewportInteraction::MouseInteractionEvent* ImmediateModeActionDispatcher::GetMouseInteractionEvent()
  130. {
  131. return const_cast<MouseInteractionEvent*>(static_cast<const ImmediateModeActionDispatcher*>(this)->GetMouseInteractionEvent());
  132. }
  133. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ExpectTrue(bool result)
  134. {
  135. Log("Expecting true");
  136. EXPECT_TRUE(result);
  137. return this;
  138. }
  139. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ExpectFalse(bool result)
  140. {
  141. Log("Expecting false");
  142. EXPECT_FALSE(result);
  143. return this;
  144. }
  145. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::GetEntityWorldTransform(AZ::EntityId entityId, AZ::Transform& transform)
  146. {
  147. Log("Getting entity world transform");
  148. transform = AzToolsFramework::GetWorldTransform(entityId);
  149. return this;
  150. }
  151. void ImmediateModeActionDispatcher::ExpectManipulatorBeingInteractedImpl()
  152. {
  153. EXPECT_TRUE(m_viewportManipulatorInteraction.GetManipulatorManager().ManipulatorBeingInteracted());
  154. }
  155. void ImmediateModeActionDispatcher::ExpectManipulatorNotBeingInteractedImpl()
  156. {
  157. EXPECT_FALSE(m_viewportManipulatorInteraction.GetManipulatorManager().ManipulatorBeingInteracted());
  158. }
  159. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ResetEvent()
  160. {
  161. Log("Resetting the event state");
  162. m_event.reset();
  163. return this;
  164. }
  165. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ExecuteBlock(const AZStd::function<void()>& blockFn)
  166. {
  167. blockFn();
  168. return this;
  169. }
  170. } // namespace AzManipulatorTestFramework