Browse Source

Fix overrides for frame component (#759)

* Update custom namespace handling that was triggering overrides for each ROS2FrameComponent in a prefab

Signed-off-by: Artur Kamieniecki <[email protected]>
Signed-off-by: Jan Hanca <[email protected]>
Co-authored-by: Jan Hanca <[email protected]>
Signed-off-by: Jan Hanca <[email protected]>
Artur Kamieniecki 11 months ago
parent
commit
e543a0c20d

+ 7 - 2
Gems/ROS2/Code/Include/ROS2/Frame/NamespaceConfiguration.h

@@ -58,9 +58,14 @@ namespace ROS2
         //! @param parentNamespace parent namespace.
         void SetParentNamespace(const AZStd::string& parentNamespace);
 
+        //! Initializes the namespace configuration.
+        //! This function should be called in the Init functions of frame components.
+        void Init();
+
     private:
-        AZStd::string m_namespace;
-        AZStd::string m_parentNamespace;
+        AZStd::string m_customNamespace = ""; //!< Custom namespace that can be set by the user
+        AZStd::string m_namespace = ""; //!< Current namespace (might be custom); set automatically
+        AZStd::string m_parentNamespace = ""; //!< Parent namespace (might be custom); set automatically
         NamespaceStrategy m_namespaceStrategy = NamespaceStrategy::Default;
         bool m_isRoot;
         AZStd::string m_entityName;

+ 1 - 0
Gems/ROS2/Code/Include/ROS2/Frame/ROS2FrameComponent.h

@@ -52,6 +52,7 @@ namespace ROS2
 
         //////////////////////////////////////////////////////////////////////////
         // Component overrides
+        void Init() override;
         void Activate() override;
         void Deactivate() override;
         //////////////////////////////////////////////////////////////////////////

+ 1 - 0
Gems/ROS2/Code/Include/ROS2/Frame/ROS2FrameEditorComponent.h

@@ -40,6 +40,7 @@ namespace ROS2
         ROS2FrameEditorComponent(const ROS2FrameConfiguration ros2FrameConfiguration);
 
         // AzToolsFramework::Components::EditorComponentBase overrides
+        void Init() override;
         void Activate() override;
         void Deactivate() override;
         void BuildGameEntity(AZ::Entity* gameEntity) override;

+ 11 - 4
Gems/ROS2/Code/Source/Frame/NamespaceConfiguration.cpp

@@ -34,7 +34,7 @@ namespace ROS2
             m_namespace = ROS2Names::RosifyName(m_entityName);
             break;
         case NamespaceStrategy::Custom:
-            // Leave the namespace as is
+            m_namespace = m_customNamespace;
             break;
         default:
             AZ_Assert(false, "Unhandled namespace strategy");
@@ -96,6 +96,12 @@ namespace ROS2
         return m_namespaceStrategy == NamespaceConfiguration::NamespaceStrategy::Custom;
     }
 
+    void NamespaceConfiguration::Init()
+    {
+        // Make sure that the namespace is up to date.
+        OnNamespaceStrategySelected();
+    }
+
     void NamespaceConfiguration::Reflect(AZ::ReflectContext* context)
     {
         if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
@@ -103,7 +109,7 @@ namespace ROS2
             serializeContext->Class<NamespaceConfiguration>()
                 ->Version(1)
                 ->Field("Namespace Strategy", &NamespaceConfiguration::m_namespaceStrategy)
-                ->Field("Namespace", &NamespaceConfiguration::m_namespace);
+                ->Field("Namespace", &NamespaceConfiguration::m_customNamespace);
 
             if (AZ::EditContext* ec = serializeContext->GetEditContext())
             {
@@ -117,9 +123,10 @@ namespace ROS2
                     ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::Empty, "Empty")
                     ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::FromEntityName, "Generate from entity name")
                     ->EnumAttribute(NamespaceConfiguration::NamespaceStrategy::Custom, "Custom")
-                    ->DataElement(AZ::Edit::UIHandlers::Default, &NamespaceConfiguration::m_namespace, "Namespace", "Namespace")
+                    ->DataElement(AZ::Edit::UIHandlers::Default, &NamespaceConfiguration::m_customNamespace, "Namespace", "Namespace")
                     ->Attribute(AZ::Edit::Attributes::Visibility, &NamespaceConfiguration::IsNamespaceCustom)
-                    ->Attribute(AZ::Edit::Attributes::ChangeValidate, &ROS2Names::ValidateNamespaceField);
+                    ->Attribute(AZ::Edit::Attributes::ChangeValidate, &ROS2Names::ValidateNamespaceField)
+                    ->Attribute(AZ::Edit::Attributes::ChangeNotify, &NamespaceConfiguration::UpdateNamespace);
             }
         }
     }

+ 5 - 0
Gems/ROS2/Code/Source/Frame/ROS2FrameComponent.cpp

@@ -139,6 +139,11 @@ namespace ROS2
 
     AZ_CLASS_ALLOCATOR_IMPL(JsonFrameComponentConfigSerializer, AZ::SystemAllocator);
 
+    void ROS2FrameComponent::Init()
+    {
+        m_namespaceConfiguration.Init();
+    }
+
     void ROS2FrameComponent::Activate()
     {
         m_namespaceConfiguration.PopulateNamespace(IsTopLevel(), GetEntity()->GetName());

+ 5 - 0
Gems/ROS2/Code/Source/Frame/ROS2FrameEditorComponent.cpp

@@ -30,6 +30,11 @@ namespace ROS2
         m_configuration = ros2FrameConfiguration;
     }
 
+    void ROS2FrameEditorComponent::Init()
+    {
+        m_configuration.m_namespaceConfiguration.Init();
+    }
+
     void ROS2FrameEditorComponent::Activate()
     {
         ROS2FrameComponentBus::Handler::BusConnect(GetEntityId());