ROS2SpawnerEditorComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #ifndef WITH_GAZEBO_MSGS
  9. static_assert(false, "This file should not be included in the build, without WITH_GAZEBO_MSGS defined.");
  10. #endif
  11. #include "ROS2SpawnerEditorComponent.h"
  12. #include "AzCore/Debug/Trace.h"
  13. #include "ROS2SpawnPointEditorComponent.h"
  14. #include "Spawner/ROS2SpawnerComponentController.h"
  15. #include <AzCore/Component/TransformBus.h>
  16. #include <AzCore/Serialization/EditContext.h>
  17. #include <AzCore/Serialization/EditContextConstants.inl>
  18. #include <ROS2/Spawner/SpawnerBus.h>
  19. namespace ROS2
  20. {
  21. ROS2SpawnerEditorComponent::ROS2SpawnerEditorComponent(const ROS2SpawnerComponentConfig& configuration)
  22. : ROS2SpawnerEditorComponentBase(configuration)
  23. {
  24. }
  25. void ROS2SpawnerEditorComponent::Reflect(AZ::ReflectContext* context)
  26. {
  27. ROS2SpawnerEditorComponentBase::Reflect(context);
  28. AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  29. if (serializeContext)
  30. {
  31. serializeContext->Class<ROS2SpawnerEditorComponent, ROS2SpawnerEditorComponentBase>()->Version(1);
  32. AZ::EditContext* editContext = serializeContext->GetEditContext();
  33. if (editContext)
  34. {
  35. editContext->Class<ROS2SpawnerEditorComponent>("ROS2 Spawner", "Spawner component")
  36. ->ClassElement(AZ::Edit::ClassElements::EditorData, "Manages spawning of robots in configurable locations")
  37. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  38. ->Attribute(AZ::Edit::Attributes::Category, "ROS2")
  39. ->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/ROS2Spawner.svg")
  40. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/ROS2Spawner.svg");
  41. }
  42. }
  43. }
  44. AZStd::unordered_map<AZStd::string, SpawnPointInfo> ROS2SpawnerEditorComponent::GetSpawnPoints() const
  45. {
  46. AZStd::unordered_map<AZStd::string, SpawnPointInfo> result;
  47. result[GetEntity()->GetName() + " - default"] =
  48. SpawnPointInfo{ "Default spawn pose defined in the Editor", m_controller.GetDefaultSpawnPose() };
  49. AZStd::vector<AZ::EntityId> children;
  50. AZ::TransformBus::EventResult(children, m_controller.GetEditorEntityId(), &AZ::TransformBus::Events::GetChildren);
  51. for (const AZ::EntityId& child : children)
  52. {
  53. AZ::Entity* childEntity = nullptr;
  54. AZ::ComponentApplicationBus::BroadcastResult(childEntity, &AZ::ComponentApplicationRequests::FindEntity, child);
  55. AZ_Assert(childEntity, "No child entity found for entity %s", child.ToString().c_str());
  56. const auto* editorSpawnPoint = childEntity->FindComponent<ROS2SpawnPointEditorComponent>();
  57. if (editorSpawnPoint != nullptr)
  58. {
  59. result.insert({ GetEntity()->GetName() + " - " + editorSpawnPoint->GetInfo().first, editorSpawnPoint->GetInfo().second });
  60. }
  61. }
  62. return result;
  63. }
  64. const AZ::Transform& ROS2SpawnerEditorComponent::GetDefaultSpawnPose() const
  65. {
  66. return m_controller.GetDefaultSpawnPose();
  67. }
  68. AZStd::unordered_map<AZStd::string, SpawnPointInfo> ROS2SpawnerEditorComponent::GetAllSpawnPointInfos() const
  69. {
  70. return GetSpawnPoints();
  71. }
  72. bool ROS2SpawnerEditorComponent::ShouldActivateController() const
  73. {
  74. return false;
  75. }
  76. void ROS2SpawnerEditorComponent::Activate()
  77. {
  78. ROS2SpawnerEditorComponentBase::Activate();
  79. ROS2SpawnerComponentConfig config = m_controller.GetConfiguration();
  80. config.m_editorEntityId = GetEntityId();
  81. AZ_Assert(config.m_editorEntityId.IsValid(), "Spawner component got an invalid entity id");
  82. m_controller.SetConfiguration(config);
  83. SpawnerRequestsBus::Handler::BusConnect(config.m_editorEntityId);
  84. }
  85. void ROS2SpawnerEditorComponent::Deactivate()
  86. {
  87. SpawnerRequestsBus::Handler::BusDisconnect();
  88. ROS2SpawnerEditorComponentBase::Deactivate();
  89. }
  90. } // namespace ROS2