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