When custom aspect ratio is set, add it to the list in the option menu, also save...
[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 #include <math.h> //for log10()
22
23 #include "profile_menu.hpp"
24 #include "options_menu.hpp"
25 #include "gui/menu.hpp"
26 #include "audio/sound_manager.hpp"
27 #include "control/joystickkeyboardcontroller.hpp"
28 #include "main.hpp"
29 #include "gettext.hpp"
30 #include "gameconfig.hpp"
31
32 Menu* options_menu   = 0;
33
34 enum OptionsMenuIDs {
35   MNID_FULLSCREEN,
36   MNID_ASPECTRATIO,
37   MNID_SOUND,
38   MNID_MUSIC
39 };
40
41 class LanguageMenu : public Menu
42 {
43 public:
44   LanguageMenu() {
45     add_label(_("Language"));
46     add_hl();
47     add_entry(0, std::string("(")+_("auto-detect language")+")");
48     add_entry(1, "English");
49
50     int mnid = 10;    
51     std::set<std::string> languages = dictionary_manager.get_languages();
52     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
53       std::string locale_name = *i;
54       TinyGetText::LanguageDef ldef = TinyGetText::get_language_def(locale_name);
55       std::string locale_fullname = locale_name;
56       if (std::string(ldef.code) == locale_name) {
57         locale_fullname = ldef.name;
58       }
59       add_entry(mnid++, locale_fullname);
60     } 
61
62     add_hl();
63     add_back(_("Back"));
64   }
65
66   virtual void menu_action(MenuItem* item) {
67     if (item->id == 0) {
68       config->locale = "";
69       dictionary_manager.set_language(config->locale);
70       config->save();
71       Menu::set_current(0);
72     }
73     else if (item->id == 1) {
74       config->locale = "en";
75       dictionary_manager.set_language(config->locale);
76       config->save();
77       Menu::set_current(0);
78     }
79     int mnid = 10;    
80     std::set<std::string> languages = dictionary_manager.get_languages();
81     for (std::set<std::string>::iterator i = languages.begin(); i != languages.end(); i++) {
82       std::string locale_name = *i;
83       if (item->id == mnid++) {
84         config->locale = locale_name;
85         dictionary_manager.set_language(config->locale);
86         config->save();
87         Menu::set_current(0);
88       }
89     }
90   }
91 };
92
93
94 class OptionsMenu : public Menu
95 {
96 public:
97   OptionsMenu();
98   virtual ~OptionsMenu();
99
100   virtual void menu_action(MenuItem* item);
101
102 protected:
103   std::auto_ptr<LanguageMenu> language_menu;
104   
105 };
106
107 OptionsMenu::OptionsMenu()
108 {
109   language_menu.reset(new LanguageMenu());
110
111   add_label(_("Options"));
112   add_hl();
113
114   add_submenu(_("Select Language"), language_menu.get())
115     ->set_help(_("Switch to another language"));
116
117   add_submenu(_("Select Profile"), get_profile_menu())
118     ->set_help(_("Switch between different savegames"));
119
120   add_toggle(MNID_SOUND, _("Profile on Startup"), config->sound_enabled)
121     ->set_help(_("Display the profile menu when the game is newly started"));
122   
123   // FIXME: Implement me: if (get_parent() == main_menu)
124   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen)
125     ->set_help(_("Let the game cover the whole screen"));
126
127   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
128   aspect->set_help(_("Adjust the aspect ratio"));
129   
130   aspect->list.push_back("16:9");
131   aspect->list.push_back("16:10");
132   aspect->list.push_back("4:3");
133   aspect->list.push_back("5:4");
134
135   std::ostringstream out;
136   out << config->aspect_width << ":" << config->aspect_height;
137   std::string aspect_ratio = out.str();
138   for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
139     {
140       if(*i == aspect_ratio)
141         {
142           aspect_ratio.clear();
143           break;
144         }
145     }
146
147   if (!aspect_ratio.empty())
148     {
149       aspect->selected = aspect->list.size();
150       aspect->list.push_back(aspect_ratio);
151     }
152   
153   if (sound_manager->is_audio_enabled()) {
154     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled)
155       ->set_help(_("Disable all sound effects in the game"));
156     add_toggle(MNID_MUSIC, _("Music"), config->music_enabled)
157       ->set_help(_("Disable all music in the game"));
158   } else {
159     add_deactive(MNID_SOUND, _("Sound (disabled)"));
160     add_deactive(MNID_SOUND, _("Music (disabled)"));
161   }
162   
163   add_submenu(_("Setup Keyboard"), main_controller->get_key_options_menu())
164     ->set_help(_("Configure how your keyboard maps to the game"));
165
166   add_submenu(_("Setup Joystick"),main_controller->get_joystick_options_menu())
167     ->set_help(_("Configure how your joystick maps to the game"));
168   add_hl();
169   add_back(_("Back"));
170 }
171
172 OptionsMenu::~OptionsMenu()
173 {
174 }
175
176 void
177 OptionsMenu::menu_action(MenuItem* item)
178 {
179   switch (item->id) {
180     case MNID_ASPECTRATIO:
181       { // FIXME: Really crude and ugly here, move to video or so
182         if(sscanf(item->list[item->selected].c_str(), "%d:%d", &config->aspect_width, &config->aspect_height) == 2) 
183           {
184             float aspect_ratio = static_cast<double>(config->aspect_width) /
185               static_cast<double>(config->aspect_height);
186
187             if (aspect_ratio > 1) {
188               SCREEN_WIDTH  = static_cast<int> (600 * aspect_ratio + 0.5);
189               SCREEN_HEIGHT = 600;
190             } else {
191               SCREEN_WIDTH  = 600;
192               SCREEN_HEIGHT = static_cast<int> (600 * 1/aspect_ratio + 0.5);
193             }
194
195             glMatrixMode(GL_PROJECTION);
196             glLoadIdentity();
197             glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
198             std::cout << __FILE__ << ":" << __LINE__ << ": change aspect ratio to " << item->list[item->selected] << std::endl;
199
200             // Reposition the menu to be in the center of the screen again
201             set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
202           }
203       }
204       break;
205
206     case MNID_FULLSCREEN:
207       if(config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) {
208         config->use_fullscreen = !config->use_fullscreen;
209         init_video();
210         config->save();
211       }
212       break;
213     case MNID_SOUND:
214       if(config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) {
215         config->sound_enabled = !config->sound_enabled;
216         sound_manager->enable_sound(config->sound_enabled);
217         config->save();
218       }
219       break;
220     case MNID_MUSIC:
221       if(config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) {
222         config->music_enabled = !config->music_enabled;
223         sound_manager->enable_music(config->music_enabled);
224         config->save();
225       }
226       break;
227     default:
228       break;
229   }
230 }
231
232 Menu* get_options_menu()
233 {
234   //static OptionsMenu menu;
235   options_menu = new OptionsMenu();
236   return options_menu;
237 }
238
239 void free_options_menu()
240 {
241   delete options_menu;
242   options_menu = 0;
243 }