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