UiCanvasProxyRefComponent.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "UiCanvasProxyRefComponent.h"
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/RTTI/BehaviorContext.h>
  12. #include <LyShine/Bus/UiCanvasBus.h>
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // PUBLIC MEMBER FUNCTIONS
  15. ////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. UiCanvasProxyRefComponent::UiCanvasProxyRefComponent()
  18. {}
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. AZ::EntityId UiCanvasProxyRefComponent::GetCanvas()
  21. {
  22. AZ::EntityId uiCanvasEntityId;
  23. if (m_canvasAssetRefEntityId.IsValid())
  24. {
  25. UiCanvasRefBus::EventResult(uiCanvasEntityId, m_canvasAssetRefEntityId, &UiCanvasRefBus::Events::GetCanvas);
  26. }
  27. return uiCanvasEntityId;
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. void UiCanvasProxyRefComponent::SetCanvasRefEntity(AZ::EntityId canvasAssetRefEntity)
  31. {
  32. m_canvasAssetRefEntityId = canvasAssetRefEntity;
  33. AZ::EntityId uiCanvasEntityId = GetCanvas();
  34. UiCanvasRefNotificationBus::Event(
  35. GetEntityId(), &UiCanvasRefNotificationBus::Events::OnCanvasRefChanged, GetEntityId(), uiCanvasEntityId);
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////////////////////////
  38. void UiCanvasProxyRefComponent::OnCanvasRefChanged([[maybe_unused]] AZ::EntityId uiCanvasRefEntity, AZ::EntityId uiCanvasEntity)
  39. {
  40. UiCanvasRefNotificationBus::Event(
  41. GetEntityId(), &UiCanvasRefNotificationBus::Events::OnCanvasRefChanged, GetEntityId(), uiCanvasEntity);
  42. }
  43. ////////////////////////////////////////////////////////////////////////////////////////////////////
  44. // PUBLIC STATIC MEMBER FUNCTIONS
  45. ////////////////////////////////////////////////////////////////////////////////////////////////////
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////
  47. void UiCanvasProxyRefComponent::Reflect(AZ::ReflectContext* context)
  48. {
  49. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  50. if (serializeContext)
  51. {
  52. serializeContext->Class<UiCanvasProxyRefComponent, AZ::Component>()
  53. ->Version(1)
  54. ->Field("CanvasAssetRefEntity", &UiCanvasProxyRefComponent::m_canvasAssetRefEntityId);
  55. AZ::EditContext* editContext = serializeContext->GetEditContext();
  56. if (editContext)
  57. {
  58. auto editInfo = editContext->Class<UiCanvasProxyRefComponent>("UI Canvas Proxy Ref", "The UI Canvas Proxy Ref component allows you to associate an entity with another entity that is managing a UI Canvas");
  59. editInfo->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  60. ->Attribute(AZ::Edit::Attributes::Category, "UI")
  61. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/UiCanvasProxyRef.svg")
  62. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/UiCanvasProxyRef.svg")
  63. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/ui/canvas-proxy-ref/")
  64. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game", 0x232b318c));
  65. editInfo->DataElement(0, &UiCanvasProxyRefComponent::m_canvasAssetRefEntityId,
  66. "Canvas Asset Ref entity", "The entity that holds the UI Canvas Asset Ref component.");
  67. }
  68. }
  69. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  70. if (behaviorContext)
  71. {
  72. behaviorContext->EBus<UiCanvasProxyRefBus>("UiCanvasProxyRefBus")
  73. ->Event("SetCanvasRefEntity", &UiCanvasProxyRefBus::Events::SetCanvasRefEntity);
  74. }
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////////////////////////
  77. // PROTECTED MEMBER FUNCTIONS
  78. ////////////////////////////////////////////////////////////////////////////////////////////////////
  79. ////////////////////////////////////////////////////////////////////////////////////////////////////
  80. void UiCanvasProxyRefComponent::Activate()
  81. {
  82. UiCanvasRefBus::Handler::BusConnect(GetEntityId());
  83. UiCanvasProxyRefBus::Handler::BusConnect(GetEntityId());
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////////////////////////
  86. void UiCanvasProxyRefComponent::Deactivate()
  87. {
  88. UiCanvasProxyRefBus::Handler::BusDisconnect();
  89. UiCanvasRefBus::Handler::BusDisconnect();
  90. }