ManifestWidgetPage.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 <QDesktopServices>
  9. #include <QMenu>
  10. #include <QTimer>
  11. #include <QScrollArea>
  12. #include <QScrollBar>
  13. #include <QMessageBox>
  14. #include <QUrl>
  15. #include <AzToolsFramework/Debug/TraceContext.h>
  16. #include <AzFramework/StringFunc/StringFunc.h>
  17. #include <AzCore/std/string/conversions.h>
  18. #include <SceneWidgets/ui_ManifestWidgetPage.h>
  19. #include <SceneAPI/SceneCore/DataTypes/Groups/IGroup.h>
  20. #include <SceneAPI/SceneCore/DataTypes/IManifestObject.h>
  21. #include <SceneAPI/SceneCore/DataTypes/Rules/IUnmodifiableRule.h>
  22. #include <SceneAPI/SceneCore/Containers/Scene.h>
  23. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  24. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  25. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  26. #include <SceneAPI/SceneCore/Events/ManifestMetaInfoBus.h>
  27. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidget.h>
  28. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidgetPage.h>
  29. namespace AZ
  30. {
  31. namespace SceneAPI
  32. {
  33. namespace UI
  34. {
  35. ManifestWidgetPage::ManifestWidgetPage(SerializeContext* context, AZStd::vector<AZ::Uuid>&& classTypeIds)
  36. : m_classTypeIds(AZStd::move(classTypeIds))
  37. , ui(new Ui::ManifestWidgetPage())
  38. , m_propertyEditor(nullptr)
  39. , m_context(context)
  40. , m_capSize(100)
  41. {
  42. ui->setupUi(this);
  43. m_propertyEditor = new AzToolsFramework::ReflectedPropertyEditor(nullptr);
  44. m_propertyEditor->Setup(context, this, true, 250);
  45. m_propertyEditor->SetReadOnlyQueryFunction(
  46. [this](const AzToolsFramework::InstanceDataNode* node)
  47. {
  48. return SetNodeReadOnlyStatus(node);
  49. });
  50. ui->m_mainLayout->insertWidget(0, m_propertyEditor);
  51. BuildAndConnectAddButton();
  52. BuildHelpButton();
  53. m_editMenu = new QMenu("Edit Scene Settings Menu", ui->m_editButton);
  54. ui->m_editButton->setMenu(m_editMenu);
  55. connect(m_editMenu, &QMenu::aboutToShow, this, &ManifestWidgetPage::AddEditMenu);
  56. connect(ui->m_saveButton, &QPushButton::clicked, this, &ManifestWidgetPage::SaveClicked);
  57. connect(ui->m_inspectButton, &QPushButton::clicked, this, &ManifestWidgetPage::InspectClicked);
  58. BusConnect();
  59. }
  60. ManifestWidgetPage::~ManifestWidgetPage()
  61. {
  62. BusDisconnect();
  63. }
  64. void ManifestWidgetPage::SetCapSize(size_t size)
  65. {
  66. m_capSize = size;
  67. }
  68. size_t ManifestWidgetPage::GetCapSize() const
  69. {
  70. return m_capSize;
  71. }
  72. bool ManifestWidgetPage::SupportsType(const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  73. {
  74. for (Uuid& id : m_classTypeIds)
  75. {
  76. if (object->RTTI_IsTypeOf(id))
  77. {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. bool ManifestWidgetPage::AddObject(const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  84. {
  85. AZ_PROFILE_FUNCTION(Editor);
  86. if (!SupportsType(object))
  87. {
  88. return false;
  89. }
  90. if (!m_propertyEditor->AddInstance(object.get(), object->RTTI_GetType()))
  91. {
  92. AZ_Assert(false, "Failed to add manifest object to Reflected Property Editor.");
  93. return false;
  94. }
  95. // Add new object to the list so it's ready for updating later on.
  96. m_objects.push_back(object);
  97. UpdateAddButtonStatus();
  98. // Guard against adding lots of ScrollToBottom calls in the case where we're performing bulk adds in a single frame.
  99. if (!m_scrollToBottomQueued)
  100. {
  101. m_scrollToBottomQueued = true;
  102. QTimer::singleShot(0, this,
  103. [this]()
  104. {
  105. ScrollToBottom();
  106. });
  107. }
  108. return true;
  109. }
  110. void ManifestWidgetPage::UpdateAddButtonStatus()
  111. {
  112. if (m_objects.size() >= m_capSize)
  113. {
  114. QString entryString(tr(m_capSize == 1 ? "entry" : "entries"));
  115. ui->m_addButton->setToolTip(tr("Maximum number of entries reached. This page can contain up to %1 %2.").arg(m_capSize).arg(entryString));
  116. ui->m_addButton->setEnabled(false);
  117. }
  118. else
  119. {
  120. ui->m_addButton->setToolTip(QString());
  121. ui->m_addButton->setEnabled(true);
  122. }
  123. }
  124. bool ManifestWidgetPage::RemoveObject(const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  125. {
  126. if (SupportsType(object))
  127. {
  128. // Explicitly keep a copy of the shared pointer to guarantee that the manifest object isn't
  129. // deleted before it can be queued for the delete deletion.
  130. AZStd::shared_ptr<DataTypes::IManifestObject> temp = object;
  131. (void)temp;
  132. auto it = AZStd::find(m_objects.begin(), m_objects.end(), object);
  133. if (it == m_objects.end())
  134. {
  135. AZ_Assert(false, "Manifest object not part of manifest page.");
  136. return false;
  137. }
  138. m_objects.erase(it);
  139. UpdateAddButtonStatus();
  140. if (m_objects.size() == 0)
  141. {
  142. // We won't get property modified event if it's the last element removed
  143. EmitObjectChanged();
  144. }
  145. // If the property editor is immediately updated here QT will do some processing in an unexpected order,
  146. // leading to heap corruption. To avoid this, keep a cached version of the deleted object and
  147. // delay the rebuilding of the property editor to the end of the update cycle.
  148. QTimer::singleShot(0, this,
  149. [this, object]()
  150. {
  151. // Explicitly keep a copy of the shared pointer to guarantee that the manifest object isn't
  152. // deleted between updates of QT.
  153. (void)object;
  154. m_propertyEditor->ClearInstances();
  155. for (auto& instance : m_objects)
  156. {
  157. if (!m_propertyEditor->AddInstance(instance.get(), instance->RTTI_GetType()))
  158. {
  159. AZ_Assert(false, "Failed to add manifest object to Reflected Property Editor.");
  160. }
  161. }
  162. RefreshPage();
  163. }
  164. );
  165. return true;
  166. }
  167. else
  168. {
  169. return false;
  170. }
  171. }
  172. size_t ManifestWidgetPage::ObjectCount() const
  173. {
  174. return m_objects.size();
  175. }
  176. void ManifestWidgetPage::Clear()
  177. {
  178. m_objects.clear();
  179. m_propertyEditor->ClearInstances();
  180. UpdateAddButtonStatus();
  181. }
  182. void ManifestWidgetPage::BeforePropertyModified(AzToolsFramework::InstanceDataNode* /*pNode*/)
  183. {
  184. }
  185. void ManifestWidgetPage::AfterPropertyModified(AzToolsFramework::InstanceDataNode* node)
  186. {
  187. if (node)
  188. {
  189. node = node->GetParent();
  190. while (node)
  191. {
  192. if (const AZ::SerializeContext::ClassData* classData = node->GetClassMetadata(); classData && classData->m_azRtti)
  193. {
  194. if (const DataTypes::IManifestObject* cast = classData->m_azRtti->Cast<DataTypes::IManifestObject>(node->FirstInstance()); cast)
  195. {
  196. AZ_Assert(AZStd::find_if(m_objects.begin(), m_objects.end(),
  197. [cast](const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  198. {
  199. return object.get() == cast;
  200. }) != m_objects.end(), "ManifestWidgetPage detected an update of a field it doesn't own.");
  201. EmitObjectChanged(cast);
  202. break;
  203. }
  204. }
  205. node = node->GetParent();
  206. }
  207. }
  208. }
  209. void ManifestWidgetPage::SetPropertyEditingActive(AzToolsFramework::InstanceDataNode* /*pNode*/)
  210. {
  211. }
  212. void ManifestWidgetPage::SetPropertyEditingComplete(AzToolsFramework::InstanceDataNode* /*pNode*/)
  213. {
  214. }
  215. void ManifestWidgetPage::SealUndoStack()
  216. {
  217. }
  218. void ManifestWidgetPage::ScrollToBottom()
  219. {
  220. m_scrollToBottomQueued = false;
  221. QScrollArea* propertyGridScrollArea = m_propertyEditor->findChild<QScrollArea*>();
  222. if (propertyGridScrollArea)
  223. {
  224. propertyGridScrollArea->verticalScrollBar()->setSliderPosition(propertyGridScrollArea->verticalScrollBar()->maximum());
  225. }
  226. }
  227. void ManifestWidgetPage::RefreshPage()
  228. {
  229. AZ_PROFILE_FUNCTION(Editor);
  230. m_propertyEditor->InvalidateAll();
  231. m_propertyEditor->ExpandAll();
  232. }
  233. void ManifestWidgetPage::OnSingleGroupAdd()
  234. {
  235. if (m_classTypeIds.size() > 0)
  236. {
  237. if (m_objects.size() >= m_capSize)
  238. {
  239. QMessageBox::warning(this, "Cap reached", QString("The group container reached its cap of %1 entries.\nPlease remove groups to free up space.").
  240. arg(m_capSize));
  241. return;
  242. }
  243. AddNewObject(m_classTypeIds[0]);
  244. UpdateAddButtonStatus();
  245. }
  246. }
  247. void ManifestWidgetPage::OnMultiGroupAdd(const Uuid& id)
  248. {
  249. if (m_objects.size() >= m_capSize)
  250. {
  251. QMessageBox::warning(this, "Cap reached", QString("The group container reached its cap of %1 entries.\nPlease remove groups to free up space.").
  252. arg(m_capSize));
  253. return;
  254. }
  255. AddNewObject(id);
  256. UpdateAddButtonStatus();
  257. }
  258. void ManifestWidgetPage::BuildAndConnectAddButton()
  259. {
  260. if (m_classTypeIds.size() == 0)
  261. {
  262. ui->m_addButton->setText("No types for this group");
  263. }
  264. else if (m_classTypeIds.size() == 1)
  265. {
  266. AZStd::string className = ClassIdToName(m_classTypeIds[0]);
  267. AZ::SerializeContext* serializeContext = nullptr;
  268. AZ::ComponentApplicationBus::BroadcastResult(
  269. serializeContext, &AZ::ComponentApplicationBus::Events::GetSerializeContext);
  270. AZ_Assert(serializeContext, "No serialize context");
  271. auto classData = serializeContext->FindClassData(m_classTypeIds[0]);
  272. if (classData && classData->m_editData)
  273. {
  274. const AZ::Edit::ElementData* editorElementData =
  275. classData->m_editData->FindElementData(AZ::Edit::ClassElements::EditorData);
  276. if (auto categoryAttribute = editorElementData->FindAttribute(AZ::Edit::Attributes::Max))
  277. {
  278. if (auto categoryAttributeData = azdynamic_cast<const AZ::Edit::AttributeData<int>*>(categoryAttribute))
  279. {
  280. m_capSize = categoryAttributeData->Get(nullptr);
  281. }
  282. }
  283. }
  284. ui->m_addButton->setText(QString::fromLatin1("Add %1").arg(className.c_str()));
  285. connect(ui->m_addButton, &QPushButton::clicked, this, &ManifestWidgetPage::OnSingleGroupAdd);
  286. }
  287. else
  288. {
  289. QMenu* menu = new QMenu();
  290. AZStd::vector<AZStd::string> classNames;
  291. for (Uuid& id : m_classTypeIds)
  292. {
  293. AZStd::string className = ClassIdToName(id);
  294. menu->addAction(className.c_str(),
  295. [this, id]()
  296. {
  297. OnMultiGroupAdd(id);
  298. }
  299. );
  300. AZStd::to_lower(className.begin(), className.end());
  301. classNames.push_back(className);
  302. }
  303. connect(menu, &QMenu::aboutToShow, this,
  304. [this, menu]()
  305. {
  306. menu->setFixedWidth(ui->m_addButton->width());
  307. }
  308. );
  309. ui->m_addButton->setMenu(menu);
  310. AZStd::string buttonText = "Add ";
  311. AzFramework::StringFunc::Join(buttonText, classNames.begin(), classNames.end(), " or ");
  312. ui->m_addButton->setText(buttonText.c_str());
  313. }
  314. }
  315. void ManifestWidgetPage::BuildHelpButton()
  316. {
  317. // Default to the root scene settings page, this is used when:
  318. // * There are no groups available to add.
  319. // * There are multiple groups available to add.
  320. // * The group to add does not have a help URL set.
  321. // * There is an issue retrieving the help URL from the group.
  322. m_helpUrl = "https://www.o3de.org/docs/user-guide/assets/scene-settings/";
  323. if (m_classTypeIds.size() == 1)
  324. {
  325. const SerializeContext::ClassData* classData = m_context->FindClassData(m_classTypeIds[0]);
  326. if (classData && classData->m_editData)
  327. {
  328. const AZ::Edit::ElementData* editorElementData =
  329. classData->m_editData->FindElementData(AZ::Edit::ClassElements::EditorData);
  330. if (auto categoryAttribute = editorElementData->FindAttribute(AZ::Edit::Attributes::HelpPageURL))
  331. {
  332. if (auto categoryAttributeData = azdynamic_cast<const AZ::Edit::AttributeData<const char*>*>(categoryAttribute))
  333. {
  334. const DataTypes::IGroup* sceneNodeGroup = nullptr;
  335. AZStd::string urlValue = categoryAttributeData->Get(&sceneNodeGroup);
  336. if (!urlValue.empty())
  337. {
  338. m_helpUrl = urlValue.c_str();
  339. }
  340. }
  341. }
  342. }
  343. }
  344. connect(ui->m_helpButton, &QPushButton::clicked, this, &ManifestWidgetPage::OnHelpButtonClicked);
  345. }
  346. void ManifestWidgetPage::OnHelpButtonClicked()
  347. {
  348. QDesktopServices::openUrl(QUrl(m_helpUrl));
  349. }
  350. AZStd::string ManifestWidgetPage::ClassIdToName(const Uuid& id) const
  351. {
  352. static const AZStd::string s_groupSuffix = "group";
  353. const SerializeContext::ClassData* classData = m_context->FindClassData(id);
  354. if (!classData)
  355. {
  356. return "<type not registered>";
  357. }
  358. AZStd::string className;
  359. if (classData->m_editData)
  360. {
  361. className = classData->m_editData->m_name;
  362. }
  363. else
  364. {
  365. className = classData->m_name;
  366. }
  367. // Get rid of "Group" suffix and all trailing whitespace (e.g. "mesh group" -> "mesh")
  368. if (className.length() > s_groupSuffix.length())
  369. {
  370. size_t potentialSuffixOffset = className.length() - s_groupSuffix.length();
  371. if (AzFramework::StringFunc::Equal(className.c_str() + potentialSuffixOffset, s_groupSuffix.c_str()))
  372. {
  373. AzFramework::StringFunc::LKeep(className, potentialSuffixOffset - 1);
  374. AzFramework::StringFunc::Strip(className, ' ');
  375. }
  376. }
  377. return className;
  378. }
  379. void ManifestWidgetPage::AddNewObject(const Uuid& id)
  380. {
  381. AZ_TraceContext("Instance id", id);
  382. const SerializeContext::ClassData* classData = m_context->FindClassData(id);
  383. AZ_Assert(classData, "Type not registered.");
  384. if (classData)
  385. {
  386. AZ_TraceContext("Object Type", classData->m_name);
  387. AZ_Assert(classData->m_factory, "Registered type has no factory to create a new instance with.");
  388. if (classData->m_factory)
  389. {
  390. ManifestWidget* parent = ManifestWidget::FindRoot(this);
  391. AZ_Assert(parent, "ManifestWidgetPage isn't docked in a ManifestWidget.");
  392. if (!parent)
  393. {
  394. return;
  395. }
  396. AZStd::shared_ptr<Containers::Scene> scene = parent->GetScene();
  397. if (!scene)
  398. {
  399. return;
  400. }
  401. Containers::SceneManifest& manifest = scene->GetManifest();
  402. void* rawInstance = classData->m_factory->Create(classData->m_name);
  403. AZ_Assert(rawInstance, "Serialization factory failed to construct new instance.");
  404. if (!rawInstance)
  405. {
  406. return;
  407. }
  408. AZStd::shared_ptr<DataTypes::IManifestObject> instance(reinterpret_cast<DataTypes::IManifestObject*>(rawInstance));
  409. EBUS_EVENT(Events::ManifestMetaInfoBus, InitializeObject, *scene, *instance);
  410. if (!manifest.AddEntry(instance))
  411. {
  412. AZ_Assert(false, "Unable to add new object to manifest.");
  413. }
  414. if (!AddObject(instance))
  415. {
  416. AZ_Assert(false, "Unable to add new object to Reflected Property Editor.");
  417. }
  418. // Refresh the page after adding this new object.
  419. RefreshPage();
  420. EmitObjectChanged();
  421. }
  422. }
  423. }
  424. void ManifestWidgetPage::EmitObjectChanged(const DataTypes::IManifestObject* object)
  425. {
  426. ManifestWidget* parent = ManifestWidget::FindRoot(this);
  427. AZ_Assert(parent, "ManifestWidgetPage isn't docked in a ManifestWidget.");
  428. if (!parent)
  429. {
  430. return;
  431. }
  432. AZStd::shared_ptr<Containers::Scene> scene = parent->GetScene();
  433. if (!scene)
  434. {
  435. return;
  436. }
  437. Events::ManifestMetaInfoBus::Broadcast(&Events::ManifestMetaInfoBus::Events::ObjectUpdated, *scene, object, this);
  438. }
  439. void ManifestWidgetPage::ObjectUpdated([[maybe_unused]] const Containers::Scene& scene, const DataTypes::IManifestObject* target, void* sender)
  440. {
  441. if (sender != this && target != nullptr && m_propertyEditor)
  442. {
  443. if (AZStd::find_if(m_objects.begin(), m_objects.end(),
  444. [target](const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  445. {
  446. return object.get() == target;
  447. }) != m_objects.end())
  448. {
  449. m_propertyEditor->InvalidateAttributesAndValues();
  450. }
  451. }
  452. }
  453. void ManifestWidgetPage::AddObjects(AZStd::vector<AZStd::shared_ptr<DataTypes::IManifestObject>>& objects)
  454. {
  455. ManifestWidget* parent = ManifestWidget::FindRoot(this);
  456. AZ_Error(SceneAPI::Utilities::ErrorWindow, parent, "ManifestWidgetPage isn't docked in a ManifestWidget.");
  457. if (!parent)
  458. {
  459. return;
  460. }
  461. AZStd::shared_ptr<Containers::Scene> scene = parent->GetScene();
  462. if (!scene)
  463. {
  464. return;
  465. }
  466. Containers::SceneManifest& manifest = scene->GetManifest();
  467. for (auto& object : objects)
  468. {
  469. if (!SupportsType(object))
  470. {
  471. continue;
  472. }
  473. if (!manifest.AddEntry(object))
  474. {
  475. AZ_Error(SceneAPI::Utilities::ErrorWindow, false, "Unable to add new object to manifest.");
  476. }
  477. else
  478. {
  479. AddObject(object);
  480. }
  481. }
  482. RefreshPage();
  483. }
  484. bool ManifestWidgetPage::SetNodeReadOnlyStatus(const AzToolsFramework::InstanceDataNode* node)
  485. {
  486. if (AzToolsFramework::InstanceDataNode* parentNode = node ? node->GetRoot() : nullptr)
  487. {
  488. AZ::SceneAPI::DataTypes::IGroup* group = m_context->Cast<AZ::SceneAPI::DataTypes::IGroup*>(
  489. parentNode->FirstInstance(), parentNode->GetClassMetadata()->m_typeId);
  490. // If this group is unmodifiable, that means it's read only.
  491. if (group && group->GetRuleContainerConst().FindFirstByType<AZ::SceneAPI::DataTypes::IUnmodifiableRule>())
  492. {
  493. return true;
  494. }
  495. }
  496. return false;
  497. }
  498. void ManifestWidgetPage::AppendUnsavedChangesToTitle(bool hasUnsavedChanges)
  499. {
  500. QString title(ui->m_saveButton->text());
  501. if (hasUnsavedChanges && title.back() != "*")
  502. {
  503. title.push_back("*");
  504. }
  505. else if (!hasUnsavedChanges && title.back() == "*")
  506. {
  507. title.remove(title.size() - 1, 1);
  508. }
  509. ui->m_saveButton->setText(title);
  510. }
  511. void ManifestWidgetPage::AddEditMenu()
  512. {
  513. m_editMenu->clear();
  514. m_editMenu->addAction(
  515. QObject::tr("Reset settings to default..."),
  516. [this]()
  517. {
  518. emit ResetSettings();
  519. });
  520. m_editMenu->addAction(
  521. QObject::tr("Clear unsaved changes..."),
  522. [this]()
  523. {
  524. emit ClearChanges();
  525. });
  526. m_editMenu->addAction(
  527. QObject::tr("Assign build script..."),
  528. [this]()
  529. {
  530. emit AssignScript();
  531. });
  532. }
  533. void ManifestWidgetPage::EnableInspector(bool enableInspector)
  534. {
  535. ui->m_inspectButton->setVisible(enableInspector);
  536. }
  537. } // namespace UI
  538. } // namespace SceneAPI
  539. } // namespace AZ
  540. #include <SceneWidgets/moc_ManifestWidgetPage.cpp>