SceneMemberComponent.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/Serialization/SerializeContext.h>
  9. #include <Components/SceneMemberComponent.h>
  10. #include <Components/PersistentIdComponent.h>
  11. namespace GraphCanvas
  12. {
  13. /////////////////////////
  14. // SceneMemberComponent
  15. /////////////////////////
  16. void SceneMemberComponent::Reflect(AZ::ReflectContext* reflectContext)
  17. {
  18. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  19. if (serializeContext)
  20. {
  21. serializeContext->Class<SceneMemberComponent, AZ::Component>()
  22. ->Version(1)
  23. ->Field("IsGroupable", &SceneMemberComponent::m_isGroupable)
  24. ;
  25. }
  26. }
  27. SceneMemberComponent::SceneMemberComponent()
  28. : m_isGroupable(false)
  29. {
  30. }
  31. SceneMemberComponent::SceneMemberComponent(bool isGroupable)
  32. : m_isGroupable(isGroupable)
  33. {
  34. }
  35. void SceneMemberComponent::Init()
  36. {
  37. AZ::EntityBus::Handler::BusConnect(GetEntityId());
  38. }
  39. void SceneMemberComponent::Activate()
  40. {
  41. SceneMemberRequestBus::Handler::BusConnect(GetEntityId());
  42. if (m_isGroupable)
  43. {
  44. GroupableSceneMemberRequestBus::Handler::BusConnect(GetEntityId());
  45. }
  46. }
  47. void SceneMemberComponent::Deactivate()
  48. {
  49. GroupableSceneMemberRequestBus::Handler::BusDisconnect();
  50. SceneMemberRequestBus::Handler::BusDisconnect();
  51. AZ::EntityBus::Handler::BusDisconnect();
  52. }
  53. void SceneMemberComponent::SetScene(const AZ::EntityId& sceneId)
  54. {
  55. if (m_sceneId != sceneId)
  56. {
  57. AZ_Warning("Graph Canvas", !m_sceneId.IsValid(), "Trying to change a SceneMember's scene without removing it from the previous scene.");
  58. if (m_sceneId.IsValid())
  59. {
  60. ClearScene(m_sceneId);
  61. }
  62. m_sceneId = sceneId;
  63. SceneMemberNotificationBus::Event(GetEntityId(), &SceneMemberNotifications::OnSceneSet, sceneId);
  64. }
  65. }
  66. void SceneMemberComponent::ClearScene(const AZ::EntityId& sceneId)
  67. {
  68. AZ_Warning("Graph Canvas", m_sceneId == sceneId, "Trying to remove a SceneMember from a scene it is not a part of.");
  69. if (m_sceneId == sceneId)
  70. {
  71. SceneMemberNotificationBus::Event(GetEntityId(), &SceneMemberNotifications::OnRemovedFromScene, sceneId);
  72. m_sceneId.SetInvalid();
  73. }
  74. }
  75. void SceneMemberComponent::SignalMemberSetupComplete()
  76. {
  77. SceneMemberNotificationBus::Event(GetEntityId(), &SceneMemberNotifications::OnMemberSetupComplete);
  78. }
  79. AZ::EntityId SceneMemberComponent::GetScene() const
  80. {
  81. return m_sceneId;
  82. }
  83. void SceneMemberComponent::OnEntityExists(const AZ::EntityId& /*entityId*/)
  84. {
  85. AZ::EntityBus::Handler::BusDisconnect();
  86. // Temporary version conversion added in 1.xx to add a PersistentId onto the SceneMembers.
  87. // Remove after a few revisions with warnings about resaving graphs.
  88. if (AZ::EntityUtils::FindFirstDerivedComponent<PersistentIdComponent>(GetEntityId()) == nullptr)
  89. {
  90. AZ::Entity* selfEntity = GetEntity();
  91. if (selfEntity)
  92. {
  93. selfEntity->CreateComponent<PersistentIdComponent>();
  94. }
  95. }
  96. }
  97. bool SceneMemberComponent::IsGrouped() const
  98. {
  99. return m_groupId.IsValid();
  100. }
  101. const AZ::EntityId& SceneMemberComponent::GetGroupId() const
  102. {
  103. return m_groupId;
  104. }
  105. void SceneMemberComponent::RegisterToGroup(const AZ::EntityId& groupId)
  106. {
  107. if (m_groupId.IsValid())
  108. {
  109. AZ_Assert(false, "Trying to register an element to two groups at the same time.");
  110. }
  111. else
  112. {
  113. m_groupId = groupId;
  114. GroupableSceneMemberNotificationBus::Event(GetEntityId(), &GroupableSceneMemberNotifications::OnGroupChanged);
  115. }
  116. }
  117. void SceneMemberComponent::UnregisterFromGroup(const AZ::EntityId& groupId)
  118. {
  119. if (m_groupId == groupId)
  120. {
  121. m_groupId.SetInvalid();
  122. GroupableSceneMemberNotificationBus::Event(GetEntityId(), &GroupableSceneMemberNotifications::OnGroupChanged);
  123. }
  124. }
  125. void SceneMemberComponent::RemoveFromGroup()
  126. {
  127. if (m_groupId.IsValid())
  128. {
  129. NodeGroupRequestBus::Event(m_groupId, &NodeGroupRequests::RemoveElementFromGroup, GetEntityId());
  130. }
  131. }
  132. }