Moved some more menu stuff out of control/joystickkeyboardcontroller.cpp
[supertux.git] / src / supertux / menu / 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/menu/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 "gui/menu_manager.hpp"
25 #include "supertux/gameconfig.hpp"
26 #include "supertux/globals.hpp"
27 #include "supertux/main.hpp"
28 #include "supertux/menu/joystick_menu.hpp"
29 #include "supertux/menu/keyboard_menu.hpp"
30 #include "supertux/menu/language_menu.hpp"
31 #include "supertux/menu/menu_storage.hpp"
32 #include "supertux/menu/profile_menu.hpp"
33 #include "util/gettext.hpp"
34 #include "video/renderer.hpp"
35
36 enum OptionsMenuIDs {
37   MNID_FULLSCREEN,
38   MNID_FULLSCREEN_RESOLUTION,
39   MNID_MAGNIFICATION,
40   MNID_ASPECTRATIO,
41   MNID_PROFILES,
42   MNID_SOUND,
43   MNID_MUSIC
44 };
45
46 OptionsMenu::OptionsMenu() :
47   language_menu()
48 {
49   language_menu.reset(new LanguageMenu());
50
51   add_label(_("Options"));
52   add_hl();
53
54   // Language change should only be possible in the main menu, since elsewhere it might not always work fully
55   // FIXME: Implement me: if (get_parent() == main_menu)
56   add_submenu(_("Select Language"), language_menu.get())
57     ->set_help(_("Select a different language to display text in"));
58
59   add_submenu(_("Select Profile"), MenuStorage::get_profile_menu())
60     ->set_help(_("Select a profile to play with"));
61
62   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
63     ->set_help(_("Select your profile immediately after start-up"));
64   
65   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
66     ->set_help(_("Fill the entire screen"));
67
68   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
69   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
70
71   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
72   magnification->set_help(_("Change the magnification of the game area"));
73
74   // These values go from screen:640/projection:1600 to
75   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
76   magnification->list.push_back("auto");
77   magnification->list.push_back("40%");
78   magnification->list.push_back("50%");
79   magnification->list.push_back("62.5%");
80   magnification->list.push_back("80%");
81   magnification->list.push_back("100%");
82   magnification->list.push_back("125%");
83   magnification->list.push_back("160%");
84   magnification->list.push_back("200%");
85   magnification->list.push_back("250%");
86
87   SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL);
88
89   if (modes == (SDL_Rect **)0) 
90   { // No resolutions at all available, bad
91
92   }
93   else if(modes == (SDL_Rect **)-1) 
94   { // All resolutions should work, so we fall back to hardcoded defaults
95     fullscreen_res->list.push_back("640x480");
96     fullscreen_res->list.push_back("800x600");
97     fullscreen_res->list.push_back("1024x768");
98     fullscreen_res->list.push_back("1152x864");
99     fullscreen_res->list.push_back("1280x960");
100     fullscreen_res->list.push_back("1280x1024");
101     fullscreen_res->list.push_back("1440x900");
102     fullscreen_res->list.push_back("1680x1050");
103     fullscreen_res->list.push_back("1600x1200");
104     fullscreen_res->list.push_back("1920x1080");
105     fullscreen_res->list.push_back("1920x1200");
106   }
107   else 
108   {
109     for(int i = 0; modes[i]; ++i)
110     {
111       std::ostringstream out;          
112       out << modes[i]->w << "x" << modes[i]->h;
113       fullscreen_res->list.push_back(out.str());
114     }
115   }
116
117   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
118   aspect->set_help(_("Adjust the aspect ratio"));
119   
120   aspect->list.push_back("auto");
121   aspect->list.push_back("5:4");
122   aspect->list.push_back("4:3");
123   aspect->list.push_back("16:10");
124   aspect->list.push_back("16:9");
125   aspect->list.push_back("1368:768");
126
127   if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
128   {
129     std::ostringstream out;
130     out << g_config->aspect_width << ":" << g_config->aspect_height;
131     std::string aspect_ratio = out.str();
132     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
133     {
134       if(*i == aspect_ratio)
135       {
136         aspect_ratio.clear();
137         break;
138       }
139     }
140
141     if (!aspect_ratio.empty())
142     {
143       aspect->selected = aspect->list.size();
144       aspect->list.push_back(aspect_ratio);
145     }
146   }
147   
148   if (sound_manager->is_audio_enabled()) {
149     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
150       ->set_help(_("Disable all sound effects"));
151     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
152       ->set_help(_("Disable all music"));
153   } else {
154     add_inactive(MNID_SOUND, _("Sound (disabled)"));
155     add_inactive(MNID_MUSIC, _("Music (disabled)"));
156   }
157   
158   add_submenu(_("Setup Keyboard"), MenuStorage::get_key_options_menu())
159     ->set_help(_("Configure key-action mappings"));
160
161   add_submenu(_("Setup Joystick"), MenuStorage::get_joystick_options_menu())
162     ->set_help(_("Configure joystick control-action mappings"));
163   add_hl();
164   add_back(_("Back"));
165 }
166
167 OptionsMenu::~OptionsMenu()
168 {
169 }
170
171 void
172 OptionsMenu::menu_action(MenuItem* item)
173 {
174   switch (item->id) {
175     case MNID_ASPECTRATIO:
176     { 
177       if (item->list[item->selected] == "auto")
178       {
179         g_config->aspect_width  = 0; // Magic values
180         g_config->aspect_height = 0;
181         Renderer::instance()->apply_config();
182         MenuManager::recalc_pos();
183       }
184       else if(sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_width, &g_config->aspect_height) == 2)
185       {
186         Renderer::instance()->apply_config();
187         MenuManager::recalc_pos();
188       }
189       else
190       {
191         assert(!"This must not be reached");
192       }
193     }
194     break;
195
196     case MNID_MAGNIFICATION:
197       if (item->list[item->selected] == "auto")
198       {
199         g_config->magnification = 0.0f; // Magic value 
200       }
201       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
202       {
203         g_config->magnification /= 100.0f;
204       }
205       Renderer::instance()->apply_config();
206       MenuManager::recalc_pos();
207       break;
208
209     case MNID_FULLSCREEN_RESOLUTION:
210       if(sscanf(item->list[item->selected].c_str(), "%dx%d", &g_config->fullscreen_width, &g_config->fullscreen_height) == 2)
211       {
212         // do nothing, changes are only applied when toggling fullscreen mode
213       }      
214       break;
215
216     case MNID_FULLSCREEN:
217       if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
218         g_config->use_fullscreen = !g_config->use_fullscreen;
219         Renderer::instance()->apply_config();
220         MenuManager::recalc_pos();
221         g_config->save();
222       }
223       break;
224
225     case MNID_SOUND:
226       if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
227         g_config->sound_enabled = !g_config->sound_enabled;
228         sound_manager->enable_sound(g_config->sound_enabled);
229         g_config->save();
230       }
231       break;
232
233     case MNID_MUSIC:
234       if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
235         g_config->music_enabled = !g_config->music_enabled;
236         sound_manager->enable_music(g_config->music_enabled);
237         g_config->save();
238       }
239       break;
240
241     default:
242       break;
243   }
244 }
245
246 /* EOF */