Debian Codegraph @

src/game/menu/settingsmenu.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "settingsmenu.hpp"
17
18 #include "audioworker.hpp"
19 #include "confirmationmenu.hpp"
20 #include "database.hpp"
21 #include "game.hpp"
22 #include "keyconfigmenu.hpp"
23 #include "renderer.hpp"
24 #include "resolutionmenu.hpp"
25 #include "settings.hpp"
26 #include "surfacemenu.hpp"
27 #include "textmenuitemview.hpp"
28 #include "track.hpp"
29 #include "trackdata.hpp"
30 #include "trackloader.hpp"
31 #include "vsyncmenu.hpp"
32
33 #include <MenuItem>
34 #include <MenuItemAction>
35 #include <MenuItemView>
36 #include <MenuManager>
37
38 #include "simple_logger.hpp"
39
40 #include <memory>
41 #include <sstream>
42
43 #include <QObject> // For QObject::tr()
44
45 // Swap interval supported only in Qt 5.3+, see also game.cpp
46 #if (QT_VERSION >= QT_VERSION_CHECK(5, 3, 0))
47 #define VSYNC_MENU
48 #endif
49
50 static const int ITEM_HEIGHT_DIV = 10;
51 static const int ITEM_TEXT_SIZE = 20;
52
53 class ResetAction : public MTFH::MenuItemAction
54 {
55 public:
56 enum class Type
57 {
58 Times,
59 Positions,
60 Tracks
61 };
62
63 ResetAction(Type type, std::shared_ptr<ConfirmationMenu> menu)
64 : m_type(type)
65 , m_confirmationMenu(menu)
66 {
67 }
68
69 private:
70 //! \reimp
71 void fire()
72 {
73 using MTFH::MenuManager;
74
75 switch (m_type)
76 {
77 case Type::Positions:
78 MenuManager::instance().pushMenu(m_confirmationMenu->id());
79 m_confirmationMenu->setText(QObject::tr("Reset best positions?").toUpper().toStdWString());
80 m_confirmationMenu->setAcceptAction(
81 []() {
82 juzzlin::L().info() << "Reset positions selected.";
83 Database::instance().resetBestPos();
84 });
85 m_confirmationMenu->setCurrentIndex(1);
86 break;
87
88 case Type::Times:
89 MenuManager::instance().pushMenu(m_confirmationMenu->id());
90 m_confirmationMenu->setText(QObject::tr("Reset record times?").toUpper().toStdWString());
91 m_confirmationMenu->setAcceptAction(
92 []() {
93 juzzlin::L().info() << "Reset times selected.";
94 Database::instance().resetLapRecords();
95 Database::instance().resetRaceRecords();
96 });
97 m_confirmationMenu->setCurrentIndex(1);
98 break;
99
100 case Type::Tracks:
101 MenuManager::instance().pushMenu(m_confirmationMenu->id());
102 m_confirmationMenu->setText(QObject::tr("Reset unlocked tracks?").toUpper().toStdWString());
103 m_confirmationMenu->setAcceptAction(
104 []() {
105 juzzlin::L().info() << "Reset tracks selected.";
106 auto && trackLoader = TrackLoader::instance();
107 for (size_t i = 0; i < trackLoader.tracks(); i++)
108 {
109 Track & track = *trackLoader.track(i);
110 if (track.trackData().index() > 0)
111 {
112 track.trackData().setIsLocked(true);
113 }
114 }
115 Database::instance().resetTrackUnlockStatuses(); });
116 m_confirmationMenu->setCurrentIndex(1);
117 break;
118 }
119 }
120
121 Type m_type;
122
123 std::shared_ptr<ConfirmationMenu> m_confirmationMenu;
124 };
125
126 static const auto CONFIRMATION_MENU_ID = "confirmationMenu";
127
128 static const auto FPS_MENU_ID = "fpsMenu";
129
130 static const auto FULL_SCREEN_RESOLUTION_MENU_ID = "fullScreenResolutionMenu";
131
132 static const auto GAME_MODE_MENU_ID = "gameModeMenu";
133
134 static const auto GFX_MENU_ID = "gfxMenu";
135
136 static const auto KEY_CONFIG_MENU_ID = "keyConfigMenu";
137
138 static const auto RESET_MENU_ID = "resetMenu";
139
140 static const auto SFX_MENU_ID = "sfxMenu";
141
142 static const auto SPLIT_TYPE_MENU_ID = "splitTypeMenu";
143
144 static const auto VSYNC_MENU_ID = "vsyncMenu";
145
146 static const auto WINDOWED_RESOLUTION_MENU_ID = "windowedResolutionMenu";
147
148 SettingsMenu::SettingsMenu(std::string id, int width, int height)
149 : SurfaceMenu("settingsBack", id, width, height, Menu::Style::VerticalList)
150 , m_confirmationMenu(std::make_shared<ConfirmationMenu>(CONFIRMATION_MENU_ID, width, height))
151 , m_fpsMenu(std::make_shared<SurfaceMenu>("settingsBack", FPS_MENU_ID, width, height, Menu::Style::VerticalList))
152 , m_fullScreenResolutionMenu(std::make_shared<ResolutionMenu>(m_confirmationMenu, FULL_SCREEN_RESOLUTION_MENU_ID, width, height, true))
153 , m_windowedResolutionMenu(std::make_shared<ResolutionMenu>(m_confirmationMenu, WINDOWED_RESOLUTION_MENU_ID, width, height, false))
154 , m_gameModeMenu(std::make_shared<SurfaceMenu>("settingsBack", GAME_MODE_MENU_ID, width, height, Menu::Style::VerticalList))
155 , m_gfxMenu(std::make_shared<SurfaceMenu>("settingsBack", GFX_MENU_ID, width, height, Menu::Style::VerticalList))
156 , m_resetMenu(std::make_shared<SurfaceMenu>("settingsBack", RESET_MENU_ID, width, height, Menu::Style::VerticalList))
157 , m_sfxMenu(std::make_shared<SurfaceMenu>("settingsBack", SFX_MENU_ID, width, height, Menu::Style::VerticalList))
158 , m_splitTypeMenu(std::make_shared<SurfaceMenu>("settingsBack", SPLIT_TYPE_MENU_ID, width, height, Menu::Style::VerticalList))
159 , m_vsyncMenu(std::make_shared<VSyncMenu>(m_confirmationMenu, VSYNC_MENU_ID, width, height))
160 , m_keyConfigMenu(std::make_shared<KeyConfigMenu>(KEY_CONFIG_MENU_ID, width, height))
161 {
162 populate(width, height);
163
164 populateFpsMenu(width, height);
165
166 populateGameModeMenu(width, height);
167
168 populateGfxMenu(width, height);
169
170 populateResetMenu(width, height);
171
172 populateSfxMenu(width, height);
173
174 populateSplitTypeMenu(width, height);
175
176 using MTFH::MenuManager;
177
178 MenuManager::instance().addMenu(m_confirmationMenu);
179 MenuManager::instance().addMenu(m_fpsMenu);
180 MenuManager::instance().addMenu(m_fullScreenResolutionMenu);
181 MenuManager::instance().addMenu(m_gameModeMenu);
182 MenuManager::instance().addMenu(m_gfxMenu);
183 MenuManager::instance().addMenu(m_keyConfigMenu);
184 MenuManager::instance().addMenu(m_resetMenu);
185 MenuManager::instance().addMenu(m_sfxMenu);
186 MenuManager::instance().addMenu(m_splitTypeMenu);
187 #ifdef VSYNC_MENU
188 MenuManager::instance().addMenu(m_vsyncMenu);
189 #endif
190 MenuManager::instance().addMenu(m_windowedResolutionMenu);
191 }
192
193 void SettingsMenu::populate(int width, int height)
194 {
195 const int itemHeight = height / ITEM_HEIGHT_DIV;
196 const int textSize = ITEM_TEXT_SIZE;
197
198 using MTFH::MenuItem;
199 using MTFH::MenuManager;
200
201 const auto gameMode = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Game mode >").toUpper().toStdWString());
202 gameMode->setView(std::make_shared<TextMenuItemView>(textSize, *gameMode));
203 gameMode->setMenuOpenAction(GAME_MODE_MENU_ID);
204
205 const auto sfx = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Sounds >").toUpper().toStdWString());
206 sfx->setView(std::make_shared<TextMenuItemView>(textSize, *sfx));
207 sfx->setMenuOpenAction(SFX_MENU_ID);
208
209 const auto gfx = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("GFX >").toUpper().toStdWString());
210 gfx->setView(std::make_shared<TextMenuItemView>(textSize, *gfx));
211 gfx->setMenuOpenAction(GFX_MENU_ID);
212
213 const auto configureKeys = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Controls >").toUpper().toStdWString());
214 configureKeys->setView(std::make_shared<TextMenuItemView>(textSize, *configureKeys));
215 configureKeys->setMenuOpenAction(KEY_CONFIG_MENU_ID);
216
217 const auto reset = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Reset >").toUpper().toStdWString());
218 reset->setView(std::make_shared<TextMenuItemView>(textSize, *reset));
219 reset->setMenuOpenAction(RESET_MENU_ID);
220
221 addItem(reset);
222 addItem(configureKeys);
223 addItem(sfx);
224 addItem(gfx);
225 addItem(gameMode);
226 }
227
228 void SettingsMenu::populateFpsMenu(int width, int height)
229 {
230 const int numItems = 2;
231 const int itemHeight = height / (numItems + 4);
232 const int textSize = ITEM_TEXT_SIZE;
233
234 using MTFH::MenuItem;
235 using MTFH::MenuManager;
236
237 const auto fps30 = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("30 fps").toUpper().toStdWString());
238 fps30->setView(std::make_shared<TextMenuItemView>(textSize, *fps30));
239 fps30->setAction(
240 []() {
241 juzzlin::L().info() << "30 fps selected.";
242 Game::instance().setFps(Game::Fps::Fps30);
243 Settings::instance().saveValue(Settings::fpsKey(), 30);
244 MenuManager::instance().popMenu();
245 });
246
247 const auto fps60 = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("60 fps").toUpper().toStdWString());
248 fps60->setView(std::make_shared<TextMenuItemView>(textSize, *fps60));
249 fps60->setAction(
250 []() {
251 juzzlin::L().info() << "60 fps selected.";
252 Game::instance().setFps(Game::Fps::Fps60);
253 Settings::instance().saveValue(Settings::fpsKey(), 60);
254 MenuManager::instance().popMenu();
255 });
256
257 m_fpsMenu->addItem(fps30);
258 m_fpsMenu->addItem(fps60);
259
260 if (Settings::instance().loadValue(Settings::fpsKey()) == 30)
261 {
262 fps30->setCurrent();
263 }
264 else
265 {
266 fps60->setCurrent();
267 }
268 }
269
270 void SettingsMenu::populateGameModeMenu(int width, int height)
271 {
272 const int itemHeight = height / ITEM_HEIGHT_DIV;
273 const int textSize = ITEM_TEXT_SIZE;
274
275 using MTFH::MenuItem;
276 using MTFH::MenuManager;
277
278 const auto twoPlayers = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Two player race").toUpper().toStdWString());
279 twoPlayers->setView(std::make_shared<TextMenuItemView>(textSize, *twoPlayers));
280 twoPlayers->setAction(
281 []() {
282 juzzlin::L().info() << "Two player race selected.";
283 Game::instance().setMode(Game::Mode::TwoPlayerRace);
284 MenuManager::instance().popMenu();
285 });
286
287 const auto onePlayer = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("One player race").toUpper().toStdWString());
288 onePlayer->setView(std::make_shared<TextMenuItemView>(textSize, *onePlayer));
289 onePlayer->setAction(
290 []() {
291 juzzlin::L().info() << "One player race selected.";
292 Game::instance().setMode(Game::Mode::OnePlayerRace);
293 MenuManager::instance().popMenu();
294 });
295
296 const auto timeTrial = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Time Trial").toUpper().toStdWString());
297 timeTrial->setView(std::make_shared<TextMenuItemView>(textSize, *timeTrial));
298 timeTrial->setAction(
299 []() {
300 juzzlin::L().info() << "Time Trial selected.";
301 Game::instance().setMode(Game::Mode::TimeTrial);
302 MenuManager::instance().popMenu();
303 });
304
305 const auto duel = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Duel").toUpper().toStdWString());
306 duel->setView(std::make_shared<TextMenuItemView>(textSize, *duel));
307 duel->setAction(
308 []() {
309 juzzlin::L().info() << "Duel selected.";
310 Game::instance().setMode(Game::Mode::Duel);
311 MenuManager::instance().popMenu();
312 });
313
314 m_gameModeMenu->addItem(duel);
315 m_gameModeMenu->addItem(timeTrial);
316 m_gameModeMenu->addItem(twoPlayers);
317 m_gameModeMenu->addItem(onePlayer);
318 }
319
320 void SettingsMenu::populateSplitTypeMenu(int width, int height)
321 {
322 const int numItems = 2;
323 const int itemHeight = height / (numItems + 4);
324 const int textSize = ITEM_TEXT_SIZE;
325
326 using MTFH::MenuItem;
327 using MTFH::MenuManager;
328
329 const auto vertical = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Vertical").toUpper().toStdWString());
330 vertical->setView(std::make_shared<TextMenuItemView>(textSize, *vertical));
331 vertical->setAction(
332 []() {
333 juzzlin::L().info() << "Vertical split selected.";
334 Game::instance().setSplitType(Game::SplitType::Vertical);
335 MenuManager::instance().popMenu();
336 });
337
338 const auto horizontal = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Horizontal").toUpper().toStdWString());
339 horizontal->setView(std::make_shared<TextMenuItemView>(textSize, *horizontal));
340 horizontal->setAction(
341 []() {
342 juzzlin::L().info() << "Horizontal split selected.";
343 Game::instance().setSplitType(Game::SplitType::Horizontal);
344 MenuManager::instance().popMenu();
345 });
346
347 m_splitTypeMenu->addItem(horizontal);
348 m_splitTypeMenu->addItem(vertical);
349 }
350
351 void SettingsMenu::populateGfxMenu(int width, int height)
352 {
353 const int itemHeight = height / ITEM_HEIGHT_DIV;
354 const int textSize = ITEM_TEXT_SIZE;
355
356 using MTFH::MenuItem;
357 using MTFH::MenuManager;
358
359 const auto selectFullScreenResolution = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Full screen resolution >").toUpper().toStdWString());
360 selectFullScreenResolution->setView(std::make_shared<TextMenuItemView>(textSize, *selectFullScreenResolution));
361 selectFullScreenResolution->setMenuOpenAction(FULL_SCREEN_RESOLUTION_MENU_ID);
362
363 const auto selectWindowedResolution = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Windowed resolution >").toUpper().toStdWString());
364 selectWindowedResolution->setView(std::make_shared<TextMenuItemView>(textSize, *selectWindowedResolution));
365 selectWindowedResolution->setMenuOpenAction(WINDOWED_RESOLUTION_MENU_ID);
366
367 const auto selectFps = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("FPS >").toUpper().toStdWString());
368 selectFps->setView(std::make_shared<TextMenuItemView>(textSize, *selectFps));
369 selectFps->setMenuOpenAction(FPS_MENU_ID);
370
371 const auto splitType = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Split type >").toUpper().toStdWString());
372 splitType->setView(std::make_shared<TextMenuItemView>(textSize, *splitType));
373 splitType->setMenuOpenAction(SPLIT_TYPE_MENU_ID);
374 #ifdef VSYNC_MENU
375 const auto vsync = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("VSync >").toUpper().toStdWString());
376 vsync->setView(std::make_shared<TextMenuItemView>(textSize, *vsync));
377 vsync->setMenuOpenAction(VSYNC_MENU_ID);
378 m_gfxMenu->addItem(vsync);
379 #endif
380 m_gfxMenu->addItem(splitType);
381 m_gfxMenu->addItem(selectFps);
382 m_gfxMenu->addItem(selectWindowedResolution);
383 m_gfxMenu->addItem(selectFullScreenResolution);
384 }
385
386 void SettingsMenu::populateSfxMenu(int width, int height)
387 {
388 const int itemHeight = height / ITEM_HEIGHT_DIV;
389 const int textSize = ITEM_TEXT_SIZE;
390
391 using MTFH::MenuItem;
392 using MTFH::MenuManager;
393
394 const auto offItem = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Off").toUpper().toStdWString());
395 offItem->setView(std::make_shared<TextMenuItemView>(textSize, *offItem));
396 offItem->setAction(
397 []() {
398 juzzlin::L().info() << "Sounds off selected.";
399 Game::instance().audioWorker().setEnabled(false);
400 Settings::instance().saveValue(Settings::soundsKey(), false);
401 MenuManager::instance().popMenu();
402 });
403
404 m_sfxMenu->addItem(MTFH::MenuItemPtr(offItem));
405
406 const auto onItem = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("On").toUpper().toStdWString());
407 onItem->setView(std::make_shared<TextMenuItemView>(textSize, *onItem));
408 onItem->setAction(
409 []() {
410 juzzlin::L().info() << "Sounds on selected.";
411 Game::instance().audioWorker().setEnabled(true);
412 Settings::instance().saveValue(Settings::soundsKey(), true);
413 MenuManager::instance().popMenu();
414 });
415
416 m_sfxMenu->addItem(onItem);
417
418 if (Game::instance().audioWorker().enabled())
419 {
420 onItem->setCurrent();
421 }
422 else
423 {
424 offItem->setCurrent();
425 }
426 }
427
428 void SettingsMenu::populateResetMenu(int width, int height)
429 {
430 const int itemHeight = height / ITEM_HEIGHT_DIV;
431 const int textSize = ITEM_TEXT_SIZE;
432
433 using MTFH::MenuItem;
434 using MTFH::MenuManager;
435
436 const auto resetRecordTimes = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Reset record times").toUpper().toStdWString());
437 resetRecordTimes->setView(std::make_shared<TextMenuItemView>(textSize, *resetRecordTimes));
438 resetRecordTimes->setAction(std::make_shared<ResetAction>(ResetAction::Type::Times, m_confirmationMenu));
439
440 const auto resetBestPositions = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Reset best positions").toUpper().toStdWString());
441 resetBestPositions->setView(std::make_shared<TextMenuItemView>(textSize, *resetBestPositions));
442 resetBestPositions->setAction(std::make_shared<ResetAction>(ResetAction::Type::Positions, m_confirmationMenu));
443
444 const auto resetUnlockedTracks = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Reset unlocked tracks").toUpper().toStdWString());
445 resetUnlockedTracks->setView(std::make_shared<TextMenuItemView>(textSize, *resetUnlockedTracks));
446 resetUnlockedTracks->setAction(std::make_shared<ResetAction>(ResetAction::Type::Tracks, m_confirmationMenu));
447
448 m_resetMenu->addItem(resetRecordTimes);
449 m_resetMenu->addItem(resetBestPositions);
450 m_resetMenu->addItem(resetUnlockedTracks);
451 }