EditorComponentIntersectionTests.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/AzManipulatorTestFrameworkTestHelpers.h>
  9. #include <AzTest/AzTest.h>
  10. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  11. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  12. #include <AzToolsFramework/Viewport/ViewportSettings.h>
  13. #include <AzToolsFramework/Viewport/ViewportTypes.h>
  14. #include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
  15. #include "Shape/EditorSphereShapeComponent.h"
  16. #include "Shape/EditorSplineComponent.h"
  17. namespace LmbrCentral
  18. {
  19. using AzToolsFramework::ViewportInteraction::BuildMouseButtons;
  20. using AzToolsFramework::ViewportInteraction::BuildMouseInteraction;
  21. using AzToolsFramework::ViewportInteraction::BuildMousePick;
  22. class EditorIntersectionComponentFixture : public UnitTest::ToolsApplicationFixture<>
  23. {
  24. public:
  25. void SetUpEditorFixtureImpl() override
  26. {
  27. AZ::SerializeContext* serializeContext = nullptr;
  28. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  29. m_editorSphereShapeComponentDescriptor =
  30. AZStd::unique_ptr<AZ::ComponentDescriptor>(EditorSphereShapeComponent::CreateDescriptor());
  31. m_editorSphereShapeComponentDescriptor->Reflect(serializeContext);
  32. m_editorSplineComponentDescriptor = AZStd::unique_ptr<AZ::ComponentDescriptor>(EditorSplineComponent::CreateDescriptor());
  33. m_editorSplineComponentDescriptor->Reflect(serializeContext);
  34. m_entityId1 = UnitTest::CreateDefaultEditorEntity("Entity1");
  35. }
  36. void TearDownEditorFixtureImpl() override
  37. {
  38. bool entityDestroyed = false;
  39. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  40. entityDestroyed, &AzToolsFramework::EditorEntityContextRequestBus::Events::DestroyEditorEntity, m_entityId1);
  41. m_editorSplineComponentDescriptor.reset();
  42. m_editorSphereShapeComponentDescriptor.reset();
  43. }
  44. AZ::EntityId m_entityId1;
  45. AZStd::unique_ptr<AZ::ComponentDescriptor> m_editorSphereShapeComponentDescriptor;
  46. AZStd::unique_ptr<AZ::ComponentDescriptor> m_editorSplineComponentDescriptor;
  47. };
  48. struct IntersectionQueryOutcome
  49. {
  50. bool m_helpersVisible;
  51. bool m_expectedIntersection;
  52. };
  53. using EditorComponentIndirectCallManipulatorViewportInteractionFixture =
  54. UnitTest::IndirectCallManipulatorViewportInteractionFixtureMixin<EditorIntersectionComponentFixture>;
  55. class EditorComponentIndirectCallManipulatorViewportInteractionFixtureParam
  56. : public EditorComponentIndirectCallManipulatorViewportInteractionFixture
  57. , public ::testing::WithParamInterface<IntersectionQueryOutcome>
  58. {
  59. public:
  60. virtual void CreateEditorComponent(AZ::Entity* entity) = 0;
  61. virtual void SetupEditorComponent(AZ::EntityId entityId) = 0;
  62. void SetUpEditorFixtureImpl() override
  63. {
  64. EditorComponentIndirectCallManipulatorViewportInteractionFixture::SetUpEditorFixtureImpl();
  65. auto* entity1 = AzToolsFramework::GetEntityById(m_entityId1);
  66. AZ_Assert(entity1, "Entity1 could not be found");
  67. entity1->Deactivate();
  68. CreateEditorComponent(entity1);
  69. entity1->Activate();
  70. AZ::TransformBus::Event(
  71. m_entityId1, &AZ::TransformBus::Events::SetWorldTM, AZ::Transform::CreateTranslation(AZ::Vector3(0.0f, 2.0f, 0.0f)));
  72. SetupEditorComponent(m_entityId1);
  73. m_cameraState = AzFramework::CreateDefaultCamera(AZ::Transform::CreateIdentity(), AzFramework::ScreenSize(1024, 768));
  74. }
  75. void VerifySelectionIntersection()
  76. {
  77. // given
  78. m_viewportManipulatorInteraction->GetViewportInteraction().SetHelpersVisible(GetParam().m_helpersVisible);
  79. const auto entity1ScreenPosition =
  80. AzFramework::WorldToScreen(AzToolsFramework::GetWorldTranslation(m_entityId1), m_cameraState);
  81. const auto viewportId = m_viewportManipulatorInteraction->GetViewportInteraction().GetViewportId();
  82. const auto mouseInteraction = BuildMouseInteraction(
  83. BuildMousePick(m_cameraState, entity1ScreenPosition),
  84. BuildMouseButtons(AzToolsFramework::ViewportInteraction::MouseButton::None),
  85. AzToolsFramework::ViewportInteraction::InteractionId(AZ::EntityId(), viewportId),
  86. AzToolsFramework::ViewportInteraction::KeyboardModifiers());
  87. // mimic mouse move
  88. m_actionDispatcher->CameraState(m_cameraState)->MousePosition(entity1ScreenPosition);
  89. // when
  90. float closestDistance = AZStd::numeric_limits<float>::max();
  91. const bool entityPicked = AzToolsFramework::PickEntity(m_entityId1, mouseInteraction, closestDistance, viewportId);
  92. // then
  93. EXPECT_THAT(entityPicked, ::testing::Eq(GetParam().m_expectedIntersection));
  94. }
  95. };
  96. class ShapeEditorComponentIndirectCallManipulatorViewportInteractionFixtureParam
  97. : public EditorComponentIndirectCallManipulatorViewportInteractionFixtureParam
  98. {
  99. public:
  100. void CreateEditorComponent(AZ::Entity* entity) override
  101. {
  102. entity->CreateComponent<EditorSphereShapeComponent>();
  103. }
  104. void SetupEditorComponent(AZ::EntityId entityId) override
  105. {
  106. LmbrCentral::SphereShapeComponentRequestsBus::Event(
  107. entityId, &LmbrCentral::SphereShapeComponentRequestsBus::Events::SetRadius, 1.0f);
  108. }
  109. };
  110. class SplineEditorComponentIndirectCallManipulatorViewportInteractionFixtureParam
  111. : public EditorComponentIndirectCallManipulatorViewportInteractionFixtureParam
  112. {
  113. public:
  114. void CreateEditorComponent(AZ::Entity* entity) override
  115. {
  116. entity->CreateComponent<EditorSplineComponent>();
  117. }
  118. void SetupEditorComponent([[maybe_unused]] AZ::EntityId entityId) override
  119. {
  120. // unused
  121. }
  122. };
  123. TEST_P(ShapeEditorComponentIndirectCallManipulatorViewportInteractionFixtureParam, ShapeIntersectionOnlyHappensWithHelpersEnabled)
  124. {
  125. VerifySelectionIntersection();
  126. }
  127. INSTANTIATE_TEST_CASE_P(
  128. All,
  129. ShapeEditorComponentIndirectCallManipulatorViewportInteractionFixtureParam,
  130. testing::Values(IntersectionQueryOutcome{ true, true }, IntersectionQueryOutcome{ false, false }));
  131. TEST_P(SplineEditorComponentIndirectCallManipulatorViewportInteractionFixtureParam, SplineIntersectionOnlyHappensWithHelpersEnabled)
  132. {
  133. VerifySelectionIntersection();
  134. }
  135. INSTANTIATE_TEST_CASE_P(
  136. All,
  137. SplineEditorComponentIndirectCallManipulatorViewportInteractionFixtureParam,
  138. testing::Values(IntersectionQueryOutcome{ true, true }, IntersectionQueryOutcome{ false, false }));
  139. } // namespace LmbrCentral