ROS2SensorsEditorSystemComponent.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <AzCore/Component/Component.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  12. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  13. #include <AzToolsFramework/ToolsComponents/GenericComponentWrapper.h>
  14. #include <Clients/ROS2SensorsSystemComponent.h>
  15. #include <ROS2Sensors/ROS2SensorsEditorBus.h>
  16. namespace ROS2Sensors
  17. {
  18. /// System component for ROS2Sensors editor
  19. class ROS2SensorsEditorSystemComponent
  20. : public ROS2SensorsSystemComponent
  21. , protected AzToolsFramework::EditorEvents::Bus::Handler
  22. , protected ROS2SensorsEditorRequestBus::Handler
  23. {
  24. using BaseSystemComponent = ROS2SensorsSystemComponent;
  25. public:
  26. AZ_COMPONENT_DECL(ROS2SensorsEditorSystemComponent);
  27. static void Reflect(AZ::ReflectContext* context);
  28. ROS2SensorsEditorSystemComponent();
  29. ~ROS2SensorsEditorSystemComponent();
  30. protected:
  31. ////////////////////////////////////////////////////////////////////////
  32. // ROS2SensorsEditorRequestBus interface implementation
  33. AZ::Component* CreateROS2GnssSensorComponent(AZ::Entity& entity, const ROS2::SensorConfiguration& sensorConfiguration) override;
  34. AZ::Component* CreateROS2CameraSensorComponent(
  35. AZ::Entity& entity,
  36. const ROS2::SensorConfiguration& sensorConfiguration,
  37. const CameraSensorConfiguration& cameraConfiguration) override;
  38. AZ::Component* CreateROS2ImuSensorComponent(
  39. AZ::Entity& entity,
  40. const ROS2::SensorConfiguration& sensorConfiguration,
  41. const ImuSensorConfiguration& imuConfiguration) override;
  42. AZ::Component* CreateROS2LidarSensorComponent(
  43. AZ::Entity& entity,
  44. const ROS2::SensorConfiguration& sensorConfiguration,
  45. const LidarSensorConfiguration& lidarConfiguration) override;
  46. AZ::Component* CreateROS2Lidar2DSensorComponent(
  47. AZ::Entity& entity,
  48. const ROS2::SensorConfiguration& sensorConfiguration,
  49. const LidarSensorConfiguration& lidar2DConfiguration) override;
  50. AZ::Component* CreateROS2OdometrySensorComponent(AZ::Entity& entity, const ROS2::SensorConfiguration& sensorConfiguration) override;
  51. ////////////////////////////////////////////////////////////////////////
  52. private:
  53. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  54. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  55. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  56. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  57. // AZ::Component
  58. void Activate() override;
  59. void Deactivate() override;
  60. //! Create a component and attach the component to the entity.
  61. //! This method ensures that game components are wrapped into GenericComponentWrapper.
  62. //! @param entity entity to which the new component is added
  63. //! @param args constructor arguments used to create the new component
  64. //! @return A pointer to the component. Returns a null pointer if the component could not be created.
  65. template<class ComponentType, typename... Args>
  66. AZ::Component* CreateComponent(AZ::Entity& entity, Args&&... args)
  67. {
  68. // Do not create a component if the same type is already added.
  69. if (entity.FindComponent<ComponentType>())
  70. {
  71. return nullptr;
  72. }
  73. // Create component.
  74. // If it's not an "editor component" then wrap it in a GenericComponentWrapper.
  75. AZ::Component* component = nullptr;
  76. if (AZ::GetRttiHelper<ComponentType>() &&
  77. AZ::GetRttiHelper<ComponentType>()->IsTypeOf(AzToolsFramework::Components::EditorComponentBase::RTTI_Type()))
  78. {
  79. component = aznew ComponentType(AZStd::forward<Args>(args)...);
  80. }
  81. else
  82. {
  83. AZ::Component* gameComponent = aznew ComponentType(AZStd::forward<Args>(args)...);
  84. component = aznew AzToolsFramework::Components::GenericComponentWrapper(gameComponent);
  85. }
  86. AZ_Assert(component, "Failed to create component: %s", AZ::AzTypeInfo<ComponentType>::Name());
  87. if (component)
  88. {
  89. if (!entity.IsComponentReadyToAdd(component) || !entity.AddComponent(component))
  90. {
  91. delete component;
  92. component = nullptr;
  93. }
  94. }
  95. return component;
  96. }
  97. };
  98. } // namespace ROS2Sensors