ROS2SpawnerEditorComponent.cpp 4.1 KB

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