EditorSphereShapeComponent.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <Shape/EditorSphereShapeComponent.h>
  9. #include <Shape/SphereShapeComponent.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <Shape/EditorShapeComponentConverters.h>
  12. #include <Shape/ShapeDisplay.h>
  13. #include <AzToolsFramework/ComponentModes/SphereComponentMode.h>
  14. namespace LmbrCentral
  15. {
  16. void EditorSphereShapeComponent::Reflect(AZ::ReflectContext* context)
  17. {
  18. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  19. {
  20. // Note: this must be called by the first EditorShapeComponent to have it's
  21. // reflect function called, which happens to be this one for now.
  22. EditorBaseShapeComponent::Reflect(*serializeContext);
  23. // Deprecate: EditorSphereColliderComponent -> EditorSphereShapeComponent
  24. serializeContext->ClassDeprecate(
  25. "EditorSphereColliderComponent",
  26. AZ::Uuid("{9A12FC39-60D2-4237-AC79-11FEDFEDB851}"),
  27. &ClassConverters::DeprecateEditorSphereColliderComponent)
  28. ;
  29. serializeContext->Class<EditorSphereShapeComponent, EditorBaseShapeComponent>()
  30. ->Version(3, &ClassConverters::UpgradeEditorSphereShapeComponent)
  31. ->Field("SphereShape", &EditorSphereShapeComponent::m_sphereShape)
  32. ->Field("ComponentMode", &EditorSphereShapeComponent::m_componentModeDelegate)
  33. ;
  34. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  35. {
  36. editContext
  37. ->Class<EditorSphereShapeComponent>(
  38. "Sphere Shape", "The Sphere Shape component creates a sphere around the associated entity")
  39. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  40. ->Attribute(AZ::Edit::Attributes::Category, "Shape")
  41. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Sphere_Shape.svg")
  42. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Sphere_Shape.svg")
  43. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  44. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  45. ->Attribute(
  46. AZ::Edit::Attributes::HelpPageURL, "https://www.o3de.org/docs/user-guide/components/reference/shape/sphere-shape/")
  47. ->DataElement(
  48. AZ::Edit::UIHandlers::Default,
  49. &EditorSphereShapeComponent::m_sphereShape,
  50. "Sphere Shape",
  51. "Sphere Shape Configuration")
  52. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorSphereShapeComponent::ConfigurationChanged)
  53. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  54. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  55. ->DataElement(
  56. AZ::Edit::UIHandlers::Default,
  57. &EditorSphereShapeComponent::m_componentModeDelegate,
  58. "Component Mode",
  59. "Sphere Shape Component Mode")
  60. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly);
  61. ;
  62. }
  63. }
  64. }
  65. void EditorSphereShapeComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  66. {
  67. EditorBaseShapeComponent::GetIncompatibleServices(incompatible);
  68. incompatible.push_back(AZ_CRC_CE("NonUniformScaleService"));
  69. }
  70. void EditorSphereShapeComponent::Init()
  71. {
  72. EditorBaseShapeComponent::Init();
  73. SetShapeComponentConfig(&m_sphereShape.ModifyShapeComponent());
  74. }
  75. void EditorSphereShapeComponent::Activate()
  76. {
  77. EditorBaseShapeComponent::Activate();
  78. m_sphereShape.Activate(GetEntityId());
  79. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(GetEntityId());
  80. const AZ::EntityComponentIdPair entityComponentIdPair(GetEntityId(), GetId());
  81. AzToolsFramework::RadiusManipulatorRequestBus::Handler::BusConnect(entityComponentIdPair);
  82. AzToolsFramework::ShapeManipulatorRequestBus::Handler::BusConnect(entityComponentIdPair);
  83. const bool allowAsymmetricalEditing = true;
  84. m_componentModeDelegate.ConnectWithSingleComponentMode<EditorSphereShapeComponent, AzToolsFramework::SphereComponentMode>(
  85. entityComponentIdPair, this, allowAsymmetricalEditing);
  86. }
  87. void EditorSphereShapeComponent::Deactivate()
  88. {
  89. m_componentModeDelegate.Disconnect();
  90. AzToolsFramework::ShapeManipulatorRequestBus::Handler::BusDisconnect();
  91. AzToolsFramework::RadiusManipulatorRequestBus::Handler::BusDisconnect();
  92. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  93. m_sphereShape.Deactivate();
  94. EditorBaseShapeComponent::Deactivate();
  95. }
  96. void EditorSphereShapeComponent::DisplayEntityViewport(
  97. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo,
  98. AzFramework::DebugDisplayRequests& debugDisplay)
  99. {
  100. DisplayShape(
  101. debugDisplay, [this]() { return CanDraw(); },
  102. [this](AzFramework::DebugDisplayRequests& debugDisplay)
  103. {
  104. DrawSphereShape(
  105. { m_sphereShape.GetSphereConfiguration().GetDrawColor(), m_shapeWireColor, m_displayFilled },
  106. m_sphereShape.GetSphereConfiguration(), debugDisplay);
  107. },
  108. m_sphereShape.GetCurrentTransform());
  109. }
  110. void EditorSphereShapeComponent::ConfigurationChanged()
  111. {
  112. m_sphereShape.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
  113. ShapeComponentNotificationsBus::Event(
  114. GetEntityId(), &ShapeComponentNotificationsBus::Events::OnShapeChanged,
  115. ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  116. AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus::Broadcast(
  117. &AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequests::Refresh,
  118. AZ::EntityComponentIdPair(GetEntityId(), GetId()));
  119. }
  120. void EditorSphereShapeComponent::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, [[maybe_unused]] const AZ::Transform&)
  121. {
  122. AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus::Broadcast(
  123. &AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequests::Refresh,
  124. AZ::EntityComponentIdPair(GetEntityId(), GetId()));
  125. }
  126. void EditorSphereShapeComponent::BuildGameEntity(AZ::Entity* gameEntity)
  127. {
  128. if (auto component = gameEntity->CreateComponent<SphereShapeComponent>())
  129. {
  130. component->SetConfiguration(m_sphereShape.GetSphereConfiguration());
  131. }
  132. if (m_visibleInGameView)
  133. {
  134. if (auto component = gameEntity->CreateComponent<SphereShapeDebugDisplayComponent>())
  135. {
  136. component->SetConfiguration(m_sphereShape.GetSphereConfiguration());
  137. }
  138. }
  139. }
  140. float EditorSphereShapeComponent::GetRadius() const
  141. {
  142. return m_sphereShape.GetSphereConfiguration().m_radius;
  143. }
  144. void EditorSphereShapeComponent::SetRadius(float radius)
  145. {
  146. m_sphereShape.SetRadius(radius);
  147. }
  148. AZ::Vector3 EditorSphereShapeComponent::GetTranslationOffset() const
  149. {
  150. return m_sphereShape.GetTranslationOffset();
  151. }
  152. void EditorSphereShapeComponent::SetTranslationOffset(const AZ::Vector3& translationOffset)
  153. {
  154. m_sphereShape.SetTranslationOffset(translationOffset);
  155. }
  156. AZ::Transform EditorSphereShapeComponent::GetManipulatorSpace() const
  157. {
  158. return GetWorldTM();
  159. }
  160. AZ::Quaternion EditorSphereShapeComponent::GetRotationOffset() const
  161. {
  162. return AZ::Quaternion::CreateIdentity();
  163. }
  164. } // namespace LmbrCentral