EditorShapeTestUtils.cpp 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <EditorShapeTestUtils.h>
  9. #include <AzToolsFramework/Entity/EditorEntityHelpers.h>
  10. #include <LmbrCentral/Shape/BoxShapeComponentBus.h>
  11. #include <LmbrCentral/Shape/CapsuleShapeComponentBus.h>
  12. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  13. #include <Shape/EditorSphereShapeComponent.h>
  14. namespace LmbrCentral
  15. {
  16. // use a large tolerance for manipulator tests, because accuracy is limited by viewport resolution
  17. static constexpr float ManipulatorTolerance = 0.1f;
  18. void EnterComponentMode(AZ::EntityId entityId, const AZ::Uuid& componentType)
  19. {
  20. AzToolsFramework::SelectEntity(entityId);
  21. AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus::Broadcast(
  22. &AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus::Events::AddSelectedComponentModesOfType,
  23. componentType);
  24. }
  25. void ExpectBoxDimensions(AZ::EntityId entityId, const AZ::Vector3& expectedBoxDimensions)
  26. {
  27. AZ::Vector3 boxDimensions = AZ::Vector3::CreateZero();
  28. BoxShapeComponentRequestsBus::EventResult(boxDimensions, entityId, &BoxShapeComponentRequests::GetBoxDimensions);
  29. EXPECT_THAT(boxDimensions, UnitTest::IsCloseTolerance(expectedBoxDimensions, ManipulatorTolerance));
  30. }
  31. void ExpectCapsuleRadius(AZ::EntityId entityId, float expectedRadius)
  32. {
  33. float radius = 0.0f;
  34. CapsuleShapeComponentRequestsBus::EventResult(radius, entityId, &CapsuleShapeComponentRequests::GetRadius);
  35. EXPECT_NEAR(radius, expectedRadius, ManipulatorTolerance);
  36. }
  37. void ExpectCapsuleHeight(AZ::EntityId entityId, float expectedHeight)
  38. {
  39. float height = 0.0f;
  40. CapsuleShapeComponentRequestsBus::EventResult(height, entityId, &CapsuleShapeComponentRequests::GetHeight);
  41. EXPECT_NEAR(height, expectedHeight, ManipulatorTolerance);
  42. }
  43. void ExpectSphereRadius(AZ::EntityId entityId, float expectedRadius)
  44. {
  45. float radius = 0.0f;
  46. SphereShapeComponentRequestsBus::EventResult(radius, entityId, &SphereShapeComponentRequests::GetRadius);
  47. EXPECT_NEAR(radius, expectedRadius, ManipulatorTolerance);
  48. }
  49. void ExpectTranslationOffset(AZ::EntityId entityId, const AZ::Vector3& expectedTranslationOffset)
  50. {
  51. AZ::Vector3 translationOffset = AZ::Vector3::CreateZero();
  52. LmbrCentral::ShapeComponentRequestsBus::EventResult(
  53. translationOffset, entityId, &LmbrCentral::ShapeComponentRequests::GetTranslationOffset);
  54. EXPECT_THAT(translationOffset, UnitTest::IsCloseTolerance(expectedTranslationOffset, ManipulatorTolerance));
  55. }
  56. void SetComponentSubMode(AZ::EntityComponentIdPair entityComponentIdPair, AzToolsFramework::ShapeComponentModeRequests::SubMode subMode)
  57. {
  58. AzToolsFramework::ShapeComponentModeRequestBus::Event(
  59. entityComponentIdPair, &AzToolsFramework::ShapeComponentModeRequests::SetShapeSubMode, subMode);
  60. }
  61. void ExpectSubMode(
  62. AZ::EntityComponentIdPair entityComponentIdPair, AzToolsFramework::ShapeComponentModeRequests::SubMode expectedSubMode)
  63. {
  64. AzToolsFramework::ShapeComponentModeRequests::SubMode subMode = AzToolsFramework::ShapeComponentModeRequests::SubMode::NumModes;
  65. AzToolsFramework::ShapeComponentModeRequestBus::EventResult(
  66. subMode, entityComponentIdPair, &AzToolsFramework::ShapeComponentModeRequests::GetShapeSubMode);
  67. EXPECT_EQ(subMode, expectedSubMode);
  68. }
  69. AzToolsFramework::ViewportInteraction::MouseInteractionResult CtrlScroll(float wheelDelta)
  70. {
  71. AzToolsFramework::ViewportInteraction::MouseInteractionEvent interactionEvent(
  72. AzToolsFramework::ViewportInteraction::MouseInteraction(), wheelDelta);
  73. interactionEvent.m_mouseEvent = AzToolsFramework::ViewportInteraction::MouseEvent::Wheel;
  74. interactionEvent.m_mouseInteraction.m_keyboardModifiers.m_keyModifiers =
  75. static_cast<AZ::u32>(AzToolsFramework::ViewportInteraction::KeyboardModifier::Ctrl);
  76. AzToolsFramework::ViewportInteraction::MouseInteractionResult handled =
  77. AzToolsFramework::ViewportInteraction::MouseInteractionResult::None;
  78. AzToolsFramework::EditorInteractionSystemViewportSelectionRequestBus::BroadcastResult(
  79. handled,
  80. &AzToolsFramework::ViewportInteraction::InternalMouseViewportRequests::InternalHandleAllMouseInteractions,
  81. interactionEvent);
  82. return handled;
  83. }
  84. } // namespace LmbrCentral