SharedThumbnail.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <AzToolsFramework/API/EditorAssetSystemAPI.h>
  9. #include <QtConcurrent/QtConcurrent>
  10. #include <SharedPreview/SharedPreviewUtils.h>
  11. #include <SharedPreview/SharedThumbnail.h>
  12. namespace AZ
  13. {
  14. namespace LyIntegration
  15. {
  16. static constexpr const int SharedThumbnailSize = 256;
  17. //////////////////////////////////////////////////////////////////////////
  18. // SharedThumbnail
  19. //////////////////////////////////////////////////////////////////////////
  20. SharedThumbnail::SharedThumbnail(AzToolsFramework::Thumbnailer::SharedThumbnailKey key)
  21. : Thumbnail(key)
  22. , m_assetInfo(SharedPreviewUtils::GetSupportedAssetInfo(key))
  23. {
  24. if (m_assetInfo.m_assetId.IsValid())
  25. {
  26. AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Handler::BusConnect(m_key);
  27. AzFramework::AssetCatalogEventBus::Handler::BusConnect();
  28. return;
  29. }
  30. AZ_Error("SharedThumbnail", false, "Failed to find matching assetId for the thumbnailKey.");
  31. m_state = State::Failed;
  32. }
  33. SharedThumbnail::~SharedThumbnail()
  34. {
  35. AzToolsFramework::Thumbnailer::ThumbnailerRendererNotificationBus::Handler::BusDisconnect();
  36. AzFramework::AssetCatalogEventBus::Handler::BusDisconnect();
  37. }
  38. void SharedThumbnail::Load()
  39. {
  40. m_state = State::Loading;
  41. AzToolsFramework::Thumbnailer::ThumbnailerRendererRequestBus::QueueEvent(
  42. m_assetInfo.m_assetType, &AzToolsFramework::Thumbnailer::ThumbnailerRendererRequests::RenderThumbnail, m_key,
  43. SharedThumbnailSize);
  44. }
  45. void SharedThumbnail::ThumbnailRendered(const QPixmap& thumbnailImage)
  46. {
  47. m_pixmap = thumbnailImage;
  48. m_state = State::Ready;
  49. QueueThumbnailUpdated();
  50. }
  51. void SharedThumbnail::ThumbnailFailedToRender()
  52. {
  53. m_state = State::Failed;
  54. QueueThumbnailUpdated();
  55. }
  56. void SharedThumbnail::OnCatalogAssetChanged([[maybe_unused]] const AZ::Data::AssetId& assetId)
  57. {
  58. if (m_assetInfo.m_assetId == assetId && (m_state == State::Ready || m_state == State::Failed))
  59. {
  60. m_state = State::Unloaded;
  61. Load();
  62. }
  63. }
  64. void SharedThumbnail::OnCatalogAssetRemoved(const AZ::Data::AssetId& assetId, const AZ::Data::AssetInfo& /*assetInfo*/)
  65. {
  66. if (m_assetInfo.m_assetId == assetId)
  67. {
  68. // Removing the thumbnail from the catalog asset doesn't remove it from the thumbnail cache.
  69. // Therefore marking the state as unloaded ensures that a new pixmap will be rendered on the next access.
  70. m_state = State::Unloaded;
  71. }
  72. }
  73. //////////////////////////////////////////////////////////////////////////
  74. // SharedThumbnailCache
  75. //////////////////////////////////////////////////////////////////////////
  76. SharedThumbnailCache::SharedThumbnailCache()
  77. : ThumbnailCache<SharedThumbnail>()
  78. {
  79. }
  80. SharedThumbnailCache::~SharedThumbnailCache() = default;
  81. int SharedThumbnailCache::GetPriority() const
  82. {
  83. // Custom thumbnails have a higher priority to override default source thumbnails
  84. return 1;
  85. }
  86. const char* SharedThumbnailCache::GetProviderName() const
  87. {
  88. return ProviderName;
  89. }
  90. bool SharedThumbnailCache::IsSupportedThumbnail(AzToolsFramework::Thumbnailer::SharedThumbnailKey key) const
  91. {
  92. return SharedPreviewUtils::IsSupportedAssetType(key);
  93. }
  94. } // namespace LyIntegration
  95. } // namespace AZ
  96. #include <SharedPreview/moc_SharedThumbnail.cpp>