Debian Codegraph @

source/code/menudef.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2013 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 http://blockattack.net
21 ===========================================================================
22 */
23 #include "global.hpp"
24 #include <iostream>
25 #include <stdlib.h>
26 #include "MenuSystem.h"
27 #include "common.h"
28 #include "HelpHowtoState.hpp"
29 #include "HelpModInfoState.hpp"
30 #include "HelpGamepadState.hpp"
31 #include "HelpAboutState.hpp"
32 #include "ShowFileState.hpp"
33 #include <fmt/core.h>
34 #include "menudef_themes.hpp"
35
36
37 #if 0
38 //Menu
39 static void PrintHi(Button* b) {
40 cout << "Hi" <<"\n";
41 }
42 #endif
43
44 //Stores the controls
45 struct control {
46 SDL_Keycode up;
47 SDL_Keycode down;
48 SDL_Keycode left;
49 SDL_Keycode right;
50 SDL_Keycode change;
51 SDL_Keycode push;
52 };
53
54 void OpenScoresDisplay();
55 extern control keySettings[3];
56
57 //Function to return the name of a key, to be displayed...
58 std::string getKeyName(SDL_Keycode key) {
59 std::string keyname(SDL_GetKeyName(key));
60 if (key == SDLK_UP) {
61 keyname = _("Up arrow");
62 }
63 if (key == SDLK_DOWN) {
64 keyname = _("Down arrow");
65 }
66 if (key == SDLK_LEFT) {
67 keyname = _("Left arrow");
68 }
69 if (key == SDLK_RIGHT) {
70 keyname = _("Right arrow");
71 }
72 if (key == SDLK_RCTRL) {
73 keyname = _("Right Ctrl");
74 }
75 if (key == SDLK_LCTRL) {
76 keyname = _("Left Ctrl");
77 }
78 if (key == SDLK_RSHIFT) {
79 keyname = _("Right shift");
80 }
81 if (key == SDLK_LSHIFT) {
82 keyname = _("Left shift");
83 }
84 if (key == SDLK_RALT) {
85 keyname = _("Right alt");
86 }
87 if (key == SDLK_LALT) {
88 keyname = _("Left alt");
89 }
90 if (key == SDLK_RETURN) {
91 keyname = _("Return");
92 }
93 if (key == SDLK_SPACE) {
94 keyname = _("Space");
95 }
96 if (globalData.verboseLevel) {
97 std::cout << key << " translated to " << keyname << "\n";
98 }
99 return keyname;
100 }
101
102 class Button_changekey : public Button {
103 private:
104 SDL_Keycode* m_key2change;
105 std::string m_keyname;
106 public:
107 Button_changekey(SDL_Keycode* key, const char* keyname);
108 virtual void doAction() override;
109 };
110
111
112 Button_changekey::Button_changekey(SDL_Keycode* key, const char* keyname)
113 : m_key2change{key}, m_keyname{keyname} {
114 setLabel(m_keyname+" : "+getKeyName(*m_key2change));
115 }
116
117 void Button_changekey::doAction() {
118 SDL_Event event;
119
120 bool finnish = false;
121 while (!finnish) {
122 SDL_Delay(10);
123 while ( SDL_PollEvent(&event) ) {
124 if (event.type == SDL_KEYDOWN) {
125 if (event.key.keysym.sym != SDLK_ESCAPE) {
126 *m_key2change = event.key.keysym.sym;
127 }
128 finnish = true;
129 }
130 }
131 }
132 setLabel(m_keyname+" : "+getKeyName(*m_key2change));
133 }
134
135 class Button_confirmVolume : public Button {
136 private:
137 std::string cvar="";
138 mutable std::string volumeLabel; /*used for cache*/
139 public:
140 explicit Button_confirmVolume(const char* cvar);
141 virtual void doAction() override { /*Do nothing. Equal to choose "back". */ };
142 virtual const std::string& getLabel() const override;
143 };
144
145 Button_confirmVolume::Button_confirmVolume(const char* cvar) : cvar{cvar} {
146 this->setPopOnRun(true);
147 }
148
149 const std::string& Button_confirmVolume::getLabel() const {
150 int volumePct = Config::getInstance()->getInt(cvar)*100.0/MIX_MAX_VOLUME;
151 this->volumeLabel = fmt::format(_("Volume: {}%"), volumePct); // std::string(_("Volume: "))+ std::to_string(volumePct)+"%" ;
152 return volumeLabel;
153 }
154
155 class Button_increaseVolume : public Button {
156 private:
157 std::string cvar="";
158 int incrementValue = 1;
159 int lowerLimit = 0;
160 int upperLimit = MIX_MAX_VOLUME;
161 public:
162 Button_increaseVolume(const char* cvar, int incrementValue);
163 virtual void doAction() override;
164 };
165
166 Button_increaseVolume::Button_increaseVolume(const char* cvar, int incrementValue)
167 : cvar{cvar},incrementValue{incrementValue} {
168 char prefix = '+';
169 double value = incrementValue;
170 if (incrementValue<0) {
171 prefix = '-';
172 value = -incrementValue;
173 }
174 value = value*100.0/MIX_MAX_VOLUME;
175 setLabel(fmt::format("{}{:.2f}", prefix, value));
176 }
177
178 void Button_increaseVolume::doAction() {
179 int newValue = Config::getInstance()->getInt(cvar)+incrementValue;
180 if (newValue<lowerLimit) {
181 newValue = lowerLimit;
182 }
183 if (newValue>upperLimit) {
184 newValue = upperLimit;
185 }
186 Config::getInstance()->setInt(cvar, newValue);
187 }
188
189 class Button_testSound : public Button {
190 public:
191 Button_testSound();
192 virtual void doAction() override;
193 };
194
195 Button_testSound::Button_testSound() {
196 setLabel(_("Test sound"));
197 }
198
199 void Button_testSound::doAction() {
200 sago::SoundHandler testSound = globalData.spriteHolder->GetDataHolder().getSoundHandler("pop");
201 Mix_VolumeChunk(testSound.get(), Config::getInstance()->getInt("volume_sound"));
202 Mix_PlayChannel(1, testSound.get(), 0);
203 }
204
205 class Button_testMusic : public Button {
206 public:
207 Button_testMusic();
208 virtual void doAction() override;
209 };
210
211 Button_testMusic::Button_testMusic() {
212 setLabel(_("Test music"));
213 }
214
215 void Button_testMusic::doAction() {
216 Mix_VolumeMusic(Config::getInstance()->getInt("volume_music"));
217 sago::MusicHandler bgMusic = globalData.spriteHolder->GetDataHolder().getMusicHandler("bgmusic");
218 Mix_PlayMusic(bgMusic.get(), -1);
219 }
220
221
222 static void runSinglePlayerEndless0() {
223 runGame(Gametype::SinglePlayerEndless, 0);
224 }
225
226 static void runSinglePlayerEndless1() {
227 runGame(Gametype::SinglePlayerEndless, 1);
228 }
229
230 static void runSinglePlayerEndless2() {
231 runGame(Gametype::SinglePlayerEndless, 2);
232 }
233
234 static void runSinglePlayerEndless3() {
235 runGame(Gametype::SinglePlayerEndless, 3);
236 }
237
238 static void runSinglePlayerEndless4() {
239 runGame(Gametype::SinglePlayerEndless, 4);
240 }
241
242 static void runSinglePlayerEndless() {
243 Menu sp(globalData.screen,_("Single Player Endless"),true);
244 Button d0,d1,d2,d3,d4;
245 d0.setPopOnRun(true);
246 d1.setPopOnRun(true);
247 d2.setPopOnRun(true);
248 d3.setPopOnRun(true);
249 d4.setPopOnRun(true);
250 d0.setLabel(_("Slow"));
251 d1.setLabel(_("Fast"));
252 d2.setLabel(_("Faster"));
253 d3.setLabel(_("Even faster"));
254 d4.setLabel(_("Fastest"));
255 d0.setAction(runSinglePlayerEndless0);
256 d1.setAction(runSinglePlayerEndless1);
257 d2.setAction(runSinglePlayerEndless2);
258 d3.setAction(runSinglePlayerEndless3);
259 d4.setAction(runSinglePlayerEndless4);
260 sp.addButton(&d0);
261 sp.addButton(&d1);
262 sp.addButton(&d2);
263 sp.addButton(&d3);
264 sp.addButton(&d4);
265 RunGameState(sp);
266 }
267
268 static void runSinglePlayerTimeTrial() {
269 runGame(Gametype::SinglePlayerTimeTrial, 0);
270 }
271
272 static void runStageClear() {
273 runGame(Gametype::StageClear, 0);
274 }
275
276 static void runSinglePlayerPuzzle() {
277 runGame(Gametype::Puzzle, 0);
278 }
279
280 class RunSinglePlayerVsButton : public Button {
281 virtual void doAction() override {
282 runGame(Gametype::SinglePlayerVs, iGeneric1);
283 }
284 };
285
286 static void runTwoPlayerTimeTrial() {
287 runGame(Gametype::TwoPlayerTimeTrial, 0);
288 }
289
290 static void runTwoPlayerVs() {
291 runGame(Gametype::TwoPlayerVs, 0);
292 }
293
294 static void SetAlwaysSoftwareLabel(Button* b) {
295 b->setLabel(Config::getInstance()->getInt("always-software")?
296 _("Always use software render: On")
297 : _("Always use software render: Off"));
298 }
299
300 static void runSetMusicVolume(const char* cvar, const char* header) {
301 Menu volumeMenu(globalData.screen, header, true);
302 Button_confirmVolume bConfirm(cvar);
303 Button_increaseVolume plus5(cvar, 5);
304 Button_increaseVolume minus5(cvar, -5);
305 Button_increaseVolume plus1(cvar, 1);
306 Button_increaseVolume minus1(cvar, -1);
307 volumeMenu.addButton(&bConfirm);
308 volumeMenu.addButton(&plus5);
309 volumeMenu.addButton(&minus5);
310 volumeMenu.addButton(&plus1);
311 volumeMenu.addButton(&minus1);
312 Button_testSound bTestSound;
313 volumeMenu.addButton(&bTestSound);
314 Button_testMusic bTestMusic;
315 volumeMenu.addButton(&bTestMusic);
316 RunGameState(volumeMenu);
317 }
318
319 static void SetMusicLabel (Button* b) {
320 if (globalData.MusicEnabled) {
321 double volume = Config::getInstance()->getInt("volume_music");
322 int volumePct = volume*100.0/MIX_MAX_VOLUME;
323 b->setLabel(std::string(_("Music: "))+ std::to_string(volumePct)+"%" );
324 }
325 else {
326 b->setLabel(_("Music: Off") );
327 }
328 }
329
330 static void SetSoundLabel (Button* b) {
331 if (globalData.SoundEnabled) {
332 double volume = Config::getInstance()->getInt("volume_sound");
333 int volumePct = volume*100.0/MIX_MAX_VOLUME;
334 b->setLabel(std::string(_("Sound: "))+ std::to_string(volumePct)+"%" );
335 }
336 else {
337 b->setLabel(_("Sound: Off") );
338 }
339 }
340
341 static void SetFullscreenLabel (Button* b) {
342 b->setLabel(globalData.bFullscreen? _("Fullscreen: On") : _("Fullscreen: Off") );
343 }
344
345 static void SetBlockVariationLabel (Button* b) {
346 b->setLabel(_("Block variants: ")+std::to_string(Config::getInstance()->getInt("basic_block_variants", 6)) );
347 }
348
349 class AlwaysSoftwareRenderButton : public Button {
350 virtual void doAction() override {
351 Config::getInstance()->setInt("always-software", !Config::getInstance()->getInt("always-software"));
352 SetAlwaysSoftwareLabel(this);
353 }
354 };
355
356 class MusicButton : public Button {
357 virtual void doAction() override {
358 runSetMusicVolume("volume_music", _("Music volume"));
359 globalData.MusicEnabled = (Config::getInstance()->getInt("volume_music") != 0);
360 SetMusicLabel(this);
361 }
362 };
363
364 class SoundButton : public Button {
365 virtual void doAction() override {
366 runSetMusicVolume("volume_sound", _("Sound volume"));
367 globalData.MusicEnabled = (Config::getInstance()->getInt("volume_sound") != 0);
368 SetSoundLabel(this);
369 }
370 };
371
372 class FullscreenButton : public Button {
373 virtual void doAction() override {
374 globalData.bFullscreen = !globalData.bFullscreen;
375 SetFullscreenLabel(this);
376 ResetFullscreen();
377 }
378 };
379
380 class BlockVariantButton : public Button {
381 virtual void doAction() override {
382 int blockVariation = Config::getInstance()->getInt("basic_block_variants", 6);
383 blockVariation++;
384 if (blockVariation>6) {
385 blockVariation = 5;
386 }
387 Config::getInstance()->setInt("basic_block_variants", blockVariation);
388 SetBlockVariationLabel(this);
389 }
390 };
391
392 static void buttonActionPlayer1Name() {
393 if ( OpenDialogbox(200, 100, globalData.player1name, _("Enter player 1 name:")) ) {
394 return; //must save if true
395 }
396 }
397
398 static void buttonActionPlayer2Name() {
399 if ( OpenDialogbox(200, 100, globalData.player2name, _("Enter player 2 name:")) ) {
400 return; //must save if true
401 }
402 }
403
404 static void buttonActionHighscores() {
405 OpenScoresDisplay();
406 }
407
408 static void ChangeKeysMenu(long playernumber) {
409 Menu km(globalData.screen,_("Change key bindings"),true);
410 Button_changekey bLeft(&keySettings[playernumber].left,_("Left") );
411 Button_changekey bRight(&keySettings[playernumber].right,_("Right") );
412 Button_changekey bUp(&keySettings[playernumber].up,_("Up") );
413 Button_changekey bDown(&keySettings[playernumber].down,_("Down") );
414 Button_changekey bPush(&keySettings[playernumber].push,_("Push") );
415 Button_changekey bSwitch(&keySettings[playernumber].change,_("Change") );
416 km.addButton(&bLeft);
417 km.addButton(&bRight);
418 km.addButton(&bUp);
419 km.addButton(&bDown);
420 km.addButton(&bPush);
421 km.addButton(&bSwitch);
422 RunGameState(km);
423 }
424
425 static void ChangeKeysMenu1() {
426 ChangeKeysMenu(0);
427 }
428
429 static void ChangeKeysMenu2() {
430 ChangeKeysMenu(2);
431 }
432
433 static void PlayerConfigMenu() {
434 Menu pcm(globalData.screen,_("Player configuration"),true);
435 Button bPlayer1Name, bPlayer2Name;
436 Button bPlayer1Keys, bPlayer2Keys;
437 bPlayer1Name.setAction(buttonActionPlayer1Name);
438 bPlayer1Name.setLabel(_("Change player 1's name") );
439 bPlayer2Name.setAction(buttonActionPlayer2Name);
440 bPlayer2Name.setLabel(_("Change player 2's name") );
441 bPlayer1Keys.setAction(ChangeKeysMenu1);
442 bPlayer1Keys.setLabel(_("Change player 1's keys") );
443 bPlayer2Keys.setAction(ChangeKeysMenu2);
444 bPlayer2Keys.setLabel(_("Change player 2's keys") );
445 pcm.addButton(&bPlayer1Name);
446 pcm.addButton(&bPlayer2Name);
447 pcm.addButton(&bPlayer1Keys);
448 pcm.addButton(&bPlayer2Keys);
449 RunGameState(pcm);
450 }
451
452
453 static void ConfigureMenu() {
454 Menu cm(globalData.screen,_("Configuration"),true);
455 AlwaysSoftwareRenderButton bSoftware;
456 MusicButton bMusic;
457 SoundButton bSound;
458 FullscreenButton buttonFullscreen;
459 BlockVariantButton bBlockVariant;
460 Button bPlayerConfig;
461 bPlayerConfig.setLabel(_("Player configuration") );
462 bPlayerConfig.setAction(PlayerConfigMenu);
463 Button bThemes;
464 bThemes.setLabel(_("Themes"));
465 bThemes.setAction(OpenThemesMenu);
466 SetAlwaysSoftwareLabel(&bSoftware);
467 SetMusicLabel(&bMusic);
468 SetSoundLabel(&bSound);
469 SetFullscreenLabel(&buttonFullscreen);
470 SetBlockVariationLabel(&bBlockVariant);
471 cm.addButton(&bMusic);
472 cm.addButton(&bSound);
473 cm.addButton(&bSoftware);
474 cm.addButton(&buttonFullscreen);
475 cm.addButton(&bBlockVariant);
476 cm.addButton(&bPlayerConfig);
477 cm.addButton(&bThemes);
478 RunGameState(cm);
479 }
480
481 static void SinglePlayerVsMenu() {
482 Menu spvs(globalData.screen,_("Single player VS"),true);
483 RunSinglePlayerVsButton d1,d2,d3,d4,d5,d6,d7;
484 d1.setPopOnRun(true);
485 d2.setPopOnRun(true);
486 d3.setPopOnRun(true);
487 d4.setPopOnRun(true);
488 d5.setPopOnRun(true);
489 d6.setPopOnRun(true);
490 d7.setPopOnRun(true);
491 d1.iGeneric1 = 0;
492 d2.iGeneric1 = 1;
493 d3.iGeneric1 = 2;
494 d4.iGeneric1 = 3;
495 d5.iGeneric1 = 4;
496 d6.iGeneric1 = 5;
497 d7.iGeneric1 = 6;
498 d1.setLabel(_("Very easy"));
499 d2.setLabel(_("Easy"));
500 d3.setLabel(_("Below normal"));
501 d4.setLabel(_("Normal"));
502 d5.setLabel(_("Above normal"));
503 d6.setLabel(_("Hard"));
504 d7.setLabel(_("Hardest"));
505 spvs.addButton(&d1);
506 spvs.addButton(&d2);
507 spvs.addButton(&d3);
508 spvs.addButton(&d4);
509 spvs.addButton(&d5);
510 spvs.addButton(&d6);
511 spvs.addButton(&d7);
512 RunGameState(spvs);
513 }
514
515 static void MultiplayerMenu() {
516 Menu mm(globalData.screen,_("Multiplayer"),true);
517 Button bTT, bVs;
518 bTT.setLabel(_("Two player - time trial"));
519 bTT.setAction(runTwoPlayerTimeTrial);
520 bVs.setLabel(_("Two player - vs"));
521 bVs.setAction(runTwoPlayerVs);
522 mm.addButton(&bTT);
523 mm.addButton(&bVs);
524 RunGameState(mm);
525 }
526
527 static void runHowto() {
528 HelpHowtoState howto;
529 RunGameState(howto);
530 }
531
532 static void runHelpModInfo() {
533 HelpModInfoState modInfo;
534 RunGameState(modInfo);
535 }
536
537 static void runHelpGamepad() {
538 HelpGamepadState helpGamepad;
539 RunGameState(helpGamepad);
540 }
541
542 static void runHelpAbout() {
543 HelpAboutState helpAbout;
544 RunGameState(helpAbout);
545 }
546
547 static void runCredits() {
548 ShowFileState creditsFile;
549 creditsFile.SetData("misc/AUTHORS", _("Credits"));
550 RunGameState(creditsFile);
551 }
552
553 static void HelpMenu() {
554 Menu m(globalData.screen, _("Help"), true);
555 Button bHowto;
556 bHowto.setLabel(_("How to"));
557 bHowto.setAction(runHowto);
558 m.addButton(&bHowto);
559 Button bGamepad;
560 bGamepad.setLabel(_("Gamepad"));
561 bGamepad.setAction(runHelpGamepad);
562 m.addButton(&bGamepad);
563 Button bModInfo;
564 bModInfo.setLabel(_("Mod info"));
565 bModInfo.setAction(runHelpModInfo);
566 m.addButton(&bModInfo);
567 Button bCredits;
568 bCredits.setLabel(_("Credits"));
569 bCredits.setAction(runCredits);
570 m.addButton(&bCredits);
571 Button bAbout;
572 bAbout.setLabel(_("About"));
573 bAbout.setAction(runHelpAbout);
574 m.addButton(&bAbout);
575 RunGameState(m);
576 }
577
578 static void SinglePlayerMenu() {
579 Menu m(globalData.screen, _("Single player"), true);
580 Button bHi,bTimetrial1, bStageClear, bPuzzle, bVs1;
581 bHi.setLabel(_("Single player - endless") );
582 bHi.setAction(runSinglePlayerEndless);
583 bTimetrial1.setLabel(_("Single player - time trial") );
584 bTimetrial1.setAction(runSinglePlayerTimeTrial);
585 bStageClear.setLabel(_("Single player - stage clear") );
586 bStageClear.setAction(runStageClear);
587 bPuzzle.setLabel(_("Single player - puzzle mode") );
588 bPuzzle.setAction(runSinglePlayerPuzzle);
589 bVs1.setLabel(_("Single player - vs") );
590 bVs1.setAction(SinglePlayerVsMenu);
591 m.addButton(&bHi);
592 m.addButton(&bTimetrial1);
593 m.addButton(&bStageClear);
594 m.addButton(&bPuzzle);
595 m.addButton(&bVs1);
596 RunGameState(m);
597 }
598
599 void SafeModeMenu() {
600 if (Config::getInstance()->getInt("always-software")) {
601 return;
602 }
603 Menu safeMode(globalData.screen, _("Game did not shutdown as it should"), true);
604 Button bOnce;
605 Button bAlways;
606 bOnce.setLabel(_("Use software renderer this time"));
607 bAlways.setLabel(_("Always use software renderer"));
608 bOnce.setPopOnRun(true);
609 bAlways.setAction([]() {
610 Config::getInstance()->setInt("always-software", 1);
611 });
612 bAlways.setPopOnRun(true);
613 safeMode.addButton(&bOnce);
614 safeMode.addButton(&bAlways);
615 RunGameState(safeMode);
616 }
617
618 void MainMenu() {
619 Menu m(globalData.screen,_("Block Attack - Rise of the blocks"),false);
620 Button bHi, bMulti, bConfigure, bHighscore, bHelp;
621 bHi.setLabel(_("Single player") );
622 bHi.setAction(SinglePlayerMenu);
623 bMulti.setLabel(_("Multi player") );
624 bMulti.setAction(MultiplayerMenu);
625 bConfigure.setLabel(_("Configure") );
626 bConfigure.setAction(ConfigureMenu);
627 bHighscore.setLabel(_("Highscores") );
628 bHighscore.setAction(buttonActionHighscores);
629 bHelp.setLabel(_("Help"));
630 bHelp.setAction(HelpMenu);
631 m.addButton(&bHi);
632 m.addButton(&bMulti);
633 m.addButton(&bConfigure);
634 m.addButton(&bHighscore);
635 m.addButton(&bHelp);
636 RunGameState(m);
637 }