Reverted part of revision 02c347cdae86.
[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 "gui/menu_manager.hpp"
22 #include "supertux/gameconfig.hpp"
23 #include "supertux/menu/joystick_menu.hpp"
24 #include "supertux/menu/keyboard_menu.hpp"
25 #include "supertux/menu/language_menu.hpp"
26 #include "supertux/menu/menu_storage.hpp"
27 #include "supertux/menu/profile_menu.hpp"
28 #include "util/string_util.hpp"
29 #include "video/renderer.hpp"
30
31 #include <algorithm>
32 #include <sstream>
33
34 enum OptionsMenuIDs {
35   MNID_FULLSCREEN,
36   MNID_FULLSCREEN_RESOLUTION,
37   MNID_MAGNIFICATION,
38   MNID_ASPECTRATIO,
39   MNID_PROFILES,
40   MNID_SOUND,
41   MNID_MUSIC
42 };
43
44 OptionsMenu::OptionsMenu() :
45   language_menu()
46 {
47   language_menu.reset(new LanguageMenu());
48
49   add_label(_("Options"));
50   add_hl();
51
52   // Language change should only be possible in the main menu, since elsewhere it might not always work fully
53   // FIXME: Implement me: if (get_parent() == main_menu)
54   add_submenu(_("Select Language"), language_menu.get())
55     ->set_help(_("Select a different language to display text in"));
56
57   add_submenu(_("Select Profile"), MenuStorage::get_profile_menu())
58     ->set_help(_("Select a profile to play with"));
59
60   add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
61     ->set_help(_("Select your profile immediately after start-up"));
62   
63   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
64     ->set_help(_("Fill the entire screen"));
65
66   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Resolution"));
67   fullscreen_res->set_help(_("Determine the resolution used in fullscreen mode (you must toggle fullscreen to complete the change)"));
68
69   MenuItem* magnification = add_string_select(MNID_MAGNIFICATION, _("Magnification"));
70   magnification->set_help(_("Change the magnification of the game area"));
71
72   // These values go from screen:640/projection:1600 to
73   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
74   magnification->list.push_back(_("auto"));
75   magnification->list.push_back("40%");
76   magnification->list.push_back("50%");
77   magnification->list.push_back("62.5%");
78   magnification->list.push_back("80%");
79   magnification->list.push_back("100%");
80   magnification->list.push_back("125%");
81   magnification->list.push_back("160%");
82   magnification->list.push_back("200%");
83   magnification->list.push_back("250%");
84   if (g_config->magnification != 0.0f) //auto
85   {
86     std::ostringstream out;
87     out << (g_config->magnification*100) << "%";
88     std::string magn = out.str();
89     size_t count = 0;
90     for (std::vector<std::string>::iterator i = magnification->list.begin(); i != magnification->list.end(); ++i)
91     {
92       if (*i == magn)
93       {
94         magnification->selected = count;
95         magn.clear();
96         break;
97       }
98       
99       ++count;
100     }
101     if (!magn.empty()) //magnification not in our list but accept anyway
102     {
103       magnification->selected = magnification->list.size();
104       magnification->list.push_back(magn);
105     }
106   }
107   
108
109   for(int disp_mode_ctr = 0; disp_mode_ctr < SDL_GetNumDisplayModes(0); disp_mode_ctr++)
110   {
111     SDL_DisplayMode* current = NULL;
112     int disp_retval = SDL_GetDisplayMode(0, // Display Index
113                                          disp_mode_ctr, // Mode index (default to first)
114                                          current); // DisplayMode structure ptr
115     if (disp_retval == -1) 
116     { // No resolutions at all available, bad
117     }
118     else if(disp_retval == 0) 
119     { // All resolutions should work, so we fall back to hardcoded defaults
120       std::stringstream width;
121       std::stringstream height;
122       width << current->w;
123       height << current->h;
124       fullscreen_res->list.push_back(width.str() + "x" + height.str());
125     }
126     else
127     {
128     }
129   }
130   // On Ubuntu/Linux resolutions are returned from highest to
131   // lowest, so reverse them
132   std::sort(fullscreen_res->list.begin(), fullscreen_res->list.end(), StringUtil::numeric_less);      
133   
134   std::ostringstream out;
135   out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height;
136   std::string fllscrn_sz = out.str();
137   size_t cnt = 0;
138   for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i) 
139   {
140     if (*i == fllscrn_sz)
141     {
142       fllscrn_sz.clear();
143       fullscreen_res->selected = cnt;
144       break;
145     }
146     ++cnt;
147   }
148   if (!fllscrn_sz.empty())
149   {
150     fullscreen_res->selected = fullscreen_res->list.size();
151     fullscreen_res->list.push_back(fllscrn_sz);
152   }
153
154   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
155   aspect->set_help(_("Adjust the aspect ratio"));
156   
157   aspect->list.push_back(_("auto"));
158   aspect->list.push_back("5:4");
159   aspect->list.push_back("4:3");
160   aspect->list.push_back("16:10");
161   aspect->list.push_back("16:9");
162   aspect->list.push_back("1368:768");
163
164   if (g_config->aspect_size != Size(0, 0))
165   {
166     std::ostringstream out;
167     out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
168     std::string aspect_ratio = out.str();
169     size_t cnt = 0;
170     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
171     {
172       if(*i == aspect_ratio)
173       {
174         aspect_ratio.clear();
175         aspect->selected = cnt;
176         break;
177       }
178       ++cnt;
179     }
180
181     if (!aspect_ratio.empty())
182     {
183       aspect->selected = aspect->list.size();
184       aspect->list.push_back(aspect_ratio);
185     }
186   }
187   
188   if (sound_manager->is_audio_enabled()) {
189     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
190       ->set_help(_("Disable all sound effects"));
191     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
192       ->set_help(_("Disable all music"));
193   } else {
194     add_inactive(MNID_SOUND, _("Sound (disabled)"));
195     add_inactive(MNID_MUSIC, _("Music (disabled)"));
196   }
197   
198   add_submenu(_("Setup Keyboard"), MenuStorage::get_key_options_menu())
199     ->set_help(_("Configure key-action mappings"));
200
201   add_submenu(_("Setup Joystick"), MenuStorage::get_joystick_options_menu())
202     ->set_help(_("Configure joystick control-action mappings"));
203   add_hl();
204   add_back(_("Back"));
205 }
206
207 OptionsMenu::~OptionsMenu()
208 {
209 }
210
211 void
212 OptionsMenu::menu_action(MenuItem* item)
213 {
214   switch (item->id) {
215     case MNID_ASPECTRATIO:
216     { 
217       if (item->list[item->selected] == _("auto"))
218       {
219         g_config->aspect_size = Size(0, 0); // Magic values
220         Renderer::instance()->apply_config();
221         MenuManager::recalc_pos();
222       }
223       else if (sscanf(item->list[item->selected].c_str(), "%d:%d", 
224                       &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
225       {
226         Renderer::instance()->apply_config();
227         MenuManager::recalc_pos();
228       }
229       else
230       {
231         assert(!"This must not be reached");
232       }
233     }
234     break;
235
236     case MNID_MAGNIFICATION:
237       if (item->list[item->selected] == _("auto"))
238       {
239         g_config->magnification = 0.0f; // Magic value 
240       }
241       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
242       {
243         g_config->magnification /= 100.0f;
244       }
245       Renderer::instance()->apply_config();
246       MenuManager::recalc_pos();
247       break;
248
249     case MNID_FULLSCREEN_RESOLUTION:
250       if(sscanf(item->list[item->selected].c_str(), "%dx%d", 
251                 &g_config->fullscreen_size.width, &g_config->fullscreen_size.height) == 2)
252       {
253         // do nothing, changes are only applied when toggling fullscreen mode
254       }      
255       break;
256
257     case MNID_FULLSCREEN:
258       if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
259         g_config->use_fullscreen = !g_config->use_fullscreen;
260         Renderer::instance()->apply_config();
261         MenuManager::recalc_pos();
262         g_config->save();
263       }
264       break;
265
266     case MNID_SOUND:
267       if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
268         g_config->sound_enabled = !g_config->sound_enabled;
269         sound_manager->enable_sound(g_config->sound_enabled);
270         g_config->save();
271       }
272       break;
273
274     case MNID_MUSIC:
275       if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
276         g_config->music_enabled = !g_config->music_enabled;
277         sound_manager->enable_music(g_config->music_enabled);
278         g_config->save();
279       }
280       break;
281
282     default:
283       break;
284   }
285 }
286
287 void
288 OptionsMenu::check_menu()
289 {
290 }
291
292 /* EOF */