ROS2ControllersEditorSystemComponent.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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
  4. * of this distribution.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0 OR MIT
  7. *
  8. */
  9. #pragma once
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  13. #include <AzToolsFramework/ToolsComponents/EditorComponentBase.h>
  14. #include <AzToolsFramework/ToolsComponents/GenericComponentWrapper.h>
  15. #include <Clients/ROS2ControllersSystemComponent.h>
  16. #include <ROS2Controllers/ROS2ControllersEditorBus.h>
  17. namespace ROS2Controllers
  18. {
  19. /// System component for ROS2Controllers editor
  20. class ROS2ControllersEditorSystemComponent
  21. : public ROS2ControllersSystemComponent
  22. , protected AzToolsFramework::EditorEvents::Bus::Handler
  23. , protected ROS2ControllersEditorRequestBus::Handler
  24. {
  25. using BaseSystemComponent = ROS2ControllersSystemComponent;
  26. public:
  27. AZ_COMPONENT_DECL(ROS2ControllersEditorSystemComponent);
  28. static void Reflect(AZ::ReflectContext* context);
  29. ROS2ControllersEditorSystemComponent();
  30. ~ROS2ControllersEditorSystemComponent();
  31. protected:
  32. ////////////////////////////////////////////////////////////////////////
  33. // ROS2ControllersEditorRequestBus interface implementation
  34. AZ::Component* CreateWheelControllerComponent(
  35. AZ::Entity& entity, const AZ::EntityId& steeringEntity, const float steeringScale) override;
  36. AZ::Component* CreateROS2RobotControlComponent(AZ::Entity& entity, const ControlConfiguration& configuration) override;
  37. AZ::Component* CreateAckermannVehicleModelComponent(
  38. AZ::Entity& entity,
  39. const VehicleDynamics::VehicleConfiguration& configuration,
  40. const float speedLimit,
  41. const float steeringLimit,
  42. const float acceleration,
  43. const PidConfiguration& steeringPid) override;
  44. AZ::Component* CreateAckermannControlComponent(AZ::Entity& entity) override;
  45. AZ::Component* CreateSkidSteeringModelComponent(
  46. AZ::Entity& entity,
  47. const VehicleDynamics::VehicleConfiguration& configuration,
  48. const float linearLimit,
  49. const float angularLimit,
  50. const float linearAcceleration,
  51. const float angularAcceleration) override;
  52. AZ::Component* CreateSkidSteeringControlComponent(AZ::Entity& entity) override;
  53. AZ::Component* CreateJointsArticulationControllerComponent(AZ::Entity& entity) override;
  54. AZ::Component* CreateJointsPIDControllerComponent(AZ::Entity& entity) override;
  55. AZ::Component* CreateJointsManipulationEditorComponent(
  56. AZ::Entity& entity, const ROS2::PublisherConfiguration& publisherConfig) override;
  57. AZ::Component* CreateJointsTrajectoryComponent(AZ::Entity& entity, const AZStd::string& actionName) override;
  58. ////////////////////////////////////////////////////////////////////////
  59. private:
  60. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  61. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  62. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  63. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  64. // AZ::Component
  65. void Activate() override;
  66. void Deactivate() override;
  67. //! Create a component and attach the component to the entity.
  68. //! This method ensures that game components are wrapped into GenericComponentWrapper.
  69. //! @param entity entity to which the new component is added
  70. //! @param args constructor arguments used to create the new component
  71. //! @return A pointer to the component. Returns a null pointer if the component could not be created.
  72. template<class ComponentType, typename... Args>
  73. AZ::Component* CreateComponent(AZ::Entity& entity, Args&&... args)
  74. {
  75. // Do not create a component if the same type is already added.
  76. if (entity.FindComponent<ComponentType>())
  77. {
  78. return nullptr;
  79. }
  80. // Create component.
  81. // If it's not an "editor component" then wrap it in a GenericComponentWrapper.
  82. AZ::Component* component = nullptr;
  83. if (AZ::GetRttiHelper<ComponentType>() &&
  84. AZ::GetRttiHelper<ComponentType>()->IsTypeOf(AzToolsFramework::Components::EditorComponentBase::RTTI_Type()))
  85. {
  86. component = aznew ComponentType(AZStd::forward<Args>(args)...);
  87. }
  88. else
  89. {
  90. AZ::Component* gameComponent = aznew ComponentType(AZStd::forward<Args>(args)...);
  91. component = aznew AzToolsFramework::Components::GenericComponentWrapper(gameComponent);
  92. }
  93. AZ_Assert(component, "Failed to create component: %s", AZ::AzTypeInfo<ComponentType>::Name());
  94. if (component)
  95. {
  96. if (!entity.IsComponentReadyToAdd(component) || !entity.AddComponent(component))
  97. {
  98. delete component;
  99. component = nullptr;
  100. }
  101. }
  102. return component;
  103. }
  104. };
  105. } // namespace ROS2Controllers