AssetEditorWindow.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "EditorDefs.h"
  9. #include "AssetEditorWindow.h"
  10. // Qt
  11. #include <QMessageBox>
  12. // AzCore
  13. #include <AzCore/Asset/AssetManager.h>
  14. #include <AzCore/UserSettings/UserSettingsComponent.h>
  15. #include <AzCore/Utils/Utils.h>
  16. // AzToolsFramework
  17. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  18. #include <AzToolsFramework/API/ViewPaneOptions.h>
  19. // Editor
  20. #include "LyViewPaneNames.h"
  21. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  22. #include <AssetEditor/ui_AssetEditorWindow.h>
  23. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  24. namespace AssetEditorUtils
  25. {
  26. AssetEditorWindow* CreateAssetEditorWithAsset(const AZ::Data::Asset<AZ::Data::AssetData> assetRef)
  27. {
  28. AssetEditorWindow* assetEditorWindow = new AssetEditorWindow();
  29. if (auto assetsWindowsToRestore = AZ::UserSettings::CreateFind<AzToolsFramework::AssetEditor::AssetEditorWindowSettings>(AZ::Crc32(AzToolsFramework::AssetEditor::AssetEditorWindowSettings::s_name), AZ::UserSettings::CT_GLOBAL))
  30. {
  31. assetsWindowsToRestore->m_openAssets.emplace(assetRef);
  32. }
  33. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequestBus::Events::Save);
  34. auto&& loadedAsset = AZ::Data::AssetManager::Instance().GetAsset(assetRef.GetId(), assetRef.GetType(), assetRef.GetAutoLoadBehavior());
  35. loadedAsset.BlockUntilLoadComplete();
  36. assetEditorWindow->OpenAsset(loadedAsset);
  37. return assetEditorWindow;
  38. }
  39. }
  40. AssetEditorWindow::AssetEditorWindow(QWidget* parent)
  41. : QWidget(parent)
  42. , m_ui(new Ui::AssetEditorWindowClass())
  43. {
  44. using namespace AzToolsFramework::AssetEditor;
  45. m_ui->setupUi(this);
  46. connect(m_ui->m_assetEditorWidget, &AssetEditorWidget::OnAssetSaveFailedSignal, this, &AssetEditorWindow::OnAssetSaveFailed);
  47. connect(m_ui->m_assetEditorWidget, &AssetEditorWidget::OnAssetOpenedSignal, this, &AssetEditorWindow::OnAssetOpened);
  48. BusConnect();
  49. }
  50. AssetEditorWindow::~AssetEditorWindow()
  51. {
  52. using namespace AzToolsFramework::AssetEditor;
  53. BusDisconnect();
  54. }
  55. void AssetEditorWindow::RegisterViewClass(const AZ::Data::Asset<AZ::Data::AssetData>& asset)
  56. {
  57. AzToolsFramework::ViewPaneOptions options;
  58. options.showInMenu = false;
  59. auto& assetName = asset.GetHint();
  60. const char* paneName = assetName.c_str();
  61. AzToolsFramework::RegisterViewPane<AssetEditorWindow>(paneName, LyViewPane::CategoryTools, options, [asset](QWidget*) {return AssetEditorUtils::CreateAssetEditorWithAsset(asset); });
  62. }
  63. void AssetEditorWindow::CreateAsset(const AZ::Data::AssetType& assetType, const AZ::Uuid& interestedComponentId)
  64. {
  65. m_ui->m_assetEditorWidget->CreateAsset(assetType, interestedComponentId);
  66. }
  67. void AssetEditorWindow::OpenAsset(const AZ::Data::Asset<AZ::Data::AssetData>& asset)
  68. {
  69. m_ui->m_assetEditorWidget->OpenAsset(asset);
  70. }
  71. void AssetEditorWindow::OpenAssetById(const AZ::Data::AssetId assetId)
  72. {
  73. AZ::Data::Asset<AZ::Data::AssetData> asset = AZ::Data::AssetManager::Instance().GetAsset<AZ::Data::AssetData>(assetId, AZ::Data::AssetLoadBehavior::NoLoad);
  74. OpenAsset(asset);
  75. }
  76. void AssetEditorWindow::SaveAssetAs(const AZStd::string_view assetPath)
  77. {
  78. if (assetPath.empty())
  79. {
  80. AZ_Warning("Asset Editor", false, "Could not save asset to empty path.");
  81. return;
  82. }
  83. auto absoluteAssetPath = AZ::IO::FixedMaxPath(AZ::Utils::GetEnginePath()) / assetPath;
  84. if (!m_ui->m_assetEditorWidget->SaveAssetToPath(absoluteAssetPath.Native()))
  85. {
  86. AZ_Warning("Asset Editor", false, "File was not saved correctly via SaveAssetAs.");
  87. }
  88. }
  89. void AssetEditorWindow::RegisterViewClass()
  90. {
  91. AzToolsFramework::ViewPaneOptions options;
  92. options.preferedDockingArea = Qt::NoDockWidgetArea;
  93. options.showOnToolsToolbar = true;
  94. options.toolbarIcon = ":/Menu/asset_editor.svg";
  95. AzToolsFramework::RegisterViewPane<AssetEditorWindow>(LyViewPane::AssetEditor, LyViewPane::CategoryTools, options);
  96. }
  97. void AssetEditorWindow::OnAssetOpened(const AZ::Data::Asset<AZ::Data::AssetData>& asset)
  98. {
  99. if (asset)
  100. {
  101. AZStd::string assetPath;
  102. AZStd::string assetName;
  103. AZStd::string extension;
  104. AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetPath, &AZ::Data::AssetCatalogRequests::GetAssetPathById, asset.GetId());
  105. AzFramework::StringFunc::Path::Split(assetPath.c_str(), nullptr, nullptr, &assetName, &extension);
  106. // AZStd::string windowTitle = AZStd::string::format("Edit Asset: %s", (assetName + extension).c_str());
  107. AZStd::string windowTitle = asset.GetHint();
  108. qobject_cast<QWidget*>(parent())->setWindowTitle(tr(windowTitle.c_str()));
  109. }
  110. else
  111. {
  112. qobject_cast<QWidget*>(parent())->setWindowTitle(tr("Asset Editor"));
  113. }
  114. }
  115. void AssetEditorWindow::closeEvent(QCloseEvent* event)
  116. {
  117. if (!m_ui->m_assetEditorWidget->SaveAllAndClose())
  118. {
  119. event->ignore();
  120. }
  121. }
  122. void AssetEditorWindow::OnAssetSaveFailed(const AZStd::string& error)
  123. {
  124. QMessageBox::warning(this, tr("Unable to Save Asset"),
  125. tr(error.c_str()), QMessageBox::Ok, QMessageBox::Ok);
  126. }
  127. #include <AssetEditor/moc_AssetEditorWindow.cpp>