Fixed a few missshapes in sound enable/disable in the option menu
[supertux.git] / src / options_menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #include <config.h>
21
22 #include "profile_menu.hpp"
23 #include "options_menu.hpp"
24 #include "gui/menu.hpp"
25 #include "audio/sound_manager.hpp"
26 #include "control/joystickkeyboardcontroller.hpp"
27 #include "main.hpp"
28 #include "gettext.hpp"
29 #include "video/renderer.hpp"
30 #include "gameconfig.hpp"
31
32 Menu* options_menu   = 0;
33
34 enum OptionsMenuIDs {
35   MNID_FULLSCREEN,
36   MNID_FULLSCREEN_RESOLUTION,
37   MNID_MAGINFICATION,
38   MNID_ASPECTRATIO,
39   MNID_PROFILES,
40   MNID_SOUND,
41   MNID_MUSIC
42 };
43
44 class LanguageMenu : public Menu
45 {
46 public:
47   LanguageMenu() {
48     add_label(_("Language"));
49     add_hl();
50     add_entry(0, std::string("(")+_("auto-detect language")+")");
51     add_entry(1, "English");
52
53     int mnid = 10;    
54     std::set<std::string> languages = dictionary_manager.get_languages();
55     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
56       std::string locale_name = *i;
57       TinyGetText::LanguageDef ldef = TinyGetText::get_language_def(locale_name);
58       std::string locale_fullname = locale_name;
59       if (std::string(ldef.code) == locale_name) {
60         locale_fullname = ldef.name;
61       }
62       add_entry(mnid++, locale_fullname);
63     } 
64
65     add_hl();
66     add_back(_("Back"));
67   }
68
69   virtual void menu_action(MenuItem* item) {
70     if (item->id == 0) {
71       config->locale = "";
72       dictionary_manager.set_language(config->locale);
73       config->save();
74       Menu::set_current(0);
75     }
76     else if (item->id == 1) {
77       config->locale = "en";
78       dictionary_manager.set_language(config->locale);
79       config->save();
80       Menu::set_current(0);
81     }
82     int mnid = 10;    
83     std::set<std::string> languages = dictionary_manager.get_languages();
84     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
85       std::string locale_name = *i;
86       if (item->id == mnid++) {
87         config->locale = locale_name;
88         dictionary_manager.set_language(config->locale);
89         config->save();
90         Menu::set_current(0);
91       }
92     }
93   }
94 };
95
96
97 class OptionsMenu : public Menu
98 {
99 public:
100   OptionsMenu();
101   virtual ~OptionsMenu();
102
103   virtual void menu_action(MenuItem* item);
104
105 protected:
106   std::auto_ptr<LanguageMenu> language_menu;
107   
108 };
109
110 OptionsMenu::OptionsMenu()
111 {
112   language_menu.reset(new LanguageMenu());
113
114   add_label(_("Options"));
115   add_hl();
116
117   // Language change should only be possible in the main menu, since elsewhere it might not always full work
118   // FIXME: Implement me: if (get_parent() == main_menu)
119   add_submenu(_("Select Language"), language_menu.get())
120     ->set_help(_("Switch to another language"));
121
122   add_submenu(_("Select Profile"), get_profile_menu())
123     ->set_help(_("Switch between different savegames"));
124
125   add_toggle(MNID_PROFILES, _("Profile on Startup"), config->sound_enabled)
126     ->set_help(_("Display the profile menu when the game is newly started"));
127   
128   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen)
129     ->set_help(_("Let the game cover the whole screen"));
130
131   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
132   fullscreen_res->set_help(_("Change the Resolution to be used in Fullscreen Mode, you have to toggle fullscreen mode to let this change take effect"));
133
134   MenuItem* maginfication = add_string_select(MNID_MAGINFICATION, _("Maginfication"));
135   maginfication->set_help(_("Change the magnification of the game area"));
136
137   // These values go from screen:640/projection:1600 to screen:1600/projection:640
138   maginfication->list.push_back("40%");
139   maginfication->list.push_back("50%");
140   maginfication->list.push_back("62.5%");
141   maginfication->list.push_back("80%");
142   maginfication->list.push_back("100%");
143   maginfication->list.push_back("125%");
144   maginfication->list.push_back("160%");
145   maginfication->list.push_back("200%");
146   maginfication->list.push_back("250%");
147
148   SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL);
149
150   if (modes == (SDL_Rect **)0) 
151     { // No resolutions at all available, bad
152
153     }
154   else if(modes == (SDL_Rect **)-1) 
155     { // All resolutions sould work, so we fall back to hardcoded defaults
156       fullscreen_res->list.push_back("640x480");
157       fullscreen_res->list.push_back("800x600");
158       fullscreen_res->list.push_back("1024x768");
159       fullscreen_res->list.push_back("1152x864");
160       fullscreen_res->list.push_back("1280x960");
161       fullscreen_res->list.push_back("1280x1024");
162       fullscreen_res->list.push_back("1440x900");
163       fullscreen_res->list.push_back("1680x1050");
164       fullscreen_res->list.push_back("1600x1200");
165       fullscreen_res->list.push_back("1920x1080");
166       fullscreen_res->list.push_back("1920x1200");
167     }
168   else 
169     {
170       for(int i = 0; modes[i]; ++i)
171         {
172           std::ostringstream out;          
173           out << modes[i]->w << "x" << modes[i]->h;
174           fullscreen_res->list.push_back(out.str());
175         }
176     }
177
178   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
179   aspect->set_help(_("Adjust the aspect ratio"));
180   
181   aspect->list.push_back("4:3");
182   aspect->list.push_back("5:4");
183   aspect->list.push_back("16:10");
184   aspect->list.push_back("16:9");
185
186   std::ostringstream out;
187   out << config->aspect_width << ":" << 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   if (sound_manager->is_audio_enabled()) {
205     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled)
206       ->set_help(_("Disable all sound effects in the game"));
207     add_toggle(MNID_MUSIC, _("Music"), config->music_enabled)
208       ->set_help(_("Disable all music in the game"));
209   } else {
210     add_deactive(MNID_SOUND, _("Sound (disabled)"));
211     add_deactive(MNID_MUSIC, _("Music (disabled)"));
212   }
213   
214   add_submenu(_("Setup Keyboard"), main_controller->get_key_options_menu())
215     ->set_help(_("Configure how your keyboard maps to the game"));
216
217   add_submenu(_("Setup Joystick"),main_controller->get_joystick_options_menu())
218     ->set_help(_("Configure how your joystick maps to the game"));
219   add_hl();
220   add_back(_("Back"));
221 }
222
223 OptionsMenu::~OptionsMenu()
224 {
225 }
226
227 void
228 OptionsMenu::menu_action(MenuItem* item)
229 {
230   switch (item->id) {
231     case MNID_ASPECTRATIO:
232       { 
233         if(sscanf(item->list[item->selected].c_str(), "%d:%d", &config->aspect_width, &config->aspect_height) == 2)
234           {
235             Renderer::instance()->apply_config();
236             Menu::recalc_pos();
237           }
238       }
239       break;
240
241     case MNID_MAGINFICATION:
242       if(sscanf(item->list[item->selected].c_str(), "%f", &config->magnification) == 1)
243         {
244           config->magnification /= 100.0f;
245           Renderer::instance()->apply_config();
246           Menu::recalc_pos();
247         }
248       break;
249
250     case MNID_FULLSCREEN_RESOLUTION:
251       if(sscanf(item->list[item->selected].c_str(), "%dx%d", &config->fullscreen_width, &config->fullscreen_height) == 2)
252         {
253           // do nothing, changes are only applied when toggling fullscreen mode
254         }      
255       break;
256
257     case MNID_FULLSCREEN:
258       if(config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
259         config->use_fullscreen = !config->use_fullscreen;
260         init_video();
261         config->save();
262       }
263       break;
264
265     case MNID_SOUND:
266       if(config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
267         config->sound_enabled = !config->sound_enabled;
268         sound_manager->enable_sound(config->sound_enabled);
269         config->save();
270       }
271       break;
272
273     case MNID_MUSIC:
274       if(config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
275         config->music_enabled = !config->music_enabled;
276         sound_manager->enable_music(config->music_enabled);
277         config->save();
278       }
279       break;
280
281     default:
282       break;
283   }
284 }
285
286 Menu* get_options_menu()
287 {
288   //static OptionsMenu menu;
289   options_menu = new OptionsMenu();
290   return options_menu;
291 }
292
293 void free_options_menu()
294 {
295   delete options_menu;
296   options_menu = 0;
297 }