EditorPolygonPrismShapeComponent.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "EditorPolygonPrismShapeComponent.h"
  9. #include "EditorPolygonPrismShapeComponentMode.h"
  10. #include "PolygonPrismShapeComponent.h"
  11. #include <AzCore/RTTI/ReflectContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzToolsFramework/Entity/EditorEntityInfoBus.h>
  15. #include <AzToolsFramework/Viewport/VertexContainerDisplay.h>
  16. #include "EditorShapeComponentConverters.h"
  17. #include "ShapeDisplay.h"
  18. namespace LmbrCentral
  19. {
  20. void EditorPolygonPrismShapeComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  21. {
  22. EditorBaseShapeComponent::GetProvidedServices(provided);
  23. provided.push_back(AZ_CRC_CE("PolygonPrismShapeService"));
  24. provided.push_back(AZ_CRC_CE("VariableVertexContainerService"));
  25. provided.push_back(AZ_CRC_CE("FixedVertexContainerService"));
  26. }
  27. void EditorPolygonPrismShapeComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  28. {
  29. EditorBaseShapeComponent::GetIncompatibleServices(incompatible);
  30. incompatible.push_back(AZ_CRC_CE("VariableVertexContainerService"));
  31. incompatible.push_back(AZ_CRC_CE("FixedVertexContainerService"));
  32. }
  33. void EditorPolygonPrismShapeComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  34. {
  35. dependent.push_back(AZ_CRC_CE("NonUniformScaleService"));
  36. }
  37. void EditorPolygonPrismShapeComponent::Init()
  38. {
  39. EditorBaseShapeComponent::Init();
  40. SetShapeComponentConfig(&m_polygonShapeConfig);
  41. }
  42. void EditorPolygonPrismShapeComponent::Activate()
  43. {
  44. EditorBaseShapeComponent::Activate();
  45. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(GetEntityId());
  46. EditorPolygonPrismShapeComponentRequestsBus::Handler::BusConnect(GetEntityId());
  47. m_polygonPrismShape.Activate(GetEntityId());
  48. // placeholder - create initial polygon prism shape if empty
  49. AZ::VertexContainer<AZ::Vector2>& vertexContainer = m_polygonPrismShape.GetPolygonPrism()->m_vertexContainer;
  50. if (vertexContainer.Empty())
  51. {
  52. vertexContainer.AddVertex(AZ::Vector2(-2.0f, -2.0f));
  53. vertexContainer.AddVertex(AZ::Vector2(2.0f, -2.0f));
  54. vertexContainer.AddVertex(AZ::Vector2(2.0f, 2.0f));
  55. vertexContainer.AddVertex(AZ::Vector2(-2.0f, 2.0f));
  56. }
  57. const auto shapeModified = [this]()
  58. {
  59. GenerateVertices();
  60. m_polygonPrismShape.ShapeChanged();
  61. };
  62. const auto vertexAdded = [this, shapeModified](size_t index)
  63. {
  64. PolygonPrismShapeComponentNotificationBus::Event(
  65. GetEntityId(), &PolygonPrismShapeComponentNotification::OnVertexAdded, index);
  66. shapeModified();
  67. };
  68. const auto vertexRemoved = [this, shapeModified](size_t index)
  69. {
  70. PolygonPrismShapeComponentNotificationBus::Event(
  71. GetEntityId(), &PolygonPrismShapeComponentNotification::OnVertexRemoved, index);
  72. shapeModified();
  73. };
  74. const auto vertexUpdated = [this, shapeModified](size_t index)
  75. {
  76. PolygonPrismShapeComponentNotificationBus::Event(
  77. GetEntityId(), &PolygonPrismShapeComponentNotification::OnVertexUpdated, index);
  78. shapeModified();
  79. };
  80. const auto verticesSet = [this, shapeModified]()
  81. {
  82. PolygonPrismShapeComponentNotificationBus::Event(
  83. GetEntityId(), &PolygonPrismShapeComponentNotification::OnVerticesSet,
  84. m_polygonPrismShape.GetPolygonPrism()->m_vertexContainer.GetVertices());
  85. shapeModified();
  86. };
  87. const auto verticesCleared = [this, shapeModified]()
  88. {
  89. PolygonPrismShapeComponentNotificationBus::Event(
  90. GetEntityId(), &PolygonPrismShapeComponentNotification::OnVerticesCleared);
  91. shapeModified();
  92. };
  93. m_polygonPrismShape.GetPolygonPrism()->SetCallbacks(
  94. vertexAdded,
  95. vertexRemoved,
  96. vertexUpdated,
  97. verticesSet,
  98. verticesCleared,
  99. shapeModified,
  100. shapeModified);
  101. GenerateVertices();
  102. // ComponentMode
  103. m_componentModeDelegate.ConnectWithSingleComponentMode<
  104. EditorPolygonPrismShapeComponent, EditorPolygonPrismShapeComponentMode>(
  105. AZ::EntityComponentIdPair(GetEntityId(), GetId()), this);
  106. }
  107. void EditorPolygonPrismShapeComponent::Deactivate()
  108. {
  109. EditorBaseShapeComponent::Deactivate();
  110. m_componentModeDelegate.Disconnect();
  111. EditorPolygonPrismShapeComponentRequestsBus::Handler::BusDisconnect();
  112. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  113. m_polygonPrismShape.Deactivate();
  114. }
  115. void EditorPolygonPrismShapeComponent::Reflect(AZ::ReflectContext* context)
  116. {
  117. EditorPolygonPrismShapeComponentMode::Reflect(context);
  118. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  119. {
  120. serializeContext->Class<EditorPolygonPrismShapeComponent, EditorBaseShapeComponent>()
  121. ->Version(3, &ClassConverters::UpgradeEditorPolygonPrismShapeComponent)
  122. ->Field("Configuration", &EditorPolygonPrismShapeComponent::m_polygonPrismShape)
  123. ->Field("ShapeConfiguration", &EditorPolygonPrismShapeComponent::m_polygonShapeConfig)
  124. ->Field("ComponentMode", &EditorPolygonPrismShapeComponent::m_componentModeDelegate)
  125. ;
  126. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  127. {
  128. editContext->Class<EditorPolygonPrismShapeComponent>(
  129. "Polygon Prism Shape", "Provides polygon prism shape")
  130. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  131. ->Attribute(AZ::Edit::Attributes::Category, "Shape")
  132. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/PolygonPrism.svg")
  133. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/PolygonPrism.svg")
  134. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  135. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/shape/polygon-prism-shape/")
  136. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  137. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorPolygonPrismShapeComponent::m_polygonPrismShape, "Configuration", "PolygonPrism Shape Configuration")
  138. // ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly) // disabled - prevents ChangeNotify attribute firing correctly
  139. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorPolygonPrismShapeComponent::m_componentModeDelegate, "Component Mode", "PolygonPrism Component Mode")
  140. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  141. ;
  142. }
  143. }
  144. }
  145. void EditorPolygonPrismShapeComponent::DisplayEntityViewport(
  146. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo,
  147. AzFramework::DebugDisplayRequests& debugDisplay)
  148. {
  149. DisplayShape(
  150. debugDisplay, [this]() { return CanDraw(); },
  151. [this](AzFramework::DebugDisplayRequests& debugDisplay)
  152. {
  153. DrawPolygonPrismShape(
  154. { m_polygonShapeConfig.GetDrawColor(), m_shapeWireColor, m_displayFilled },
  155. m_polygonPrismMesh, debugDisplay);
  156. debugDisplay.SetColor(m_shapeWireColor);
  157. if (m_componentModeDelegate.AddedToComponentMode())
  158. {
  159. AzToolsFramework::VertexContainerDisplay::DisplayVertexContainerIndices(
  160. debugDisplay, AzToolsFramework::VariableVerticesVertexContainer<AZ::Vector2>(
  161. m_polygonPrismShape.GetPolygonPrism()->m_vertexContainer),
  162. m_polygonPrismShape.GetCurrentTransform(),
  163. m_polygonPrismShape.GetCurrentNonUniformScale(),
  164. IsSelected());
  165. }
  166. },
  167. m_polygonPrismShape.GetCurrentTransform());
  168. }
  169. void EditorPolygonPrismShapeComponent::BuildGameEntity(AZ::Entity* gameEntity)
  170. {
  171. if (auto component = gameEntity->CreateComponent<PolygonPrismShapeComponent>())
  172. {
  173. const bool isActive = GetEntity() ? (GetEntity()->GetState() == AZ::Entity::State::Active) : false;
  174. if (isActive)
  175. {
  176. m_polygonPrismShape.Deactivate();
  177. }
  178. component->m_polygonPrismShape = m_polygonPrismShape;
  179. if (isActive)
  180. {
  181. m_polygonPrismShape.Activate(GetEntityId());
  182. }
  183. }
  184. if (m_visibleInGameView)
  185. {
  186. PolygonPrismShapeDebugDisplayComponent* debugDisplayComponent = gameEntity->CreateComponent<PolygonPrismShapeDebugDisplayComponent>(*m_polygonPrismShape.GetPolygonPrism());
  187. debugDisplayComponent->SetShapeConfig(m_polygonShapeConfig);
  188. }
  189. }
  190. void EditorPolygonPrismShapeComponent::GenerateVertices()
  191. {
  192. GeneratePolygonPrismMesh(
  193. m_polygonPrismShape.GetPolygonPrism()->m_vertexContainer.GetVertices(),
  194. m_polygonPrismShape.GetPolygonPrism()->GetHeight(), m_polygonPrismShape.GetCurrentNonUniformScale(), m_polygonPrismMesh);
  195. }
  196. } // namespace LmbrCentral