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