EditorStreamingImageAssetCtrl.cpp 7.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 <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. bool isValid = AzToolsFramework::AssetBrowser::AssetPickerDialog::EvaluateSelection();
  34. // If we have a valid selection (a streaming image asset), we need to also verify
  35. // that its pixel format is supported by the image data retrieval API
  36. if (isValid)
  37. {
  38. const auto productEntry = azrtti_cast<const ProductAssetBrowserEntry*>(m_selection.GetResult());
  39. isValid = Internal::IsImageDataPixelAPISupportedForAsset(productEntry->GetAssetId());
  40. }
  41. return isValid;
  42. }
  43. StreamingImagePropertyAssetCtrl::StreamingImagePropertyAssetCtrl(QWidget* parent)
  44. : AzToolsFramework::PropertyAssetCtrl(parent)
  45. {
  46. }
  47. void StreamingImagePropertyAssetCtrl::PickAssetSelectionFromDialog(AssetSelectionModel& selection, QWidget* parent)
  48. {
  49. // We need to override and use our own picker dialog so that we can
  50. // disable the OK button if a streaming image asset with an unsupported
  51. // format has been selected
  52. SupportedImageAssetPickerDialog dialog(selection, parent);
  53. dialog.exec();
  54. }
  55. void StreamingImagePropertyAssetCtrl::OnAutocomplete(const QModelIndex& index)
  56. {
  57. auto assetId = m_model->GetAssetIdFromIndex(GetSourceIndex(index));
  58. // Only allow the autocompleter to select an asset if it has
  59. // a supported pixel format
  60. if (Internal::IsImageDataPixelAPISupportedForAsset(assetId))
  61. {
  62. SetSelectedAssetID(assetId);
  63. }
  64. }
  65. void StreamingImagePropertyAssetCtrl::UpdateAssetDisplay()
  66. {
  67. AzToolsFramework::PropertyAssetCtrl::UpdateAssetDisplay();
  68. // If there is a valid asset selected but it's not a supported pixel format,
  69. // show the error message state for this property
  70. if (m_selectedAssetID.IsValid() && !Internal::IsImageDataPixelAPISupportedForAsset(m_selectedAssetID))
  71. {
  72. UpdateErrorButtonWithMessage(
  73. AZStd::string::format("Image asset (%s) has an unsupported pixel format", GetCurrentAssetHint().c_str())
  74. );
  75. }
  76. }
  77. AZ::TypeId StreamingImagePropertyHandler::GetHandledType() const
  78. {
  79. return AZ::GetAssetClassId();
  80. }
  81. AZ::u32 StreamingImagePropertyHandler::GetHandlerName() const
  82. {
  83. return AZ_CRC_CE("GradientSignalStreamingImageAsset");
  84. }
  85. bool StreamingImagePropertyHandler::IsDefaultHandler() const
  86. {
  87. // We don't this to be registered as a default handler, because we don't want
  88. // any other AZ::RPI::StreamingImageAsset fields using this handler.
  89. // We only want this handler to be used if it was explicitly requested by name,
  90. // which in this case is for the image gradient asset since it needs to validate
  91. // the format is supported by the pixel retrieval API.
  92. return false;
  93. }
  94. QWidget* StreamingImagePropertyHandler::GetFirstInTabOrder(StreamingImagePropertyAssetCtrl* widget)
  95. {
  96. return widget->GetFirstInTabOrder();
  97. }
  98. QWidget* StreamingImagePropertyHandler::GetLastInTabOrder(StreamingImagePropertyAssetCtrl* widget)
  99. {
  100. return widget->GetLastInTabOrder();
  101. }
  102. void StreamingImagePropertyHandler::UpdateWidgetInternalTabbing(StreamingImagePropertyAssetCtrl* widget)
  103. {
  104. widget->UpdateTabOrder();
  105. }
  106. QWidget* StreamingImagePropertyHandler::CreateGUI(QWidget* parent)
  107. {
  108. // This is the same logic as the AssetPropertyHandlerDefault, only we create our own
  109. // StreamingImagePropertyAssetCtrl instead for the GUI widget
  110. StreamingImagePropertyAssetCtrl* newCtrl = aznew StreamingImagePropertyAssetCtrl(parent);
  111. QObject::connect(newCtrl, &StreamingImagePropertyAssetCtrl::OnAssetIDChanged, this, [newCtrl](AZ::Data::AssetId newAssetID)
  112. {
  113. (void)newAssetID;
  114. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, newCtrl);
  115. AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::OnEditingFinished, newCtrl);
  116. });
  117. return newCtrl;
  118. }
  119. void StreamingImagePropertyHandler::ConsumeAttribute(StreamingImagePropertyAssetCtrl* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  120. {
  121. // Let ConsumeAttributeForPropertyAssetCtrl handle all of the attributes
  122. AzToolsFramework::ConsumeAttributeForPropertyAssetCtrl(GUI, attrib, attrValue, debugName);
  123. }
  124. void StreamingImagePropertyHandler::WriteGUIValuesIntoProperty(size_t index, StreamingImagePropertyAssetCtrl* GUI, property_t& instance, AzToolsFramework::InstanceDataNode* node)
  125. {
  126. // Let the AssetPropertyHandlerDefault handle writing the GUI value into the property
  127. AzToolsFramework::AssetPropertyHandlerDefault::WriteGUIValuesIntoPropertyInternal(index, GUI, instance, node);
  128. }
  129. bool StreamingImagePropertyHandler::ReadValuesIntoGUI(size_t index, StreamingImagePropertyAssetCtrl* GUI, const property_t& instance, AzToolsFramework::InstanceDataNode* node)
  130. {
  131. // Let the AssetPropertyHandlerDefault handle reading values into the GUI
  132. return AzToolsFramework::AssetPropertyHandlerDefault::ReadValuesIntoGUIInternal(index, GUI, instance, node);
  133. }
  134. 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
  135. {
  136. // Let the AssetPropertyHandlerDefault handle the downcast
  137. return AzToolsFramework::AssetPropertyHandlerDefault::CastToInternal(instance, node);
  138. }
  139. void StreamingImagePropertyHandler::Register()
  140. {
  141. using namespace AzToolsFramework;
  142. PropertyTypeRegistrationMessages::Bus::Broadcast(
  143. &PropertyTypeRegistrationMessages::Bus::Events::RegisterPropertyType, aznew StreamingImagePropertyHandler());
  144. }
  145. }