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