355102baadfbada0c4f208fe2d0451c29a1b6230
[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   language_menu()
106 {
107   language_menu.reset(new LanguageMenu());
108
109   add_label(_("Options"));
110   add_hl();
111
112   // Language change should only be possible in the main menu, since elsewhere it might not always work fully
113   // FIXME: Implement me: if (get_parent() == main_menu)
114   add_submenu(_("Select Language"), language_menu.get())
115     ->set_help(_("Select a different language to display text in"));
116
117   add_submenu(_("Select Profile"), get_profile_menu())
118     ->set_help(_("Select a profile to play with"));
119
120   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
121     ->set_help(_("Select your profile immediately after start-up"));
122   
123   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
124     ->set_help(_("Fill the entire screen"));
125
126   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
127   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
128
129   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
130   magnification->set_help(_("Change the magnification of the game area"));
131
132   // These values go from screen:640/projection:1600 to
133   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
134   magnification->list.push_back("auto");
135   magnification->list.push_back("40%");
136   magnification->list.push_back("50%");
137   magnification->list.push_back("62.5%");
138   magnification->list.push_back("80%");
139   magnification->list.push_back("100%");
140   magnification->list.push_back("125%");
141   magnification->list.push_back("160%");
142   magnification->list.push_back("200%");
143   magnification->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 should 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("auto");
179   aspect->list.push_back("5:4");
180   aspect->list.push_back("4:3");
181   aspect->list.push_back("16:10");
182   aspect->list.push_back("16:9");
183   aspect->list.push_back("1368:768");
184
185   if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
186   {
187     std::ostringstream out;
188     out << g_config->aspect_width << ":" << g_config->aspect_height;
189     std::string aspect_ratio = out.str();
190     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
191     {
192       if(*i == aspect_ratio)
193       {
194         aspect_ratio.clear();
195         break;
196       }
197     }
198
199     if (!aspect_ratio.empty())
200     {
201       aspect->selected = aspect->list.size();
202       aspect->list.push_back(aspect_ratio);
203     }
204   }
205   
206   if (sound_manager->is_audio_enabled()) {
207     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
208       ->set_help(_("Disable all sound effects"));
209     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
210       ->set_help(_("Disable all music"));
211   } else {
212     add_inactive(MNID_SOUND, _("Sound (disabled)"));
213     add_inactive(MNID_MUSIC, _("Music (disabled)"));
214   }
215   
216   add_submenu(_("Setup Keyboard"), g_main_controller->get_key_options_menu())
217     ->set_help(_("Configure key-action mappings"));
218
219   add_submenu(_("Setup Joystick") ,g_main_controller->get_joystick_options_menu())
220     ->set_help(_("Configure joystick control-action mappings"));
221   add_hl();
222   add_back(_("Back"));
223 }
224
225 OptionsMenu::~OptionsMenu()
226 {
227 }
228
229 void
230 OptionsMenu::menu_action(MenuItem* item)
231 {
232   switch (item->id) {
233     case MNID_ASPECTRATIO:
234     { 
235       if (item->list[item->selected] == "auto")
236       {
237         g_config->aspect_width  = 0; // Magic values
238         g_config->aspect_height = 0;
239         Renderer::instance()->apply_config();
240         Menu::recalc_pos();
241       }
242       else if(sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_width, &g_config->aspect_height) == 2)
243       {
244         Renderer::instance()->apply_config();
245         Menu::recalc_pos();
246       }
247       else
248       {
249         assert(!"This must not be reached");
250       }
251     }
252     break;
253
254     case MNID_MAGNIFICATION:
255       if (item->list[item->selected] == "auto")
256       {
257         g_config->magnification = 0.0f; // Magic value 
258       }
259       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
260       {
261         g_config->magnification /= 100.0f;
262       }
263       Renderer::instance()->apply_config();
264       Menu::recalc_pos();
265       break;
266
267     case MNID_FULLSCREEN_RESOLUTION:
268       if(sscanf(item->list[item->selected].c_str(), "%dx%d", &g_config->fullscreen_width, &g_config->fullscreen_height) == 2)
269       {
270         // do nothing, changes are only applied when toggling fullscreen mode
271       }      
272       break;
273
274     case MNID_FULLSCREEN:
275       if(g_config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
276         g_config->use_fullscreen = !g_config->use_fullscreen;
277         init_video(); // FIXME: Should call apply_config instead
278         Menu::recalc_pos();
279         g_config->save();
280       }
281       break;
282
283     case MNID_SOUND:
284       if(g_config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
285         g_config->sound_enabled = !g_config->sound_enabled;
286         sound_manager->enable_sound(g_config->sound_enabled);
287         g_config->save();
288       }
289       break;
290
291     case MNID_MUSIC:
292       if(g_config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
293         g_config->music_enabled = !g_config->music_enabled;
294         sound_manager->enable_music(g_config->music_enabled);
295         g_config->save();
296       }
297       break;
298
299     default:
300       break;
301   }
302 }
303
304 Menu* get_options_menu()
305 {
306   //static OptionsMenu menu;
307   options_menu = new OptionsMenu();
308   return options_menu;
309 }
310
311 void free_options_menu()
312 {
313   delete options_menu;
314   options_menu = 0;
315 }
316
317 /* EOF */