AzManipulatorTestFrameworkUtils.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 <AzCore/Component/TransformBus.h>
  9. #include <AzFramework/Viewport/ViewportScreen.h>
  10. #include <AzManipulatorTestFramework/AzManipulatorTestFrameworkUtils.h>
  11. #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
  12. #include <AzToolsFramework/ViewportSelection/EditorTransformComponentSelectionRequestBus.h>
  13. namespace AzManipulatorTestFramework
  14. {
  15. using InteractionId = AzToolsFramework::ViewportInteraction::InteractionId;
  16. using KeyboardModifiers = AzToolsFramework::ViewportInteraction::KeyboardModifiers;
  17. using MouseInteraction = AzToolsFramework::ViewportInteraction::MouseInteraction;
  18. using MouseInteractionEvent = AzToolsFramework::ViewportInteraction::MouseInteractionEvent;
  19. using MouseButton = AzToolsFramework::ViewportInteraction::MouseButton;
  20. using MouseButtons = AzToolsFramework::ViewportInteraction::MouseButtons;
  21. using MouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent;
  22. using MousePick = AzToolsFramework::ViewportInteraction::MousePick;
  23. // create a default sphere view for a manipulator for simple intersection
  24. template<typename Manipulator>
  25. void SetupManipulatorView(
  26. AZStd::shared_ptr<Manipulator> manipulator,
  27. const AzToolsFramework::ManipulatorManagerId manipulatorManagerId,
  28. const AZ::Vector3& position,
  29. const float radius)
  30. {
  31. // unit sphere view
  32. auto sphereView = AzToolsFramework::CreateManipulatorViewSphere(
  33. AZ::Colors::Red, radius,
  34. []([[maybe_unused]] const MouseInteraction& mouseInteraction, [[maybe_unused]] const bool mouseOver,
  35. const AZ::Color& defaultColor)
  36. {
  37. return defaultColor;
  38. },
  39. true);
  40. // unit sphere bound
  41. AzToolsFramework::Picking::BoundShapeSphere sphereBound;
  42. sphereBound.m_center = position;
  43. sphereBound.m_radius = radius;
  44. // we need the view to construct the manipulator bounds after the manipulator has been registered
  45. auto view = sphereView.get();
  46. // construct view and register with manager
  47. AzToolsFramework::ManipulatorViews views;
  48. views.emplace_back(AZStd::move(sphereView));
  49. manipulator->SetViews(AZStd::move(views));
  50. manipulator->Register(manipulatorManagerId);
  51. // this would occur internally when the manipulator is drawn but we must do manually here to ensure that the
  52. // bounds will always be valid upon instantiation
  53. view->RefreshBound(manipulatorManagerId, manipulator->GetManipulatorId(), sphereBound);
  54. }
  55. AZStd::shared_ptr<AzToolsFramework::LinearManipulator> CreateLinearManipulator(
  56. const AzToolsFramework::ManipulatorManagerId manipulatorManagerId, const AZ::Vector3& position, const float radius)
  57. {
  58. auto manipulator = AzToolsFramework::LinearManipulator::MakeShared(AZ::Transform::CreateIdentity());
  59. manipulator->SetLocalPosition(position);
  60. SetupManipulatorView(manipulator, manipulatorManagerId, position, radius);
  61. return manipulator;
  62. }
  63. AZStd::shared_ptr<AzToolsFramework::PlanarManipulator> CreatePlanarManipulator(
  64. const AzToolsFramework::ManipulatorManagerId manipulatorManagerId, const AZ::Vector3& position, const float radius)
  65. {
  66. auto manipulator = AzToolsFramework::PlanarManipulator::MakeShared(AZ::Transform::CreateIdentity());
  67. manipulator->SetLocalPosition(position);
  68. SetupManipulatorView(manipulator, manipulatorManagerId, position, radius);
  69. return manipulator;
  70. }
  71. void DispatchMouseInteractionEvent(const MouseInteractionEvent& event)
  72. {
  73. AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Event(
  74. AzToolsFramework::GetEntityContextId(),
  75. &AzToolsFramework::ViewportInteraction::InternalMouseViewportRequests::InternalHandleAllMouseInteractions, event);
  76. }
  77. AzFramework::CameraState SetCameraStatePosition(const AZ::Vector3& position, AzFramework::CameraState& cameraState)
  78. {
  79. cameraState.m_position = position;
  80. return cameraState;
  81. }
  82. AzFramework::CameraState SetCameraStateDirection(const AZ::Vector3& direction, AzFramework::CameraState& cameraState)
  83. {
  84. const auto transform = AZ::Transform::CreateLookAt(cameraState.m_position, cameraState.m_position + direction);
  85. AzFramework::SetCameraTransform(cameraState, transform);
  86. return cameraState;
  87. }
  88. AzFramework::ScreenPoint GetCameraStateViewportCenter(const AzFramework::CameraState& cameraState)
  89. {
  90. return { cameraState.m_viewportSize.m_width / 2, cameraState.m_viewportSize.m_height / 2 };
  91. }
  92. void DragMouse(
  93. const AzFramework::CameraState& cameraState,
  94. AzManipulatorTestFramework::ImmediateModeActionDispatcher* actionDispatcher,
  95. const AZ::Vector3& worldStart,
  96. const AZ::Vector3& worldEnd,
  97. const AzToolsFramework::ViewportInteraction::KeyboardModifier keyboardModifier)
  98. {
  99. const auto screenStart = AzFramework::WorldToScreen(worldStart, cameraState);
  100. const auto screenEnd = AzFramework::WorldToScreen(worldEnd, cameraState);
  101. actionDispatcher
  102. ->CameraState(cameraState)
  103. ->MousePosition(screenStart)
  104. ->KeyboardModifierDown(keyboardModifier)
  105. ->MouseLButtonDown()
  106. ->MousePosition(screenEnd)
  107. ->MouseLButtonUp();
  108. }
  109. } // namespace AzManipulatorTestFramework