Removed unimplemented "Profiles on Startup"
[supertux.git] / src / supertux / menu / options_menu.cpp
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/menu_manager.hpp"
22 #include "supertux/gameconfig.hpp"
23 #include "supertux/menu/joystick_menu.hpp"
24 #include "supertux/menu/keyboard_menu.hpp"
25 #include "supertux/menu/language_menu.hpp"
26 #include "supertux/menu/menu_storage.hpp"
27 #include "supertux/menu/profile_menu.hpp"
28 #include "util/string_util.hpp"
29 #include "video/renderer.hpp"
30
31 #include <algorithm>
32 #include <sstream>
33 #include <stdio.h>
34
35 enum OptionsMenuIDs {
36   MNID_FULLSCREEN,
37   MNID_FULLSCREEN_RESOLUTION,
38   MNID_MAGNIFICATION,
39   MNID_ASPECTRATIO,
40   MNID_SOUND,
41   MNID_MUSIC
42 };
43
44 OptionsMenu::OptionsMenu(bool complete)
45 {
46   add_label(_("Options"));
47   add_hl();
48
49   if (complete)
50   {
51     // Language and profile changes are only be possible in the
52     // main menu, since elsewhere it might not always work fully
53     add_submenu(_("Select Language"), MenuStorage::LANGUAGE_MENU)
54       ->set_help(_("Select a different language to display text in"));
55
56     add_submenu(_("Select Profile"), MenuStorage::PROFILE_MENU)
57       ->set_help(_("Select a profile to play with"));
58   }
59
60   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
61     ->set_help(_("Fill the entire screen"));
62
63   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
64   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
65
66   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
67   magnification->set_help(_("Change the magnification of the game area"));
68
69   // These values go from screen:640/projection:1600 to
70   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
71   magnification->list.push_back(_("auto"));
72   magnification->list.push_back("40%");
73   magnification->list.push_back("50%");
74   magnification->list.push_back("62.5%");
75   magnification->list.push_back("80%");
76   magnification->list.push_back("100%");
77   magnification->list.push_back("125%");
78   magnification->list.push_back("160%");
79   magnification->list.push_back("200%");
80   magnification->list.push_back("250%");
81   if (g_config->magnification != 0.0f) //auto
82   {
83     std::ostringstream out;
84     out << (g_config->magnification*100) << "%";
85     std::string magn = out.str();
86     size_t count = 0;
87     for (std::vector<std::string>::iterator i = magnification->list.begin(); i != magnification->list.end(); ++i)
88     {
89       if (*i == magn)
90       {
91         magnification->selected = count;
92         magn.clear();
93         break;
94       }
95
96       ++count;
97     }
98     if (!magn.empty()) //magnification not in our list but accept anyway
99     {
100       magnification->selected = magnification->list.size();
101       magnification->list.push_back(magn);
102     }
103   }
104
105   int display_mode_count = SDL_GetNumDisplayModes(0);
106   for(int i = 0; i < display_mode_count; ++i)
107   {
108     SDL_DisplayMode mode;
109     int ret = SDL_GetDisplayMode(0, i, &mode);
110     if (ret != 0)
111     {
112       log_warning << "failed to get display mode: " << SDL_GetError() << std::endl;
113     }
114     else
115     {
116       std::ostringstream out;
117       out << mode.w << "x" << mode.h << "@" << mode.refresh_rate;
118       fullscreen_res->list.push_back(out.str());
119     }
120   }
121   fullscreen_res->list.push_back("Desktop");
122
123   std::ostringstream out;
124   std::string fullscreen_size_str = "Desktop";
125   if (g_config->fullscreen_size != Size(0, 0))
126   {
127     out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height << "@" << g_config->fullscreen_refresh_rate;
128     fullscreen_size_str = out.str();
129   }
130   size_t cnt = 0;
131   for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i)
132   {
133     if (*i == fullscreen_size_str)
134     {
135       fullscreen_size_str.clear();
136       fullscreen_res->selected = cnt;
137       break;
138     }
139     ++cnt;
140   }
141   if (!fullscreen_size_str.empty())
142   {
143     fullscreen_res->selected = fullscreen_res->list.size();
144     fullscreen_res->list.push_back(fullscreen_size_str);
145   }
146
147   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
148   aspect->set_help(_("Adjust the aspect ratio"));
149
150   aspect->list.push_back(_("auto"));
151   aspect->list.push_back("5:4");
152   aspect->list.push_back("4:3");
153   aspect->list.push_back("16:10");
154   aspect->list.push_back("16:9");
155   aspect->list.push_back("1368:768");
156
157   if (g_config->aspect_size != Size(0, 0))
158   {
159     std::ostringstream out;
160     out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
161     std::string aspect_ratio = out.str();
162     size_t cnt = 0;
163     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
164     {
165       if(*i == aspect_ratio)
166       {
167         aspect_ratio.clear();
168         aspect->selected = cnt;
169         break;
170       }
171       ++cnt;
172     }
173
174     if (!aspect_ratio.empty())
175     {
176       aspect->selected = aspect->list.size();
177       aspect->list.push_back(aspect_ratio);
178     }
179   }
180
181   if (sound_manager->is_audio_enabled()) {
182     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
183       ->set_help(_("Disable all sound effects"));
184     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
185       ->set_help(_("Disable all music"));
186   } else {
187     add_inactive(MNID_SOUND, _("Sound (disabled)"));
188     add_inactive(MNID_MUSIC, _("Music (disabled)"));
189   }
190
191   add_submenu(_("Setup Keyboard"), MenuStorage::KEYBOARD_MENU)
192     ->set_help(_("Configure key-action mappings"));
193
194   add_submenu(_("Setup Joystick"), MenuStorage::JOYSTICK_MENU)
195     ->set_help(_("Configure joystick control-action mappings"));
196   add_hl();
197   add_back(_("Back"));
198 }
199
200 OptionsMenu::~OptionsMenu()
201 {
202 }
203
204 void
205 OptionsMenu::menu_action(MenuItem* item)
206 {
207   switch (item->id) {
208     case MNID_ASPECTRATIO:
209       {
210         if (item->list[item->selected] == _("auto"))
211         {
212           g_config->aspect_size = Size(0, 0); // Magic values
213           Renderer::instance()->apply_config();
214           MenuManager::instance().on_window_resize();
215         }
216         else if (sscanf(item->list[item->selected].c_str(), "%d:%d",
217                         &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
218         {
219           Renderer::instance()->apply_config();
220           MenuManager::instance().on_window_resize();
221         }
222         else
223         {
224           assert(!"This must not be reached");
225         }
226       }
227       break;
228
229     case MNID_MAGNIFICATION:
230       if (item->list[item->selected] == _("auto"))
231       {
232         g_config->magnification = 0.0f; // Magic value
233       }
234       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
235       {
236         g_config->magnification /= 100.0f;
237       }
238       Renderer::instance()->apply_config();
239       MenuManager::instance().on_window_resize();
240       break;
241
242     case MNID_FULLSCREEN_RESOLUTION:
243       {
244         int width;
245         int height;
246         int refresh_rate;
247         if (item->list[item->selected] == "Desktop")
248         {
249           g_config->fullscreen_size.width = 0;
250           g_config->fullscreen_size.height = 0;
251           g_config->fullscreen_refresh_rate = 0;
252         }
253         else if(sscanf(item->list[item->selected].c_str(), "%dx%d@%d",
254                   &width, &height, &refresh_rate) == 3)
255         {
256           // do nothing, changes are only applied when toggling fullscreen mode
257           g_config->fullscreen_size.width = width;
258           g_config->fullscreen_size.height = height;
259           g_config->fullscreen_refresh_rate = refresh_rate;
260         }
261       }
262       break;
263
264     case MNID_FULLSCREEN:
265       if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
266         g_config->use_fullscreen = !g_config->use_fullscreen;
267         Renderer::instance()->apply_config();
268         MenuManager::instance().on_window_resize();
269         g_config->save();
270       }
271       break;
272
273     case MNID_SOUND:
274       if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
275         g_config->sound_enabled = !g_config->sound_enabled;
276         sound_manager->enable_sound(g_config->sound_enabled);
277         g_config->save();
278       }
279       break;
280
281     case MNID_MUSIC:
282       if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
283         g_config->music_enabled = !g_config->music_enabled;
284         sound_manager->enable_music(g_config->music_enabled);
285         g_config->save();
286       }
287       break;
288
289     default:
290       break;
291   }
292 }
293
294 void
295 OptionsMenu::check_menu()
296 {
297 }
298
299 /* EOF */