More -Weffc++ cleanup
[supertux.git] / src / supertux / 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 "audio/sound_manager.hpp"
19 #include "control/joystickkeyboardcontroller.hpp"
20 #include "gui/menu.hpp"
21 #include "supertux/gameconfig.hpp"
22 #include "supertux/main.hpp"
23 #include "supertux/profile_menu.hpp"
24 #include "util/gettext.hpp"
25 #include "video/renderer.hpp"
26
27 Menu* options_menu   = 0;
28
29 enum OptionsMenuIDs {
30   MNID_FULLSCREEN,
31   MNID_FULLSCREEN_RESOLUTION,
32   MNID_MAGNIFICATION,
33   MNID_ASPECTRATIO,
34   MNID_PROFILES,
35   MNID_SOUND,
36   MNID_MUSIC
37 };
38
39 class LanguageMenu : public Menu
40 {
41 public:
42   LanguageMenu() {
43     add_label(_("Language"));
44     add_hl();
45     add_entry(0, std::string("<")+_("auto-detect")+">");
46     add_entry(1, "English");
47
48     int mnid = 10;    
49     std::set<std::string> languages = dictionary_manager.get_languages();
50     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
51       std::string locale_name = *i;
52       TinyGetText::LanguageDef ldef = TinyGetText::get_language_def(locale_name);
53       std::string locale_fullname = locale_name;
54       if (std::string(ldef.code) == locale_name) {
55         locale_fullname = ldef.name;
56       }
57       add_entry(mnid++, locale_fullname);
58     } 
59
60     add_hl();
61     add_back(_("Back"));
62   }
63
64   virtual void menu_action(MenuItem* item) {
65     if (item->id == 0) {
66       g_config->locale = "";
67       dictionary_manager.set_language(g_config->locale);
68       g_config->save();
69       Menu::pop_current();
70     }
71     else if (item->id == 1) {
72       g_config->locale = "en";
73       dictionary_manager.set_language(g_config->locale);
74       g_config->save();
75       Menu::pop_current();
76     }
77     int mnid = 10;    
78     std::set<std::string> languages = dictionary_manager.get_languages();
79     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
80       std::string locale_name = *i;
81       if (item->id == mnid++) {
82         g_config->locale = locale_name;
83         dictionary_manager.set_language(g_config->locale);
84         g_config->save();
85         Menu::pop_current();
86       }
87     }
88   }
89 };
90
91 class OptionsMenu : public Menu
92 {
93 public:
94   OptionsMenu();
95   virtual ~OptionsMenu();
96
97   virtual void menu_action(MenuItem* item);
98
99 protected:
100   std::auto_ptr<LanguageMenu> language_menu;
101   
102 };
103
104 OptionsMenu::OptionsMenu()
105 {
106   language_menu.reset(new LanguageMenu());
107
108   add_label(_("Options"));
109   add_hl();
110
111   // Language change should only be possible in the main menu, since elsewhere it might not always work fully
112   // FIXME: Implement me: if (get_parent() == main_menu)
113   add_submenu(_("Select Language"), language_menu.get())
114     ->set_help(_("Select a different language to display text in"));
115
116   add_submenu(_("Select Profile"), get_profile_menu())
117     ->set_help(_("Select a profile to play with"));
118
119   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
120     ->set_help(_("Select your profile immediately after start-up"));
121   
122   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
123     ->set_help(_("Fill the entire screen"));
124
125   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
126   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
127
128   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
129   magnification->set_help(_("Change the magnification of the game area"));
130
131   // These values go from screen:640/projection:1600 to
132   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
133   magnification->list.push_back("auto");
134   magnification->list.push_back("40%");
135   magnification->list.push_back("50%");
136   magnification->list.push_back("62.5%");
137   magnification->list.push_back("80%");
138   magnification->list.push_back("100%");
139   magnification->list.push_back("125%");
140   magnification->list.push_back("160%");
141   magnification->list.push_back("200%");
142   magnification->list.push_back("250%");
143
144   SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL);
145
146   if (modes == (SDL_Rect **)0) 
147   { // No resolutions at all available, bad
148
149   }
150   else if(modes == (SDL_Rect **)-1) 
151   { // All resolutions should work, so we fall back to hardcoded defaults
152     fullscreen_res->list.push_back("640x480");
153     fullscreen_res->list.push_back("800x600");
154     fullscreen_res->list.push_back("1024x768");
155     fullscreen_res->list.push_back("1152x864");
156     fullscreen_res->list.push_back("1280x960");
157     fullscreen_res->list.push_back("1280x1024");
158     fullscreen_res->list.push_back("1440x900");
159     fullscreen_res->list.push_back("1680x1050");
160     fullscreen_res->list.push_back("1600x1200");
161     fullscreen_res->list.push_back("1920x1080");
162     fullscreen_res->list.push_back("1920x1200");
163   }
164   else 
165   {
166     for(int i = 0; modes[i]; ++i)
167     {
168       std::ostringstream out;          
169       out << modes[i]->w << "x" << modes[i]->h;
170       fullscreen_res->list.push_back(out.str());
171     }
172   }
173
174   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
175   aspect->set_help(_("Adjust the aspect ratio"));
176   
177   aspect->list.push_back("auto");
178   aspect->list.push_back("5:4");
179   aspect->list.push_back("4:3");
180   aspect->list.push_back("16:10");
181   aspect->list.push_back("16:9");
182   aspect->list.push_back("1368:768");
183
184   if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
185   {
186     std::ostringstream out;
187     out << g_config->aspect_width << ":" << g_config->aspect_height;
188     std::string aspect_ratio = out.str();
189     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
190     {
191       if(*i == aspect_ratio)
192       {
193         aspect_ratio.clear();
194         break;
195       }
196     }
197
198     if (!aspect_ratio.empty())
199     {
200       aspect->selected = aspect->list.size();
201       aspect->list.push_back(aspect_ratio);
202     }
203   }
204   
205   if (sound_manager->is_audio_enabled()) {
206     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
207       ->set_help(_("Disable all sound effects"));
208     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
209       ->set_help(_("Disable all music"));
210   } else {
211     add_inactive(MNID_SOUND, _("Sound (disabled)"));
212     add_inactive(MNID_MUSIC, _("Music (disabled)"));
213   }
214   
215   add_submenu(_("Setup Keyboard"), g_main_controller->get_key_options_menu())
216     ->set_help(_("Configure key-action mappings"));
217
218   add_submenu(_("Setup Joystick") ,g_main_controller->get_joystick_options_menu())
219     ->set_help(_("Configure joystick control-action mappings"));
220   add_hl();
221   add_back(_("Back"));
222 }
223
224 OptionsMenu::~OptionsMenu()
225 {
226 }
227
228 void
229 OptionsMenu::menu_action(MenuItem* item)
230 {
231   switch (item->id) {
232     case MNID_ASPECTRATIO:
233     { 
234       if (item->list[item->selected] == "auto")
235       {
236         g_config->aspect_width  = 0; // Magic values
237         g_config->aspect_height = 0;
238         Renderer::instance()->apply_config();
239         Menu::recalc_pos();
240       }
241       else if(sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_width, &g_config->aspect_height) == 2)
242       {
243         Renderer::instance()->apply_config();
244         Menu::recalc_pos();
245       }
246       else
247       {
248         assert(!"This must not be reached");
249       }
250     }
251     break;
252
253     case MNID_MAGNIFICATION:
254       if (item->list[item->selected] == "auto")
255       {
256         g_config->magnification = 0.0f; // Magic value 
257       }
258       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
259       {
260         g_config->magnification /= 100.0f;
261       }
262       Renderer::instance()->apply_config();
263       Menu::recalc_pos();
264       break;
265
266     case MNID_FULLSCREEN_RESOLUTION:
267       if(sscanf(item->list[item->selected].c_str(), "%dx%d", &g_config->fullscreen_width, &g_config->fullscreen_height) == 2)
268       {
269         // do nothing, changes are only applied when toggling fullscreen mode
270       }      
271       break;
272
273     case MNID_FULLSCREEN:
274       if(g_config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
275         g_config->use_fullscreen = !g_config->use_fullscreen;
276         init_video(); // FIXME: Should call apply_config instead
277         Menu::recalc_pos();
278         g_config->save();
279       }
280       break;
281
282     case MNID_SOUND:
283       if(g_config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
284         g_config->sound_enabled = !g_config->sound_enabled;
285         sound_manager->enable_sound(g_config->sound_enabled);
286         g_config->save();
287       }
288       break;
289
290     case MNID_MUSIC:
291       if(g_config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
292         g_config->music_enabled = !g_config->music_enabled;
293         sound_manager->enable_music(g_config->music_enabled);
294         g_config->save();
295       }
296       break;
297
298     default:
299       break;
300   }
301 }
302
303 Menu* get_options_menu()
304 {
305   //static OptionsMenu menu;
306   options_menu = new OptionsMenu();
307   return options_menu;
308 }
309
310 void free_options_menu()
311 {
312   delete options_menu;
313   options_menu = 0;
314 }
315
316 /* EOF */