Debian Codegraph @

src/game/menu/resolutionmenu.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2012 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 "resolutionmenu.hpp"
17 #include "confirmationmenu.hpp"
18 #include "game.hpp"
19 #include "renderer.hpp"
20 #include "settings.hpp"
21 #include "textmenuitemview.hpp"
22
23 #include <MenuItem>
24 #include <MenuItemAction>
25 #include <MenuItemView>
26 #include <MenuManager>
27
28 #include "simple_logger.hpp"
29
30 #include <QObject> // for tr()
31 #include <QScreen>
32
33 #include <memory>
34 #include <sstream>
35
36 struct Resolution
37 {
38 int hRes, vRes;
39 };
40
41 class ResolutionItem : public MTFH::MenuItem
42 {
43 public:
44 class SaveResolutionAction : public MTFH::MenuItemAction
45 {
46 public:
47 //! Constructor.
48 SaveResolutionAction(ResolutionItem & parent, bool fullScreen)
49 : m_parent(parent)
50 , m_fullScreen(fullScreen)
51 {
52 }
53
54 bool fullScreen() const
55 {
56 return m_fullScreen;
57 }
58
59 //! \reimp
60 virtual void fire()
61 {
62 const int hRes = m_parent.hRes();
63 const int vRes = m_parent.vRes();
64 Settings::instance().saveResolution(hRes, vRes, m_fullScreen);
65 juzzlin::L().info()
66 << "Resolution set: " << hRes << " " << vRes
67 << " Full screen: " << m_fullScreen;
68
69 if (Game::instance().renderer().fullScreen() && m_fullScreen)
70 {
71 Game::instance().renderer().setResolution(QSize(hRes, vRes));
72 }
73 else
74 {
75 Game::instance().exitGame();
76 }
77 }
78
79 private:
80 ResolutionItem & m_parent;
81 bool m_fullScreen;
82 };
83
84 //! Constructor.
85 ResolutionItem(ConfirmationMenuPtr confirmationMenu,
86 int hRes, int vRes, bool fullScreen, int width, int height, std::wstring text = L"")
87 : MenuItem(width, height, text)
88 , m_confirmationMenu(confirmationMenu)
89 , m_saveResolutionAction(std::make_shared<SaveResolutionAction>(*this, fullScreen))
90 , m_hRes(hRes)
91 , m_vRes(vRes)
92 {
93 }
94
95 //! Destructor.
96 virtual ~ResolutionItem()
97 {
98 }
99
100 //! \reimp
101 virtual void setSelected(bool flag)
102 {
103 MenuItem::setSelected(flag);
104
105 if (flag)
106 {
107 // Change the virtual resolution immediately if changing from fullscreen to fullscreen
108 if (Game::instance().renderer().fullScreen() && std::dynamic_pointer_cast<SaveResolutionAction>(m_saveResolutionAction)->fullScreen())
109 {
110 m_saveResolutionAction->fire();
111 }
112 else
113 {
114 MTFH::MenuManager::instance().pushMenu(m_confirmationMenu->id());
115 m_confirmationMenu->setText(QObject::tr("Restart to change the resolution.").toStdWString());
116 m_confirmationMenu->setAcceptAction(m_saveResolutionAction);
117 m_confirmationMenu->setCurrentIndex(1);
118 }
119 }
120 }
121
122 int hRes() const
123 {
124 return m_hRes;
125 }
126
127 int vRes() const
128 {
129 return m_vRes;
130 }
131
132 private:
133 ConfirmationMenuPtr m_confirmationMenu;
134
135 MTFH::MenuItemActionPtr m_saveResolutionAction;
136
137 int m_hRes;
138
139 int m_vRes;
140 };
141
142 ResolutionMenu::ResolutionMenu(
143 ConfirmationMenuPtr confirmationMenu, std::string id, int width, int height, bool fullScreen)
144 : SurfaceMenu("settingsBack", id, width, height)
145 , m_confirmationMenu(confirmationMenu)
146 {
147 const int numResolutions = 8;
148 const int fullHRes = Game::instance().screen()->geometry().width();
149 const int fullVRes = Game::instance().screen()->geometry().height();
150 const int itemHeight = height / (numResolutions + 3);
151
152 int itemHRes = fullHRes;
153 int itemVRes = fullVRes;
154 for (int i = 0; i < numResolutions; i++)
155 {
156 std::wstringstream resString;
157 resString << itemHRes << "x" << itemVRes;
158 const auto resolution =
159 std::make_shared<ResolutionItem>(m_confirmationMenu, itemHRes, itemVRes, fullScreen, width, itemHeight, resString.str());
160 resolution->setView(std::make_shared<TextMenuItemView>(20, *resolution));
161 addItem(resolution);
162
163 itemHRes -= fullHRes / numResolutions;
164 itemVRes -= fullVRes / numResolutions;
165 }
166
167 reverseItems();
168
169 enter();
170 }
171
172 void ResolutionMenu::enter()
173 {
174 setCurrentIndex(static_cast<int>(itemCount()) - 1);
175
176 // Set the current active resolution selected
177 for (unsigned int i = 0; i < itemCount(); i++)
178 {
179 if (auto resolution = std::dynamic_pointer_cast<ResolutionItem>(item(i)))
180 {
181 int hRes = 0;
182 int vRes = 0;
183 bool fullScreen = false;
184
185 Settings::instance().loadResolution(hRes, vRes, fullScreen);
186 if (resolution->hRes() == hRes && resolution->vRes() == vRes)
187 {
188 resolution->setCurrent();
189 break;
190 }
191 }
192 }
193 }