EditorStreamingImageAssetCtrl.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <Atom/RPI.Public/RPIUtils.h>
  9. #include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
  10. #include <AzToolsFramework/UI/PropertyEditor/Model/AssetCompleterModel.h>
  11. #include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
  12. #include <Editor/EditorStreamingImageAssetCtrl.h>
  13. namespace GradientSignal
  14. {
  15. namespace Internal
  16. {
  17. bool IsImageDataPixelAPISupportedForAsset(const AZ::Data::AssetId& assetId)
  18. {
  19. auto streamingImageAsset = AZ::Data::AssetManager::Instance().GetAsset<AZ::RPI::StreamingImageAsset>(assetId, AZ::Data::AssetLoadBehavior::PreLoad);
  20. streamingImageAsset.BlockUntilLoadComplete();
  21. // Verify if the streaming image asset has a pixel format that is supported
  22. // for the image data pixel retrieval API
  23. AZ::RHI::Format format = streamingImageAsset->GetImageDescriptor().m_format;
  24. return AZ::RPI::IsImageDataPixelAPISupported(format);
  25. }
  26. }
  27. SupportedImageAssetPickerDialog::SupportedImageAssetPickerDialog(AssetSelectionModel& selection, QWidget* parent)
  28. : AzToolsFramework::AssetBrowser::AssetPickerDialog(selection, parent)
  29. {
  30. }
  31. bool SupportedImageAssetPickerDialog::EvaluateSelection() const
  32. {
  33. using namespace AzToolsFramework::AssetBrowser;
  34. if (!AssetPickerDialog::EvaluateSelection())
  35. {
  36. return false;
  37. }
  38. // note that this function is called as part of the internals of the asset picker dialog
  39. // and that the selection currently selected refers to the actual entry picked in the UI by the user
  40. // and not necessarily the products of that file. The user could have clicked on a source file, a folder, etc.
  41. // If we have a valid selection (a streaming image asset), we need to also verify
  42. // that its pixel format is supported by the image data retrieval API
  43. const AssetBrowserEntry* entry = m_selection.GetResult();
  44. if (!entry)
  45. {
  46. return false;
  47. }
  48. if ((entry->GetEntryType() != AssetBrowserEntry::AssetEntryType::Source) && (entry->GetEntryType() != AssetBrowserEntry::AssetEntryType::Product))
  49. {
  50. return false;
  51. }
  52. bool foundValidImage = false;
  53. entry->VisitDown( // checks itself, and all its children.
  54. [&](const auto& currentEntry)
  55. {
  56. if (const auto productEntry = azrtti_cast<const ProductAssetBrowserEntry*>(currentEntry))
  57. {
  58. if (productEntry->GetAssetType() == AZ::AzTypeInfo<AZ::RPI::StreamingImageAsset>::Uuid())
  59. {
  60. if (Internal::IsImageDataPixelAPISupportedForAsset(productEntry->GetAssetId()))
  61. {
  62. foundValidImage = true;
  63. return false; // returning false from the visitor stops it from continuing to search.
  64. }
  65. }
  66. }
  67. return true; // continue searching for more...
  68. });
  69. return foundValidImage;
  70. }
  71. StreamingImagePropertyAssetCtrl::StreamingImagePropertyAssetCtrl(QWidget* parent)
  72. : AzToolsFramework::PropertyAssetCtrl(parent)
  73. {
  74. }
  75. void StreamingImagePropertyAssetCtrl::PickAssetSelectionFromDialog(AssetSelectionModel& selection, QWidget* parent)
  76. {
  77. // We need to override and use our own picker dialog so that we can
  78. // disable the OK button if a streaming image asset with an unsupported
  79. // format has been selected
  80. SupportedImageAssetPickerDialog dialog(selection, parent);
  81. dialog.exec();
  82. }
  83. bool StreamingImagePropertyAssetCtrl::CanAcceptAsset(const AZ::Data::AssetId& assetId, const AZ::Data::AssetType& assetType) const
  84. {
  85. using namespace AzToolsFramework::AssetBrowser;
  86. if (!PropertyAssetCtrl::CanAcceptAsset(assetId, assetType))
  87. {
  88. return false;
  89. }
  90. // If the asset is a streaming image asset, we need to verify that its pixel format
  91. // is supported by the image data retrieval API
  92. if (assetType == AZ::AzTypeInfo<AZ::RPI::StreamingImageAsset>::Uuid())
  93. {
  94. return Internal::IsImageDataPixelAPISupportedForAsset(assetId);
  95. }
  96. return false;
  97. }
  98. void StreamingImagePropertyAssetCtrl::OnAutocomplete(const QModelIndex& index)
  99. {
  100. auto assetId = m_model->GetAssetIdFromIndex(GetSourceIndex(index));
  101. // Only allow the autocompleter to select an asset if it has
  102. // a supported pixel format
  103. if (Internal::IsImageDataPixelAPISupportedForAsset(assetId))
  104. {
  105. SetSelectedAssetID(assetId);
  106. }
  107. }
  108. void StreamingImagePropertyAssetCtrl::UpdateAssetDisplay()
  109. {
  110. AzToolsFramework::PropertyAssetCtrl::UpdateAssetDisplay();
  111. // If there is a valid asset selected but it's not a supported pixel format,
  112. // show the error message state for this property
  113. if (m_selectedAssetID.IsValid() && !Internal::IsImageDataPixelAPISupportedForAsset(m_selectedAssetID))
  114. {
  115. UpdateErrorButtonWithMessage(
  116. AZStd::string::format("Image asset (%s) has an unsupported pixel format", GetCurrentAssetHint().c_str())
  117. );
  118. }
  119. }
  120. AZ::TypeId StreamingImagePropertyHandler::GetHandledType() const
  121. {
  122. return AZ::GetAssetClassId();
  123. }
  124. AZ::u32 StreamingImagePropertyHandler::GetHandlerName() const
  125. {
  126. return AZ_CRC_CE("GradientSignalStreamingImageAsset");
  127. }
  128. bool StreamingImagePropertyHandler::IsDefaultHandler() const
  129. {
  130. // We don't this to be registered as a default handler, because we don't want
  131. // any other AZ::RPI::StreamingImageAsset fields using this handler.
  132. // We only want this handler to be used if it was explicitly requested by name,
  133. // which in this case is for the image gradient asset since it needs to validate
  134. // the format is supported by the pixel retrieval API.
  135. return false;
  136. }
  137. QWidget* StreamingImagePropertyHandler::GetFirstInTabOrder(StreamingImagePropertyAssetCtrl* widget)
  138. {
  139. return widget->GetFirstInTabOrder();
  140. }
  141. QWidget* StreamingImagePropertyHandler::GetLastInTabOrder(StreamingImagePropertyAssetCtrl* widget)
  142. {
  143. return widget->GetLastInTabOrder();
  144. }
  145. void StreamingImagePropertyHandler::UpdateWidgetInternalTabbing(StreamingImagePropertyAssetCtrl* widget)
  146. {
  147. widget->UpdateTabOrder();
  148. }
  149. QWidget* StreamingImagePropertyHandler::CreateGUI(QWidget* parent)
  150. {
  151. // This is the same logic as the AssetPropertyHandlerDefault, only we create our own
  152. // StreamingImagePropertyAssetCtrl instead for the GUI widget
  153. StreamingImagePropertyAssetCtrl* newCtrl = aznew StreamingImagePropertyAssetCtrl(parent);
  154. QObject::connect(newCtrl, &StreamingImagePropertyAssetCtrl::OnAssetIDChanged, this, [newCtrl](AZ::Data::AssetId newAssetID)
  155. {
  156. (void)newAssetID;
  157. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, newCtrl);
  158. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::OnEditingFinished, newCtrl);
  159. });
  160. return newCtrl;
  161. }
  162. void StreamingImagePropertyHandler::ConsumeAttribute(StreamingImagePropertyAssetCtrl* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  163. {
  164. // Let ConsumeAttributeForPropertyAssetCtrl handle all of the attributes
  165. AzToolsFramework::ConsumeAttributeForPropertyAssetCtrl(GUI, attrib, attrValue, debugName);
  166. }
  167. void StreamingImagePropertyHandler::WriteGUIValuesIntoProperty(size_t index, StreamingImagePropertyAssetCtrl* GUI, property_t& instance, AzToolsFramework::InstanceDataNode* node)
  168. {
  169. // Let the AssetPropertyHandlerDefault handle writing the GUI value into the property
  170. AzToolsFramework::AssetPropertyHandlerDefault::WriteGUIValuesIntoPropertyInternal(index, GUI, instance, node);
  171. }
  172. bool StreamingImagePropertyHandler::ReadValuesIntoGUI(size_t index, StreamingImagePropertyAssetCtrl* GUI, const property_t& instance, AzToolsFramework::InstanceDataNode* node)
  173. {
  174. // Let the AssetPropertyHandlerDefault handle reading values into the GUI
  175. return AzToolsFramework::AssetPropertyHandlerDefault::ReadValuesIntoGUIInternal(index, GUI, instance, node);
  176. }
  177. AZ::Data::Asset<AZ::Data::AssetData>* StreamingImagePropertyHandler::CastTo(void* instance, const AzToolsFramework::InstanceDataNode* node, [[maybe_unused]] const AZ::Uuid& fromId, [[maybe_unused]] const AZ::Uuid& toId) const
  178. {
  179. // Let the AssetPropertyHandlerDefault handle the downcast
  180. return AzToolsFramework::AssetPropertyHandlerDefault::CastToInternal(instance, node);
  181. }
  182. void StreamingImagePropertyHandler::Register()
  183. {
  184. using namespace AzToolsFramework;
  185. PropertyTypeRegistrationMessages::Bus::Broadcast(
  186. &PropertyTypeRegistrationMessages::Bus::Events::RegisterPropertyType, aznew StreamingImagePropertyHandler());
  187. }
  188. }