ROS2FrameGameSystemComponent.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "ROS2FrameGameSystemComponent.h"
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/std/ranges/elements_view.h>
  12. #include <ROS2/Frame/ROS2FrameComponent.h>
  13. #include <ROS2/Frame/ROS2FrameComponentBus.h>
  14. namespace ROS2
  15. {
  16. ROS2FrameGameSystemComponent::ROS2FrameGameSystemComponent()
  17. {
  18. if (ROS2FrameSystemInterface::Get() == nullptr)
  19. {
  20. ROS2FrameSystemInterface::Register(this);
  21. }
  22. if (ROS2FrameTrackingInterface::Get() == nullptr)
  23. {
  24. ROS2FrameTrackingInterface::Register(this);
  25. }
  26. }
  27. ROS2FrameGameSystemComponent::~ROS2FrameGameSystemComponent()
  28. {
  29. if (ROS2FrameSystemInterface::Get() == this)
  30. {
  31. ROS2FrameSystemInterface::Unregister(this);
  32. }
  33. if (ROS2FrameTrackingInterface::Get() == this)
  34. {
  35. ROS2FrameTrackingInterface::Unregister(this);
  36. }
  37. }
  38. void ROS2FrameGameSystemComponent::Reflect(AZ::ReflectContext* context)
  39. {
  40. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  41. {
  42. serialize->Class<ROS2FrameGameSystemComponent, AZ::Component>()->Version(1);
  43. }
  44. if (auto bc = azrtti_cast<AZ::BehaviorContext*>(context))
  45. {
  46. bc->EBus<ROS2FrameTrackingRequestBus>("ROS2FrameTrackingRequestBus")
  47. ->Event("IsFrameRegistered", &ROS2FrameTrackingRequestBus::Events::IsFrameRegistered)
  48. ->Event("GetRegisteredFrameCount", &ROS2FrameTrackingRequestBus::Events::GetRegisteredFrameCount)
  49. ->Event("GetNamespacedFrameId", &ROS2FrameTrackingRequestBus::Events::GetNamespacedFrameId)
  50. ->Event("GetAllNamespacedFrameIds", &ROS2FrameTrackingRequestBus::Events::GetAllNamespacedFrameIds)
  51. ->Event("DisableEntitySafely", &ROS2FrameTrackingRequestBus::Events::DisableEntitySafely)
  52. ->Event("EnableEntitySafely", &ROS2FrameTrackingRequestBus::Events::EnableEntitySafely);
  53. }
  54. }
  55. void ROS2FrameGameSystemComponent::Activate()
  56. {
  57. ROS2FrameTrackingRequestBus::Handler::BusConnect();
  58. }
  59. void ROS2FrameGameSystemComponent::Deactivate()
  60. {
  61. ROS2FrameTrackingRequestBus::Handler::BusDisconnect();
  62. m_cachedChildTransforms.clear();
  63. m_registeredEntities.clear();
  64. m_entityIdToFrameId.clear();
  65. m_frameIdToEntityId.clear();
  66. }
  67. void ROS2FrameGameSystemComponent::RegisterFrame(const AZ::EntityId& frameEntityId)
  68. {
  69. m_registeredEntities.insert(frameEntityId);
  70. AZStd::string namespaceFrameId = "";
  71. ROS2FrameComponentBus::EventResult(namespaceFrameId, frameEntityId, &ROS2FrameComponentBus::Events::GetNamespacedFrameID);
  72. if (!namespaceFrameId.empty())
  73. {
  74. m_frameIdToEntityId[namespaceFrameId] = frameEntityId;
  75. m_entityIdToFrameId[frameEntityId] = namespaceFrameId;
  76. }
  77. }
  78. void ROS2FrameGameSystemComponent::UnregisterFrame(const AZ::EntityId& frameEntityId)
  79. {
  80. m_registeredEntities.erase(frameEntityId);
  81. AZStd::string namespacedFrameId = m_entityIdToFrameId[frameEntityId];
  82. m_frameIdToEntityId.erase(namespacedFrameId);
  83. m_entityIdToFrameId.erase(frameEntityId);
  84. }
  85. const AZStd::unordered_set<AZ::EntityId>& ROS2FrameGameSystemComponent::GetRegisteredFrameEntityIds() const
  86. {
  87. return m_registeredEntities;
  88. }
  89. bool ROS2FrameGameSystemComponent::IsFrameRegistered(const AZ::EntityId& frameEntityId) const
  90. {
  91. return m_registeredEntities.contains(frameEntityId);
  92. }
  93. size_t ROS2FrameGameSystemComponent::GetRegisteredFrameCount() const
  94. {
  95. return m_registeredEntities.size();
  96. }
  97. AZ::EntityId ROS2FrameGameSystemComponent::GetFrameEntityByNamespacedId(const AZStd::string& namespacedFrameId) const
  98. {
  99. auto it = m_frameIdToEntityId.find(namespacedFrameId);
  100. return (it != m_frameIdToEntityId.end()) ? it->second : AZ::EntityId();
  101. }
  102. AZStd::string ROS2FrameGameSystemComponent::GetNamespacedFrameId(const AZ::EntityId& frameEntityId) const
  103. {
  104. auto it = m_entityIdToFrameId.find(frameEntityId);
  105. return (it != m_entityIdToFrameId.end()) ? it->second : "";
  106. }
  107. AZStd::unordered_set<AZStd::string> ROS2FrameGameSystemComponent::GetAllNamespacedFrameIds() const
  108. {
  109. const auto valueView = AZStd::ranges::views::values(m_entityIdToFrameId);
  110. AZStd::unordered_set<AZStd::string> frameIds{ valueView.begin(), valueView.end() };
  111. return frameIds;
  112. }
  113. void ROS2FrameGameSystemComponent::DisableEntitySafely(const AZ::EntityId& entityToDisable)
  114. {
  115. // Disabling an entity is safe, but it cause children to be orphaned in the transform hierarchy.
  116. // That cause issues on reparenting in O3DE.
  117. // In this system component, we will cache local transform of childrens, so when the parent is re-enabled,
  118. AZ::Entity* entity = nullptr;
  119. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityToDisable);
  120. AZ_Warning("ROS2FrameGameSystemComponent", entity, "Entity to disable not found, entity id: %s", entityToDisable.ToString().c_str());
  121. if (!entity)
  122. {
  123. return;
  124. }
  125. const auto children = entity->GetTransform()->GetChildren();
  126. AZStd::vector<EntityIdTransformPair> childTransforms;
  127. for (const auto& childId : children)
  128. {
  129. //get local transform of the child
  130. AZ::Transform childLocalTransform = AZ::Transform::CreateIdentity();
  131. AZ::TransformBus::EventResult(childLocalTransform, childId, &AZ::TransformBus::Events::GetLocalTM);
  132. childTransforms.emplace_back(EntityIdTransformPair{childId, childLocalTransform});
  133. }
  134. m_cachedChildTransforms.emplace(entityToDisable, childTransforms);
  135. entity->Deactivate();
  136. }
  137. void ROS2FrameGameSystemComponent::EnableEntitySafely(const AZ::EntityId& entityToEnable)
  138. {
  139. auto it = m_cachedChildTransforms.find(entityToEnable);
  140. if (it == m_cachedChildTransforms.end())
  141. {
  142. AZ_Warning("ROS2FrameGameSystemComponent", false, "EnableEntitySafely called on entity that was not disabled safely, entity id: %s", entityToEnable.ToString().c_str());
  143. return;
  144. }
  145. AZ::Entity* entity = nullptr;
  146. AZ::ComponentApplicationBus::BroadcastResult(entity, &AZ::ComponentApplicationRequests::FindEntity, entityToEnable);
  147. AZ_Warning("ROS2FrameGameSystemComponent", entity, "Entity to enable not found, entity id: %s", entityToEnable.ToString().c_str());
  148. if (!entity)
  149. {
  150. return;
  151. }
  152. entity->Activate();
  153. //restore local transform of the children
  154. for (const auto& [childId, childLocalTransform] : it->second)
  155. {
  156. AZ::TransformBus::Event(childId, &AZ::TransformBus::Events::SetLocalTM, childLocalTransform);
  157. }
  158. m_cachedChildTransforms.erase(it);
  159. }
  160. } // namespace ROS2