PrefabMakerPage.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #include "PrefabMakerPage.h"
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Debug/Trace.h>
  11. #include <AzCore/EBus/Results.h>
  12. #include <AzCore/Math/Transform.h>
  13. #include <AzCore/std/smart_ptr/make_shared.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <ROS2/Spawner/SpawnerBus.h>
  16. #include <ROS2/Spawner/SpawnerInfo.h>
  17. #include <RobotImporter/RobotImporterWidget.h>
  18. #include <optional>
  19. #include <qcombobox.h>
  20. #include <qvariant.h>
  21. namespace ROS2RobotImporter
  22. {
  23. PrefabMakerPage::PrefabMakerPage([[maybe_unused]] RobotImporterWidget* parent)
  24. : QWizardPage(parent)
  25. , m_success(false)
  26. {
  27. AZ::EBusAggregateResults<AZStd::unordered_map<AZStd::string, ROS2::SpawnPointInfo>> allActiveSpawnPoints;
  28. ROS2::SpawnerRequestsBus::BroadcastResult(allActiveSpawnPoints, &ROS2::SpawnerRequestsBus::Events::GetAllSpawnPointInfos);
  29. m_spawnPointsComboBox = new QComboBox(this);
  30. m_spawnPointsComboBox->addItem(tr(zeroPoint.data()));
  31. for (const auto& spawnPointMap : allActiveSpawnPoints.values)
  32. {
  33. for (const auto& spawnPoint : spawnPointMap)
  34. {
  35. m_spawnPointsComboBox->addItem(spawnPoint.first.c_str());
  36. m_spawnPointsInfos.insert({ spawnPoint.first.c_str(), spawnPoint.second });
  37. }
  38. }
  39. m_prefabName = new QLineEdit(this);
  40. m_createButton = new QPushButton(tr("Create Prefab"), this);
  41. m_log = new QTextEdit(this);
  42. m_log->acceptRichText();
  43. m_log->setReadOnly(true);
  44. setTitle(tr("Prefab creation"));
  45. QVBoxLayout* layout = new QVBoxLayout;
  46. QHBoxLayout* layoutInner = new QHBoxLayout;
  47. layoutInner->addWidget(m_prefabName);
  48. layoutInner->addWidget(m_createButton);
  49. layout->addLayout(layoutInner);
  50. QLabel* spawnPointListLabel;
  51. if (allActiveSpawnPoints.values.size() == 0)
  52. {
  53. spawnPointListLabel = new QLabel(tr("Select spawn position (No spawn positions were detected)"), this);
  54. }
  55. else
  56. {
  57. spawnPointListLabel = new QLabel(tr("Select spawn position"), this);
  58. }
  59. layout->addWidget(spawnPointListLabel);
  60. layout->addWidget(m_spawnPointsComboBox);
  61. layout->addWidget(m_log);
  62. setLayout(layout);
  63. connect(m_createButton, &QPushButton::pressed, this, &PrefabMakerPage::onCreateButtonPressed);
  64. }
  65. void PrefabMakerPage::SetProposedPrefabName(const AZStd::string prefabName)
  66. {
  67. m_prefabName->setText(QString::fromUtf8(prefabName.data(), int(prefabName.size())));
  68. }
  69. AZStd::string PrefabMakerPage::GetPrefabName() const
  70. {
  71. return AZStd::string(m_prefabName->text().toUtf8().constData());
  72. }
  73. void PrefabMakerPage::ReportProgress(const AZStd::string& progressForUser)
  74. {
  75. m_log->setMarkdown(QString::fromUtf8(progressForUser.data(), int(progressForUser.size())));
  76. }
  77. void PrefabMakerPage::SetSuccess(bool success)
  78. {
  79. m_success = success;
  80. emit completeChanged();
  81. }
  82. bool PrefabMakerPage::isComplete() const
  83. {
  84. return m_success;
  85. }
  86. AZStd::optional<AZ::Transform> PrefabMakerPage::getSelectedSpawnPoint() const
  87. {
  88. if (!m_spawnPointsInfos.empty())
  89. {
  90. AZStd::string spawnPointName(m_spawnPointsComboBox->currentText().toStdString().c_str());
  91. if (IsZeroPoint(spawnPointName))
  92. {
  93. return AZStd::nullopt;
  94. }
  95. if (auto spawnInfo = m_spawnPointsInfos.find(spawnPointName); spawnInfo != m_spawnPointsInfos.end())
  96. {
  97. return spawnInfo->second.pose;
  98. }
  99. }
  100. return AZStd::nullopt;
  101. }
  102. bool PrefabMakerPage::IsZeroPoint(AZStd::string spawnPointName)
  103. {
  104. return spawnPointName == PrefabMakerPage::zeroPoint;
  105. }
  106. } // namespace ROS2RobotImporter