ROS2FrameEditorComponent.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "ROS2FrameSystemBus.h"
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzCore/Component/EntityBus.h>
  12. #include <AzCore/Component/EntityId.h>
  13. #include <AzCore/Component/EntityUtils.h>
  14. #include <AzCore/Serialization/EditContext.h>
  15. #include <AzCore/Serialization/EditContextConstants.inl>
  16. #include <AzCore/Serialization/SerializeContext.h>
  17. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  18. #include <ROS2/Frame/ROS2FrameComponent.h>
  19. #include <ROS2/Frame/ROS2FrameEditorComponent.h>
  20. #include <ROS2/Frame/ROS2FrameEditorComponentBus.h>
  21. #include <ROS2/ROS2Bus.h>
  22. #include <ROS2/Utilities/ROS2Names.h>
  23. namespace ROS2
  24. {
  25. ROS2FrameEditorComponent::ROS2FrameEditorComponent(const ROS2FrameConfiguration ros2FrameConfiguration)
  26. {
  27. m_configuration = ros2FrameConfiguration;
  28. }
  29. void ROS2FrameEditorComponent::Init()
  30. {
  31. m_configuration.m_namespaceConfiguration.Init();
  32. }
  33. void ROS2FrameEditorComponent::Activate()
  34. {
  35. ROS2FrameEditorComponentBus::Handler::BusConnect(GetEntityId());
  36. AZ::EntityBus::Handler::BusConnect(GetEntityId());
  37. if (auto* frameSystemInterface = ROS2FrameSystemInterface::Get())
  38. {
  39. frameSystemInterface->RegisterFrame(GetEntityId());
  40. }
  41. }
  42. void ROS2FrameEditorComponent::Deactivate()
  43. {
  44. if (auto* frameSystemInterface = ROS2FrameSystemInterface::Get())
  45. {
  46. frameSystemInterface->UnregisterFrame(GetEntityId());
  47. }
  48. AZ::EntityBus::Handler::BusDisconnect();
  49. ROS2FrameEditorComponentBus::Handler::BusDisconnect();
  50. }
  51. AZStd::string ROS2FrameEditorComponent::GetGlobalFrameName() const
  52. {
  53. return ROS2Names::GetNamespacedName(GetNamespace(), AZStd::string("odom"));
  54. }
  55. bool ROS2FrameEditorComponent::IsTopLevel() const
  56. {
  57. return ROS2FrameSystemInterface::Get()->IsTopLevel(GetEntityId());
  58. }
  59. AZStd::string ROS2FrameEditorComponent::GetFrameID() const
  60. {
  61. return ROS2Names::GetNamespacedName(GetNamespace(), m_configuration.m_frameName);
  62. }
  63. void ROS2FrameEditorComponent::SetFrameID(const AZStd::string& frameId)
  64. {
  65. m_configuration.m_frameName = frameId;
  66. }
  67. AZStd::string ROS2FrameEditorComponent::GetNamespace() const
  68. {
  69. return m_configuration.m_namespaceConfiguration.GetNamespace();
  70. }
  71. void ROS2FrameEditorComponent::UpdateNamespace(const AZStd::string& parentNamespace)
  72. {
  73. m_configuration.m_namespaceConfiguration.SetParentNamespace(parentNamespace);
  74. m_configuration.m_namespaceConfiguration.PopulateNamespace(IsTopLevel(), GetEntity()->GetName());
  75. m_configuration.SetEffectiveNamespace(GetNamespace());
  76. AzToolsFramework::PropertyEditorEntityChangeNotificationBus::Event(
  77. GetEntityId(),
  78. &AzToolsFramework::PropertyEditorEntityChangeNotificationBus::Events::OnEntityComponentPropertyChanged,
  79. GetEntity()->FindComponent<ROS2FrameEditorComponent>()->GetId());
  80. ROS2FrameEditorComponentNotificationBus::Event(
  81. GetEntityId(), &ROS2FrameEditorComponentNotificationBus::Events::OnConfigurationChange);
  82. }
  83. void ROS2FrameEditorComponent::UpdateNamespaceConfiguration(
  84. const AZStd::string& ros2Namespace, const NamespaceConfiguration::NamespaceStrategy& strategy)
  85. {
  86. m_configuration.m_namespaceConfiguration.SetNamespace(ros2Namespace, strategy);
  87. }
  88. AZ::Name ROS2FrameEditorComponent::GetJointName() const
  89. {
  90. return AZ::Name(ROS2Names::GetNamespacedName(GetNamespace(), m_configuration.m_jointName).c_str());
  91. }
  92. void ROS2FrameEditorComponent::SetJointName(const AZStd::string& jointName)
  93. {
  94. m_configuration.m_jointName = jointName;
  95. }
  96. void ROS2FrameEditorComponent::Reflect(AZ::ReflectContext* context)
  97. {
  98. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  99. {
  100. serialize->Class<ROS2FrameEditorComponent, AzToolsFramework::Components::EditorComponentBase>()->Version(1)->Field(
  101. "ROS2FrameConfiguration", &ROS2FrameEditorComponent::m_configuration);
  102. if (AZ::EditContext* ec = serialize->GetEditContext())
  103. {
  104. ec->Class<ROS2FrameEditorComponent>("ROS2 Frame", "[ROS2 Frame component]")
  105. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  106. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  107. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Level"))
  108. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  109. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/ROS2Frame.svg")
  110. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/ROS2Frame.svg")
  111. ->DataElement(
  112. AZ::Edit::UIHandlers::Default,
  113. &ROS2FrameEditorComponent::m_configuration,
  114. "ROS 2 Frame Configuration",
  115. "Configuration of ROS 2 reference frame")
  116. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  117. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &ROS2FrameEditorComponent::OnFrameConfigurationChange)
  118. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues);
  119. }
  120. }
  121. }
  122. AZ::EntityId ROS2FrameEditorComponent::GetFrameParent() const
  123. {
  124. return ROS2FrameSystemInterface::Get()->GetParentEntityId(GetEntityId());
  125. }
  126. AZStd::set<AZ::EntityId> ROS2FrameEditorComponent::GetFrameChildren() const
  127. {
  128. return ROS2FrameSystemInterface::Get()->GetChildrenEntityId(GetEntityId());
  129. }
  130. AZ::Crc32 ROS2FrameEditorComponent::OnFrameConfigurationChange()
  131. {
  132. ROS2FrameSystemInterface::Get()->NotifyChange(GetEntityId());
  133. return AZ::Edit::PropertyRefreshLevels::EntireTree;
  134. }
  135. void ROS2FrameEditorComponent::OnEntityNameChanged(const AZStd::string& name)
  136. {
  137. OnFrameConfigurationChange();
  138. }
  139. void ROS2FrameEditorComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  140. {
  141. provided.push_back(AZ_CRC_CE("ROS2Frame"));
  142. }
  143. void ROS2FrameEditorComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  144. {
  145. incompatible.push_back(AZ_CRC_CE("ROS2Frame"));
  146. }
  147. void ROS2FrameEditorComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  148. {
  149. required.push_back(AZ_CRC_CE("TransformService"));
  150. }
  151. ROS2FrameEditorComponent::ROS2FrameEditorComponent(const AZStd::string& frameId)
  152. {
  153. SetFrameID(frameId);
  154. }
  155. void ROS2FrameEditorComponent::BuildGameEntity(AZ::Entity* gameEntity)
  156. {
  157. gameEntity->CreateComponent<ROS2FrameComponent>(m_configuration);
  158. }
  159. ROS2FrameConfiguration ROS2FrameEditorComponent::GetConfiguration() const
  160. {
  161. return m_configuration;
  162. }
  163. } // namespace ROS2