Debian Codegraph @

src/game/menu/difficultymenu.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2017 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 "difficultymenu.hpp"
17
18 #include "difficultyprofile.hpp"
19 #include "game.hpp"
20 #include "lapcountmenu.hpp"
21 #include "settings.hpp"
22 #include "textmenuitemview.hpp"
23
24 #include <MenuItem>
25 #include <MenuManager>
26
27 #include "simple_logger.hpp"
28
29 static const int ITEM_TEXT_SIZE = 40;
30
31 std::string DifficultyMenu::MenuId = "difficulty";
32
33 DifficultyMenu::DifficultyMenu(int width, int height)
34 : SurfaceMenu("trackSelectionBack", MenuId, width, height, Menu::Style::VerticalList)
35 {
36 const int itemHeight = height / 8;
37 const int textSize = ITEM_TEXT_SIZE;
38
39 using MTFH::MenuItem;
40 using MTFH::MenuManager;
41
42 const auto easyItem = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Easy").toUpper().toStdWString());
43 easyItem->setView(std::make_shared<TextMenuItemView>(textSize, *easyItem));
44 easyItem->setAction([]() {
45 juzzlin::L().info() << "Easy selected.";
46 const DifficultyProfile::Difficulty chosenDifficulty = DifficultyProfile::Difficulty::Easy;
47 Settings::instance().saveDifficulty(chosenDifficulty);
48 Game::instance().difficultyProfile().setDifficulty(chosenDifficulty);
49 MenuManager::instance().pushMenu(LapCountMenu::MenuId);
50 });
51
52 const auto mediumItem = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Medium").toUpper().toStdWString());
53 mediumItem->setView(std::make_shared<TextMenuItemView>(textSize, *mediumItem));
54 mediumItem->setAction([]() {
55 juzzlin::L().info() << "Medium selected.";
56 const DifficultyProfile::Difficulty chosenDifficulty = DifficultyProfile::Difficulty::Medium;
57 Settings::instance().saveDifficulty(chosenDifficulty);
58 Game::instance().difficultyProfile().setDifficulty(chosenDifficulty);
59 MenuManager::instance().pushMenu(LapCountMenu::MenuId);
60 });
61
62 const auto hardItem = std::make_shared<MenuItem>(width, itemHeight, QObject::tr("Hard").toUpper().toStdWString());
63 hardItem->setView(std::make_shared<TextMenuItemView>(textSize, *hardItem));
64 hardItem->setAction([]() {
65 juzzlin::L().info() << "Hard selected.";
66 const DifficultyProfile::Difficulty chosenDifficulty = DifficultyProfile::Difficulty::Hard;
67 Settings::instance().saveDifficulty(chosenDifficulty);
68 Game::instance().difficultyProfile().setDifficulty(chosenDifficulty);
69 MenuManager::instance().pushMenu(LapCountMenu::MenuId);
70 });
71
72 addItem(hardItem);
73 addItem(mediumItem);
74 addItem(easyItem);
75
76 const DifficultyProfile::Difficulty difficulty = Game::instance().difficultyProfile().difficulty();
77 switch (difficulty)
78 {
79 case DifficultyProfile::Difficulty::Easy:
80 easyItem->setCurrent();
81 break;
82
83 case DifficultyProfile::Difficulty::Medium:
84 mediumItem->setCurrent();
85 break;
86
87 case DifficultyProfile::Difficulty::Hard:
88 hardItem->setCurrent();
89 break;
90 }
91 }