Debian Codegraph @

Source/Core/DolphinQt/Achievements/AchievementSettingsWidget.cpp

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #ifdef USE_RETRO_ACHIEVEMENTS
5 #include "DolphinQt/Achievements/AchievementSettingsWidget.h"
6
7 #include <QLabel>
8 #include <QLineEdit>
9 #include <QPushButton>
10 #include <QString>
11 #include <QVBoxLayout>
12
13 #include "Core/AchievementManager.h"
14 #include "Core/Config/AchievementSettings.h"
15 #include "Core/Config/UISettings.h"
16 #include "Core/Core.h"
17 #include "Core/Movie.h"
18 #include "Core/System.h"
19 #include "UICommon/DiscordPresence.h"
20
21 #include "DolphinQt/Config/ControllerInterface/ControllerInterfaceWindow.h"
22 #include "DolphinQt/Config/ToolTipControls/ToolTipCheckBox.h"
23 #include "DolphinQt/QtUtils/ModalMessageBox.h"
24 #include "DolphinQt/QtUtils/SignalBlocking.h"
25 #include "DolphinQt/Settings.h"
26
27 AchievementSettingsWidget::AchievementSettingsWidget(QWidget* parent) : QWidget(parent)
28 {
29 CreateLayout();
30 LoadSettings();
31 ConnectWidgets();
32
33 connect(&Settings::Instance(), &Settings::ConfigChanged, this,
34 &AchievementSettingsWidget::LoadSettings);
35 }
36
37 void AchievementSettingsWidget::UpdateData(int login_failed_code)
38 {
39 if (login_failed_code != RC_OK)
40 {
41 switch (login_failed_code)
42 {
43 case RC_LOGIN_REQUIRED:
44 case RC_EXPIRED_TOKEN:
45 m_common_login_failed->setText(tr("Logged Out - Credentials Expired"));
46 break;
47 case RC_INVALID_CREDENTIALS:
48 m_common_login_failed->setText(tr("Login Failed - Invalid Username/Password"));
49 break;
50 case RC_NO_RESPONSE:
51 m_common_login_failed->setText(tr("Login Failed - No Internet Connection"));
52 break;
53 default:
54 m_common_login_failed->setText(tr("Login Failed - Server Error"));
55 break;
56 }
57 m_common_login_failed->setVisible(true);
58 }
59 LoadSettings();
60 }
61
62 void AchievementSettingsWidget::CreateLayout()
63 {
64 m_common_layout = new QVBoxLayout();
65 m_common_integration_enabled_input =
66 new ToolTipCheckBox(tr("Enable RetroAchievements.org Integration"));
67 m_common_integration_enabled_input->SetDescription(
68 tr("Enable integration with RetroAchievements for earning achievements and competing in "
69 "leaderboards.<br><br>Must log in with a RetroAchievements account to use. Dolphin does "
70 "not save your password locally and uses an API token to maintain login."));
71 m_common_username_label = new QLabel(tr("Username"));
72 m_common_username_input = new QLineEdit(QStringLiteral(""));
73 m_common_password_label = new QLabel(tr("Password"));
74 m_common_password_input = new QLineEdit(QStringLiteral(""));
75 m_common_password_input->setEchoMode(QLineEdit::Password);
76 m_common_login_button = new QPushButton(tr("Log In"));
77 m_common_logout_button = new QPushButton(tr("Log Out"));
78 m_common_login_failed = new QLabel(tr("Login Failed"));
79 m_common_login_failed->setStyleSheet(QStringLiteral("QLabel { color : red; }"));
80 m_common_login_failed->setVisible(false);
81 m_common_hardcore_enabled_input = new ToolTipCheckBox(tr("Enable Hardcore Mode"));
82 m_common_hardcore_enabled_input->SetDescription(
83 tr("Enable Hardcore Mode on RetroAchievements.<br><br>Hardcore Mode is intended to provide "
84 "an experience as close to gaming on the original hardware as possible. RetroAchievements "
85 "rankings are primarily oriented towards Hardcore points (Softcore points are tracked but "
86 "not as heavily emphasized) and leaderboards require Hardcore Mode to be on.<br><br>To "
87 "ensure this experience, the following features will be disabled, as they give emulator "
88 "players an advantage over console players:<br>- Loading states<br>-- Saving states is "
89 "allowed<br>- Emulator speeds below 100%<br>-- Frame advance is disabled<br>-- Turbo is "
90 "allowed<br>- Cheats<br>- Memory patches<br>-- File patches are allowed<br>- Debug "
91 "UI<br>- Freelook<br><br><dolphin_emphasis>This cannot be turned on while a game is "
92 "playing.</dolphin_emphasis><br>Close your current game before enabling.<br>Be aware that "
93 "turning Hardcore Mode off while a game is running requires the game to be closed before "
94 "re-enabling."));
95 m_common_unofficial_enabled_input = new ToolTipCheckBox(tr("Enable Unofficial Achievements"));
96 m_common_unofficial_enabled_input->SetDescription(
97 tr("Enable unlocking unofficial achievements as well as official "
98 "achievements.<br><br>Unofficial achievements may be optional or unfinished achievements "
99 "that have not been deemed official by RetroAchievements and may be useful for testing or "
100 "simply for fun.<br><br>Setting takes effect on next game load."));
101 m_common_encore_enabled_input = new ToolTipCheckBox(tr("Enable Encore Achievements"));
102 m_common_encore_enabled_input->SetDescription(
103 tr("Enable unlocking achievements in Encore Mode.<br><br>Encore Mode re-enables achievements "
104 "the player has already unlocked on the site so that the player will be notified if they "
105 "meet the unlock conditions again, useful for custom speedrun criteria or simply for fun."
106 "<br><br>Setting takes effect on next game load."));
107 m_common_spectator_enabled_input = new ToolTipCheckBox(tr("Enable Spectator Mode"));
108 m_common_spectator_enabled_input->SetDescription(
109 tr("Enable unlocking achievements in Spectator Mode.<br><br>While in Spectator Mode, "
110 "achievements and leaderboards will be processed and displayed on screen, but will not be "
111 "submitted to the server.<br><br>If this is on at game launch, it will not be turned off "
112 "until game close, because a RetroAchievements session will not be created.<br><br>If "
113 "this is off at game launch, it can be toggled freely while the game is running."));
114 m_common_leaderboard_tracker_enabled_input = new ToolTipCheckBox(tr("Show Leaderboard Tracker"));
115 m_common_leaderboard_tracker_enabled_input->SetDescription(
116 tr("Show the on-screen RetroAchievements leaderboard tracker.<br><br>This appears in the "
117 "bottom-right corner while competing in a leaderboard."));
118 m_common_challenge_indicators_enabled_input =
119 new ToolTipCheckBox(tr("Show Challenge Indicators"));
120 m_common_challenge_indicators_enabled_input->SetDescription(
121 tr("Show the on-screen RetroAchievements challenge indicators.<br><br>These appear as "
122 "achievement badges in the bottom-right corner while tracking progress within a "
123 "challenge."));
124 m_common_discord_presence_enabled_input = new ToolTipCheckBox(tr("Enable Discord Presence"));
125 m_common_discord_presence_enabled_input->SetDescription(
126 tr("Use RetroAchievements rich presence in your Discord status.<br><br>Show Current Game on "
127 "Discord must be enabled."));
128 m_common_progress_enabled_input = new ToolTipCheckBox(tr("Enable Progress Notifications"));
129 m_common_progress_enabled_input->SetDescription(
130 tr("Enable progress notifications on achievements.<br><br>Displays a brief popup message "
131 "whenever the player makes progress on an achievement that tracks an accumulated value, "
132 "such as 60 out of 120 stars."));
133
134 m_common_layout->addWidget(m_common_integration_enabled_input);
135 m_common_layout->addWidget(m_common_username_label);
136 m_common_layout->addWidget(m_common_username_input);
137 m_common_layout->addWidget(m_common_password_label);
138 m_common_layout->addWidget(m_common_password_input);
139 m_common_layout->addWidget(m_common_login_button);
140 m_common_layout->addWidget(m_common_logout_button);
141 m_common_layout->addWidget(m_common_login_failed);
142 // i18n: Settings that affect the functionality of unlocking achievements.
143 m_common_layout->addWidget(new QLabel(tr("Function Settings")));
144 m_common_layout->addWidget(m_common_hardcore_enabled_input);
145 m_common_layout->addWidget(m_common_unofficial_enabled_input);
146 m_common_layout->addWidget(m_common_encore_enabled_input);
147 m_common_layout->addWidget(m_common_spectator_enabled_input);
148 // i18n: Settings that affect how achievements are displayed while playing.
149 m_common_layout->addWidget(new QLabel(tr("Display Settings")));
150 #ifdef USE_DISCORD_PRESENCE
151 m_common_layout->addWidget(m_common_discord_presence_enabled_input);
152 #endif // USE_DISCORD_PRESENCE
153 m_common_layout->addWidget(m_common_leaderboard_tracker_enabled_input);
154 m_common_layout->addWidget(m_common_challenge_indicators_enabled_input);
155 m_common_layout->addWidget(m_common_progress_enabled_input);
156
157 m_common_layout->setAlignment(Qt::AlignTop);
158 setLayout(m_common_layout);
159 }
160
161 void AchievementSettingsWidget::ConnectWidgets()
162 {
163 connect(m_common_integration_enabled_input, &QCheckBox::toggled, this,
164 &AchievementSettingsWidget::ToggleRAIntegration);
165 connect(m_common_login_button, &QPushButton::clicked, this, &AchievementSettingsWidget::Login);
166 connect(m_common_logout_button, &QPushButton::clicked, this, &AchievementSettingsWidget::Logout);
167 connect(m_common_hardcore_enabled_input, &QCheckBox::toggled, this,
168 &AchievementSettingsWidget::ToggleHardcore);
169 connect(m_common_unofficial_enabled_input, &QCheckBox::toggled, this,
170 &AchievementSettingsWidget::ToggleUnofficial);
171 connect(m_common_encore_enabled_input, &QCheckBox::toggled, this,
172 &AchievementSettingsWidget::ToggleEncore);
173 connect(m_common_spectator_enabled_input, &QCheckBox::toggled, this,
174 &AchievementSettingsWidget::ToggleSpectator);
175 connect(m_common_leaderboard_tracker_enabled_input, &QCheckBox::toggled, this,
176 &AchievementSettingsWidget::ToggleLeaderboardTracker);
177 connect(m_common_challenge_indicators_enabled_input, &QCheckBox::toggled, this,
178 &AchievementSettingsWidget::ToggleChallengeIndicators);
179 connect(m_common_discord_presence_enabled_input, &QCheckBox::toggled, this,
180 &AchievementSettingsWidget::ToggleDiscordPresence);
181 connect(m_common_progress_enabled_input, &QCheckBox::toggled, this,
182 &AchievementSettingsWidget::ToggleProgress);
183 }
184
185 void AchievementSettingsWidget::OnControllerInterfaceConfigure()
186 {
187 ControllerInterfaceWindow* window = new ControllerInterfaceWindow(this);
188 window->setAttribute(Qt::WA_DeleteOnClose, true);
189 window->setWindowModality(Qt::WindowModality::WindowModal);
190 window->show();
191 }
192
193 void AchievementSettingsWidget::LoadSettings()
194 {
195 Core::System& system = Core::System::GetInstance();
196
197 bool enabled = Config::Get(Config::RA_ENABLED);
198 bool hardcore_enabled = Config::Get(Config::RA_HARDCORE_ENABLED);
199 bool logged_out = Config::Get(Config::RA_API_TOKEN).empty();
200 std::string username = Config::Get(Config::RA_USERNAME);
201
202 SignalBlocking(m_common_integration_enabled_input)->setChecked(enabled);
203 SignalBlocking(m_common_username_label)->setEnabled(enabled);
204 if (!username.empty())
205 SignalBlocking(m_common_username_input)->setText(QString::fromStdString(username));
206 SignalBlocking(m_common_username_input)->setEnabled(enabled && logged_out);
207 SignalBlocking(m_common_password_label)->setVisible(logged_out);
208 SignalBlocking(m_common_password_label)->setEnabled(enabled);
209 SignalBlocking(m_common_password_input)->setVisible(logged_out);
210 SignalBlocking(m_common_password_input)->setEnabled(enabled);
211 SignalBlocking(m_common_login_button)->setVisible(logged_out);
212 SignalBlocking(m_common_login_button)->setEnabled(enabled && Core::IsUninitialized(system));
213 if (!enabled || Core::IsUninitialized(system))
214 SignalBlocking(m_common_login_button)->setText(tr("Log In"));
215 else
216 SignalBlocking(m_common_login_button)->setText(tr("To log in, stop the current emulation."));
217
218 SignalBlocking(m_common_logout_button)->setVisible(!logged_out);
219 SignalBlocking(m_common_logout_button)->setEnabled(enabled);
220
221 SignalBlocking(m_common_hardcore_enabled_input)
222 ->setChecked(Config::Get(Config::RA_HARDCORE_ENABLED));
223 SignalBlocking(m_common_hardcore_enabled_input)
224 ->setEnabled(enabled && (hardcore_enabled || (Core::IsUninitialized(system) &&
225 !system.GetMovie().IsPlayingInput())));
226
227 SignalBlocking(m_common_unofficial_enabled_input)
228 ->setChecked(Config::Get(Config::RA_UNOFFICIAL_ENABLED));
229 SignalBlocking(m_common_unofficial_enabled_input)->setEnabled(enabled);
230
231 SignalBlocking(m_common_encore_enabled_input)->setChecked(Config::Get(Config::RA_ENCORE_ENABLED));
232 SignalBlocking(m_common_encore_enabled_input)->setEnabled(enabled);
233
234 SignalBlocking(m_common_spectator_enabled_input)
235 ->setChecked(Config::Get(Config::RA_SPECTATOR_ENABLED));
236 SignalBlocking(m_common_spectator_enabled_input)->setEnabled(enabled);
237
238 SignalBlocking(m_common_leaderboard_tracker_enabled_input)
239 ->setChecked(Config::Get(Config::RA_LEADERBOARD_TRACKER_ENABLED));
240 SignalBlocking(m_common_leaderboard_tracker_enabled_input)->setEnabled(enabled);
241
242 SignalBlocking(m_common_challenge_indicators_enabled_input)
243 ->setChecked(Config::Get(Config::RA_CHALLENGE_INDICATORS_ENABLED));
244 SignalBlocking(m_common_challenge_indicators_enabled_input)->setEnabled(enabled);
245
246 SignalBlocking(m_common_discord_presence_enabled_input)
247 ->setChecked(Config::Get(Config::RA_DISCORD_PRESENCE_ENABLED));
248 SignalBlocking(m_common_discord_presence_enabled_input)
249 ->setEnabled(enabled && Config::Get(Config::MAIN_USE_DISCORD_PRESENCE));
250
251 SignalBlocking(m_common_progress_enabled_input)
252 ->setChecked(Config::Get(Config::RA_PROGRESS_ENABLED));
253 SignalBlocking(m_common_progress_enabled_input)->setEnabled(enabled);
254 }
255
256 void AchievementSettingsWidget::SaveSettings()
257 {
258 Config::ConfigChangeCallbackGuard config_guard;
259
260 Config::SetBaseOrCurrent(Config::RA_ENABLED, m_common_integration_enabled_input->isChecked());
261 Config::SetBaseOrCurrent(Config::RA_HARDCORE_ENABLED,
262 m_common_hardcore_enabled_input->isChecked());
263 Config::SetBaseOrCurrent(Config::RA_UNOFFICIAL_ENABLED,
264 m_common_unofficial_enabled_input->isChecked());
265 Config::SetBaseOrCurrent(Config::RA_ENCORE_ENABLED, m_common_encore_enabled_input->isChecked());
266 Config::SetBaseOrCurrent(Config::RA_SPECTATOR_ENABLED,
267 m_common_spectator_enabled_input->isChecked());
268 Config::SetBaseOrCurrent(Config::RA_LEADERBOARD_TRACKER_ENABLED,
269 m_common_leaderboard_tracker_enabled_input->isChecked());
270 Config::SetBaseOrCurrent(Config::RA_CHALLENGE_INDICATORS_ENABLED,
271 m_common_challenge_indicators_enabled_input->isChecked());
272 Config::SetBaseOrCurrent(Config::RA_DISCORD_PRESENCE_ENABLED,
273 m_common_discord_presence_enabled_input->isChecked());
274 Config::SetBaseOrCurrent(Config::RA_PROGRESS_ENABLED,
275 m_common_progress_enabled_input->isChecked());
276 Config::Save();
277 }
278
279 void AchievementSettingsWidget::ToggleRAIntegration()
280 {
281 SaveSettings();
282
283 auto& instance = AchievementManager::GetInstance();
284 if (Config::Get(Config::RA_ENABLED))
285 instance.Init(reinterpret_cast<void*>(winId()));
286 else
287 instance.Shutdown();
288 }
289
290 void AchievementSettingsWidget::Login()
291 {
292 m_common_login_failed->setVisible(false);
293 Config::SetBaseOrCurrent(Config::RA_USERNAME, m_common_username_input->text().toStdString());
294 AchievementManager::GetInstance().Login(m_common_password_input->text().toStdString());
295 m_common_password_input->setText(QString());
296 SaveSettings();
297 }
298
299 void AchievementSettingsWidget::Logout()
300 {
301 auto confirm = ModalMessageBox::question(
302 this, tr("Confirm Logout"), tr("Are you sure you want to log out of RetroAchievements?"),
303 QMessageBox::Yes | QMessageBox::No, QMessageBox::NoButton, Qt::ApplicationModal);
304 if (confirm == QMessageBox::Yes)
305 {
306 AchievementManager::GetInstance().Logout();
307 SaveSettings();
308 }
309 }
310
311 void AchievementSettingsWidget::ToggleHardcore()
312 {
313 if (Config::Get(Config::RA_HARDCORE_ENABLED))
314 {
315 auto confirm = ModalMessageBox::question(
316 this, tr("Confirm Hardcore Off"), tr("Are you sure you want to turn hardcore mode off?"),
317 QMessageBox::Yes | QMessageBox::No, QMessageBox::NoButton, Qt::ApplicationModal);
318 if (confirm != QMessageBox::Yes)
319 {
320 SignalBlocking(m_common_hardcore_enabled_input)->setChecked(true);
321 return;
322 }
323 }
324 SaveSettings();
325 }
326
327 void AchievementSettingsWidget::ToggleUnofficial()
328 {
329 SaveSettings();
330 }
331
332 void AchievementSettingsWidget::ToggleEncore()
333 {
334 SaveSettings();
335 }
336
337 void AchievementSettingsWidget::ToggleSpectator()
338 {
339 SaveSettings();
340 AchievementManager::GetInstance().SetSpectatorMode();
341 }
342
343 void AchievementSettingsWidget::ToggleLeaderboardTracker()
344 {
345 SaveSettings();
346 }
347
348 void AchievementSettingsWidget::ToggleChallengeIndicators()
349 {
350 SaveSettings();
351 }
352
353 void AchievementSettingsWidget::ToggleDiscordPresence()
354 {
355 SaveSettings();
356 Discord::UpdateDiscordPresence();
357 }
358
359 void AchievementSettingsWidget::ToggleProgress()
360 {
361 SaveSettings();
362 }
363
364 #endif // USE_RETRO_ACHIEVEMENTS