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