3
0

TexturePropertyEditor.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "TexturePropertyEditor.h"
  9. // warning C4251: 'QBrush::d': class 'QScopedPointer<QBrushData,QBrushDataPointerDeleter>' needs to have dll-interface to be used by clients of class 'QBrush'
  10. // warning C4800: 'uint': forcing value to bool 'true' or 'false' (performance warning)
  11. AZ_PUSH_DISABLE_WARNING(4800 4251, "-Wunknown-warning-option")
  12. #include <Source/Editor/ui_TexturePropertyEditor.h>
  13. AZ_POP_DISABLE_WARNING
  14. #include <Source/BuilderSettings/BuilderSettingManager.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  17. #include <AzCore/IO/FileIO.h>
  18. // warning C4251: 'QBrush::d': class 'QScopedPointer<QBrushData,QBrushDataPointerDeleter>' needs to have dll-interface to be used by clients of class 'QBrush'
  19. // warning C4800: 'uint': forcing value to bool 'true' or 'false' (performance warning)
  20. // warning C4244: 'argument': conversion from 'UINT64' to 'AZ::u32',
  21. AZ_PUSH_DISABLE_WARNING(4244 4800 4251, "-Wunknown-warning-option")
  22. #include <QBoxLayout>
  23. #include <QKeyEvent>
  24. #include <QPushButton>
  25. #include <QToolButton>
  26. #include <QCheckBox>
  27. #include <QComboBox>
  28. #include <QUrl>
  29. #include <QDesktopServices>
  30. AZ_POP_DISABLE_WARNING
  31. void InitTexturePropertyEditorResources()
  32. {
  33. Q_INIT_RESOURCE(ImageProcessing);
  34. }
  35. namespace ImageProcessingAtomEditor
  36. {
  37. TexturePropertyEditor::TexturePropertyEditor(const AZ::Uuid& sourceTextureId, QWidget* parent /*= nullptr*/)
  38. : AzQtComponents::StyledDialog(parent, Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowTitleHint)
  39. , m_ui(new Ui::TexturePropertyEditor)
  40. , m_textureSetting(sourceTextureId)
  41. , m_validImage(true)
  42. {
  43. if (m_textureSetting.m_img == nullptr)
  44. {
  45. m_validImage = false;
  46. return;
  47. }
  48. InitTexturePropertyEditorResources();
  49. m_ui->setupUi(this);
  50. m_ui->helpBtn->setToolTip(QString("Visit the Texture Settings documentation on o3de.org."));
  51. m_ui->saveBtn->setToolTip(QString("Save settings to a .assetinfo sidecar file. The texture source asset is automatically processed on save."));
  52. m_ui->saveBtn->setDefault(true);
  53. m_ui->cancelBtn->setToolTip(QString("Close Texture Settings."));
  54. //Initialize all the format string here
  55. EditorHelper::InitPixelFormatString();
  56. //TexturePreviewWidget will be the widget to preview mipmaps
  57. m_previewWidget.reset(aznew TexturePreviewWidget(m_textureSetting, this));
  58. m_ui->mainLayout->layout()->addWidget(m_previewWidget.data());
  59. //TexturePresetSelectionWidget will be the widget to select the preset for the texture
  60. m_presetSelectionWidget.reset(aznew TexturePresetSelectionWidget(m_textureSetting, this));
  61. m_ui->settingsLayout->layout()->addWidget(m_presetSelectionWidget.data());
  62. //ResolutionSettingWidget will be the table section to display mipmap resolution for each platform
  63. m_resolutionSettingWidget.reset(aznew ResolutionSettingWidget(ResoultionWidgetType::TexturePropety, m_textureSetting, this));
  64. m_ui->settingsLayout->layout()->addWidget(m_resolutionSettingWidget.data());
  65. //MipmapSettingWidget will be simple ReflectedProperty editor to reflect mipmap settings section
  66. m_mipmapSettingWidget.reset(aznew MipmapSettingWidget(m_textureSetting, this));
  67. m_ui->settingsLayout->layout()->addWidget(m_mipmapSettingWidget.data());
  68. QObject::connect(m_ui->helpBtn, &QToolButton::clicked, this, &TexturePropertyEditor::OnHelp);
  69. QObject::connect(m_ui->saveBtn, &QPushButton::clicked, this, &TexturePropertyEditor::OnSave);
  70. QObject::connect(m_ui->cancelBtn, &QPushButton::clicked, this, &QDialog::reject);
  71. EditorInternalNotificationBus::Handler::BusConnect();
  72. // When checkbox and combobox is focused, they will intercept the space shortcut, need to disable focus on them first
  73. // to get space shortcut pass through
  74. QList<QCheckBox*> checkBoxWidgets = QObject::findChildren<QCheckBox*>();
  75. for (QCheckBox* widget: checkBoxWidgets)
  76. {
  77. widget->setFocusPolicy(Qt::NoFocus);
  78. }
  79. QList<QComboBox*> comboBoxWidgets = QObject::findChildren<QComboBox*>();
  80. for (QComboBox* widget : comboBoxWidgets)
  81. {
  82. widget->setFocusPolicy(Qt::NoFocus);
  83. }
  84. this->setFocusPolicy(Qt::StrongFocus);
  85. }
  86. TexturePropertyEditor::~TexturePropertyEditor()
  87. {
  88. EditorInternalNotificationBus::Handler::BusDisconnect();
  89. }
  90. bool TexturePropertyEditor::HasValidImage()
  91. {
  92. return m_validImage;
  93. }
  94. void TexturePropertyEditor::OnEditorSettingsChanged([[maybe_unused]] bool needRefresh, const AZStd::string& /*platform*/)
  95. {
  96. m_textureSetting.m_modified = true;
  97. }
  98. void TexturePropertyEditor::OnHelp()
  99. {
  100. QString webLink = tr("https://o3de.org/docs/user-guide/assets/texture-settings/");
  101. QDesktopServices::openUrl(QUrl(webLink));
  102. }
  103. void TexturePropertyEditor::OnSave()
  104. {
  105. if (!m_validImage)
  106. {
  107. return;
  108. }
  109. bool sourceControlActive = false;
  110. AzToolsFramework::SourceControlConnectionRequestBus::BroadcastResult(sourceControlActive, &AzToolsFramework::SourceControlConnectionRequests::IsActive);
  111. AZStd::string outputPath = m_textureSetting.m_fullPath + ImageProcessingAtom::TextureSettings::ExtensionName;
  112. if (sourceControlActive)
  113. {
  114. using ApplicationBus = AzToolsFramework::ToolsApplicationRequestBus;
  115. bool checkoutResult = false;
  116. ApplicationBus::BroadcastResult(checkoutResult, &ApplicationBus::Events::RequestEditForFileBlocking, outputPath.c_str(), "Checking out .imagesetting file", []([[maybe_unused]] int& current, [[maybe_unused]] int& max) {});
  117. if (checkoutResult)
  118. {
  119. SaveTextureSetting(outputPath);
  120. }
  121. else
  122. {
  123. AZ_Error("Texture Editor", false, "Cannot checkout file '%s' from source control.", outputPath.c_str());
  124. }
  125. }
  126. else
  127. {
  128. const bool fileExisted = AZ::IO::FileIOBase::GetInstance()->Exists(outputPath.c_str());
  129. const bool fileReadOnly = AZ::IO::FileIOBase::GetInstance()->IsReadOnly(outputPath.c_str());
  130. if (!fileExisted || !fileReadOnly)
  131. {
  132. SaveTextureSetting(outputPath);
  133. }
  134. }
  135. }
  136. void TexturePropertyEditor::SaveTextureSetting(AZStd::string outputPath)
  137. {
  138. if (!m_validImage)
  139. {
  140. return;
  141. }
  142. ImageProcessingAtom::TextureSettings& baseSetting = m_textureSetting.GetMultiplatformTextureSetting();
  143. for (auto& it : m_textureSetting.m_settingsMap)
  144. {
  145. baseSetting.ApplySettings(it.second, it.first);
  146. }
  147. ImageProcessingAtom::StringOutcome outcome = ImageProcessingAtom::TextureSettings::WriteTextureSetting(outputPath, baseSetting);
  148. if (!outcome.IsSuccess())
  149. {
  150. AZ_Error("Texture Editor", false, "Cannot save texture settings to %s!", outputPath.data());
  151. }
  152. }
  153. bool TexturePropertyEditor::event(QEvent* event)
  154. {
  155. bool needsBlocking = false;
  156. if (m_previewWidget)
  157. {
  158. needsBlocking = m_previewWidget->OnQtEvent(event);
  159. }
  160. return needsBlocking ? true : QWidget::event(event);
  161. }
  162. }//namespace ImageProcessingAtomEditor
  163. #include <Source/Editor/moc_TexturePropertyEditor.cpp>