EditorQuadShapeComponent.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "QuadShapeComponent.h"
  9. #include <AzCore/Serialization/EditContext.h>
  10. #include "EditorQuadShapeComponent.h"
  11. #include "EditorShapeComponentConverters.h"
  12. #include "ShapeDisplay.h"
  13. namespace LmbrCentral
  14. {
  15. void EditorQuadShapeComponent::Reflect(AZ::ReflectContext* context)
  16. {
  17. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  18. {
  19. serializeContext->Class<EditorQuadShapeComponent, EditorBaseShapeComponent>()
  20. ->Version(1)
  21. ->Field("QuadShape", &EditorQuadShapeComponent::m_quadShape)
  22. ;
  23. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  24. {
  25. editContext->Class<EditorQuadShapeComponent>(
  26. "Quad Shape", "The Quad Shape component creates a quad around the associated entity")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::Category, "Shape")
  29. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Quad_Shape.svg")
  30. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Component_Placeholder.svg")
  31. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  32. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  33. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/shape/quad-shape/")
  34. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorQuadShapeComponent::m_quadShape, "Quad Shape", "Quad Shape Configuration")
  35. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorQuadShapeComponent::ConfigurationChanged)
  36. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  37. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  38. ;
  39. }
  40. }
  41. }
  42. void EditorQuadShapeComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  43. {
  44. EditorBaseShapeComponent::GetProvidedServices(provided);
  45. provided.push_back(AZ_CRC_CE("QuadShapeService"));
  46. }
  47. void EditorQuadShapeComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  48. {
  49. dependent.push_back(AZ_CRC_CE("NonUniformScaleService"));
  50. }
  51. void EditorQuadShapeComponent::Init()
  52. {
  53. EditorBaseShapeComponent::Init();
  54. SetShapeComponentConfig(&m_quadShape.ModifyShapeComponent());
  55. }
  56. void EditorQuadShapeComponent::Activate()
  57. {
  58. EditorBaseShapeComponent::Activate();
  59. m_quadShape.Activate(GetEntityId());
  60. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(GetEntityId());
  61. }
  62. void EditorQuadShapeComponent::Deactivate()
  63. {
  64. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  65. m_quadShape.Deactivate();
  66. EditorBaseShapeComponent::Deactivate();
  67. }
  68. void EditorQuadShapeComponent::DisplayEntityViewport(
  69. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo,
  70. AzFramework::DebugDisplayRequests& debugDisplay)
  71. {
  72. DisplayShape(
  73. debugDisplay, [this]() { return CanDraw(); },
  74. [this](AzFramework::DebugDisplayRequests& debugDisplay)
  75. {
  76. DrawQuadShape(
  77. { m_quadShape.GetQuadConfiguration().GetDrawColor(), m_shapeWireColor, m_displayFilled },
  78. m_quadShape.GetQuadConfiguration(), debugDisplay, m_quadShape.GetCurrentNonUniformScale());
  79. },
  80. m_quadShape.GetCurrentTransform());
  81. }
  82. void EditorQuadShapeComponent::ConfigurationChanged()
  83. {
  84. m_quadShape.InvalidateCache(InvalidateShapeCacheReason::ShapeChange);
  85. ShapeComponentNotificationsBus::Event(
  86. GetEntityId(), &ShapeComponentNotificationsBus::Events::OnShapeChanged,
  87. ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  88. }
  89. void EditorQuadShapeComponent::BuildGameEntity(AZ::Entity* gameEntity)
  90. {
  91. if (auto component = gameEntity->CreateComponent<QuadShapeComponent>())
  92. {
  93. component->SetConfiguration(m_quadShape.GetQuadConfiguration());
  94. }
  95. if (m_visibleInGameView)
  96. {
  97. if (auto component = gameEntity->CreateComponent<QuadShapeDebugDisplayComponent>())
  98. {
  99. component->SetConfiguration(m_quadShape.GetQuadConfiguration());
  100. }
  101. }
  102. }
  103. } // namespace LmbrCentral