Added experimental and unfinished window resize more fixes will follow later, might...
[supertux.git] / src / options_menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobas Glaesser <tobi.web@gmx.de>
5 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #include <config.h>
21
22 #include "profile_menu.hpp"
23 #include "options_menu.hpp"
24 #include "gui/menu.hpp"
25 #include "audio/sound_manager.hpp"
26 #include "control/joystickkeyboardcontroller.hpp"
27 #include "main.hpp"
28 #include "gettext.hpp"
29 #include "gameconfig.hpp"
30
31 Menu* options_menu   = 0;
32
33 enum OptionsMenuIDs {
34   MNID_FULLSCREEN,
35   MNID_FULLSCREEN_RESOLUTION,
36   MNID_PROJECTION_AREA,
37   MNID_ASPECTRATIO,
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 language")+")");
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       config->locale = "";
70       dictionary_manager.set_language(config->locale);
71       config->save();
72       Menu::set_current(0);
73     }
74     else if (item->id == 1) {
75       config->locale = "en";
76       dictionary_manager.set_language(config->locale);
77       config->save();
78       Menu::set_current(0);
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         config->locale = locale_name;
86         dictionary_manager.set_language(config->locale);
87         config->save();
88         Menu::set_current(0);
89       }
90     }
91   }
92 };
93
94
95 class OptionsMenu : public Menu
96 {
97 public:
98   OptionsMenu();
99   virtual ~OptionsMenu();
100
101   virtual void menu_action(MenuItem* item);
102
103 protected:
104   std::auto_ptr<LanguageMenu> language_menu;
105   
106 };
107
108 OptionsMenu::OptionsMenu()
109 {
110   language_menu.reset(new LanguageMenu());
111
112   add_label(_("Options"));
113   add_hl();
114
115   add_submenu(_("Select Language"), language_menu.get())
116     ->set_help(_("Switch to another language"));
117
118   add_submenu(_("Select Profile"), get_profile_menu())
119     ->set_help(_("Switch between different savegames"));
120
121   add_toggle(MNID_SOUND, _("Profile on Startup"), config->sound_enabled)
122     ->set_help(_("Display the profile menu when the game is newly started"));
123   
124   // FIXME: Implement me: if (get_parent() == main_menu)
125   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen)
126     ->set_help(_("Let the game cover the whole screen"));
127
128   MenuItem* projection = add_string_select(MNID_PROJECTION_AREA, _("Projection Area"));
129   projection->set_help(_("Change the visible area"));
130
131   projection->list.push_back("640x480");
132   projection->list.push_back("800x600");
133   projection->list.push_back("1024x768");
134   projection->list.push_back("1152x864");
135   projection->list.push_back("1280x960");
136   projection->list.push_back("1280x1024");
137   projection->list.push_back("1440x900");
138   projection->list.push_back("1680x1050");
139   projection->list.push_back("1600x1200");
140   projection->list.push_back("1920x1080");
141   projection->list.push_back("1920x1200");
142
143   MenuItem* fullscreen_res = add_string_select(MNID_FULLSCREEN_RESOLUTION, _("Fullscreen Resolution"));
144   fullscreen_res->set_help(_("Change the Fullscreen Resolution"));
145
146   // FIXME: Hardcoded values are evil, these should be queried from the Xorg server
147   fullscreen_res->list.push_back("640x480");
148   fullscreen_res->list.push_back("800x600");
149   fullscreen_res->list.push_back("1024x768");
150   fullscreen_res->list.push_back("1152x864");
151   fullscreen_res->list.push_back("1280x960");
152   fullscreen_res->list.push_back("1280x1024");
153   fullscreen_res->list.push_back("1440x900");
154   fullscreen_res->list.push_back("1680x1050");
155   fullscreen_res->list.push_back("1600x1200");
156   fullscreen_res->list.push_back("1920x1080");
157   fullscreen_res->list.push_back("1920x1200");
158
159   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
160   aspect->set_help(_("Adjust the aspect ratio"));
161   
162   aspect->list.push_back("16:9");
163   aspect->list.push_back("16:10");
164   aspect->list.push_back("4:3");
165   aspect->list.push_back("5:4");
166
167   std::ostringstream out;
168   out << config->aspect_width << ":" << config->aspect_height;
169   std::string aspect_ratio = out.str();
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           break;
176         }
177     }
178
179   if (!aspect_ratio.empty())
180     {
181       aspect->selected = aspect->list.size();
182       aspect->list.push_back(aspect_ratio);
183     }
184   
185   if (sound_manager->is_audio_enabled()) {
186     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled)
187       ->set_help(_("Disable all sound effects in the game"));
188     add_toggle(MNID_MUSIC, _("Music"), config->music_enabled)
189       ->set_help(_("Disable all music in the game"));
190   } else {
191     add_deactive(MNID_SOUND, _("Sound (disabled)"));
192     add_deactive(MNID_SOUND, _("Music (disabled)"));
193   }
194   
195   add_submenu(_("Setup Keyboard"), main_controller->get_key_options_menu())
196     ->set_help(_("Configure how your keyboard maps to the game"));
197
198   add_submenu(_("Setup Joystick"),main_controller->get_joystick_options_menu())
199     ->set_help(_("Configure how your joystick maps to the game"));
200   add_hl();
201   add_back(_("Back"));
202 }
203
204 OptionsMenu::~OptionsMenu()
205 {
206 }
207
208 void
209 OptionsMenu::menu_action(MenuItem* item)
210 {
211   switch (item->id) {
212     case MNID_ASPECTRATIO:
213       { // FIXME: Really crude and ugly here, move to video or so
214         if(sscanf(item->list[item->selected].c_str(), "%d:%d", &config->aspect_width, &config->aspect_height) == 2) 
215           {
216             float aspect_ratio = static_cast<double>(config->aspect_width) /
217               static_cast<double>(config->aspect_height);
218
219             if (aspect_ratio > 1) {
220               SCREEN_WIDTH  = static_cast<int> (600 * aspect_ratio + 0.5);
221               SCREEN_HEIGHT = 600;
222             } else {
223               SCREEN_WIDTH  = 600;
224               SCREEN_HEIGHT = static_cast<int> (600 * 1/aspect_ratio + 0.5);
225             }
226
227             glMatrixMode(GL_PROJECTION);
228             glLoadIdentity();
229             glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
230             std::cout << __FILE__ << ":" << __LINE__ << ": change aspect ratio to " << item->list[item->selected] << std::endl;
231
232             // Reposition the menu to be in the center of the screen again
233             set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
234           }
235       }
236       break;
237
238     case MNID_FULLSCREEN:
239       if(config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
240         config->use_fullscreen = !config->use_fullscreen;
241         init_video();
242         config->save();
243       }
244       break;
245     case MNID_SOUND:
246       if(config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
247         config->sound_enabled = !config->sound_enabled;
248         sound_manager->enable_sound(config->sound_enabled);
249         config->save();
250       }
251       break;
252     case MNID_MUSIC:
253       if(config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
254         config->music_enabled = !config->music_enabled;
255         sound_manager->enable_music(config->music_enabled);
256         config->save();
257       }
258       break;
259     default:
260       break;
261   }
262 }
263
264 Menu* get_options_menu()
265 {
266   //static OptionsMenu menu;
267   options_menu = new OptionsMenu();
268   return options_menu;
269 }
270
271 void free_options_menu()
272 {
273   delete options_menu;
274   options_menu = 0;
275 }