Ver código fonte

Change spawnning of robots during import (#598)

* Change spawnning of robots during import

---------

Signed-off-by: Artur Kamieniecki <[email protected] >
Artur Kamieniecki 1 ano atrás
pai
commit
d0b9f7e23e

+ 19 - 11
Gems/ROS2/Code/Source/RobotImporter/Pages/PrefabMakerPage.cpp

@@ -32,13 +32,15 @@ namespace ROS2
         SpawnerRequestsBus::BroadcastResult(allActiveSpawnPoints, &SpawnerRequestsBus::Events::GetAllSpawnPointInfos);
 
         m_spawnPointsComboBox = new QComboBox(this);
-        m_spawnPointsInfos = allActiveSpawnPoints.values;
 
-        for (int i = 0; i < allActiveSpawnPoints.values.size(); i++)
+        m_spawnPointsComboBox->addItem(tr(zeroPoint.data()));
+
+        for (const auto& spawnPointMap : allActiveSpawnPoints.values)
         {
-            for (const auto& element : allActiveSpawnPoints.values[i])
+            for (const auto& spawnPoint : spawnPointMap)
             {
-                m_spawnPointsComboBox->addItem(element.first.c_str(), QVariant(i));
+                m_spawnPointsComboBox->addItem(spawnPoint.first.c_str());
+                m_spawnPointsInfos.insert({ spawnPoint.first.c_str(), spawnPoint.second });
             }
         }
 
@@ -55,11 +57,11 @@ namespace ROS2
         QLabel* spawnPointListLabel;
         if (allActiveSpawnPoints.values.size() == 0)
         {
-            spawnPointListLabel = new QLabel("Select spawn position (No spawn positions were detected)", this);
+            spawnPointListLabel = new QLabel(tr("Select spawn position (No spawn positions were detected)"), this);
         }
         else
         {
-            spawnPointListLabel = new QLabel("Select spawn position", this);
+            spawnPointListLabel = new QLabel(tr("Select spawn position"), this);
         }
         layout->addWidget(spawnPointListLabel);
         layout->addWidget(m_spawnPointsComboBox);
@@ -95,15 +97,21 @@ namespace ROS2
     {
         if (!m_spawnPointsInfos.empty())
         {
-            int vectorIndex = m_spawnPointsComboBox->currentData().toInt();
-            AZStd::string mapKey(m_spawnPointsComboBox->currentText().toStdString().c_str());
-            auto& map = m_spawnPointsInfos[vectorIndex];
-            if (auto spawnInfo = map.find(mapKey);
-                spawnInfo != map.end())
+            AZStd::string spawnPointName(m_spawnPointsComboBox->currentText().toStdString().c_str());
+            if (IsZeroPoint(spawnPointName))
+            {
+                return AZStd::nullopt;
+            }
+            if (auto spawnInfo = m_spawnPointsInfos.find(spawnPointName); spawnInfo != m_spawnPointsInfos.end())
             {
                 return spawnInfo->second.pose;
             }
         }
         return AZStd::nullopt;
     }
+
+    bool PrefabMakerPage::IsZeroPoint(AZStd::string spawnPointName)
+    {
+        return spawnPointName == PrefabMakerPage::zeroPoint;
+    }
 } // namespace ROS2

+ 5 - 1
Gems/ROS2/Code/Source/RobotImporter/Pages/PrefabMakerPage.h

@@ -41,12 +41,16 @@ namespace ROS2
         void onCreateButtonPressed();
 
     private:
+        static bool IsZeroPoint(AZStd::string spawnPointName);
+
+        static constexpr AZStd::string_view zeroPoint = "Simulation origin";
+
         bool m_success;
         QLineEdit* m_prefabName;
         QPushButton* m_createButton;
         QTextEdit* m_log;
         QComboBox* m_spawnPointsComboBox;
-        AZStd::vector<SpawnPointInfoMap> m_spawnPointsInfos;
+        SpawnPointInfoMap m_spawnPointsInfos;
         RobotImporterWidget* m_parentImporterWidget;
     };
 } // namespace ROS2

+ 4 - 1
Gems/ROS2/Code/Source/RobotImporter/URDF/URDFPrefabMaker.cpp

@@ -468,7 +468,6 @@ namespace ROS2
         if (!linkEntityIdsWithoutParent.empty() && linkEntityIdsWithoutParent.front().IsValid())
         {
             AZ::EntityId contentEntityId = linkEntityIdsWithoutParent.front();
-            MoveEntityToDefaultSpawnPoint(contentEntityId, m_spawnPosition);
             AddRobotControl(contentEntityId);
         }
 
@@ -542,6 +541,10 @@ namespace ROS2
             auto prefabInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabPublicInterface>::Get();
             [[maybe_unused]] auto createPrefabOutcome =
                 prefabInterface->InstantiatePrefab(relativePath.String(), AZ::EntityId(), AZ::Vector3::CreateZero());
+            if (createPrefabOutcome.IsSuccess())
+            {
+                MoveEntityToDefaultSpawnPoint(createPrefabOutcome.GetValue(), m_spawnPosition);
+            }
         }
         else
         {

+ 5 - 6
Gems/ROS2/Code/Source/Spawner/ROS2SpawnerEditorComponent.cpp

@@ -46,11 +46,13 @@ namespace ROS2
 
     AZStd::unordered_map<AZStd::string, SpawnPointInfo> ROS2SpawnerEditorComponent::GetSpawnPoints() const
     {
+        AZStd::unordered_map<AZStd::string, SpawnPointInfo> result;
+        result[GetEntity()->GetName() + " - default"] =
+            SpawnPointInfo{ "Default spawn pose defined in the Editor", m_controller.GetDefaultSpawnPose() };
+
         AZStd::vector<AZ::EntityId> children;
         AZ::TransformBus::EventResult(children, m_controller.GetEditorEntityId(), &AZ::TransformBus::Events::GetChildren);
 
-        AZStd::unordered_map<AZStd::string, SpawnPointInfo> result;
-
         for (const AZ::EntityId& child : children)
         {
             AZ::Entity* childEntity = nullptr;
@@ -61,13 +63,10 @@ namespace ROS2
 
             if (editorSpawnPoint != nullptr)
             {
-                result.insert(editorSpawnPoint->GetInfo());
+                result.insert({ GetEntity()->GetName() + " - " + editorSpawnPoint->GetInfo().first, editorSpawnPoint->GetInfo().second });
             }
         }
 
-        // setting name of spawn point component "default" in a child entity will have no effect since it is overwritten here with the
-        // default spawn pose of spawner
-        result["default"] = SpawnPointInfo{ "Default spawn pose defined in the Editor", m_controller.GetDefaultSpawnPose() };
         return result;
     }