ImmediateModeActionDispatcher.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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& manipulatorViewportInteraction)
  28. : m_manipulatorViewportInteraction(manipulatorViewportInteraction)
  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_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  45. }
  46. void ImmediateModeActionDispatcher::SetSnapToGridImpl(const bool enabled)
  47. {
  48. m_manipulatorViewportInteraction.GetViewportInteraction().SetGridSnapping(enabled);
  49. }
  50. void ImmediateModeActionDispatcher::SetStickySelectImpl(const bool enabled)
  51. {
  52. m_manipulatorViewportInteraction.GetViewportInteraction().SetStickySelect(enabled);
  53. }
  54. void ImmediateModeActionDispatcher::GridSizeImpl(const float size)
  55. {
  56. m_manipulatorViewportInteraction.GetViewportInteraction().SetGridSize(size);
  57. }
  58. void ImmediateModeActionDispatcher::CameraStateImpl(const AzFramework::CameraState& cameraState)
  59. {
  60. m_manipulatorViewportInteraction.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. auto mouseEvent = *GetMouseInteractionEvent();
  67. mouseEvent.m_mouseInteraction.m_mouseButtons.m_mouseButtons = aznumeric_cast<AZ::u32>(MouseButton::Left);
  68. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(mouseEvent);
  69. // the mouse position will be the same as the previous event, thus the delta will be 0
  70. MouseMoveAfterButton();
  71. }
  72. void ImmediateModeActionDispatcher::MouseLButtonUpImpl()
  73. {
  74. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  75. auto mouseEvent = *GetMouseInteractionEvent();
  76. mouseEvent.m_mouseInteraction.m_mouseButtons.m_mouseButtons = aznumeric_cast<AZ::u32>(MouseButton::Left);
  77. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(mouseEvent);
  78. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  79. // the mouse position will be the same as the previous event, thus the delta will be 0
  80. MouseMoveAfterButton();
  81. }
  82. void ImmediateModeActionDispatcher::MouseMButtonDownImpl()
  83. {
  84. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Middle);
  85. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Down;
  86. auto mouseEvent = *GetMouseInteractionEvent();
  87. mouseEvent.m_mouseInteraction.m_mouseButtons.m_mouseButtons = aznumeric_cast<AZ::u32>(MouseButton::Middle);
  88. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(mouseEvent);
  89. // the mouse position will be the same as the previous event, thus the delta will be 0
  90. MouseMoveAfterButton();
  91. }
  92. void ImmediateModeActionDispatcher::MouseMButtonUpImpl()
  93. {
  94. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  95. auto mouseEvent = *GetMouseInteractionEvent();
  96. mouseEvent.m_mouseInteraction.m_mouseButtons.m_mouseButtons = aznumeric_cast<AZ::u32>(MouseButton::Middle);
  97. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(mouseEvent);
  98. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Middle);
  99. // the mouse position will be the same as the previous event, thus the delta will be 0
  100. MouseMoveAfterButton();
  101. }
  102. void ImmediateModeActionDispatcher::MouseRButtonDownImpl()
  103. {
  104. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Right);
  105. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Down;
  106. auto mouseEvent = *GetMouseInteractionEvent();
  107. mouseEvent.m_mouseInteraction.m_mouseButtons.m_mouseButtons = aznumeric_cast<AZ::u32>(MouseButton::Right);
  108. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(mouseEvent);
  109. // the mouse position will be the same as the previous event, thus the delta will be 0
  110. MouseMoveAfterButton();
  111. }
  112. void ImmediateModeActionDispatcher::MouseRButtonUpImpl()
  113. {
  114. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Up;
  115. auto mouseEvent = *GetMouseInteractionEvent();
  116. mouseEvent.m_mouseInteraction.m_mouseButtons.m_mouseButtons = aznumeric_cast<AZ::u32>(MouseButton::Right);
  117. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(mouseEvent);
  118. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Right);
  119. // the mouse position will be the same as the previous event, thus the delta will be 0
  120. MouseMoveAfterButton();
  121. }
  122. void ImmediateModeActionDispatcher::MouseLButtonDoubleClickImpl()
  123. {
  124. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::DoubleClick;
  125. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  126. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  127. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_mouseButtons.m_mouseButtons, MouseButton::Left);
  128. // the mouse position will be the same as the previous event, thus the delta will be 0
  129. MouseMoveAfterButton();
  130. }
  131. void ImmediateModeActionDispatcher::MousePositionImpl(const AzFramework::ScreenPoint& position)
  132. {
  133. const auto cameraState = m_manipulatorViewportInteraction.GetViewportInteraction().GetCameraState();
  134. GetMouseInteractionEvent()->m_mouseInteraction.m_mousePick =
  135. AzToolsFramework::ViewportInteraction::BuildMousePick(cameraState, position);
  136. GetMouseInteractionEvent()->m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Move;
  137. m_manipulatorViewportInteraction.GetManipulatorManager().ConsumeMouseInteractionEvent(*m_event);
  138. }
  139. void ImmediateModeActionDispatcher::KeyboardModifierDownImpl(const KeyboardModifier keyModifier)
  140. {
  141. ToggleOn(GetMouseInteractionEvent()->m_mouseInteraction.m_keyboardModifiers.m_keyModifiers, keyModifier);
  142. }
  143. void ImmediateModeActionDispatcher::KeyboardModifierUpImpl(const KeyboardModifier keyModifier)
  144. {
  145. ToggleOff(GetMouseInteractionEvent()->m_mouseInteraction.m_keyboardModifiers.m_keyModifiers, keyModifier);
  146. }
  147. void ImmediateModeActionDispatcher::SetEntityWorldTransformImpl(AZ::EntityId entityId, const AZ::Transform& transform)
  148. {
  149. AzToolsFramework::SetWorldTransform(entityId, transform);
  150. }
  151. void ImmediateModeActionDispatcher::SetSelectedEntityImpl(AZ::EntityId entity)
  152. {
  153. AzToolsFramework::SelectEntity(entity);
  154. }
  155. void ImmediateModeActionDispatcher::SetSelectedEntitiesImpl(const AzToolsFramework::EntityIdList& entities)
  156. {
  157. AzToolsFramework::SelectEntities(entities);
  158. }
  159. void ImmediateModeActionDispatcher::EnterComponentModeImpl(const AZ::Uuid& uuid)
  160. {
  161. using AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus;
  162. ComponentModeSystemRequestBus::Broadcast(&ComponentModeSystemRequestBus::Events::AddSelectedComponentModesOfType, uuid);
  163. }
  164. const AzToolsFramework::ViewportInteraction::MouseInteractionEvent* ImmediateModeActionDispatcher::GetMouseInteractionEvent() const
  165. {
  166. if (!m_event)
  167. {
  168. m_event = AZStd::unique_ptr<MouseInteractionEvent>(AZStd::make_unique<MouseInteractionEvent>());
  169. m_event->m_mouseInteraction.m_interactionId.m_viewportId =
  170. m_manipulatorViewportInteraction.GetViewportInteraction().GetViewportId();
  171. }
  172. return m_event.get();
  173. }
  174. AzToolsFramework::ViewportInteraction::MouseInteractionEvent* ImmediateModeActionDispatcher::GetMouseInteractionEvent()
  175. {
  176. return const_cast<MouseInteractionEvent*>(static_cast<const ImmediateModeActionDispatcher*>(this)->GetMouseInteractionEvent());
  177. }
  178. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ExpectTrue(bool result)
  179. {
  180. Log("Expecting true");
  181. EXPECT_TRUE(result);
  182. return this;
  183. }
  184. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ExpectFalse(bool result)
  185. {
  186. Log("Expecting false");
  187. EXPECT_FALSE(result);
  188. return this;
  189. }
  190. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::GetEntityWorldTransform(AZ::EntityId entityId, AZ::Transform& transform)
  191. {
  192. Log("Getting entity world transform");
  193. transform = AzToolsFramework::GetWorldTransform(entityId);
  194. return this;
  195. }
  196. void ImmediateModeActionDispatcher::ExpectManipulatorBeingInteractedImpl()
  197. {
  198. EXPECT_TRUE(m_manipulatorViewportInteraction.GetManipulatorManager().ManipulatorBeingInteracted());
  199. }
  200. void ImmediateModeActionDispatcher::ExpectManipulatorNotBeingInteractedImpl()
  201. {
  202. EXPECT_FALSE(m_manipulatorViewportInteraction.GetManipulatorManager().ManipulatorBeingInteracted());
  203. }
  204. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ResetEvent()
  205. {
  206. Log("Resetting the event state");
  207. m_event.reset();
  208. return this;
  209. }
  210. ImmediateModeActionDispatcher* ImmediateModeActionDispatcher::ExecuteBlock(const AZStd::function<void()>& blockFn)
  211. {
  212. blockFn();
  213. return this;
  214. }
  215. } // namespace AzManipulatorTestFramework