Skip to content

Commit d0b9f7e

Browse files
Change spawnning of robots during import (o3de#598)
* Change spawnning of robots during import --------- Signed-off-by: Artur Kamieniecki <[email protected] >
1 parent dcf6474 commit d0b9f7e

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

Gems/ROS2/Code/Source/RobotImporter/Pages/PrefabMakerPage.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ namespace ROS2
3232
SpawnerRequestsBus::BroadcastResult(allActiveSpawnPoints, &SpawnerRequestsBus::Events::GetAllSpawnPointInfos);
3333

3434
m_spawnPointsComboBox = new QComboBox(this);
35-
m_spawnPointsInfos = allActiveSpawnPoints.values;
3635

37-
for (int i = 0; i < allActiveSpawnPoints.values.size(); i++)
36+
m_spawnPointsComboBox->addItem(tr(zeroPoint.data()));
37+
38+
for (const auto& spawnPointMap : allActiveSpawnPoints.values)
3839
{
39-
for (const auto& element : allActiveSpawnPoints.values[i])
40+
for (const auto& spawnPoint : spawnPointMap)
4041
{
41-
m_spawnPointsComboBox->addItem(element.first.c_str(), QVariant(i));
42+
m_spawnPointsComboBox->addItem(spawnPoint.first.c_str());
43+
m_spawnPointsInfos.insert({ spawnPoint.first.c_str(), spawnPoint.second });
4244
}
4345
}
4446

@@ -55,11 +57,11 @@ namespace ROS2
5557
QLabel* spawnPointListLabel;
5658
if (allActiveSpawnPoints.values.size() == 0)
5759
{
58-
spawnPointListLabel = new QLabel("Select spawn position (No spawn positions were detected)", this);
60+
spawnPointListLabel = new QLabel(tr("Select spawn position (No spawn positions were detected)"), this);
5961
}
6062
else
6163
{
62-
spawnPointListLabel = new QLabel("Select spawn position", this);
64+
spawnPointListLabel = new QLabel(tr("Select spawn position"), this);
6365
}
6466
layout->addWidget(spawnPointListLabel);
6567
layout->addWidget(m_spawnPointsComboBox);
@@ -95,15 +97,21 @@ namespace ROS2
9597
{
9698
if (!m_spawnPointsInfos.empty())
9799
{
98-
int vectorIndex = m_spawnPointsComboBox->currentData().toInt();
99-
AZStd::string mapKey(m_spawnPointsComboBox->currentText().toStdString().c_str());
100-
auto& map = m_spawnPointsInfos[vectorIndex];
101-
if (auto spawnInfo = map.find(mapKey);
102-
spawnInfo != map.end())
100+
AZStd::string spawnPointName(m_spawnPointsComboBox->currentText().toStdString().c_str());
101+
if (IsZeroPoint(spawnPointName))
102+
{
103+
return AZStd::nullopt;
104+
}
105+
if (auto spawnInfo = m_spawnPointsInfos.find(spawnPointName); spawnInfo != m_spawnPointsInfos.end())
103106
{
104107
return spawnInfo->second.pose;
105108
}
106109
}
107110
return AZStd::nullopt;
108111
}
112+
113+
bool PrefabMakerPage::IsZeroPoint(AZStd::string spawnPointName)
114+
{
115+
return spawnPointName == PrefabMakerPage::zeroPoint;
116+
}
109117
} // namespace ROS2

Gems/ROS2/Code/Source/RobotImporter/Pages/PrefabMakerPage.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ namespace ROS2
4141
void onCreateButtonPressed();
4242

4343
private:
44+
static bool IsZeroPoint(AZStd::string spawnPointName);
45+
46+
static constexpr AZStd::string_view zeroPoint = "Simulation origin";
47+
4448
bool m_success;
4549
QLineEdit* m_prefabName;
4650
QPushButton* m_createButton;
4751
QTextEdit* m_log;
4852
QComboBox* m_spawnPointsComboBox;
49-
AZStd::vector<SpawnPointInfoMap> m_spawnPointsInfos;
53+
SpawnPointInfoMap m_spawnPointsInfos;
5054
RobotImporterWidget* m_parentImporterWidget;
5155
};
5256
} // namespace ROS2

Gems/ROS2/Code/Source/RobotImporter/URDF/URDFPrefabMaker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ namespace ROS2
468468
if (!linkEntityIdsWithoutParent.empty() && linkEntityIdsWithoutParent.front().IsValid())
469469
{
470470
AZ::EntityId contentEntityId = linkEntityIdsWithoutParent.front();
471-
MoveEntityToDefaultSpawnPoint(contentEntityId, m_spawnPosition);
472471
AddRobotControl(contentEntityId);
473472
}
474473

@@ -542,6 +541,10 @@ namespace ROS2
542541
auto prefabInterface = AZ::Interface<AzToolsFramework::Prefab::PrefabPublicInterface>::Get();
543542
[[maybe_unused]] auto createPrefabOutcome =
544543
prefabInterface->InstantiatePrefab(relativePath.String(), AZ::EntityId(), AZ::Vector3::CreateZero());
544+
if (createPrefabOutcome.IsSuccess())
545+
{
546+
MoveEntityToDefaultSpawnPoint(createPrefabOutcome.GetValue(), m_spawnPosition);
547+
}
545548
}
546549
else
547550
{

Gems/ROS2/Code/Source/Spawner/ROS2SpawnerEditorComponent.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ namespace ROS2
4646

4747
AZStd::unordered_map<AZStd::string, SpawnPointInfo> ROS2SpawnerEditorComponent::GetSpawnPoints() const
4848
{
49+
AZStd::unordered_map<AZStd::string, SpawnPointInfo> result;
50+
result[GetEntity()->GetName() + " - default"] =
51+
SpawnPointInfo{ "Default spawn pose defined in the Editor", m_controller.GetDefaultSpawnPose() };
52+
4953
AZStd::vector<AZ::EntityId> children;
5054
AZ::TransformBus::EventResult(children, m_controller.GetEditorEntityId(), &AZ::TransformBus::Events::GetChildren);
5155

52-
AZStd::unordered_map<AZStd::string, SpawnPointInfo> result;
53-
5456
for (const AZ::EntityId& child : children)
5557
{
5658
AZ::Entity* childEntity = nullptr;
@@ -61,13 +63,10 @@ namespace ROS2
6163

6264
if (editorSpawnPoint != nullptr)
6365
{
64-
result.insert(editorSpawnPoint->GetInfo());
66+
result.insert({ GetEntity()->GetName() + " - " + editorSpawnPoint->GetInfo().first, editorSpawnPoint->GetInfo().second });
6567
}
6668
}
6769

68-
// setting name of spawn point component "default" in a child entity will have no effect since it is overwritten here with the
69-
// default spawn pose of spawner
70-
result["default"] = SpawnPointInfo{ "Default spawn pose defined in the Editor", m_controller.GetDefaultSpawnPose() };
7170
return result;
7271
}
7372

0 commit comments

Comments
 (0)