3
0

TexturePresetSelectionWidget.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "TexturePresetSelectionWidget.h"
  9. #include <Source/Editor/ui_TexturePresetSelectionWidget.h>
  10. #include <QCheckBox>
  11. #include <QComboBox>
  12. #include <QPushButton>
  13. #include <AzFramework/StringFunc/StringFunc.h>
  14. #include <BuilderSettings/PresetSettings.h>
  15. #include <AzQtComponents/Components/Widgets/CheckBox.h>
  16. #include <Atom/RPI.Public/AssetTagBus.h>
  17. namespace ImageProcessingAtomEditor
  18. {
  19. using namespace ImageProcessingAtom;
  20. AZStd::string GetImageFileMask(const AZStd::string& imageFilePath)
  21. {
  22. const char FileMaskDelimiter = '_';
  23. //get file name
  24. AZStd::string fileName;
  25. QString lowerFileName = imageFilePath.data();
  26. lowerFileName = lowerFileName.toLower();
  27. AzFramework::StringFunc::Path::GetFileName(lowerFileName.toUtf8().constData(), fileName);
  28. //get the substring from last '_'
  29. size_t lastUnderScore = fileName.find_last_of(FileMaskDelimiter);
  30. if (lastUnderScore != AZStd::string::npos)
  31. {
  32. return fileName.substr(lastUnderScore);
  33. }
  34. return AZStd::string();
  35. }
  36. TexturePresetSelectionWidget::TexturePresetSelectionWidget(EditorTextureSetting& textureSetting, QWidget* parent /*= nullptr*/)
  37. : QWidget(parent)
  38. , m_ui(new Ui::TexturePresetSelectionWidget)
  39. , m_textureSetting(&textureSetting)
  40. {
  41. m_ui->setupUi(this);
  42. // Add presets into combo box
  43. m_presetList.clear();
  44. m_presetList = BuilderSettingManager::Instance()->GetFullPresetList();
  45. QStringList stringList;
  46. foreach (const auto& presetName, m_presetList)
  47. {
  48. stringList.append(QString(presetName.GetCStr()));
  49. }
  50. stringList.sort();
  51. m_ui->presetComboBox->addItems(stringList);
  52. // Set current preset
  53. const auto& currPreset = m_textureSetting->GetMultiplatformTextureSetting().m_preset;
  54. const PresetSettings* presetSetting = BuilderSettingManager::Instance()->GetPreset(currPreset);
  55. if (presetSetting)
  56. {
  57. m_ui->presetComboBox->setCurrentText(presetSetting->m_name.GetCStr());
  58. QObject::connect(m_ui->presetComboBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &TexturePresetSelectionWidget::OnChangePreset);
  59. // Suppress engine reduction checkbox
  60. m_ui->serCheckBox->setCheckState(m_textureSetting->GetMultiplatformTextureSetting().m_suppressEngineReduce ? Qt::CheckState::Checked : Qt::CheckState::Unchecked);
  61. SetCheckBoxReadOnly(m_ui->serCheckBox, presetSetting->m_suppressEngineReduce);
  62. QObject::connect(m_ui->serCheckBox, &QCheckBox::clicked, this, &TexturePresetSelectionWidget::OnCheckBoxStateChanged);
  63. }
  64. // Reset btn
  65. QObject::connect(m_ui->resetBtn, &QPushButton::clicked, this, &TexturePresetSelectionWidget::OnRestButton);
  66. // PresetInfo btn
  67. QObject::connect(m_ui->infoBtn, &QPushButton::clicked, this, &TexturePresetSelectionWidget::OnPresetInfoButton);
  68. // Style the Checkbox
  69. AzQtComponents::CheckBox::applyToggleSwitchStyle(m_ui->serCheckBox);
  70. // Tooltips
  71. m_ui->activePresetLabel->setToolTip(QString("Choose a texture preset based on the texture's intended use case."));
  72. m_ui->infoBtn->setToolTip(QString("Display the property details for the selected texture preset"));
  73. m_ui->resetBtn->setToolTip(QString("Reset all values to the default values for the selected texture preset."));
  74. m_ui->maxResLabel->setToolTip(QString("Use the maximum texture resolution regardless of the target platform specification. Use this setting for textures that feature text or other details that should be legible."));
  75. AZStd::vector<AZ::Name> textureTags;
  76. AZ::RPI::ImageTagBus::BroadcastResult(textureTags, &AZ::RPI::ImageTagBus::Events::GetTags);
  77. for (const AZ::Name& tag : textureTags)
  78. {
  79. m_ui->tagComboBox->addItem(tag.GetCStr());
  80. }
  81. m_ui->tagList->setSortingEnabled(true);
  82. for (const AZStd::string& tag : m_textureSetting->GetMultiplatformTextureSetting().m_tags)
  83. {
  84. m_ui->tagList->addItem(QString(tag.c_str()));
  85. }
  86. QObject::connect(m_ui->tagAddButton, &QPushButton::released, this, &TexturePresetSelectionWidget::OnTagAdded);
  87. QObject::connect(m_ui->tagRemoveButton, &QPushButton::released, this, &TexturePresetSelectionWidget::OnTagRemoved);
  88. EditorInternalNotificationBus::Handler::BusConnect();
  89. }
  90. TexturePresetSelectionWidget::~TexturePresetSelectionWidget()
  91. {
  92. EditorInternalNotificationBus::Handler::BusDisconnect();
  93. }
  94. void TexturePresetSelectionWidget::OnCheckBoxStateChanged(bool checked)
  95. {
  96. for (auto& it : m_textureSetting->m_settingsMap)
  97. {
  98. it.second.m_suppressEngineReduce = checked;
  99. }
  100. m_textureSetting->SetIsOverrided();
  101. EditorInternalNotificationBus::Broadcast(&EditorInternalNotificationBus::Events::OnEditorSettingsChanged, false, BuilderSettingManager::s_defaultPlatform);
  102. }
  103. void TexturePresetSelectionWidget::OnRestButton()
  104. {
  105. m_textureSetting->SetToPreset(PresetName(m_ui->presetComboBox->currentText().toUtf8().data()));
  106. EditorInternalNotificationBus::Broadcast(&EditorInternalNotificationBus::Events::OnEditorSettingsChanged, true, BuilderSettingManager::s_defaultPlatform);
  107. }
  108. void TexturePresetSelectionWidget::OnChangePreset(int index)
  109. {
  110. QString text = m_ui->presetComboBox->itemText(index);
  111. m_textureSetting->SetToPreset(PresetName(text.toUtf8().data()));
  112. EditorInternalNotificationBus::Broadcast(&EditorInternalNotificationBus::Events::OnEditorSettingsChanged, true, BuilderSettingManager::s_defaultPlatform);
  113. }
  114. void ImageProcessingAtomEditor::TexturePresetSelectionWidget::OnPresetInfoButton()
  115. {
  116. const auto& currPreset = m_textureSetting->GetMultiplatformTextureSetting().m_preset;
  117. const PresetSettings* presetSetting = BuilderSettingManager::Instance()->GetPreset(currPreset);
  118. m_presetPopup.reset(new PresetInfoPopup(presetSetting, this));
  119. m_presetPopup->installEventFilter(this);
  120. m_presetPopup->show();
  121. }
  122. void TexturePresetSelectionWidget::OnTagAdded()
  123. {
  124. QString selectedTag = m_ui->tagComboBox->currentText();
  125. if (selectedTag.isEmpty())
  126. {
  127. return;
  128. }
  129. auto& tags = m_textureSetting->GetMultiplatformTextureSetting().m_tags;
  130. if (!tags.emplace(selectedTag.toUtf8().data()).second)
  131. {
  132. return;
  133. }
  134. m_ui->tagList->addItem(selectedTag);
  135. }
  136. void TexturePresetSelectionWidget::OnTagRemoved()
  137. {
  138. QListWidgetItem* item = m_ui->tagList->currentItem();
  139. if (!item)
  140. {
  141. return;
  142. }
  143. auto& tags = m_textureSetting->GetMultiplatformTextureSetting().m_tags;
  144. tags.erase(item->text().toUtf8().data());
  145. delete item;
  146. }
  147. void TexturePresetSelectionWidget::OnEditorSettingsChanged(bool needRefresh, const AZStd::string& /*platform*/)
  148. {
  149. if (needRefresh)
  150. {
  151. bool oldState = m_ui->serCheckBox->blockSignals(true);
  152. m_ui->serCheckBox->setChecked(m_textureSetting->GetMultiplatformTextureSetting().m_suppressEngineReduce);
  153. // If the preset's SER is true, texture setting should not override
  154. const auto& currPreset = m_textureSetting->GetMultiplatformTextureSetting().m_preset;
  155. const PresetSettings* presetSetting = BuilderSettingManager::Instance()->GetPreset(currPreset);
  156. if (presetSetting)
  157. {
  158. SetCheckBoxReadOnly(m_ui->serCheckBox, presetSetting->m_suppressEngineReduce);
  159. // If there is preset info dialog open, update the text
  160. if (m_presetPopup && m_presetPopup->isVisible())
  161. {
  162. m_presetPopup->RefreshPresetInfoLabel(presetSetting);
  163. }
  164. }
  165. m_ui->serCheckBox->blockSignals(oldState);
  166. }
  167. }
  168. void ImageProcessingAtomEditor::TexturePresetSelectionWidget::SetCheckBoxReadOnly(QCheckBox* checkBox, bool readOnly)
  169. {
  170. checkBox->setAttribute(Qt::WA_TransparentForMouseEvents, readOnly);
  171. checkBox->setFocusPolicy(readOnly ? Qt::NoFocus : Qt::StrongFocus);
  172. checkBox->setEnabled(!readOnly);
  173. }
  174. }//namespace ImageProcessingAtomEditor
  175. #include <Source/Editor/moc_TexturePresetSelectionWidget.cpp>