MaterialEditorWindow.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/RHI/Factory.h>
  9. #include <Atom/RPI.Edit/Material/MaterialSourceData.h>
  10. #include <Atom/RPI.Edit/Material/MaterialTypeSourceData.h>
  11. #include <AtomToolsFramework/Document/AtomToolsDocumentSystemRequestBus.h>
  12. #include <AtomToolsFramework/Util/Util.h>
  13. #include <AzQtComponents/Components/StyleManager.h>
  14. #include <AzQtComponents/Components/WindowDecorationWrapper.h>
  15. #include <Document/MaterialDocumentRequestBus.h>
  16. #include <Viewport/MaterialViewportWidget.h>
  17. #include <Window/CreateMaterialDialog/CreateMaterialDialog.h>
  18. #include <Window/HelpDialog/HelpDialog.h>
  19. #include <Window/MaterialEditorWindow.h>
  20. #include <Window/MaterialEditorWindowSettings.h>
  21. #include <Window/MaterialInspector/MaterialInspector.h>
  22. #include <Window/PerformanceMonitor/PerformanceMonitorWidget.h>
  23. #include <Window/SettingsDialog/SettingsDialog.h>
  24. #include <Window/ViewportSettingsInspector/ViewportSettingsInspector.h>
  25. AZ_PUSH_DISABLE_WARNING(4251 4800, "-Wunknown-warning-option") // disable warnings spawned by QT
  26. #include <QApplication>
  27. #include <QByteArray>
  28. #include <QCloseEvent>
  29. #include <QDesktopServices>
  30. #include <QFileDialog>
  31. #include <QUrl>
  32. #include <QWindow>
  33. AZ_POP_DISABLE_WARNING
  34. namespace MaterialEditor
  35. {
  36. MaterialEditorWindow::MaterialEditorWindow(QWidget* parent /* = 0 */)
  37. : Base(parent)
  38. {
  39. resize(1280, 1024);
  40. // Among other things, we need the window wrapper to save the main window size, position, and state
  41. auto mainWindowWrapper =
  42. new AzQtComponents::WindowDecorationWrapper(AzQtComponents::WindowDecorationWrapper::OptionAutoTitleBarButtons);
  43. mainWindowWrapper->setGuest(this);
  44. mainWindowWrapper->enableSaveRestoreGeometry("O3DE", "MaterialEditor", "mainWindowGeometry");
  45. // set the style sheet for RPE highlighting and other styling
  46. AzQtComponents::StyleManager::setStyleSheet(this, QStringLiteral(":/MaterialEditor.qss"));
  47. QApplication::setWindowIcon(QIcon(":/Icons/materialeditor.svg"));
  48. AZ::Name apiName = AZ::RHI::Factory::Get().GetName();
  49. if (!apiName.IsEmpty())
  50. {
  51. QString title = QString{ "%1 (%2)" }.arg(QApplication::applicationName()).arg(apiName.GetCStr());
  52. setWindowTitle(title);
  53. }
  54. else
  55. {
  56. AZ_Assert(false, "Render API name not found");
  57. setWindowTitle(QApplication::applicationName());
  58. }
  59. setObjectName("MaterialEditorWindow");
  60. m_toolBar = new MaterialEditorToolBar(this);
  61. m_toolBar->setObjectName("ToolBar");
  62. addToolBar(m_toolBar);
  63. m_materialViewport = new MaterialViewportWidget(centralWidget());
  64. m_materialViewport->setObjectName("Viewport");
  65. m_materialViewport->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
  66. centralWidget()->layout()->addWidget(m_materialViewport);
  67. m_assetBrowser->SetFilterState("", AZ::RPI::StreamingImageAsset::Group, true);
  68. m_assetBrowser->SetFilterState("", AZ::RPI::MaterialAsset::Group, true);
  69. m_assetBrowser->SetOpenHandler([](const AZStd::string& absolutePath) {
  70. if (AzFramework::StringFunc::Path::IsExtension(absolutePath.c_str(), AZ::RPI::MaterialSourceData::Extension))
  71. {
  72. AtomToolsFramework::AtomToolsDocumentSystemRequestBus::Broadcast(
  73. &AtomToolsFramework::AtomToolsDocumentSystemRequestBus::Events::OpenDocument, absolutePath);
  74. return;
  75. }
  76. if (AzFramework::StringFunc::Path::IsExtension(absolutePath.c_str(), AZ::RPI::MaterialTypeSourceData::Extension))
  77. {
  78. return;
  79. }
  80. QDesktopServices::openUrl(QUrl::fromLocalFile(absolutePath.c_str()));
  81. });
  82. AddDockWidget("Inspector", new MaterialInspector, Qt::RightDockWidgetArea, Qt::Vertical);
  83. AddDockWidget("Viewport Settings", new ViewportSettingsInspector, Qt::LeftDockWidgetArea, Qt::Vertical);
  84. AddDockWidget("Performance Monitor", new PerformanceMonitorWidget, Qt::BottomDockWidgetArea, Qt::Horizontal);
  85. SetDockWidgetVisible("Viewport Settings", false);
  86. SetDockWidgetVisible("Performance Monitor", false);
  87. // Restore geometry and show the window
  88. mainWindowWrapper->showFromSettings();
  89. // Restore additional state for docked windows
  90. auto windowSettings = AZ::UserSettings::CreateFind<MaterialEditorWindowSettings>(
  91. AZ::Crc32("MaterialEditorWindowSettings"), AZ::UserSettings::CT_GLOBAL);
  92. if (!windowSettings->m_mainWindowState.empty())
  93. {
  94. QByteArray windowState(windowSettings->m_mainWindowState.data(), static_cast<int>(windowSettings->m_mainWindowState.size()));
  95. m_advancedDockManager->restoreState(windowState);
  96. }
  97. OnDocumentOpened(AZ::Uuid::CreateNull());
  98. }
  99. void MaterialEditorWindow::ResizeViewportRenderTarget(uint32_t width, uint32_t height)
  100. {
  101. QSize requestedViewportSize = QSize(width, height) / devicePixelRatioF();
  102. QSize currentViewportSize = m_materialViewport->size();
  103. QSize offset = requestedViewportSize - currentViewportSize;
  104. QSize requestedWindowSize = size() + offset;
  105. resize(requestedWindowSize);
  106. AZ_Assert(
  107. m_materialViewport->size() == requestedViewportSize,
  108. "Resizing the window did not give the expected viewport size. Requested %d x %d but got %d x %d.",
  109. requestedViewportSize.width(), requestedViewportSize.height(), m_materialViewport->size().width(),
  110. m_materialViewport->size().height());
  111. [[maybe_unused]] QSize newDeviceSize = m_materialViewport->size();
  112. AZ_Warning(
  113. "Material Editor", static_cast<uint32_t>(newDeviceSize.width()) == width && static_cast<uint32_t>(newDeviceSize.height()) == height,
  114. "Resizing the window did not give the expected frame size. Requested %d x %d but got %d x %d.", width, height,
  115. newDeviceSize.width(), newDeviceSize.height());
  116. }
  117. void MaterialEditorWindow::LockViewportRenderTargetSize(uint32_t width, uint32_t height)
  118. {
  119. m_materialViewport->LockRenderTargetSize(width, height);
  120. }
  121. void MaterialEditorWindow::UnlockViewportRenderTargetSize()
  122. {
  123. m_materialViewport->UnlockRenderTargetSize();
  124. }
  125. bool MaterialEditorWindow::GetCreateDocumentParams(AZStd::string& openPath, AZStd::string& savePath)
  126. {
  127. CreateMaterialDialog createDialog(this);
  128. createDialog.adjustSize();
  129. if (createDialog.exec() == QDialog::Accepted &&
  130. !createDialog.m_materialFileInfo.absoluteFilePath().isEmpty() &&
  131. !createDialog.m_materialTypeFileInfo.absoluteFilePath().isEmpty())
  132. {
  133. savePath = createDialog.m_materialFileInfo.absoluteFilePath().toUtf8().constData();
  134. openPath = createDialog.m_materialTypeFileInfo.absoluteFilePath().toUtf8().constData();
  135. return true;
  136. }
  137. return false;
  138. }
  139. bool MaterialEditorWindow::GetOpenDocumentParams(AZStd::string& openPath)
  140. {
  141. const AZStd::vector<AZ::Data::AssetType> assetTypes = { azrtti_typeid<AZ::RPI::MaterialAsset>() };
  142. openPath = AtomToolsFramework::GetOpenFileInfo(assetTypes).absoluteFilePath().toUtf8().constData();
  143. return !openPath.empty();
  144. }
  145. void MaterialEditorWindow::OpenSettings()
  146. {
  147. SettingsDialog dialog(this);
  148. dialog.exec();
  149. }
  150. void MaterialEditorWindow::OpenHelp()
  151. {
  152. HelpDialog dialog(this);
  153. dialog.exec();
  154. }
  155. void MaterialEditorWindow::closeEvent(QCloseEvent* closeEvent)
  156. {
  157. // Capture docking state before shutdown
  158. auto windowSettings = AZ::UserSettings::CreateFind<MaterialEditorWindowSettings>(
  159. AZ::Crc32("MaterialEditorWindowSettings"), AZ::UserSettings::CT_GLOBAL);
  160. QByteArray windowState = m_advancedDockManager->saveState();
  161. windowSettings->m_mainWindowState.assign(windowState.begin(), windowState.end());
  162. Base::closeEvent(closeEvent);
  163. }
  164. } // namespace MaterialEditor
  165. #include <Window/moc_MaterialEditorWindow.cpp>