Updated addon repository URL and improved debug output on download
[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_SOUND,
41   MNID_MUSIC,
42   MNID_DEVELOPER_MODE
43 };
44
45 OptionsMenu::OptionsMenu(bool complete)
46 {
47   add_label(_("Options"));
48   add_hl();
49
50   if (complete)
51   {
52     // Language and profile changes are only be possible in the
53     // main menu, since elsewhere it might not always work fully
54     add_submenu(_("Select Language"), MenuStorage::LANGUAGE_MENU)
55       ->set_help(_("Select a different language to display text in"));
56
57     add_submenu(_("Select Profile"), MenuStorage::PROFILE_MENU)
58       ->set_help(_("Select a profile to play with"));
59   }
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   std::string last_display_mode;
108   for(int i = 0; i < display_mode_count; ++i)
109   {
110     SDL_DisplayMode mode;
111     int ret = SDL_GetDisplayMode(0, i, &mode);
112     if (ret != 0)
113     {
114       log_warning << "failed to get display mode: " << SDL_GetError() << std::endl;
115     }
116     else
117     {
118       std::ostringstream out;
119       out << mode.w << "x" << mode.h << "@" << mode.refresh_rate;
120       if(last_display_mode == out.str())
121         continue;
122       last_display_mode = out.str();
123       fullscreen_res->list.push_back(out.str());
124     }
125   }
126   fullscreen_res->list.push_back("Desktop");
127
128   std::string fullscreen_size_str = "Desktop";
129   {
130     std::ostringstream out;
131     if (g_config->fullscreen_size != Size(0, 0))
132     {
133       out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height << "@" << g_config->fullscreen_refresh_rate;
134       fullscreen_size_str = out.str();
135     }
136   }
137
138   size_t cnt = 0;
139   for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i)
140   {
141     if (*i == fullscreen_size_str)
142     {
143       fullscreen_size_str.clear();
144       fullscreen_res->selected = cnt;
145       break;
146     }
147     ++cnt;
148   }
149   if (!fullscreen_size_str.empty())
150   {
151     fullscreen_res->selected = fullscreen_res->list.size();
152     fullscreen_res->list.push_back(fullscreen_size_str);
153   }
154
155   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
156   aspect->set_help(_("Adjust the aspect ratio"));
157
158   aspect->list.push_back(_("auto"));
159   aspect->list.push_back("5:4");
160   aspect->list.push_back("4:3");
161   aspect->list.push_back("16:10");
162   aspect->list.push_back("16:9");
163   aspect->list.push_back("1368:768");
164
165   if (g_config->aspect_size != Size(0, 0))
166   {
167     std::ostringstream out;
168     out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
169     std::string aspect_ratio = out.str();
170     size_t cnt_ = 0;
171     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
172     {
173       if(*i == aspect_ratio)
174       {
175         aspect_ratio.clear();
176         aspect->selected = cnt_;
177         break;
178       }
179       ++cnt_;
180     }
181
182     if (!aspect_ratio.empty())
183     {
184       aspect->selected = aspect->list.size();
185       aspect->list.push_back(aspect_ratio);
186     }
187   }
188
189   if (SoundManager::current()->is_audio_enabled()) {
190     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
191       ->set_help(_("Disable all sound effects"));
192     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
193       ->set_help(_("Disable all music"));
194   } else {
195     add_inactive(MNID_SOUND, _("Sound (disabled)"));
196     add_inactive(MNID_MUSIC, _("Music (disabled)"));
197   }
198
199   add_submenu(_("Setup Keyboard"), MenuStorage::KEYBOARD_MENU)
200     ->set_help(_("Configure key-action mappings"));
201
202   add_submenu(_("Setup Joystick"), MenuStorage::JOYSTICK_MENU)
203     ->set_help(_("Configure joystick control-action mappings"));
204
205   if (g_config->developer_mode)
206   {
207     add_toggle(MNID_DEVELOPER_MODE, _("Developer Mode"), g_config->developer_mode);
208   }
209
210   add_hl();
211   add_back(_("Back"));
212 }
213
214 OptionsMenu::~OptionsMenu()
215 {
216 }
217
218 void
219 OptionsMenu::menu_action(MenuItem* item)
220 {
221   switch (item->id) {
222     case MNID_ASPECTRATIO:
223       {
224         if (item->list[item->selected] == _("auto"))
225         {
226           g_config->aspect_size = Size(0, 0); // Magic values
227           VideoSystem::current()->get_renderer().apply_config();
228           MenuManager::instance().on_window_resize();
229         }
230         else if (sscanf(item->list[item->selected].c_str(), "%d:%d",
231                         &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
232         {
233           VideoSystem::current()->get_renderer().apply_config();
234           MenuManager::instance().on_window_resize();
235         }
236         else
237         {
238           assert(!"This must not be reached");
239         }
240       }
241       break;
242
243     case MNID_MAGNIFICATION:
244       if (item->list[item->selected] == _("auto"))
245       {
246         g_config->magnification = 0.0f; // Magic value
247       }
248       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
249       {
250         g_config->magnification /= 100.0f;
251       }
252       VideoSystem::current()->get_renderer().apply_config();
253       MenuManager::instance().on_window_resize();
254       break;
255
256     case MNID_FULLSCREEN_RESOLUTION:
257       {
258         int width;
259         int height;
260         int refresh_rate;
261         if (item->list[item->selected] == "Desktop")
262         {
263           g_config->fullscreen_size.width = 0;
264           g_config->fullscreen_size.height = 0;
265           g_config->fullscreen_refresh_rate = 0;
266         }
267         else if(sscanf(item->list[item->selected].c_str(), "%dx%d@%d",
268                   &width, &height, &refresh_rate) == 3)
269         {
270           // do nothing, changes are only applied when toggling fullscreen mode
271           g_config->fullscreen_size.width = width;
272           g_config->fullscreen_size.height = height;
273           g_config->fullscreen_refresh_rate = refresh_rate;
274         }
275       }
276       break;
277
278     case MNID_FULLSCREEN:
279       if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
280         g_config->use_fullscreen = !g_config->use_fullscreen;
281         VideoSystem::current()->get_renderer().apply_config();
282         MenuManager::instance().on_window_resize();
283         g_config->save();
284       }
285       break;
286
287     case MNID_SOUND:
288       if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
289         g_config->sound_enabled = !g_config->sound_enabled;
290         SoundManager::current()->enable_sound(g_config->sound_enabled);
291         g_config->save();
292       }
293       break;
294
295     case MNID_MUSIC:
296       if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
297         g_config->music_enabled = !g_config->music_enabled;
298         SoundManager::current()->enable_music(g_config->music_enabled);
299         g_config->save();
300       }
301       break;
302
303     case MNID_DEVELOPER_MODE:
304       g_config->developer_mode = is_toggled(MNID_DEVELOPER_MODE);
305       log_info << "developer mode: " << g_config->developer_mode << std::endl;
306       break;
307
308     default:
309       break;
310   }
311 }
312
313 /* EOF */