ROS2FrameSystemComponent.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #pragma once
  9. #include "ROS2FrameSystemBus.h"
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Component/EntityId.h>
  13. #include <AzCore/Component/TransformBus.h>
  14. #include <AzCore/Math/Transform.h>
  15. #include <AzCore/RTTI/RTTIMacros.h>
  16. #include <AzCore/std/containers/map.h>
  17. #include <AzCore/std/containers/vector.h>
  18. #include <AzCore/std/string/string.h>
  19. namespace ROS2
  20. {
  21. //! Handler class for the ROS2FrameEditorComponent. It watches for changes in the entity tree and notifies about moves.
  22. //! Used by the ROS2FrameSystemComponent, to track changes in the entity tree. It notifies the ROS2FrameSystemComponent
  23. //! and calls the appropriate functions to update the changes.
  24. class ROS2FrameSystemTransformHandler : public AZ::TransformNotificationBus::Handler
  25. {
  26. public:
  27. AZ_RTTI(ROS2FrameSystemTransformHandler, "{0b4324bb-4c36-4a86-8827-a044a6ac44e8}");
  28. //! Override of the AZ::TransformNotificationBus.
  29. void OnParentChanged(AZ::EntityId oldParent, AZ::EntityId newParent) override;
  30. //! Add a frame entity which should be notified about a change in the tree.
  31. //! @param frameEntityId frame to notify.
  32. void AddFrameEntity(AZ::EntityId frameEntityId);
  33. //! Remove a frame entity which shouldn't be notified about a change in the tree.
  34. //! @param frameEntityId frame to remove.
  35. void RemoveFrameEntity(AZ::EntityId frameEntityId);
  36. //! Get the number of frame entities which will be notified about a change in the tree.
  37. //! @return size of the m_frameEntities.
  38. unsigned int GetFrameCount();
  39. private:
  40. AZStd::set<AZ::EntityId> m_frameEntities;
  41. };
  42. //! Component which manages the frame entities and their hierarchy.
  43. //! It is responsible for updating the namespaces of the frame entities and their children.
  44. //! It also notifies the ROS2FrameEditorComponent about changes in the tree.
  45. //! Used to register, unregister, track the frame entities in the level entity tree.
  46. class ROS2FrameSystemComponent
  47. : public AZ::Component
  48. , public ROS2FrameSystemInterface::Registrar
  49. {
  50. public:
  51. AZ_COMPONENT(ROS2FrameSystemComponent, "{360c4b45-ac02-42d2-9e1a-1d77eb22a054}");
  52. static void Reflect(AZ::ReflectContext* context);
  53. // AZ::Component overrides.
  54. void Activate() override;
  55. void Deactivate() override;
  56. // ROS2FrameSystemInterface::Registrar overrides.
  57. void RegisterFrame(const AZ::EntityId& frameEntityId) override;
  58. void UnregisterFrame(const AZ::EntityId& frameEntityId) override;
  59. void MoveFrame(const AZ::EntityId& frameEntityId, const AZ::EntityId& newParent) override;
  60. void NotifyChange(const AZ::EntityId& frameEntityId) override;
  61. bool IsTopLevel(const AZ::EntityId& frameEntityId) const override;
  62. AZ::EntityId GetParentEntityId(const AZ::EntityId& frameEntityId) const override;
  63. AZStd::set<AZ::EntityId> GetChildrenEntityId(const AZ::EntityId& frameEntityId) const override;
  64. ROS2FrameSystemComponent();
  65. ~ROS2FrameSystemComponent();
  66. private:
  67. //! Find the path from the frameEntity to the frame parent of that entity.
  68. //! This path will include the frameEntity and the frame parent.
  69. //! If there is no frame parent, path to the root entity (included) will be returned.
  70. //! @param frameEntityId frame to find the path to the parent.
  71. //! @return vector of entityIds which represent the path to the parent. frameEntityId is first, parent is last.
  72. AZStd::vector<AZ::EntityId> FindFrameParentPath(AZ::EntityId frameEntityId);
  73. AZ::TransformInterface* GetEntityTransformInterface(const AZ::Entity* entity);
  74. //! Updates the namespaces of all children of the frameEntity.
  75. //! @param frameEntity frame to be updated.
  76. //! @param parentNamespace namespace of the parent frame. Empty if no parent is present.
  77. //! @param isActive boolean value describing if the frameEntity is currently active.
  78. void UpdateNamespaces(AZ::EntityId frameEntity, AZStd::string parentNamespace = "", bool isActive = true);
  79. //! Updates the namespaces of all children of the frameEntity.
  80. //! @param frameEntity frame to be updated.
  81. //! @param frameParentEntity entityId of the parent frame.
  82. //! @param isActive boolean value describing if the frameEntity is currently active.
  83. void UpdateNamespaces(AZ::EntityId frameEntity, AZ::EntityId frameParentEntity, bool isActive = true);
  84. void MoveFrameDetach(const AZ::EntityId& frameEntityId, const AZStd::set<AZ::EntityId>& newPathToParentFrameSet);
  85. void MoveFrameAttach(
  86. const AZ::EntityId& frameEntityId, const AZ::EntityId& newFrameParent, const AZStd::vector<AZ::EntityId>& newPathToParentFrame);
  87. AZStd::vector<AZ::EntityId> GetAllPredecessors(const AZ::EntityId& frameEntityId) const;
  88. AZStd::vector<AZ::EntityId> GetAllSuccessors(const AZ::EntityId& frameEntityId) const;
  89. AZStd::map<AZ::EntityId, AZStd::set<AZ::EntityId>> m_frameChildren;
  90. AZStd::map<AZ::EntityId, AZ::EntityId> m_frameParent;
  91. AZStd::map<AZ::EntityId, AZStd::set<AZ::EntityId>> m_watchedEntities;
  92. AZStd::map<AZ::EntityId, ROS2FrameSystemTransformHandler> m_watchedEntitiesHandlers;
  93. //! Check to prevent multiple conversions at the same time.
  94. bool m_conversionNeeded = false;
  95. };
  96. } // namespace ROS2