ManifestVectorWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <QEvent>
  9. #include <QMenu>
  10. #include <QTimer>
  11. #include <QMessageBox>
  12. #include <RowWidgets/ui_ManifestVectorWidget.h>
  13. #include <AzCore/EBus/EBus.h>
  14. #include <AzCore/Component/ComponentApplicationBus.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/std/smart_ptr/shared_ptr.h>
  17. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  18. #include <AzToolsFramework/UI/PropertyEditor/PropertyRowWidget.hxx>
  19. #include <AzToolsFramework/UI/PropertyEditor/ReflectedPropertyEditor.hxx>
  20. #include <AzToolsFramework/Debug/TraceContext.h>
  21. #include <SceneAPI/SceneCore/Containers/Scene.h>
  22. #include <SceneAPI/SceneCore/Containers/SceneManifest.h>
  23. #include <SceneAPI/SceneCore/DataTypes/IManifestObject.h>
  24. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  25. #include <SceneAPI/SceneCore/Events/ManifestMetaInfoBus.h>
  26. #include <SceneAPI/SceneUI/RowWidgets/ManifestVectorWidget.h>
  27. #include <SceneAPI/SceneUI/SceneWidgets/ManifestWidget.h>
  28. namespace AZ
  29. {
  30. namespace SceneAPI
  31. {
  32. namespace UI
  33. {
  34. AZ_CLASS_ALLOCATOR_IMPL(ManifestVectorWidget, SystemAllocator);
  35. ManifestVectorWidget::ManifestVectorWidget(SerializeContext* serializeContext, QWidget* parent)
  36. : QWidget(parent)
  37. , m_serializeContext(serializeContext)
  38. , m_propertyEditor(nullptr)
  39. , m_ui(new Ui::ManifestVectorWidget())
  40. , m_capSize(50)
  41. {
  42. m_ui->setupUi(this);
  43. m_propertyEditor = new AzToolsFramework::ReflectedPropertyEditor(this);
  44. m_propertyEditor->Setup(m_serializeContext, this, false, 175);
  45. m_propertyEditor->show();
  46. m_ui->m_mainLayout->insertWidget(1, m_propertyEditor);
  47. m_ui->m_addObjectButton->setProperty("class", "FixedMenu");
  48. connect(m_ui->m_addObjectButton, &QPushButton::pressed, this, &ManifestVectorWidget::DisplayAddPrompt);
  49. // Add empty menu for visual consistency.
  50. m_ui->m_addObjectButton->setMenu(new QMenu(this));
  51. BusConnect();
  52. }
  53. ManifestVectorWidget::~ManifestVectorWidget()
  54. {
  55. BusDisconnect();
  56. }
  57. void ManifestVectorWidget::SetManifestVector(const ManifestVectorType& manifestVector, DataTypes::IManifestObject* ownerObject)
  58. {
  59. AZ_Assert(ownerObject, "ManifestVectorWidgets must be initialized with a non-null owner object.");
  60. m_manifestVector = manifestVector;
  61. m_ownerObject = ownerObject;
  62. UpdatePropertyGrid();
  63. }
  64. ManifestVectorWidget::ManifestVectorType ManifestVectorWidget::GetManifestVector()
  65. {
  66. return m_manifestVector;
  67. }
  68. void ManifestVectorWidget::SetCollectionName(const AZStd::string& name)
  69. {
  70. m_ui->m_containerTitle->setText(name.c_str());
  71. }
  72. void ManifestVectorWidget::SetCapSize(size_t cap)
  73. {
  74. m_capSize = cap;
  75. }
  76. void ManifestVectorWidget::SetCollectionTypeName(const AZStd::string& typeName)
  77. {
  78. QString addString = QString("Add ") + typeName.c_str();
  79. m_ui->m_addObjectButton->setText(addString);
  80. }
  81. bool ManifestVectorWidget::ContainsManifestObject(const DataTypes::IManifestObject* object) const
  82. {
  83. for (auto& containedObject : m_manifestVector)
  84. {
  85. if (containedObject.get() == object)
  86. {
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. bool ManifestVectorWidget::RemoveManifestObject(const DataTypes::IManifestObject* object)
  93. {
  94. AZ_TraceContext("Remove object type", object->RTTI_GetTypeName());
  95. for (auto it = m_manifestVector.begin(); it < m_manifestVector.end(); ++it)
  96. {
  97. if ((*it).get() == object)
  98. {
  99. object->OnUserRemoved();
  100. m_manifestVector.erase(it);
  101. QTimer::singleShot(0, this,
  102. [this]()
  103. {
  104. UpdatePropertyGrid();
  105. EmitObjectChanged(m_ownerObject);
  106. });
  107. return true;
  108. }
  109. }
  110. AZ_TracePrintf(Utilities::WarningWindow, "Tried to remove an object that was not contained in the vector.");
  111. return false;
  112. }
  113. void ManifestVectorWidget::DisplayAddPrompt()
  114. {
  115. Events::ManifestMetaInfo::ModifiersList availableManifestUUIDs;
  116. ManifestWidget* root = ManifestWidget::FindRoot(this);
  117. AZ_Assert(root, "ManifestVectorWidget is not a child of a ManifestWidget.");
  118. if (!root)
  119. {
  120. return;
  121. }
  122. AZStd::shared_ptr<Containers::Scene> scene = root->GetScene();
  123. if (!scene)
  124. {
  125. return;
  126. }
  127. EBUS_EVENT(Events::ManifestMetaInfoBus, GetAvailableModifiers, availableManifestUUIDs, *scene, *m_ownerObject);
  128. AZ_TraceContext("Parent manifest object type", m_ownerObject->RTTI_GetTypeName());
  129. QMenu* objectMenu = m_ui->m_addObjectButton->menu();
  130. objectMenu->clear();
  131. for (auto& manifestUUID : availableManifestUUIDs)
  132. {
  133. const AZ::SerializeContext::ClassData* manifestClassData = m_serializeContext->FindClassData(manifestUUID);
  134. AZ_TraceContext("Child manifest object UUID", manifestUUID.ToString<AZStd::string>());
  135. if (!manifestClassData)
  136. {
  137. AZ_TracePrintf(Utilities::WarningWindow, "Class data was not registered for class, it will not be available as an option");
  138. continue;
  139. }
  140. AZStd::string displayName;
  141. if (manifestClassData->m_editData && manifestClassData->m_editData->m_name[0] != '\0')
  142. {
  143. displayName = manifestClassData->m_editData->m_name;
  144. }
  145. else if(manifestClassData->m_name[0] != '\0')
  146. {
  147. displayName = manifestClassData->m_name;
  148. }
  149. else
  150. {
  151. AZ_TracePrintf(Utilities::WarningWindow, "Class data did not contain a human readable name for class, it will not be available as an option");
  152. continue;
  153. }
  154. QAction* objectCreateAction = new QAction(displayName.c_str(), m_ui->m_addObjectButton);
  155. connect(objectCreateAction, &QAction::triggered, this,
  156. [this, manifestClassData, displayName]()
  157. {
  158. this->AddNewObject(manifestClassData->m_factory, displayName);
  159. });
  160. objectMenu->addAction(objectCreateAction);
  161. }
  162. }
  163. void ManifestVectorWidget::AddNewObject(SerializeContext::IObjectFactory* factory, const AZStd::string& objectName)
  164. {
  165. if (m_manifestVector.size() >= m_capSize)
  166. {
  167. QMessageBox::warning(this, "Cap reached", QString("The %1 container reached its cap of %2 entries.\nPlease remove entries to free up space.").
  168. arg(m_ui->m_containerTitle->text()).arg(m_capSize));
  169. return;
  170. }
  171. AZ_TraceContext("Object name", objectName);
  172. AZStd::shared_ptr<DataTypes::IManifestObject> newObject(static_cast<DataTypes::IManifestObject*>(factory->Create(objectName.c_str())));
  173. if (newObject)
  174. {
  175. newObject->OnUserAdded();
  176. }
  177. ManifestWidget* root = ManifestWidget::FindRoot(this);
  178. AZ_Assert(root, "ManifestVectorWidget is not a child of a ManifestWidget.");
  179. if (!root)
  180. {
  181. return;
  182. }
  183. AZ_TraceContext("Object type", newObject->RTTI_GetTypeName());
  184. AZStd::shared_ptr<Containers::Scene> scene = root->GetScene();
  185. EBUS_EVENT(Events::ManifestMetaInfoBus, InitializeObject, *scene, *newObject);
  186. m_manifestVector.push_back(newObject);
  187. UpdatePropertyGrid();
  188. EmitObjectChanged(m_ownerObject);
  189. }
  190. void ManifestVectorWidget::UpdatePropertyGrid()
  191. {
  192. QSignalBlocker(this);
  193. m_propertyEditor->ClearInstances();
  194. for (auto &object : m_manifestVector)
  195. {
  196. if(object)
  197. {
  198. m_propertyEditor->AddInstance(object.get(), object->RTTI_GetType());
  199. }
  200. }
  201. m_propertyEditor->InvalidateAll();
  202. m_propertyEditor->ExpandAll();
  203. }
  204. void ManifestVectorWidget::EmitObjectChanged(const DataTypes::IManifestObject* object)
  205. {
  206. emit valueChanged();
  207. ManifestWidget* root = ManifestWidget::FindRoot(this);
  208. AZ_Assert(root, "ManifestVectorWidget is not a child of a ManifestWidget.");
  209. if (!root)
  210. {
  211. return;
  212. }
  213. AZStd::shared_ptr<Containers::Scene> scene = root->GetScene();
  214. if (!scene)
  215. {
  216. return;
  217. }
  218. Events::ManifestMetaInfoBus::Broadcast(&Events::ManifestMetaInfoBus::Events::ObjectUpdated, *scene, object, this);
  219. }
  220. void ManifestVectorWidget::AfterPropertyModified(AzToolsFramework::InstanceDataNode* node)
  221. {
  222. // The immediate parent may not have the manifest object, so check the full ancestry.
  223. while (node && node->GetParent())
  224. {
  225. AzToolsFramework::InstanceDataNode* owner = node->GetParent();
  226. const AZ::SerializeContext::ClassData* classData = owner->GetClassMetadata();
  227. if (classData && classData->m_azRtti)
  228. {
  229. const DataTypes::IManifestObject* cast = classData->m_azRtti->Cast<DataTypes::IManifestObject>(owner->FirstInstance());
  230. if (cast)
  231. {
  232. AZ_Assert(AZStd::find_if(m_manifestVector.begin(), m_manifestVector.end(),
  233. [cast](const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  234. {
  235. return object.get() == cast;
  236. }) != m_manifestVector.end(), "ManifestVectorWidget detected an update of a field it doesn't own.");
  237. EmitObjectChanged(cast);
  238. return;
  239. }
  240. }
  241. node = node->GetParent();
  242. }
  243. }
  244. void ManifestVectorWidget::RequestPropertyContextMenu(AzToolsFramework::InstanceDataNode* /*node*/, const QPoint& /*point*/)
  245. {
  246. }
  247. void ManifestVectorWidget::BeforePropertyModified(AzToolsFramework::InstanceDataNode* /*node*/)
  248. {
  249. }
  250. void ManifestVectorWidget::SetPropertyEditingActive(AzToolsFramework::InstanceDataNode* /*node*/)
  251. {
  252. }
  253. void ManifestVectorWidget::SetPropertyEditingComplete(AzToolsFramework::InstanceDataNode* /*node*/)
  254. {
  255. }
  256. void ManifestVectorWidget::SealUndoStack()
  257. {
  258. }
  259. void ManifestVectorWidget::ObjectUpdated([[maybe_unused]] const Containers::Scene& scene, const DataTypes::IManifestObject* target, void* sender)
  260. {
  261. if (sender != this && target != nullptr && m_propertyEditor)
  262. {
  263. if (AZStd::find_if(m_manifestVector.begin(), m_manifestVector.end(),
  264. [target](const AZStd::shared_ptr<DataTypes::IManifestObject>& object)
  265. {
  266. return object.get() == target;
  267. }) != m_manifestVector.end())
  268. {
  269. m_propertyEditor->InvalidateAttributesAndValues();
  270. }
  271. }
  272. }
  273. } // namespace UI
  274. } // namespace SceneAPI
  275. } // namespace AZ
  276. #include <RowWidgets/moc_ManifestVectorWidget.cpp>