EditorFocusModeFixture.cpp 5.4 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 <Tests/FocusMode/EditorFocusModeFixture.h>
  9. #include <AzCore/Component/TransformBus.h>
  10. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  11. #include <Tests/BoundsTestComponent.h>
  12. namespace UnitTest
  13. {
  14. void ClearSelectedEntities()
  15. {
  16. AzToolsFramework::ToolsApplicationRequestBus::Broadcast(
  17. &AzToolsFramework::ToolsApplicationRequestBus::Events::SetSelectedEntities, AzToolsFramework::EntityIdList());
  18. }
  19. AzToolsFramework::EntityIdList EditorFocusModeFixture::GetSelectedEntities()
  20. {
  21. AzToolsFramework::EntityIdList selectedEntities;
  22. AzToolsFramework::ToolsApplicationRequestBus::BroadcastResult(
  23. selectedEntities, &AzToolsFramework::ToolsApplicationRequestBus::Events::GetSelectedEntities);
  24. return selectedEntities;
  25. }
  26. void EditorFocusModeFixture::SetUpEditorFixtureImpl()
  27. {
  28. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  29. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  30. // in the unit tests.
  31. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  32. m_containerEntityInterface = AZ::Interface<AzToolsFramework::ContainerEntityInterface>::Get();
  33. ASSERT_TRUE(m_containerEntityInterface != nullptr);
  34. m_focusModeInterface = AZ::Interface<AzToolsFramework::FocusModeInterface>::Get();
  35. ASSERT_TRUE(m_focusModeInterface != nullptr);
  36. if (auto* console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
  37. {
  38. console->GetCvarValue("ed_enableOutlinerOverrideManagement", m_ed_enableOutlinerOverrideManagement);
  39. console->PerformCommand("ed_enableOutlinerOverrideManagement true");
  40. }
  41. // register a simple component implementing BoundsRequestBus and EditorComponentSelectionRequestsBus
  42. GetApplication()->RegisterComponentDescriptor(UnitTest::BoundsTestComponent::CreateDescriptor());
  43. AzToolsFramework::EditorEntityContextRequestBus::BroadcastResult(
  44. m_editorEntityContextId, &AzToolsFramework::EditorEntityContextRequestBus::Events::GetEditorEntityContextId);
  45. GenerateTestHierarchy();
  46. // Clear the focus, disabling focus mode
  47. m_focusModeInterface->ClearFocusRoot(m_editorEntityContextId);
  48. // Clear selection
  49. ClearSelectedEntities();
  50. }
  51. void EditorFocusModeFixture::TearDownEditorFixtureImpl()
  52. {
  53. // Clear Container Entity preserved open states
  54. m_containerEntityInterface->Clear(m_editorEntityContextId);
  55. // Clear the focus, disabling focus mode
  56. m_focusModeInterface->ClearFocusRoot(m_editorEntityContextId);
  57. // Clear selection
  58. ClearSelectedEntities();
  59. AZ::SettingsRegistryInterface* registry = AZ::SettingsRegistry::Get();
  60. registry->Set("/O3DE/Autoexec/ConsoleCommands/ed_enableOutlinerOverrideManagement", m_ed_enableOutlinerOverrideManagement);
  61. }
  62. void EditorFocusModeFixture::GenerateTestHierarchy()
  63. {
  64. /*
  65. * City
  66. * |_ Street
  67. * |_ Car
  68. * | |_ Passenger
  69. * |_ SportsCar
  70. * |_ Passenger
  71. */
  72. m_entityMap[CityEntityName] = CreateEditorEntity(CityEntityName, AZ::EntityId());
  73. m_entityMap[StreetEntityName] = CreateEditorEntity(StreetEntityName, m_entityMap[CityEntityName]);
  74. m_entityMap[CarEntityName] = CreateEditorEntity(CarEntityName, m_entityMap[StreetEntityName]);
  75. m_entityMap[Passenger1EntityName] = CreateEditorEntity(Passenger1EntityName, m_entityMap[CarEntityName]);
  76. m_entityMap[SportsCarEntityName] = CreateEditorEntity(SportsCarEntityName, m_entityMap[StreetEntityName]);
  77. m_entityMap[Passenger2EntityName] = CreateEditorEntity(Passenger2EntityName, m_entityMap[SportsCarEntityName]);
  78. // Add a BoundsTestComponent to the Car entity.
  79. AZ::Entity* entity = AzToolsFramework::GetEntityById(m_entityMap[CarEntityName]);
  80. entity->Deactivate();
  81. entity->CreateComponent<UnitTest::BoundsTestComponent>();
  82. entity->Activate();
  83. // Move the City so that it is in view
  84. AZ::TransformBus::Event(m_entityMap[CityEntityName], &AZ::TransformBus::Events::SetWorldTranslation, s_worldCityEntityPosition);
  85. // Move the CarEntity so that it's not overlapping with the rest
  86. AZ::TransformBus::Event(m_entityMap[CarEntityName], &AZ::TransformBus::Events::SetWorldTranslation, s_worldCarEntityPosition);
  87. // Setup the camera so the entities is in view.
  88. AzFramework::SetCameraTransform(
  89. m_cameraState,
  90. AZ::Transform::CreateFromQuaternionAndTranslation(
  91. AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(0.0f, 0.0f, 0.0f)), CameraPosition));
  92. }
  93. AZ::EntityId EditorFocusModeFixture::CreateEditorEntity(const char* name, AZ::EntityId parentId)
  94. {
  95. AZ::Entity* entity = nullptr;
  96. UnitTest::CreateDefaultEditorEntity(name, &entity);
  97. // Parent
  98. AZ::TransformBus::Event(entity->GetId(), &AZ::TransformInterface::SetParent, parentId);
  99. return entity->GetId();
  100. }
  101. } // namespace UnitTest