ROS2FrameEditorComponent.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "ROS2FrameEditorComponent.h"
  9. #include "NamespaceComputation.h"
  10. #include "ROS2FrameSystemBus.h"
  11. #include <AzCore/Component/ComponentApplicationBus.h>
  12. #include <AzCore/Component/Entity.h>
  13. #include <AzCore/Component/EntityBus.h>
  14. #include <AzCore/Component/EntityId.h>
  15. #include <AzCore/Component/EntityUtils.h>
  16. #include <AzCore/Component/TransformBus.h>
  17. #include <AzCore/Serialization/EditContext.h>
  18. #include <AzCore/Serialization/EditContextConstants.inl>
  19. #include <AzCore/Serialization/SerializeContext.h>
  20. #include <AzCore/Settings/SettingsRegistry.h>
  21. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  22. #include <ROS2/Frame/ROS2FrameComponent.h>
  23. #include <ROS2/Frame/ROS2FrameComponentBus.h>
  24. #include <ROS2/Frame/ROS2FrameEditorComponentBus.h>
  25. #include <ROS2/ROS2Bus.h>
  26. #include <ROS2/ROS2NamesBus.h>
  27. namespace ROS2
  28. {
  29. ROS2FrameEditorComponent::ROS2FrameEditorComponent(const ROS2FrameConfiguration ros2FrameConfiguration)
  30. {
  31. m_configuration = ros2FrameConfiguration;
  32. }
  33. void ROS2FrameEditorComponent::Init()
  34. {
  35. }
  36. void ROS2FrameEditorComponent::Activate()
  37. {
  38. ROS2FrameEditorComponentBus::Handler::BusConnect(GetEntityId());
  39. AZ::EntityBus::Handler::BusConnect(GetEntityId());
  40. if (auto* frameSystemInterface = ROS2FrameSystemInterface::Get())
  41. {
  42. frameSystemInterface->RegisterFrame(GetEntityId());
  43. }
  44. UpdateNamespace();
  45. ROS2FrameComponentBus::Handler::BusConnect(GetEntityId());
  46. }
  47. void ROS2FrameEditorComponent::Deactivate()
  48. {
  49. ROS2FrameComponentBus::Handler::BusDisconnect();
  50. if (auto* frameSystemInterface = ROS2FrameSystemInterface::Get())
  51. {
  52. frameSystemInterface->UnregisterFrame(GetEntityId());
  53. }
  54. AZ::EntityBus::Handler::BusDisconnect();
  55. ROS2FrameEditorComponentBus::Handler::BusDisconnect();
  56. }
  57. AZStd::string ROS2FrameEditorComponent::GetGlobalFrameID() const
  58. {
  59. const AZStd::string odometryFrame = GetGlobalFrameIDFromRegistry();
  60. const auto computedNamespace = ComputeNamespace(GetEntityId());
  61. return GetNamespacedName(computedNamespace, odometryFrame);
  62. }
  63. AZStd::string ROS2FrameEditorComponent::GetNamespacedFrameID() const
  64. {
  65. auto computedNamespace = ComputeNamespace(GetEntityId());
  66. return GetNamespacedName(computedNamespace, m_configuration.m_frameName);
  67. }
  68. AZStd::string ROS2FrameEditorComponent::GetNamespace() const
  69. {
  70. return ComputeNamespace(GetEntityId());
  71. }
  72. void ROS2FrameEditorComponent::UpdateNamespace()
  73. {
  74. m_effectiveNamespace = ComputeNamespace(GetEntityId());
  75. m_fullName = GetNamespacedName(m_effectiveNamespace, m_configuration.m_frameName);
  76. AzToolsFramework::PropertyEditorEntityChangeNotificationBus::Event(
  77. GetEntityId(), &AzToolsFramework::PropertyEditorEntityChangeNotificationBus::Events::OnEntityComponentPropertyChanged, GetId());
  78. }
  79. AZStd::string ROS2FrameEditorComponent::GetNamespacedJointName() const
  80. {
  81. auto computedNamespace = ComputeNamespace(GetEntityId());
  82. return GetNamespacedName(computedNamespace, m_configuration.m_jointName);
  83. }
  84. void ROS2FrameEditorComponent::SetJointName(const AZStd::string& jointName)
  85. {
  86. m_configuration.m_jointName = jointName;
  87. }
  88. void ROS2FrameEditorComponent::Reflect(AZ::ReflectContext* context)
  89. {
  90. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  91. {
  92. serialize->Class<ROS2FrameEditorComponent, AzToolsFramework::Components::EditorComponentBase>()->Version(1)->Field(
  93. "ROS2FrameConfiguration", &ROS2FrameEditorComponent::m_configuration);
  94. if (AZ::EditContext* ec = serialize->GetEditContext())
  95. {
  96. ec->Class<ROS2FrameEditorComponent>("ROS2 Frame", "[ROS2 Frame component]")
  97. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  98. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  99. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Level"))
  100. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  101. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/ROS2Frame.svg")
  102. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/ROS2Frame.svg")
  103. ->DataElement(
  104. AZ::Edit::UIHandlers::Default,
  105. &ROS2FrameEditorComponent::m_configuration,
  106. "ROS 2 Frame Configuration",
  107. "Configuration of ROS 2 reference frame")
  108. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  109. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &ROS2FrameEditorComponent::OnFrameConfigurationChange)
  110. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  111. ->ClassElement(AZ::Edit::ClassElements::Group, "Info")
  112. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  113. ->UIElement(AZ::Edit::UIHandlers::Label, "Effective namespace", "")
  114. ->Attribute(AZ::Edit::Attributes::ValueText, &ROS2FrameEditorComponent::m_effectiveNamespace)
  115. ->UIElement(AZ::Edit::UIHandlers::Label, "Full name", "")
  116. ->Attribute(AZ::Edit::Attributes::ValueText, &ROS2FrameEditorComponent::m_fullName);
  117. }
  118. }
  119. }
  120. AZ::EntityId ROS2FrameEditorComponent::GetFrameParent() const
  121. {
  122. const auto ancestors = GetAllAncestorTransformBus(GetEntityId());
  123. return GetFirstEntityWithROS2FrameComponent(ancestors);
  124. }
  125. AZStd::set<AZ::EntityId> ROS2FrameEditorComponent::GetFrameDescendants() const
  126. {
  127. // get all descendants
  128. AZStd::vector<AZ::EntityId> children;
  129. AZ::TransformBus::EventResult(children, GetEntityId(), &AZ::TransformBus::Events::GetAllDescendants);
  130. // filter only those with ROS2FrameComponent
  131. const auto ros2Children = GetEntitiesWithROS2FrameComponent(children);
  132. return AZStd::set<AZ::EntityId>(ros2Children.begin(), ros2Children.end());
  133. }
  134. AZ::Crc32 ROS2FrameEditorComponent::OnFrameConfigurationChange()
  135. {
  136. m_effectiveNamespace = ComputeNamespace(GetEntityId());
  137. m_fullName = GetNamespacedName(m_effectiveNamespace, m_configuration.m_frameName);
  138. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  139. }
  140. void ROS2FrameEditorComponent::OnEntityNameChanged(const AZStd::string& name)
  141. {
  142. OnFrameConfigurationChange();
  143. }
  144. void ROS2FrameEditorComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  145. {
  146. provided.push_back(AZ_CRC_CE("ROS2Frame"));
  147. }
  148. void ROS2FrameEditorComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  149. {
  150. incompatible.push_back(AZ_CRC_CE("ROS2Frame"));
  151. }
  152. void ROS2FrameEditorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  153. {
  154. required.push_back(AZ_CRC_CE("TransformService"));
  155. }
  156. void ROS2FrameEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
  157. {
  158. gameEntity->CreateComponent<ROS2FrameComponent>(m_configuration);
  159. }
  160. ROS2FrameConfiguration ROS2FrameEditorComponent::GetConfiguration() const
  161. {
  162. return m_configuration;
  163. }
  164. void ROS2FrameEditorComponent::SetConfiguration(const ROS2FrameConfiguration& config)
  165. {
  166. AZ_Assert(GetEntity()->GetState() != AZ::Entity::State::Active, "API can be called only for disabled components");
  167. m_configuration = config;
  168. }
  169. AZStd::string ROS2FrameEditorComponent::GetJointName() const
  170. {
  171. return m_configuration.m_jointName;
  172. }
  173. AZStd::string ROS2FrameEditorComponent::GetFrameName() const
  174. {
  175. return m_configuration.m_frameName;
  176. }
  177. } // namespace ROS2