AzManipulatorTestFrameworkFixture.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/AzManipulatorTestFrameworkFixture.h>
  9. #include <AzToolsFramework/ViewportSelection/EditorInteractionSystemViewportSelectionRequestBus.h>
  10. namespace UnitTest
  11. {
  12. using MouseInteraction = AzToolsFramework::ViewportInteraction::MouseInteraction;
  13. using MouseInteractionEvent = AzToolsFramework::ViewportInteraction::MouseInteractionEvent;
  14. using MouseButton = AzToolsFramework::ViewportInteraction::MouseButton;
  15. using MouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent;
  16. using MousePick = AzToolsFramework::ViewportInteraction::MousePick;
  17. AZStd::shared_ptr<AzToolsFramework::LinearManipulator> CreateLinearManipulator(
  18. const AzToolsFramework::ManipulatorManagerId manipulatorManagerId)
  19. {
  20. auto manipulator = AzToolsFramework::LinearManipulator::MakeShared(AZ::Transform::CreateIdentity());
  21. // unit sphere view
  22. auto sphereView = AzToolsFramework::CreateManipulatorViewSphere(
  23. AZ::Colors::Red, 1.0f,
  24. [](const MouseInteraction& /*mouseInteraction*/, const bool /*mouseOver*/,
  25. const AZ::Color& defaultColor)
  26. {
  27. return defaultColor;
  28. }, true);
  29. // unit sphere bound
  30. AzToolsFramework::Picking::BoundShapeSphere sphereBound;
  31. sphereBound.m_center = AZ::Vector3::CreateZero();
  32. sphereBound.m_radius = 1.0f;
  33. // we need the view to construct the manipulator bounds after the manipulator has been registered
  34. auto view = sphereView.get();
  35. // construct view and register with manager
  36. AzToolsFramework::ManipulatorViews views;
  37. views.emplace_back(AZStd::move(sphereView));
  38. manipulator->SetViews(AZStd::move(views));
  39. manipulator->Register(manipulatorManagerId);
  40. // this would occur internally when the manipulator is drawn but we must do manually here
  41. view->RefreshBound(manipulatorManagerId, manipulator->GetManipulatorId(), sphereBound);
  42. return manipulator;
  43. }
  44. MousePick CreateWorldSpaceMousePickRay(const AZ::Vector3& origin, const AZ::Vector3& direction)
  45. {
  46. MousePick mousePick;
  47. mousePick.m_rayOrigin = origin;
  48. mousePick.m_rayDirection = direction;
  49. return mousePick;
  50. }
  51. MouseInteraction CreateMouseInteraction(const MousePick& worldSpaceRay, MouseButton button)
  52. {
  53. AzToolsFramework::ViewportInteraction::MouseInteraction interaction;
  54. interaction.m_mousePick = worldSpaceRay;
  55. interaction.m_mouseButtons.m_mouseButtons = static_cast<AZ::u32>(button);
  56. return interaction;
  57. }
  58. MouseInteractionEvent CreateMouseInteractionEvent(
  59. const MousePick& worldSpaceRay, MouseButton button, MouseEvent event)
  60. {
  61. return MouseInteractionEvent(CreateMouseInteraction(worldSpaceRay, button), event);
  62. }
  63. void DispatchMouseInteractionEvent(const MouseInteractionEvent& event)
  64. {
  65. AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::Event(
  66. AzToolsFramework::GetEntityContextId(),
  67. &AzToolsFramework::ViewportInteraction::InternalMouseViewportRequests::InternalHandleAllMouseInteractions,
  68. event);
  69. }
  70. AzFramework::CameraState SetCameraStatePosition(const AZ::Vector3& position, AzFramework::CameraState& cameraState)
  71. {
  72. cameraState.m_position = position;
  73. return cameraState;
  74. }
  75. AzFramework::CameraState SetCameraStateDirection(const AZ::Vector3& direction, AzFramework::CameraState& cameraState)
  76. {
  77. const auto transform = AZ::Transform::CreateLookAt(cameraState.m_position, cameraState.m_position + direction);
  78. AzFramework::SetCameraTransform(cameraState, transform);
  79. return cameraState;
  80. }
  81. } // namespace UnitTest