SourceHandlePropertyAssetCtrl.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <ScriptCanvas/Components/EditorUtils.h>
  9. #include <Editor/View/Widgets/SourceHandlePropertyAssetCtrl.h>
  10. #include <AzFramework/StringFunc/StringFunc.h>
  11. #include <AzToolsFramework/AssetBrowser/AssetBrowserBus.h>
  12. #include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
  13. #include <AzToolsFramework/UI/PropertyEditor/Model/AssetCompleterModel.h>
  14. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  15. namespace ScriptCanvasEditor
  16. {
  17. SourceHandlePropertyAssetCtrl::SourceHandlePropertyAssetCtrl(QWidget* parent)
  18. : AzToolsFramework::PropertyAssetCtrl(parent)
  19. {
  20. }
  21. AzToolsFramework::AssetBrowser::AssetSelectionModel SourceHandlePropertyAssetCtrl::GetAssetSelectionModel()
  22. {
  23. auto selectionModel = AssetSelectionModel::SourceAssetTypeSelection(m_sourceAssetFilterPattern);
  24. selectionModel.SetTitle(m_title);
  25. return selectionModel;
  26. }
  27. void SourceHandlePropertyAssetCtrl::PopupAssetPicker()
  28. {
  29. // Request the AssetBrowser Dialog and set a type filter
  30. AssetSelectionModel selection = GetAssetSelectionModel();
  31. selection.SetSelectedFilePath(m_selectedSourcePath.c_str());
  32. AZStd::string defaultDirectory;
  33. if (m_defaultDirectoryCallback)
  34. {
  35. m_defaultDirectoryCallback->Invoke(m_editNotifyTarget, defaultDirectory);
  36. selection.SetDefaultDirectory(defaultDirectory);
  37. }
  38. AssetBrowserComponentRequestBus::Broadcast(&AssetBrowserComponentRequests::PickAssets, selection, parentWidget());
  39. if (selection.IsValid())
  40. {
  41. const auto source = azrtti_cast<const SourceAssetBrowserEntry*>(selection.GetResult());
  42. AZ_Assert(source, "Incorrect entry type selected. Expected source.");
  43. if (source)
  44. {
  45. SetSelectedSourcePath(source->GetFullPath());
  46. }
  47. }
  48. }
  49. void SourceHandlePropertyAssetCtrl::ClearAssetInternal()
  50. {
  51. SetSelectedSourcePath("");
  52. PropertyAssetCtrl::ClearAssetInternal();
  53. }
  54. void SourceHandlePropertyAssetCtrl::ConfigureAutocompleter()
  55. {
  56. if (m_completerIsConfigured)
  57. {
  58. return;
  59. }
  60. AzToolsFramework::PropertyAssetCtrl::ConfigureAutocompleter();
  61. AssetSelectionModel selection = GetAssetSelectionModel();
  62. m_model->SetFetchEntryType(AssetBrowserEntry::AssetEntryType::Source);
  63. m_model->SetFilter(selection.GetDisplayFilter());
  64. }
  65. void SourceHandlePropertyAssetCtrl::SetSourceAssetFilterPattern(const QRegExp& filterPattern)
  66. {
  67. m_sourceAssetFilterPattern = filterPattern;
  68. }
  69. AZ::IO::Path SourceHandlePropertyAssetCtrl::GetSelectedSourcePath() const
  70. {
  71. return m_selectedSourcePath;
  72. }
  73. void SourceHandlePropertyAssetCtrl::SetSelectedSourcePath(const AZ::IO::Path& sourcePath)
  74. {
  75. m_selectedSourcePath = sourcePath;
  76. AZStd::string displayText;
  77. if (!sourcePath.empty())
  78. {
  79. AzFramework::StringFunc::Path::GetFileName(sourcePath.c_str(), displayText);
  80. }
  81. m_browseEdit->setText(displayText.c_str());
  82. // The AssetID gets ignored, the only important bit is triggering the change for the RequestWrite
  83. emit OnAssetIDChanged(AZ::Data::AssetId());
  84. }
  85. void SourceHandlePropertyAssetCtrl::OnAutocomplete(const QModelIndex& index)
  86. {
  87. SetSelectedSourcePath(m_model->GetPathFromIndex(GetSourceIndex(index)));
  88. }
  89. QWidget* SourceHandlePropertyHandler::CreateGUI(QWidget* pParent)
  90. {
  91. SourceHandlePropertyAssetCtrl* newCtrl = aznew SourceHandlePropertyAssetCtrl(pParent);
  92. connect(newCtrl, &SourceHandlePropertyAssetCtrl::OnAssetIDChanged, this, [newCtrl](AZ::Data::AssetId newAssetID)
  93. {
  94. (void)newAssetID;
  95. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, newCtrl);
  96. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::OnEditingFinished, newCtrl);
  97. });
  98. return newCtrl;
  99. }
  100. void SourceHandlePropertyHandler::ConsumeAttribute(SourceHandlePropertyAssetCtrl* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  101. {
  102. // Let ConsumeAttributeForPropertyAssetCtrl handle all of the common attributes
  103. AzToolsFramework::ConsumeAttributeForPropertyAssetCtrl(GUI, attrib, attrValue, debugName);
  104. if (attrib == AZ::Edit::Attributes::SourceAssetFilterPattern)
  105. {
  106. AZStd::string filterPattern;
  107. if (attrValue->Read<AZStd::string>(filterPattern))
  108. {
  109. GUI->SetSourceAssetFilterPattern(QRegExp(filterPattern.c_str(), Qt::CaseInsensitive, QRegExp::Wildcard));
  110. }
  111. }
  112. }
  113. void SourceHandlePropertyHandler::WriteGUIValuesIntoProperty
  114. ( [[maybe_unused]] size_t index
  115. , [[maybe_unused]] SourceHandlePropertyAssetCtrl* GUI
  116. , [[maybe_unused]] property_t& instance
  117. , [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
  118. {
  119. (void)index;
  120. (void)node;
  121. auto sourceHandle = SourceHandle::FromRelativePath(nullptr, GUI->GetSelectedSourcePath());
  122. auto completeSourceHandle = CompleteDescription(sourceHandle);
  123. if (completeSourceHandle)
  124. {
  125. instance = property_t(*CompleteDescription(sourceHandle));
  126. }
  127. else
  128. {
  129. instance = property_t();
  130. }
  131. }
  132. bool SourceHandlePropertyHandler::ReadValuesIntoGUI(size_t index, SourceHandlePropertyAssetCtrl* GUI, const property_t& instance, AzToolsFramework::InstanceDataNode* node)
  133. {
  134. (void)index;
  135. (void)node;
  136. GUI->blockSignals(true);
  137. GUI->SetSelectedSourcePath(instance.RelativePath());
  138. const AzToolsFramework::InstanceDataNode* parentNode = node->GetParent();
  139. AZ_Assert(parentNode && parentNode->HasInstances(), "Configuration instance is missing.");
  140. // Set notify target to the parent configuration instance.
  141. GUI->SetEditNotifyTarget(parentNode->FirstInstance());
  142. GUI->blockSignals(false);
  143. return false;
  144. }
  145. }