EditorBoxShapeComponent.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <AzCore/Math/Transform.h>
  9. #include <AzCore/RTTI/ReflectContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzToolsFramework/ComponentModes/BoxComponentMode.h>
  12. #include <AzToolsFramework/Maths/TransformUtils.h>
  13. #include "BoxShapeComponent.h"
  14. #include "EditorBoxShapeComponent.h"
  15. #include "EditorShapeComponentConverters.h"
  16. #include "ShapeDisplay.h"
  17. namespace LmbrCentral
  18. {
  19. void EditorBoxShapeComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. // Deprecate: EditorBoxColliderComponent -> EditorBoxShapeComponent
  24. serializeContext->ClassDeprecate(
  25. "EditorBoxColliderComponent",
  26. AZ::Uuid("{E1707478-4F5F-4C28-A31A-EF42B7BD2A68}"),
  27. &ClassConverters::DeprecateEditorBoxColliderComponent)
  28. ;
  29. serializeContext->Class<EditorBoxShapeComponent, EditorBaseShapeComponent>()
  30. ->Version(3, &ClassConverters::UpgradeEditorBoxShapeComponent)
  31. ->Field("BoxShape", &EditorBoxShapeComponent::m_boxShape)
  32. ->Field("ComponentMode", &EditorBoxShapeComponent::m_componentModeDelegate)
  33. ;
  34. if (auto editContext = serializeContext->GetEditContext())
  35. {
  36. editContext->Class<EditorBoxShapeComponent>(
  37. "Box Shape", "The Box Shape component creates a box around the associated entity")
  38. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  39. ->Attribute(AZ::Edit::Attributes::Category, "Shape")
  40. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Box_Shape.svg")
  41. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Box_Shape.svg")
  42. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  43. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  44. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/shape/box-shape/")
  45. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorBoxShapeComponent::m_boxShape, "Box Shape", "Box Shape Configuration")
  46. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  47. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorBoxShapeComponent::ConfigurationChanged)
  48. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorBoxShapeComponent::m_componentModeDelegate, "Component Mode", "Box Shape Component Mode")
  49. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  50. ;
  51. }
  52. }
  53. }
  54. void EditorBoxShapeComponent::Init()
  55. {
  56. EditorBaseShapeComponent::Init();
  57. SetShapeComponentConfig(&m_boxShape.ModifyConfiguration());
  58. }
  59. void EditorBoxShapeComponent::Activate()
  60. {
  61. EditorBaseShapeComponent::Activate();
  62. m_boxShape.Activate(GetEntityId());
  63. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(GetEntityId());
  64. const AZ::EntityComponentIdPair entityComponentIdPair(GetEntityId(), GetId());
  65. AzToolsFramework::BoxManipulatorRequestBus::Handler::BusConnect(entityComponentIdPair);
  66. AzToolsFramework::ShapeManipulatorRequestBus::Handler::BusConnect(entityComponentIdPair);
  67. // ComponentMode
  68. const bool allowAsymmetricalEditing = true;
  69. m_componentModeDelegate.ConnectWithSingleComponentMode<EditorBoxShapeComponent, AzToolsFramework::BoxComponentMode>(
  70. entityComponentIdPair, this, allowAsymmetricalEditing);
  71. }
  72. void EditorBoxShapeComponent::Deactivate()
  73. {
  74. m_componentModeDelegate.Disconnect();
  75. AzToolsFramework::ShapeManipulatorRequestBus::Handler::BusDisconnect();
  76. AzToolsFramework::BoxManipulatorRequestBus::Handler::BusDisconnect();
  77. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  78. m_boxShape.Deactivate();
  79. EditorBaseShapeComponent::Deactivate();
  80. }
  81. void EditorBoxShapeComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  82. {
  83. EditorBaseShapeComponent::GetProvidedServices(provided);
  84. provided.push_back(AZ_CRC_CE("BoxShapeService"));
  85. }
  86. void EditorBoxShapeComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  87. {
  88. dependent.push_back(AZ_CRC_CE("NonUniformScaleService"));
  89. }
  90. void EditorBoxShapeComponent::DisplayEntityViewport(
  91. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo,
  92. AzFramework::DebugDisplayRequests& debugDisplay)
  93. {
  94. DisplayShape(
  95. debugDisplay, [this]() { return CanDraw(); },
  96. [this](AzFramework::DebugDisplayRequests& debugDisplay)
  97. {
  98. DrawBoxShape(
  99. { m_boxShape.GetBoxConfiguration().GetDrawColor(), m_shapeWireColor, m_boxShape.GetBoxConfiguration().IsFilled() },
  100. m_boxShape.GetBoxConfiguration(), debugDisplay, m_boxShape.GetCurrentNonUniformScale());
  101. },
  102. m_boxShape.GetCurrentTransform());
  103. }
  104. void EditorBoxShapeComponent::ConfigurationChanged()
  105. {
  106. m_boxShape.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
  107. ShapeComponentNotificationsBus::Event(GetEntityId(),
  108. &ShapeComponentNotificationsBus::Events::OnShapeChanged,
  109. ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  110. AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus::Broadcast(
  111. &AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequests::Refresh,
  112. AZ::EntityComponentIdPair(GetEntityId(), GetId()));
  113. }
  114. void EditorBoxShapeComponent::BuildGameEntity(AZ::Entity* gameEntity)
  115. {
  116. if (BoxShapeComponent* boxShapeComponent = gameEntity->CreateComponent<BoxShapeComponent>())
  117. {
  118. boxShapeComponent->SetConfiguration(m_boxShape.GetBoxConfiguration());
  119. }
  120. if (m_visibleInGameView)
  121. {
  122. if (auto component = gameEntity->CreateComponent<BoxShapeDebugDisplayComponent>())
  123. {
  124. component->SetConfiguration(m_boxShape.GetBoxConfiguration());
  125. }
  126. }
  127. }
  128. void EditorBoxShapeComponent::OnTransformChanged(
  129. const AZ::Transform& /*local*/, const AZ::Transform& /*world*/)
  130. {
  131. AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequestBus::Broadcast(
  132. &AzToolsFramework::ComponentModeFramework::ComponentModeSystemRequests::Refresh,
  133. AZ::EntityComponentIdPair(GetEntityId(), GetId()));
  134. }
  135. AZ::Vector3 EditorBoxShapeComponent::GetDimensions() const
  136. {
  137. return m_boxShape.GetBoxDimensions();
  138. }
  139. void EditorBoxShapeComponent::SetDimensions(const AZ::Vector3& dimensions)
  140. {
  141. return m_boxShape.SetBoxDimensions(dimensions);
  142. }
  143. AZ::Vector3 EditorBoxShapeComponent::GetTranslationOffset() const
  144. {
  145. return m_boxShape.GetTranslationOffset();
  146. }
  147. void EditorBoxShapeComponent::SetTranslationOffset(const AZ::Vector3& translationOffset)
  148. {
  149. m_boxShape.SetTranslationOffset(translationOffset);
  150. }
  151. AZ::Transform EditorBoxShapeComponent::GetCurrentLocalTransform() const
  152. {
  153. return AZ::Transform::CreateTranslation(m_boxShape.GetTranslationOffset());
  154. }
  155. AZ::Transform EditorBoxShapeComponent::GetManipulatorSpace() const
  156. {
  157. return GetWorldTM();
  158. }
  159. AZ::Quaternion EditorBoxShapeComponent::GetRotationOffset() const
  160. {
  161. return AZ::Quaternion::CreateIdentity();
  162. }
  163. } // namespace LmbrCentral