1 // SuperTux
2 // Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
3 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/menu/options_menu.hpp"
19
20 #include "audio/sound_manager.hpp"
21 #include "gui/dialog.hpp"
22 #include "gui/item_goto.hpp"
23 #include "gui/item_stringselect.hpp"
24 #include "gui/item_toggle.hpp"
25 #include "gui/menu_item.hpp"
26 #include "gui/menu_manager.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "supertux/game_session.hpp"
29 #include "supertux/globals.hpp"
30 #include "supertux/menu/menu_storage.hpp"
31 #include "util/gettext.hpp"
32 #include "util/log.hpp"
33 #include "video/renderer.hpp"
34
35 #ifdef __EMSCRIPTEN__
36 #include <emscripten.h>
37 #include <emscripten/html5.h>
38 #endif
39
40 namespace {
41
less_than_volume(const std::string & lhs,const std::string & rhs)42 bool less_than_volume(const std::string& lhs, const std::string& rhs) {
43 int lhs_i, rhs_i;
44 if (sscanf(lhs.c_str(), "%i", &lhs_i) == 1 &&
45 sscanf(rhs.c_str(), "%i", &rhs_i) == 1)
46 {
47 return lhs_i < rhs_i;
48 }
49
50 return false;
51 }
52
53
54 } // namespace
55
56 enum OptionsMenuIDs {
57 MNID_WINDOW_RESIZABLE,
58 MNID_WINDOW_RESOLUTION,
59 MNID_FULLSCREEN,
60 MNID_FULLSCREEN_RESOLUTION,
61 #ifdef __EMSCRIPTEN__
62 MNID_FIT_WINDOW,
63 #endif
64 MNID_MAGNIFICATION,
65 MNID_ASPECTRATIO,
66 MNID_VSYNC,
67 MNID_SOUND,
68 MNID_MUSIC,
69 MNID_SOUND_VOLUME,
70 MNID_MUSIC_VOLUME,
71 MNID_DEVELOPER_MODE,
72 MNID_CHRISTMAS_MODE,
73 MNID_TRANSITIONS,
74 MNID_CONFIRMATION_DIALOG,
75 MNID_PAUSE_ON_FOCUSLOSS,
76 MNID_CUSTOM_CURSOR
77 #ifdef ENABLE_TOUCHSCREEN_SUPPORT
78 , MNID_MOBILE_CONTROLS
79 #endif
80 };
81
OptionsMenu(bool complete)82 OptionsMenu::OptionsMenu(bool complete) :
83 next_magnification(0),
84 next_aspect_ratio(0),
85 next_window_resolution(0),
86 next_resolution(0),
87 next_vsync(0),
88 next_sound_volume(0),
89 next_music_volume(0),
90 magnifications(),
91 aspect_ratios(),
92 window_resolutions(),
93 resolutions(),
94 vsyncs(),
95 sound_volumes(),
96 music_volumes()
97 {
98 add_label(_("Options"));
99 add_hl();
100
101 magnifications.clear();
102 // These values go from screen:640/projection:1600 to
103 // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
104 magnifications.push_back(_("auto"));
105 #ifndef ENABLE_TOUCHSCREEN_SUPPORT
106 magnifications.push_back("40%");
107 magnifications.push_back("50%");
108 magnifications.push_back("62.5%");
109 magnifications.push_back("80%");
110 #endif
111 magnifications.push_back("100%");
112 magnifications.push_back("125%");
113 magnifications.push_back("160%");
114 magnifications.push_back("200%");
115 magnifications.push_back("250%");
116 // Gets the actual magnification:
117 if (g_config->magnification != 0.0f) //auto
118 {
119 std::ostringstream out;
120 out << (g_config->magnification*100) << "%";
121 std::string magn = out.str();
122 int count = 0;
123 for (const auto& magnification : magnifications)
124 {
125 if (magnification == magn)
126 {
127 next_magnification = count;
128 magn.clear();
129 break;
130 }
131
132 ++count;
133 }
134 if (!magn.empty()) //magnification not in our list but accept anyway
135 {
136 next_magnification = static_cast<int>(magnifications.size());
137 magnifications.push_back(magn);
138 }
139 }
140
141 aspect_ratios.clear();
142 aspect_ratios.push_back(_("auto"));
143 aspect_ratios.push_back("5:4");
144 aspect_ratios.push_back("4:3");
145 aspect_ratios.push_back("16:10");
146 aspect_ratios.push_back("16:9");
147 aspect_ratios.push_back("1368:768");
148 // Gets the actual aspect ratio:
149 if (g_config->aspect_size != Size(0, 0)) //auto
150 {
151 std::ostringstream out;
152 out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
153 std::string aspect_ratio = out.str();
154 int cnt_ = 0;
155 for (const auto& ratio : aspect_ratios)
156 {
157 if (ratio == aspect_ratio)
158 {
159 aspect_ratio.clear();
160 next_aspect_ratio = cnt_;
161 break;
162 }
163 ++cnt_;
164 }
165
166 if (!aspect_ratio.empty())
167 {
168 next_aspect_ratio = static_cast<int>(aspect_ratios.size());
169 aspect_ratios.push_back(aspect_ratio);
170 }
171 }
172
173 {
174 window_resolutions = { "640x480", "854x480", "800x600", "1280x720", "1280x800",
175 "1440x900", "1920x1080", "1920x1200", "2560x1440" };
176 next_window_resolution = -1;
177 Size window_size = VideoSystem::current()->get_window_size();
178 std::ostringstream out;
179 out << window_size.width << "x" << window_size.height;
180 std::string window_size_text = out.str();
181 for (size_t i = 0; i < window_resolutions.size(); ++i)
182 {
183 if (window_resolutions[i] == window_size_text)
184 {
185 next_window_resolution = static_cast<int>(i);
186 break;
187 }
188 }
189 if (next_window_resolution == -1)
190 {
191 window_resolutions.insert(window_resolutions.begin(), window_size_text);
192 next_window_resolution = 0;
193 }
194 }
195
196 resolutions.clear();
197 int display_mode_count = SDL_GetNumDisplayModes(0);
198 std::string last_display_mode;
199 for (int i = 0; i < display_mode_count; ++i)
200 {
201 SDL_DisplayMode mode;
202 int ret = SDL_GetDisplayMode(0, i, &mode);
203 if (ret != 0)
204 {
205 log_warning << "failed to get display mode: " << SDL_GetError() << std::endl;
206 }
207 else
208 {
209 std::ostringstream out;
210 out << mode.w << "x" << mode.h;
211 if (mode.refresh_rate)
212 out << "@" << mode.refresh_rate;
213 if (last_display_mode == out.str())
214 continue;
215 last_display_mode = out.str();
216 resolutions.insert(resolutions.begin(), out.str());
217 }
218 }
219 resolutions.push_back("Desktop");
220
221 std::string fullscreen_size_str = _("Desktop");
222 {
223 std::ostringstream out;
224 if (g_config->fullscreen_size != Size(0, 0))
225 {
226 out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height;
227 if (g_config->fullscreen_refresh_rate)
228 out << "@" << g_config->fullscreen_refresh_rate;
229 fullscreen_size_str = out.str();
230 }
231 }
232
233 int cnt = 0;
234 for (const auto& res : resolutions)
235 {
236 if (res == fullscreen_size_str)
237 {
238 fullscreen_size_str.clear();
239 next_resolution = cnt;
240 break;
241 }
242 ++cnt;
243 }
244 if (!fullscreen_size_str.empty())
245 {
246 next_resolution = static_cast<int>(resolutions.size());
247 resolutions.push_back(fullscreen_size_str);
248 }
249
250 { // vsync
251 vsyncs.push_back(_("on"));
252 vsyncs.push_back(_("off"));
253 vsyncs.push_back(_("adaptive"));
254 int mode = VideoSystem::current()->get_vsync();
255
256 switch (mode)
257 {
258 case -1:
259 next_vsync = 2;
260 break;
261
262 case 0:
263 next_vsync = 1;
264 break;
265
266 case 1:
267 next_vsync = 0;
268 break;
269
270 default:
271 log_warning << "Unknown swap mode: " << mode << std::endl;
272 next_vsync = 0;
273 }
274 }
275
276 // Sound Volume
277 sound_volumes.clear();
278 for (const char* percent : {"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"}) {
279 sound_volumes.push_back(percent);
280 }
281
282 std::ostringstream sound_vol_stream;
283 sound_vol_stream << g_config->sound_volume << "%";
284 std::string sound_vol_string = sound_vol_stream.str();
285
286 if (std::find(sound_volumes.begin(),
287 sound_volumes.end(), sound_vol_string) == sound_volumes.end())
288 {
289 sound_volumes.push_back(sound_vol_string);
290 }
291
292 std::sort(sound_volumes.begin(), sound_volumes.end(), less_than_volume);
293
294 std::ostringstream out;
295 out << g_config->sound_volume << "%";
296 std::string sound_volume = out.str();
297 int cnt_ = 0;
298 for (const auto& volume : sound_volumes)
299 {
300 if (volume == sound_volume)
301 {
302 sound_volume.clear();
303 next_sound_volume = cnt_;
304 break;
305 }
306 ++cnt_;
307 }
308
309 // Music Volume
310 music_volumes.clear();
311 for (const char* percent : {"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"}) {
312 music_volumes.push_back(percent);
313 }
314
315 std::ostringstream music_vol_stream;
316 music_vol_stream << g_config->music_volume << "%";
317 std::string music_vol_string = music_vol_stream.str();
318
319 if (std::find(music_volumes.begin(),
320 music_volumes.end(), music_vol_string) == music_volumes.end())
321 {
322 music_volumes.push_back(music_vol_string);
323 }
324
325 std::sort(music_volumes.begin(), music_volumes.end(), less_than_volume);
326
327 out.str("");
328 out.clear();
329 out << g_config->music_volume << "%";
330 std::string music_volume = out.str();
331 cnt_ = 0;
332 for (const auto& volume : music_volumes)
333 {
334 if (volume == music_volume)
335 {
336 music_volume.clear();
337 next_music_volume = cnt_;
338 break;
339 }
340 ++cnt_;
341 }
342
343 if (complete)
344 {
345 // Language and profile changes are only be possible in the
346 // main menu, since elsewhere it might not always work fully
347 add_submenu(_("Select Language"), MenuStorage::LANGUAGE_MENU)
348 .set_help(_("Select a different language to display text in"));
349
350 add_submenu(_("Language Packs"), MenuStorage::LANGPACK_MENU)
351 .set_help(_("Language packs contain up-to-date translations"));
352
353 add_submenu(_("Select Profile"), MenuStorage::PROFILE_MENU)
354 .set_help(_("Select a profile to play with"));
355 }
356
357 #if !defined(ENABLE_TOUCHSCREEN_SUPPORT) && !defined(__EMSCRIPTEN__)
358 add_toggle(MNID_FULLSCREEN,_("Window Resizable"), &g_config->window_resizable)
359 .set_help(_("Allow window resizing, might require a restart to take effect"));
360
361 MenuItem& window_res = add_string_select(MNID_WINDOW_RESOLUTION, _("Window Resolution"), &next_window_resolution, window_resolutions);
362 window_res.set_help(_("Resize the window to the given size"));
363
364 add_toggle(MNID_FULLSCREEN,_("Fullscreen"), &g_config->use_fullscreen)
365 .set_help(_("Fill the entire screen"));
366
367 MenuItem& fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Fullscreen Resolution"), &next_resolution, resolutions);
368 fullscreen_res.set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
369 #endif
370
371 #if 0
372 #ifdef __EMSCRIPTEN__
373 MenuItem& fit_window = add_toggle(MNID_FIT_WINDOW, _("Fit to browser"), &g_config->fit_window);
374 fit_window.set_help(_("Fit the resolution to the size of your browser"));
375 #endif
376 #endif
377
378 MenuItem& magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"), &next_magnification, magnifications);
379 magnification.set_help(_("Change the magnification of the game area"));
380
381 MenuItem& vsync = add_string_select(MNID_VSYNC, _("VSync"), &next_vsync, vsyncs);
382 vsync.set_help(_("Set the VSync mode"));
383
384 #if !defined(ENABLE_TOUCHSCREEN_SUPPORT) && !defined(__EMSCRIPTEN__)
385 MenuItem& aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"), &next_aspect_ratio, aspect_ratios);
386 aspect.set_help(_("Adjust the aspect ratio"));
387 #endif
388
389 if (SoundManager::current()->is_audio_enabled())
390 {
391 add_toggle(MNID_SOUND, _("Sound"), &g_config->sound_enabled)
392 .set_help(_("Disable all sound effects"));
393 add_toggle(MNID_MUSIC, _("Music"), &g_config->music_enabled)
394 .set_help(_("Disable all music"));
395
396 MenuItem& sound_volume_select = add_string_select(MNID_SOUND_VOLUME, _("Sound Volume"), &next_sound_volume, sound_volumes);
397 sound_volume_select.set_help(_("Adjust sound volume"));
398
399 MenuItem& music_volume_select = add_string_select(MNID_MUSIC_VOLUME, _("Music Volume"), &next_music_volume, music_volumes);
400 music_volume_select.set_help(_("Adjust music volume"));
401 }
402 else
403 {
404 add_inactive( _("Sound (disabled)"));
405 add_inactive( _("Music (disabled)"));
406 }
407
408 add_submenu(_("Setup Keyboard"), MenuStorage::KEYBOARD_MENU)
409 .set_help(_("Configure key-action mappings"));
410
411 #ifndef UBUNTU_TOUCH
412 add_submenu(_("Setup Joystick"), MenuStorage::JOYSTICK_MENU)
413 .set_help(_("Configure joystick control-action mappings"));
414 #endif
415
416 #ifdef ENABLE_TOUCHSCREEN_SUPPORT
417 add_toggle(MNID_MOBILE_CONTROLS, _("On-screen controls"), &g_config->mobile_controls)
418 .set_help(_("Toggle on-screen controls for mobile devices"));
419 #endif
420 MenuItem& enable_transitions = add_toggle(MNID_TRANSITIONS, _("Enable transitions"), &g_config->transitions_enabled);
421 enable_transitions.set_help(_("Enable screen transitions and smooth menu animation"));
422
423 if (g_config->developer_mode)
424 {
425 add_toggle(MNID_DEVELOPER_MODE, _("Developer Mode"), &g_config->developer_mode);
426 }
427
428 if (g_config->is_christmas() || g_config->christmas_mode)
429 {
430 add_toggle(MNID_CHRISTMAS_MODE, _("Christmas Mode"), &g_config->christmas_mode);
431 }
432
433 add_toggle(MNID_CONFIRMATION_DIALOG, _("Confirmation Dialog"), &g_config->confirmation_dialog).set_help(_("Confirm aborting level"));
434 add_toggle(MNID_PAUSE_ON_FOCUSLOSS, _("Pause on focus loss"), &g_config->pause_on_focusloss)
435 .set_help(_("Automatically pause the game when the window loses focus"));
436 add_toggle(MNID_CUSTOM_CURSOR, _("Use custom mouse cursor"), &g_config->custom_mouse_cursor).set_help(_("Whether the game renders its own cursor or uses the system's cursor"));
437
438 add_submenu(_("Integrations and presence"), MenuStorage::INTEGRATIONS_MENU)
439 .set_help(_("Manage whether SuperTux should display the levels you play on your social media profiles (Discord)"));
440
441 add_hl();
442 add_back(_("Back"));
443 }
444
~OptionsMenu()445 OptionsMenu::~OptionsMenu()
446 {
447 }
448
449 void
menu_action(MenuItem & item)450 OptionsMenu::menu_action(MenuItem& item)
451 {
452 switch (item.get_id()) {
453 case MNID_ASPECTRATIO:
454 {
455 if (aspect_ratios[next_aspect_ratio] == _("auto"))
456 {
457 g_config->aspect_size = Size(0, 0); // Magic values
458 VideoSystem::current()->apply_config();
459 MenuManager::instance().on_window_resize();
460 }
461 else if (sscanf(aspect_ratios[next_aspect_ratio].c_str(), "%d:%d",
462 &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
463 {
464 VideoSystem::current()->apply_config();
465 MenuManager::instance().on_window_resize();
466 }
467 else
468 {
469 log_fatal << "Invalid aspect ratio " << aspect_ratios[next_aspect_ratio] << " specified" << std::endl;
470 assert(false);
471 }
472 }
473 break;
474
475 case MNID_MAGNIFICATION:
476 if (magnifications[next_magnification] == _("auto"))
477 {
478 g_config->magnification = 0.0f; // Magic value
479 }
480 else if (sscanf(magnifications[next_magnification].c_str(), "%f", &g_config->magnification) == 1)
481 {
482 g_config->magnification /= 100.0f;
483 }
484 VideoSystem::current()->apply_config();
485 MenuManager::instance().on_window_resize();
486 break;
487
488 case MNID_WINDOW_RESIZABLE:
489 if (!g_config->window_resizable) {
490 next_window_resolution = 0;
491 }
492 break;
493
494 case MNID_WINDOW_RESOLUTION:
495 {
496 int width;
497 int height;
498 if (sscanf(window_resolutions[next_window_resolution].c_str(), "%dx%d",
499 &width, &height) != 2)
500 {
501 log_fatal << "can't parse " << window_resolutions[next_window_resolution] << std::endl;
502 }
503 else
504 {
505 g_config->window_size = Size(width, height);
506 VideoSystem::current()->apply_config();
507 MenuManager::instance().on_window_resize();
508 }
509 }
510 break;
511
512 case MNID_FULLSCREEN_RESOLUTION:
513 {
514 int width;
515 int height;
516 int refresh_rate;
517 if (resolutions[next_resolution] == "Desktop")
518 {
519 g_config->fullscreen_size.width = 0;
520 g_config->fullscreen_size.height = 0;
521 g_config->fullscreen_refresh_rate = 0;
522 }
523 else if (sscanf(resolutions[next_resolution].c_str(), "%dx%d@%d",
524 &width, &height, &refresh_rate) == 3)
525 {
526 // do nothing, changes are only applied when toggling fullscreen mode
527 g_config->fullscreen_size.width = width;
528 g_config->fullscreen_size.height = height;
529 g_config->fullscreen_refresh_rate = refresh_rate;
530 }
531 else if (sscanf(resolutions[next_resolution].c_str(), "%dx%d",
532 &width, &height) == 2)
533 {
534 g_config->fullscreen_size.width = width;
535 g_config->fullscreen_size.height = height;
536 g_config->fullscreen_refresh_rate = 0;
537 }
538 }
539 break;
540
541 #ifdef __EMSCRIPTEN__
542 case MNID_FIT_WINDOW:
543 {
544 // Emscripten's Clang detects the "$" in the macro as part of C++ code
545 // although it isn't even Javascript, it's Emscripten's way to pass
546 // arguments from C++ to Javascript
547 #pragma GCC diagnostic push
548 #pragma GCC diagnostic ignored "-Wdollar-in-identifier-extension"
549 int resultds = EM_ASM_INT({
550 if (window.supertux_setAutofit)
551 window.supertux_setAutofit($0);
552
553 return !!window.supertux_setAutofit;
554 }, g_config->fit_window);
555 #pragma GCC diagnostic pop
556
557 if (!resultds)
558 {
559 Dialog::show_message(_("The game couldn't detect your browser resolution.\n"
560 "This most likely happens because it is not embedded\n"
561 "in the SuperTux custom HTML template.\n"));
562 }
563 }
564 break;
565 #endif
566
567 case MNID_VSYNC:
568 switch (next_vsync)
569 {
570 case 2:
571 VideoSystem::current()->set_vsync(-1);
572 break;
573
574 case 1:
575 VideoSystem::current()->set_vsync(0);
576 break;
577
578 case 0:
579 VideoSystem::current()->set_vsync(1);
580 break;
581
582 default:
583 assert(false);
584 break;
585 }
586 break;
587
588 case MNID_FULLSCREEN:
589 VideoSystem::current()->apply_config();
590 MenuManager::instance().on_window_resize();
591 g_config->save();
592 break;
593
594 case MNID_SOUND:
595 SoundManager::current()->enable_sound(g_config->sound_enabled);
596 g_config->save();
597 break;
598
599 case MNID_SOUND_VOLUME:
600 if (sscanf(sound_volumes[next_sound_volume].c_str(), "%i", &g_config->sound_volume) == 1)
601 {
602 bool sound_enabled = g_config->sound_volume > 0 ? true : false;
603 SoundManager::current()->enable_sound(sound_enabled);
604 SoundManager::current()->set_sound_volume(g_config->sound_volume);
605 g_config->save();
606 }
607 break;
608
609 case MNID_MUSIC:
610 SoundManager::current()->enable_music(g_config->music_enabled);
611 g_config->save();
612 break;
613
614 case MNID_MUSIC_VOLUME:
615 if (sscanf(music_volumes[next_music_volume].c_str(), "%i", &g_config->music_volume) == 1)
616 {
617 bool music_enabled = g_config->music_volume > 0 ? true : false;
618 SoundManager::current()->enable_music(music_enabled);
619 SoundManager::current()->set_music_volume(g_config->music_volume);
620 g_config->save();
621 }
622 break;
623
624 case MNID_CUSTOM_CURSOR:
625 SDL_ShowCursor(g_config->custom_mouse_cursor ? 0 : 1);
626 break;
627
628 default:
629 break;
630 }
631 }
632
633 /* EOF */
634