ROS2EditorSystemComponent.h 4.4 KB

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