Debian Codegraph @

Source/Core/DolphinQt/Config/GameConfigWidget.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Config/GameConfigWidget.h"
5
6 #include <QFont>
7 #include <QGroupBox>
8 #include <QHBoxLayout>
9 #include <QIcon>
10 #include <QLabel>
11 #include <QTabWidget>
12 #include <QTimer>
13 #include <QToolTip>
14 #include <QVBoxLayout>
15
16 #include "Common/CommonPaths.h"
17 #include "Common/Config/Config.h"
18 #include "Common/Config/Layer.h"
19 #include "Common/FileUtil.h"
20
21 #include "Core/Config/GraphicsSettings.h"
22 #include "Core/Config/MainSettings.h"
23 #include "Core/ConfigLoaders/GameConfigLoader.h"
24 #include "Core/ConfigManager.h"
25 #include "DolphinQt/Config/ConfigControls/ConfigBool.h"
26 #include "DolphinQt/Config/ConfigControls/ConfigChoice.h"
27 #include "DolphinQt/Config/ConfigControls/ConfigFloatSlider.h"
28 #include "DolphinQt/Config/ConfigControls/ConfigInteger.h"
29 #include "DolphinQt/Config/ConfigControls/ConfigRadio.h"
30 #include "DolphinQt/Config/GameConfigEdit.h"
31 #include "DolphinQt/Config/Graphics/GraphicsPane.h"
32 #include "DolphinQt/QtUtils/QtUtils.h"
33 #include "DolphinQt/QtUtils/WrapInScrollArea.h"
34
35 #include "UICommon/GameFile.h"
36
37 static void PopulateTab(QTabWidget* tab, const std::string& path, std::string& game_id,
38 u16 revision, bool read_only)
39 {
40 for (const std::string& filename : ConfigLoaders::GetGameIniFilenames(game_id, revision))
41 {
42 const std::string ini_path = path + filename;
43 if (File::Exists(ini_path))
44 {
45 auto* edit = new GameConfigEdit(nullptr, QString::fromStdString(ini_path), read_only);
46 tab->addTab(edit, QString::fromStdString(filename));
47 }
48 }
49 }
50
51 GameConfigWidget::GameConfigWidget(const UICommon::GameFile& game) : m_game(game)
52 {
53 m_game_id = m_game.GetGameID();
54
55 m_gameini_local_path =
56 QString::fromStdString(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
57
58 m_layer = std::make_unique<Config::Layer>(
59 ConfigLoaders::GenerateLocalGameConfigLoader(m_game_id, m_game.GetRevision()));
60 m_global_layer = std::make_unique<Config::Layer>(
61 ConfigLoaders::GenerateGlobalGameConfigLoader(m_game_id, m_game.GetRevision()));
62
63 CreateWidgets();
64 connect(&Settings::Instance(), &Settings::ConfigChanged, this, &GameConfigWidget::LoadSettings);
65
66 PopulateTab(m_default_tab, File::GetSysDirectory() + GAMESETTINGS_DIR DIR_SEP, m_game_id,
67 m_game.GetRevision(), true);
68 PopulateTab(m_local_tab, File::GetUserPath(D_GAMESETTINGS_IDX), m_game_id, m_game.GetRevision(),
69 false);
70
71 bool game_id_tab = false;
72 for (int i = 0; i < m_local_tab->count(); i++)
73 {
74 if (m_local_tab->tabText(i).toStdString() == m_game_id + ".ini")
75 game_id_tab = true;
76 }
77
78 if (game_id_tab == false)
79 {
80 // Create new local game ini tab if none exists.
81 auto* edit = new GameConfigEdit(
82 nullptr, QString::fromStdString(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"),
83 false);
84 m_local_tab->addTab(edit, QString::fromStdString(m_game_id + ".ini"));
85 }
86
87 // Fails to change font if it's directly called at this time. Is there a better workaround?
88 QTimer::singleShot(100, this, [this] {
89 SetItalics();
90 Config::OnConfigChanged();
91 });
92 }
93
94 void GameConfigWidget::CreateWidgets()
95 {
96 Config::Layer* layer = m_layer.get();
97 // Core
98 auto* core_box = new QGroupBox(tr("Core"));
99 auto* core_layout = new QGridLayout;
100 core_box->setLayout(core_layout);
101
102 m_enable_dual_core = new ConfigBool(tr("Enable Dual Core"), Config::MAIN_CPU_THREAD, layer);
103 m_enable_mmu = new ConfigBool(tr("Enable MMU"), Config::MAIN_MMU, layer);
104 m_enable_fprf = new ConfigBool(tr("Enable FPRF"), Config::MAIN_FPRF, layer);
105 m_sync_gpu = new ConfigBool(tr("Synchronize GPU thread"), Config::MAIN_SYNC_GPU, layer);
106 m_emulate_disc_speed =
107 new ConfigBool(tr("Emulate Disc Speed"), Config::MAIN_FAST_DISC_SPEED, layer, true);
108 m_use_dsp_hle = new ConfigBool(tr("DSP HLE (fast)"), Config::MAIN_DSP_HLE, layer);
109
110 const std::vector<std::string> choice{tr("auto").toStdString(), tr("none").toStdString(),
111 tr("fake-completion").toStdString()};
112 m_deterministic_dual_core =
113 new ConfigStringChoice(choice, Config::MAIN_GPU_DETERMINISM_MODE, layer);
114
115 m_enable_mmu->SetDescription(tr(
116 "Enables the Memory Management Unit, needed for some games. (ON = Compatible, OFF = Fast)"));
117
118 m_enable_fprf->SetDescription(
119 tr("Enables Floating Point Result Flag calculation, needed for a few "
120 "games. (ON = Compatible, OFF = Fast)"));
121 m_sync_gpu->SetDescription(
122 tr("Synchronizes the GPU and CPU threads to help prevent random freezes "
123 "in Dual core mode. (ON = Compatible, OFF = Fast)"));
124 m_emulate_disc_speed->SetDescription(
125 tr("Enable emulated disc speed. Disabling this can cause crashes "
126 "and other problems in some games. "
127 "(ON = Compatible, OFF = Unlocked)"));
128
129 core_layout->addWidget(m_enable_dual_core, 0, 0);
130 core_layout->addWidget(m_enable_mmu, 1, 0);
131 core_layout->addWidget(m_enable_fprf, 2, 0);
132 core_layout->addWidget(m_sync_gpu, 3, 0);
133 core_layout->addWidget(m_emulate_disc_speed, 4, 0);
134 core_layout->addWidget(m_use_dsp_hle, 5, 0);
135 core_layout->addWidget(new QLabel(tr("Deterministic dual core:")), 6, 0);
136 core_layout->addWidget(m_deterministic_dual_core, 6, 1);
137
138 // Stereoscopy
139 auto* stereoscopy_box = new QGroupBox(tr("Stereoscopy"));
140 auto* stereoscopy_layout = new QGridLayout;
141 stereoscopy_box->setLayout(stereoscopy_layout);
142
143 m_depth_slider =
144 new ConfigFloatSlider(100, 200, Config::GFX_STEREO_DEPTH_PERCENTAGE, 1.0f, layer);
145 m_convergence_slider =
146 new ConfigFloatSlider(0, 1000, Config::GFX_STEREO_CONVERGENCE, 0.01f, layer);
147 auto* const depth_slider_value = new QLabel();
148 auto* const convergence_slider_value = new QLabel();
149 m_use_monoscopic_shadows =
150 new ConfigBool(tr("Monoscopic Shadows"), Config::GFX_STEREO_EFB_MONO_DEPTH, layer);
151
152 m_depth_slider->SetDescription(
153 tr("This value is multiplied with the depth set in the graphics configuration."));
154 m_convergence_slider->SetDescription(
155 tr("This value is added to the convergence value set in the graphics configuration."));
156 m_use_monoscopic_shadows->SetDescription(
157 tr("Use a single depth buffer for both eyes. Needed for a few games."));
158
159 stereoscopy_layout->addWidget(new ConfigFloatLabel(tr("Depth Percentage:"), m_depth_slider), 0,
160 0);
161 stereoscopy_layout->addWidget(m_depth_slider, 0, 1);
162 stereoscopy_layout->addWidget(depth_slider_value, 0, 2);
163 stereoscopy_layout->addWidget(new ConfigFloatLabel(tr("Convergence:"), m_convergence_slider), 1,
164 0);
165 stereoscopy_layout->addWidget(m_convergence_slider, 1, 1);
166 stereoscopy_layout->addWidget(convergence_slider_value, 1, 2);
167 stereoscopy_layout->addWidget(m_use_monoscopic_shadows, 2, 0);
168
169 depth_slider_value->setText(QString::asprintf("%.0f%%", m_depth_slider->GetValue()));
170 convergence_slider_value->setText(QString::asprintf("%.2f", m_convergence_slider->GetValue()));
171
172 auto* general_layout = new QVBoxLayout;
173 general_layout->addWidget(core_box);
174 general_layout->addWidget(stereoscopy_box);
175 general_layout->addStretch();
176
177 auto* general_widget = new QWidget;
178 general_widget->setLayout(general_layout);
179
180 // Editor tab
181 auto* advanced_layout = new QVBoxLayout;
182
183 auto* default_group = new QGroupBox(tr("Default Config (Read Only)"));
184 auto* default_layout = new QVBoxLayout;
185 m_default_tab = new QTabWidget;
186
187 default_group->setLayout(default_layout);
188 default_layout->addWidget(m_default_tab);
189
190 auto* local_group = new QGroupBox(tr("User Config"));
191 auto* local_layout = new QVBoxLayout;
192 m_local_tab = new QTabWidget;
193
194 local_group->setLayout(local_layout);
195 local_layout->addWidget(m_local_tab);
196
197 advanced_layout->addWidget(default_group);
198 advanced_layout->addWidget(local_group);
199
200 auto* advanced_widget = new QWidget;
201
202 advanced_widget->setLayout(advanced_layout);
203
204 auto* layout = new QVBoxLayout;
205 auto* tab_widget = new QTabWidget;
206 tab_widget->addTab(general_widget, tr("General"));
207
208 auto* const gfx_widget = new GraphicsPane{nullptr, m_layer.get()};
209 tab_widget->addTab(gfx_widget, tr("Graphics"));
210
211 const int editor_index = tab_widget->addTab(advanced_widget, tr("Editor"));
212
213 connect(tab_widget, &QTabWidget::currentChanged, this, [this, editor_index](int index) {
214 // Update the ini editor after editing other tabs.
215 if (index == editor_index)
216 {
217 // Layer only auto-saves when it is destroyed.
218 m_layer->Save();
219
220 // There can be multiple ini loaded for a game, only replace the one related to the game
221 // ini being edited.
222 for (int i = 0; i < m_local_tab->count(); i++)
223 {
224 if (m_local_tab->tabText(i).toStdString() == m_game_id + ".ini")
225 {
226 m_local_tab->removeTab(i);
227
228 auto* edit = new GameConfigEdit(
229 nullptr,
230 QString::fromStdString(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"),
231 false);
232
233 m_local_tab->insertTab(i, edit, QString::fromStdString(m_game_id + ".ini"));
234 break;
235 }
236 }
237 }
238
239 // Update other tabs after using ini editor.
240 if (m_prev_tab_index == editor_index)
241 {
242 // Load won't clear deleted keys, so everything is wiped before loading.
243 m_layer->DeleteAllKeys();
244 m_layer->Load();
245 Config::OnConfigChanged();
246 }
247
248 m_prev_tab_index = index;
249 });
250
251 connect(m_depth_slider, &ConfigFloatSlider::valueChanged, this, [this, depth_slider_value] {
252 depth_slider_value->setText(QString::asprintf("%.0f%%", m_depth_slider->GetValue()));
253 });
254 connect(m_convergence_slider, &ConfigFloatSlider::valueChanged, this,
255 [this, convergence_slider_value] {
256 convergence_slider_value->setText(
257 QString::asprintf("%.2f", m_convergence_slider->GetValue()));
258 });
259
260 const QString help_msg = tr(
261 "Italics mark default game settings, bold marks user settings.\nRight-click to remove user "
262 "settings.\nGraphics tabs don't display the value of a default game setting.\nAnti-Aliasing "
263 "settings are disabled when the global graphics backend doesn't "
264 "match the game setting.");
265
266 auto* const help_label = new QLabel(tr("These settings override core Dolphin settings."));
267
268 auto* const help_widget =
269 QtUtils::CreateIconWarning(this, QStyle::SP_MessageBoxQuestion, help_label);
270
271 help_widget->setToolTip(help_msg);
272
273 layout->addWidget(help_widget);
274 layout->addWidget(tab_widget);
275 setLayout(layout);
276 }
277
278 GameConfigWidget::~GameConfigWidget()
279 {
280 // Destructor saves the layer to file.
281 m_layer.reset();
282
283 // If a game is running and the game properties window is closed, update local game layer with
284 // any new changes. Not sure if doing it more frequently is safe.
285 auto local_layer = Config::GetLayer(Config::LayerType::LocalGame);
286 if (local_layer && SConfig::GetInstance().GetGameID() == m_game_id)
287 {
288 local_layer->DeleteAllKeys();
289 local_layer->Load();
290 Config::OnConfigChanged();
291 }
292
293 // Delete empty configs
294 if (File::GetSize(m_gameini_local_path.toStdString()) == 0)
295 File::Delete(m_gameini_local_path.toStdString());
296 }
297
298 void GameConfigWidget::LoadSettings()
299 {
300 // Load globals
301 auto update_bool = [this](auto config, bool reverse = false) {
302 const Config::Location& setting = config->GetLocation();
303
304 // Don't overwrite local with global
305 if (m_layer->Exists(setting) || !m_global_layer->Exists(setting))
306 return;
307
308 std::optional<bool> value = m_global_layer->Get<bool>(config->GetLocation());
309
310 if (value.has_value())
311 {
312 const QSignalBlocker blocker(config);
313 config->setChecked(value.value() ^ reverse);
314 }
315 };
316
317 auto update_int = [this](auto config) {
318 const Config::Location& setting = config->GetLocation();
319
320 if (m_layer->Exists(setting) || !m_global_layer->Exists(setting))
321 return;
322
323 std::optional<int> value = m_global_layer->Get<int>(setting);
324
325 if (value.has_value())
326 {
327 const QSignalBlocker blocker(config);
328 config->setValue(value.value());
329 }
330 };
331
332 for (ConfigBool* config : {m_enable_dual_core, m_enable_mmu, m_enable_fprf, m_sync_gpu,
333 m_use_dsp_hle, m_use_monoscopic_shadows})
334 {
335 update_bool(config);
336 }
337
338 update_bool(m_emulate_disc_speed, true);
339
340 update_int(m_depth_slider);
341 update_int(m_convergence_slider);
342 }
343
344 void GameConfigWidget::SetItalics()
345 {
346 // Mark system game settings with italics. Called once because it should never change.
347 auto italics = [this](auto config) {
348 if (!m_global_layer->Exists(config->GetLocation()))
349 return;
350
351 QFont ifont = config->font();
352 ifont.setItalic(true);
353 config->setFont(ifont);
354 };
355
356 for (auto* config : findChildren<ConfigBool*>())
357 italics(config);
358 for (auto* config : findChildren<ConfigFloatSlider*>())
359 italics(config);
360 for (auto* config : findChildren<ConfigInteger*>())
361 italics(config);
362 for (auto* config : findChildren<ConfigRadioInt*>())
363 italics(config);
364 for (auto* config : findChildren<ConfigChoice*>())
365 italics(config);
366 for (auto* config : findChildren<ConfigStringChoice*>())
367 italics(config);
368
369 for (auto* config : findChildren<ConfigComplexChoice*>())
370 {
371 std::pair<Config::Location, Config::Location> location = config->GetLocation();
372 if (m_global_layer->Exists(location.first) || m_global_layer->Exists(location.second))
373 {
374 QFont ifont = config->font();
375 ifont.setItalic(true);
376 config->setFont(ifont);
377 }
378 }
379 }