Debian Codegraph @

source/code/MenuSystem.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2012 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 https://blockattack.net
21 ===========================================================================
22 */
23
24
25 #include "BlockGame.hpp"
26 #include "MenuSystem.h"
27 #include "common.h"
28 #include "global.hpp"
29 #include "gamecontroller.h"
30 #include "menudef_themes.hpp"
31
32 static int oldmousex = 0;
33 static int oldmousey = 0;
34
35 void ButtonGfx::setSurfaces() {
36 this->xsize = globalData.spriteHolder->GetSprite(menu_marked).GetWidth();
37 this->ysize = globalData.spriteHolder->GetSprite(menu_marked).GetHeight();
38 if (globalData.verboseLevel) {
39 std::cout << "Surfaces set, size: " << this->xsize << " , " << this->ysize << "\n";
40 }
41 }
42
43 sago::SagoTextField* ButtonGfx::getLabel(const std::string& text, bool marked) const {
44 if (!marked) {
45 const auto& theLabel = labels.find(text);
46 if (theLabel != labels.end()) {
47 return labels[text].get();
48 }
49 }
50 else {
51 const auto& theLabel = labels_marked.find(text);
52 if (theLabel != labels_marked.end()) {
53 return labels_marked[text].get();
54 }
55 }
56 std::shared_ptr<sago::SagoTextField> newField = std::make_shared<sago::SagoTextField>();
57 newField->SetHolder(&globalData.spriteHolder->GetDataHolder());
58 newField->SetFont("freeserif");
59 newField->SetFontSize(30);
60 newField->SetOutline(1, {64,64,64,255});
61 if (marked) {
62 newField->SetOutline(3, {32,32,32,255});
63 }
64 newField->SetText(text);
65 if (!marked) {
66 labels[text] = newField;
67 return labels[text].get();
68 }
69 labels_marked[text] = newField;
70 return labels_marked[text].get();
71 }
72
73 Button::Button(const Button& b) : action{b.action}, popOnRun{b.popOnRun}, label{b.label}, marked{b.marked} {
74 }
75
76 Button& Button::operator=(const Button& other) {
77 action = other.action;
78 label = other.label;
79 marked = other.marked;
80 popOnRun = other.popOnRun;
81 return *this;
82 }
83
84 void Button::setLabel(const std::string& text) {
85 label = text;
86 standardButton.setSurfaces();
87 }
88
89 void Button::setAction(void (*action2run)(void)) {
90 action = action2run;
91 }
92
93 void Button::doAction() {
94 if (action) {
95 action();
96 return;
97 }
98 std::cerr << "Warning: button \"" << label << "\" has no action assigned!";
99 }
100
101 void Button::setPopOnRun(bool popOnRun) {
102 this->popOnRun = popOnRun;
103 }
104
105 bool Button::isPopOnRun() const {
106 return popOnRun;
107 }
108
109 static void drawToScreen(const Button& b) {
110 if (b.marked) {
111 globalData.spriteHolder->GetSprite(b.standardButton.menu_marked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y, &globalData.logicalResize);
112 }
113 else {
114 globalData.spriteHolder->GetSprite(b.standardButton.menu_unmarked).Draw(globalData.screen, SDL_GetTicks(), b.x, b.y, &globalData.logicalResize);
115 }
116
117 b.standardButton.getLabel(b.getLabel(), b.marked)->Draw(globalData.screen, b.x+b.standardButton.xsize/2,b.y+b.standardButton.ysize/2,
118 sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::center, &globalData.logicalResize);
119 }
120
121
122 static bool isClicked(const Button& b, int x,int y) {
123 if ( x >= b.x && y >= b.y && x<= b.x+b.standardButton.xsize && y <= b.y + b.standardButton.ysize) {
124 return true;
125 }
126 return false;
127 }
128
129 void Menu::drawSelf(SDL_Renderer* target) {
130 DrawBackground(target);
131 for (const Button* b : buttons) {
132 drawToScreen(*b);
133 }
134 drawToScreen(exit);
135 exit.standardButton.getLabel(title, false)->Draw(target, 50, 50, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize);
136 }
137
138
139 void Menu::placeButtons() {
140 int nextY = 100;
141 int X = 50;
142 for (Button* it : buttons) {
143 X = (globalData.xsize - it->standardButton.xsize)/2;
144 it->x = X;
145 it->y = nextY;
146 nextY += it->standardButton.ysize+10;
147 }
148 exit.x = X;
149 exit.y = nextY;
150 }
151
152 void Menu::addButton(Button* b) {
153 buttons.push_back(b);
154 b->marked = false;
155 placeButtons();
156 }
157
158 Menu::Menu(SDL_Renderer* screen) {
159 buttons = std::vector<Button*>(10);
160 isSubmenu = true;
161 this->screen = screen;
162 exit.setLabel( _("Back") );
163 }
164
165 Menu::Menu(SDL_Renderer* screen,bool submenu) {
166 buttons = std::vector<Button*>(0);
167 isSubmenu = submenu;
168 this->screen = screen;
169 if (isSubmenu) {
170 exit.setLabel( _("Back") );
171 }
172 else {
173 exit.setLabel( _("Exit") );
174 }
175 }
176
177 Menu::Menu(SDL_Renderer* screen, const std::string& title, bool submenu) {
178 buttons = std::vector<Button*>(0);
179 isSubmenu = submenu;
180 this->screen = screen;
181 this->title = title;
182 if (isSubmenu) {
183 exit.setLabel(_("Back") );
184 }
185 else {
186 exit.setLabel(_("Exit") );
187 }
188 }
189
190 bool isUpEvent(const SDL_Event& event) {
191 if ( event.type == SDL_KEYDOWN ) {
192 if (event.key.keysym.sym == SDLK_UP) {
193 return true;
194 }
195 }
196 return GameControllerIsUpEvent(event);
197 }
198
199 bool isDownEvent(const SDL_Event& event) {
200 if ( event.type == SDL_KEYDOWN ) {
201 if (event.key.keysym.sym == SDLK_DOWN) {
202 return true;
203 }
204 }
205 return GameControllerIsDownEvent(event);
206 }
207
208 bool isLeftEvent(const SDL_Event& event) {
209 if ( event.type == SDL_KEYDOWN ) {
210 if (event.key.keysym.sym == SDLK_LEFT) {
211 return true;
212 }
213 }
214 return GameControllerIsLeftEvent(event);
215 }
216
217 bool isRightEvent(const SDL_Event& event) {
218 if ( event.type == SDL_KEYDOWN ) {
219 if (event.key.keysym.sym == SDLK_RIGHT) {
220 return true;
221 }
222 }
223 return GameControllerIsRightEvent(event);
224 }
225
226 bool isEscapeEvent(const SDL_Event& event) {
227 if ( event.type == SDL_KEYDOWN ) {
228 if ( event.key.keysym.sym == SDLK_ESCAPE ) {
229 return true;
230 }
231 }
232 if (event.type == SDL_CONTROLLERBUTTONDOWN) {
233 if (event.cbutton.button == SDL_CONTROLLER_BUTTON_B || event.cbutton.button == SDL_CONTROLLER_BUTTON_BACK ) {
234 return true;
235 }
236 }
237 return false;
238 }
239
240 bool isConfirmEvent(const SDL_Event& event) {
241 if ( event.type == SDL_KEYDOWN ) {
242 if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER ) {
243 return true;
244 }
245 }
246 if (event.type == SDL_CONTROLLERBUTTONDOWN) {
247 if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A ) {
248 return true;
249 }
250 }
251 return false;
252 }
253
254
255 bool Menu::IsActive() {
256 return running;
257 }
258 void Menu::Draw(SDL_Renderer* target) {
259 placeButtons();
260 drawSelf(target);
261 #if DEBUG
262 static unsigned long int Frames;
263 static unsigned long int Ticks;
264 static char FPS[10];
265 static sago::SagoTextField fpsField;
266 sagoTextSetBlueFont(fpsField);
267 Frames++;
268 if (SDL_GetTicks() >= Ticks + 1000) {
269 if (Frames > 999) {
270 Frames=999;
271 }
272 snprintf(FPS, sizeof(FPS), "%lu fps", Frames);
273 Frames = 0;
274 Ticks = SDL_GetTicks();
275 }
276 fpsField.SetText(FPS);
277 fpsField.Draw(globalData.screen, globalData.xsize - 4, 4, sago::SagoTextField::Alignment::right, sago::SagoTextField::VerticalAlignment::top, &globalData.logicalResize);
278 #endif
279 }
280 void Menu::ProcessInput(const SDL_Event& event, bool& processed) {
281 if (GameControllerIsConnectionEvent(event)) {
282 UnInitGameControllers();
283 InitGameControllers();
284 processed = true;
285 }
286
287 if (isUpEvent(event)) {
288 marked--;
289 if (marked<0) {
290 marked = buttons.size(); //not -1, since exit is after the last element in the list
291 }
292 processed = true;
293 }
294
295 if (isDownEvent(event)) {
296 marked++;
297 if (marked> (int)buttons.size()) {
298 marked = 0;
299 }
300 processed = true;
301 }
302
303 if (isEscapeEvent(event) && isSubmenu) {
304 running = false;
305 processed = true;
306 }
307
308 if (isConfirmEvent(event)) {
309 bMouseUp = false;
310 if (marked < (int)buttons.size()) {
311 buttons.at(marked)->doAction();
312 if (buttons.at(marked)->isPopOnRun()) {
313 running = false;
314 }
315 }
316 if (marked == (int)buttons.size()) {
317 running = false;
318 }
319 processed = true;
320 }
321 }
322
323 void Menu::Update() {
324 for (int i=0; i<(int)buttons.size(); i++) {
325 buttons.at(i)->marked = (i == marked);
326 }
327 exit.marked = (marked == (int)buttons.size());
328 Uint8 buttonState = SDL_GetMouseState(nullptr,nullptr);
329 // If the mouse button is released, make bMouseUp equal true
330 if ( (buttonState&SDL_BUTTON(1))==0) {
331 bMouseUp=true;
332 }
333 int mousex = globalData.mousex;
334 int mousey = globalData.mousey;
335 globalData.logicalResize.PhysicalToLogical(globalData.mousex, globalData.mousey, mousex, mousey);
336
337 if (abs(mousex-oldmousex)>5 || abs(mousey-oldmousey)>5) {
338 for (int i=0; i< (int)buttons.size(); ++i) {
339 if (isClicked(*buttons.at(i), mousex, mousey)) {
340 marked = i;
341 }
342 }
343 if (isClicked(exit, mousex, mousey)) {
344 marked = buttons.size();
345 }
346 oldmousex = mousex;
347 oldmousey = mousey;
348 }
349
350 //mouse clicked
351 if ( (buttonState&SDL_BUTTON(1) )==SDL_BUTTON(1) && bMouseUp) {
352 bMouseUp = false;
353 for (int i=0; i< (int)buttons.size(); ++i) {
354 if (isClicked(*buttons.at(i), mousex, mousey)) {
355 buttons.at(i)->doAction();
356 if (buttons.at(i)->isPopOnRun()) {
357 running = false;
358 }
359 //Quit here to ensure that we do not continue checking buttons after we have done the action.
360 return;
361 }
362 }
363 if (isClicked(exit, mousex, mousey)) {
364 running = false;
365 }
366 }
367 }
368