AssetBrowserContextProvider.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <AssetBrowserContextProvider.h>
  9. #include <AssetImporterPlugin.h>
  10. #include <QtCore/QMimeData>
  11. #include <QMenu>
  12. #include <AzToolsFramework/AssetBrowser/AssetBrowserEntry.h>
  13. #include <SceneAPI/SceneCore/Events/AssetImportRequest.h>
  14. #include <AzToolsFramework/API/EditorAssetSystemAPI.h> // for ebus events
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. namespace AZ
  17. {
  18. using namespace AzToolsFramework::AssetBrowser;
  19. AssetBrowserContextProvider::AssetBrowserContextProvider()
  20. {
  21. AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Handler::BusConnect();
  22. AzToolsFramework::AssetBrowser::AssetBrowserPreviewRequestBus::Handler::BusConnect();
  23. }
  24. AssetBrowserContextProvider::~AssetBrowserContextProvider()
  25. {
  26. AzToolsFramework::AssetBrowser::AssetBrowserPreviewRequestBus::Handler::BusDisconnect();
  27. AzToolsFramework::AssetBrowser::AssetBrowserInteractionNotificationBus::Handler::BusDisconnect();
  28. }
  29. bool AssetBrowserContextProvider::HandlesSource(const AzToolsFramework::AssetBrowser::SourceAssetBrowserEntry* entry) const
  30. {
  31. AZStd::unordered_set<AZStd::string> extensions;
  32. AZ::SceneAPI::Events::AssetImportRequestBus::Broadcast(
  33. &AZ::SceneAPI::Events::AssetImportRequestBus::Events::GetSupportedFileExtensions, extensions);
  34. if (extensions.empty())
  35. {
  36. return false;
  37. }
  38. AZStd::string targetExtension = entry->GetExtension();
  39. for (const AZStd::string& potentialExtension : extensions)
  40. {
  41. const char* extension = potentialExtension.c_str();
  42. if (AzFramework::StringFunc::Equal(extension, targetExtension.c_str()))
  43. {
  44. return true;
  45. }
  46. }
  47. return false;
  48. }
  49. AzToolsFramework::AssetBrowser::SourceFileDetails AssetBrowserContextProvider::GetSourceFileDetails(const char* fullSourceFileName)
  50. {
  51. AZStd::string extensionString;
  52. if (AzFramework::StringFunc::Path::GetExtension(fullSourceFileName, extensionString, true))
  53. {
  54. // this does include the "." in the extension.
  55. AZStd::unordered_set<AZStd::string> extensions;
  56. AZ::SceneAPI::Events::AssetImportRequestBus::Broadcast(
  57. &AZ::SceneAPI::Events::AssetImportRequestBus::Events::GetSupportedFileExtensions, extensions);
  58. for (AZStd::string potentialExtension : extensions)
  59. {
  60. if (AzFramework::StringFunc::Equal(extensionString.c_str(), potentialExtension.c_str()))
  61. {
  62. return AzToolsFramework::AssetBrowser::SourceFileDetails("Icons/AssetBrowser/FBX_80.svg");
  63. }
  64. }
  65. }
  66. return AzToolsFramework::AssetBrowser::SourceFileDetails();
  67. }
  68. void AssetBrowserContextProvider::PreviewSceneSettings(const AzToolsFramework::AssetBrowser::AssetBrowserEntry* selectedEntry)
  69. {
  70. using namespace AzToolsFramework;
  71. if (const SourceAssetBrowserEntry* sourceEntry = azrtti_cast<const SourceAssetBrowserEntry*>(selectedEntry))
  72. {
  73. if (HandlesSource(sourceEntry) && sourceEntry != m_currentEntry)
  74. {
  75. if (AssetImporterPlugin::GetInstance()->EditImportSettings(sourceEntry->GetFullPath()))
  76. {
  77. m_currentEntry = sourceEntry;
  78. }
  79. }
  80. }
  81. }
  82. bool AssetBrowserContextProvider::HandleSource(const AzToolsFramework::AssetBrowser::AssetBrowserEntry* selectedEntry) const
  83. {
  84. using namespace AzToolsFramework;
  85. if (const SourceAssetBrowserEntry* sourceEntry = azrtti_cast<const SourceAssetBrowserEntry*>(selectedEntry))
  86. {
  87. return HandlesSource(sourceEntry);
  88. }
  89. return false;
  90. }
  91. QMainWindow* AssetBrowserContextProvider::GetSceneSettings()
  92. {
  93. return AssetImporterPlugin::GetInstance()->OpenImportSettings();
  94. }
  95. bool AssetBrowserContextProvider::SaveBeforeClosing()
  96. {
  97. return AssetImporterPlugin::GetInstance()->SaveBeforeClosing();
  98. }
  99. }