3
0

SharedPreviewUtils.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <API/EditorAssetSystemAPI.h>
  9. #include <AssetBrowser/Thumbnails/ProductThumbnail.h>
  10. #include <AssetBrowser/Thumbnails/SourceThumbnail.h>
  11. #include <Atom/Feature/Utils/LightingPreset.h>
  12. #include <Atom/Feature/Utils/ModelPreset.h>
  13. #include <Atom/RPI.Edit/Common/AssetUtils.h>
  14. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  15. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  16. #include <Atom/RPI.Reflect/Material/MaterialTypeAsset.h>
  17. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  18. #include <Atom/RPI.Reflect/System/AnyAsset.h>
  19. #include <AtomToolsFramework/Util/Util.h>
  20. #include <SharedPreview/SharedPreviewUtils.h>
  21. namespace AZ
  22. {
  23. namespace LyIntegration
  24. {
  25. namespace SharedPreviewUtils
  26. {
  27. AZStd::vector<AZ::Uuid> GetSupportedAssetTypes()
  28. {
  29. return { RPI::ModelAsset::RTTI_Type(), RPI::MaterialAsset::RTTI_Type(), RPI::MaterialTypeAsset::RTTI_Type(),
  30. RPI::AnyAsset::RTTI_Type() };
  31. }
  32. bool IsSupportedAssetType(AzToolsFramework::Thumbnailer::SharedThumbnailKey key)
  33. {
  34. return GetSupportedAssetInfo(key).m_assetId.IsValid();
  35. }
  36. AZ::Data::AssetInfo GetSupportedAssetInfo(AzToolsFramework::Thumbnailer::SharedThumbnailKey key)
  37. {
  38. AZStd::vector<AZ::Data::AssetInfo> productsAssetInfo;
  39. // if it's a source thumbnail key, find first product with a matching asset type
  40. auto sourceKey = azrtti_cast<const AzToolsFramework::AssetBrowser::SourceThumbnailKey*>(key.data());
  41. if (sourceKey)
  42. {
  43. bool foundIt = false;
  44. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(
  45. foundIt, &AzToolsFramework::AssetSystemRequestBus::Events::GetAssetsProducedBySourceUUID,
  46. sourceKey->GetSourceUuid(), productsAssetInfo);
  47. }
  48. // if it's a product thumbnail key just return its assetInfo
  49. auto productKey = azrtti_cast<const AzToolsFramework::AssetBrowser::ProductThumbnailKey*>(key.data());
  50. if (productKey)
  51. {
  52. AZ::Data::AssetInfo assetInfo;
  53. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  54. assetInfo, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetInfoById, productKey->GetAssetId());
  55. productsAssetInfo.push_back(assetInfo);
  56. }
  57. // Search the product assets for a matching asset type ID in priority order of the supported type IDs
  58. for (const auto& typeId : GetSupportedAssetTypes())
  59. {
  60. for (const auto& assetInfo : productsAssetInfo)
  61. {
  62. if (assetInfo.m_assetType == typeId)
  63. {
  64. const auto& path = AZ::RPI::AssetUtils::GetSourcePathByAssetId(assetInfo.m_assetId);
  65. if (!AtomToolsFramework::IsDocumentPathPreviewable(path))
  66. {
  67. continue;
  68. }
  69. // Reject any assets that don't match supported source file extensions
  70. if (assetInfo.m_assetType == RPI::AnyAsset::RTTI_Type())
  71. {
  72. if (!path.ends_with(AZ::Render::LightingPreset::Extension) &&
  73. !path.ends_with(AZ::Render::ModelPreset::Extension))
  74. {
  75. continue;
  76. }
  77. }
  78. return assetInfo;
  79. }
  80. }
  81. }
  82. return AZ::Data::AssetInfo();
  83. }
  84. AZ::Data::AssetId GetAssetIdForProductPath(const AZStd::string_view productPath)
  85. {
  86. if (!productPath.empty())
  87. {
  88. return AZ::RPI::AssetUtils::GetAssetIdForProductPath(productPath.data());
  89. }
  90. return AZ::Data::AssetId();
  91. }
  92. QString WordWrap(const QString& string, int maxLength)
  93. {
  94. QString result;
  95. int length = 0;
  96. for (const QChar& c : string)
  97. {
  98. if (c == '\n')
  99. {
  100. length = 0;
  101. }
  102. else if (length > maxLength)
  103. {
  104. result.append('\n');
  105. length = 0;
  106. }
  107. else
  108. {
  109. length++;
  110. }
  111. result.append(c);
  112. }
  113. return result;
  114. }
  115. } // namespace SharedPreviewUtils
  116. } // namespace LyIntegration
  117. } // namespace AZ