PersistentIdComponent.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <Components/PersistentIdComponent.h>
  9. namespace GraphCanvas
  10. {
  11. // Add implementation of PersistentIdComponent SaveData RTTI functions
  12. AZ_RTTI_NO_TYPE_INFO_IMPL(PersistentIdComponentSaveData, SceneMemberComponentSaveData<PersistentIdComponentSaveData>);
  13. //////////////////////////
  14. // PersistentIdComponent
  15. //////////////////////////
  16. void PersistentIdComponent::Reflect(AZ::ReflectContext* reflectContext)
  17. {
  18. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflectContext);
  19. if (serializeContext)
  20. {
  21. serializeContext->Class<PersistentIdComponentSaveData, ComponentSaveData>()
  22. ->Version(1)
  23. ->Field("PersistentId", &PersistentIdComponentSaveData::m_persistentId)
  24. ;
  25. serializeContext->Class<PersistentIdComponent, AZ::Component>()
  26. ->Version(1)
  27. ->Field("SaveData", &PersistentIdComponent::m_saveData)
  28. ;
  29. }
  30. }
  31. PersistentIdComponent::PersistentIdComponent()
  32. : AZ::Component()
  33. , m_previousId(PersistentGraphMemberId::CreateNull())
  34. {
  35. }
  36. void PersistentIdComponent::Init()
  37. {
  38. m_previousId = PersistentGraphMemberId::CreateNull();
  39. }
  40. void PersistentIdComponent::Activate()
  41. {
  42. SceneMemberNotificationBus::Handler::BusConnect(GetEntityId());
  43. m_saveData.Activate(GetEntityId());
  44. }
  45. void PersistentIdComponent::Deactivate()
  46. {
  47. PersistentIdRequestBus::Handler::BusDisconnect();
  48. PersistentMemberRequestBus::Handler::BusDisconnect();
  49. }
  50. void PersistentIdComponent::OnSceneSet(const AZ::EntityId& graphId)
  51. {
  52. if (!PersistentIdRequestBus::Handler::BusIsConnected())
  53. {
  54. PersistentIdRequestBus::Handler::BusConnect(m_saveData.m_persistentId);
  55. PersistentMemberRequestBus::Handler::BusConnect(GetEntityId());
  56. }
  57. else if (!graphId.IsValid())
  58. {
  59. PersistentIdRequestBus::Handler::BusDisconnect();
  60. PersistentMemberRequestBus::Handler::BusDisconnect();
  61. }
  62. }
  63. void PersistentIdComponent::OnSceneMemberDeserialized(const AZ::EntityId& /*graphId*/, const GraphSerialization& /*serializationTarget*/)
  64. {
  65. m_previousId = m_saveData.m_persistentId;
  66. m_saveData.RemapId();
  67. }
  68. AZ::EntityId PersistentIdComponent::MapToEntityId() const
  69. {
  70. return GetEntityId();
  71. }
  72. PersistentGraphMemberId PersistentIdComponent::GetPreviousGraphMemberId() const
  73. {
  74. return m_previousId;
  75. }
  76. PersistentGraphMemberId PersistentIdComponent::GetPersistentGraphMemberId() const
  77. {
  78. return m_saveData.m_persistentId;
  79. }
  80. }