Added MenuManager to keep track of created Menus
[supertux.git] / src / supertux / title_screen.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias 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 <version.h>
19
20 #include "supertux/title_screen.hpp"
21
22 #include <algorithm>
23 #include <physfs.h>
24
25 #include "addon/addon_manager.hpp"
26 #include "audio/sound_manager.hpp"
27 #include "gui/menu.hpp"
28 #include "gui/menu_item.hpp"
29 #include "lisp/parser.hpp"
30 #include "object/camera.hpp"
31 #include "object/player.hpp"
32 #include "supertux/fadeout.hpp"
33 #include "supertux/gameconfig.hpp"
34 #include "supertux/globals.hpp"
35 #include "supertux/mainloop.hpp"
36 #include "supertux/menu/menu_manager.hpp"
37 #include "supertux/menu/options_menu.hpp"
38 #include "supertux/resources.hpp"
39 #include "supertux/sector.hpp"
40 #include "supertux/textscroller.hpp"
41 #include "supertux/world.hpp"
42 #include "util/file_system.hpp"
43 #include "util/gettext.hpp"
44 #include "util/reader.hpp"
45 #include "video/drawing_context.hpp"
46
47 enum MainMenuIDs {
48   MNID_STARTGAME,
49   MNID_LEVELS_CONTRIB,
50   MNID_ADDONS,
51   MNID_OPTIONMENU,
52   MNID_LEVELEDITOR,
53   MNID_CREDITS,
54   MNID_QUITMAINMENU
55 };
56
57 TitleScreen::TitleScreen() :
58   main_menu(),
59   contrib_menu(),
60   contrib_world_menu(),
61   main_world(),
62   contrib_worlds(),
63   addons_menu(),
64   addons(),
65   current_world(),
66   frame(),
67   controller(),
68   titlesession()
69 {
70   controller.reset(new CodeController());
71   titlesession.reset(new GameSession("levels/misc/menu.stl"));
72
73   Player* player = titlesession->get_current_sector()->player;
74   player->set_controller(controller.get());
75   player->set_speedlimit(230); //MAX_WALK_XM
76
77   generate_main_menu();
78
79   frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
80 }
81
82 void
83 TitleScreen::update_load_game_menu()
84 {
85 }
86
87 void
88 TitleScreen::free_contrib_menu()
89 {
90   for(std::vector<World*>::iterator i = contrib_worlds.begin();
91       i != contrib_worlds.end(); ++i)
92     delete *i;
93
94   contrib_worlds.clear();
95 }
96
97 void
98 TitleScreen::generate_contrib_menu()
99 {
100   /** Generating contrib levels list by making use of Level Subset  */
101   std::vector<std::string> level_worlds;
102   char** files = PHYSFS_enumerateFiles("levels/");
103   for(const char* const* filename = files; *filename != 0; ++filename) {
104     std::string filepath = std::string("levels/") + *filename;
105     if(PHYSFS_isDirectory(filepath.c_str()))
106       level_worlds.push_back(filepath);
107   }
108   PHYSFS_freeList(files);
109
110   free_contrib_menu();
111   contrib_menu.reset(new Menu());
112
113   contrib_menu->add_label(_("Contrib Levels"));
114   contrib_menu->add_hl();
115
116   int i = 0;
117   for (std::vector<std::string>::iterator it = level_worlds.begin();
118        it != level_worlds.end(); ++it) {
119     try {
120       std::auto_ptr<World> world (new World());
121       world->load(*it + "/info");
122       if(world->hide_from_contribs) {
123         continue;
124       }
125       contrib_menu->add_entry(i++, world->title);
126       contrib_worlds.push_back(world.release());
127     } catch(std::exception& e) {
128       log_warning << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
129     }
130   }
131
132   contrib_menu->add_hl();
133   contrib_menu->add_back(_("Back"));
134 }
135
136 std::string
137 TitleScreen::get_level_name(const std::string& filename)
138 {
139   try {
140     lisp::Parser parser;
141     const lisp::Lisp* root = parser.parse(filename);
142
143     const lisp::Lisp* level = root->get_lisp("supertux-level");
144     if(!level)
145       return "";
146
147     std::string name;
148     level->get("name", name);
149     return name;
150   } catch(std::exception& e) {
151     log_warning << "Problem getting name of '" << filename << "': "
152                 << e.what() << std::endl;
153     return "";
154   }
155 }
156
157 void
158 TitleScreen::check_levels_contrib_menu()
159 {
160   int index = contrib_menu->check();
161   if (index == -1)
162     return;
163
164   current_world = contrib_worlds[index];
165
166   if(!current_world->is_levelset) {
167     start_game();
168   } else {
169     contrib_world_menu.reset(new Menu());
170
171     contrib_world_menu->add_label(current_world->title);
172     contrib_world_menu->add_hl();
173
174     for (unsigned int i = 0; i < current_world->get_num_levels(); ++i)
175     {
176       /** get level's title */
177       std::string filename = current_world->get_level_filename(i);
178       std::string title = get_level_name(filename);
179       contrib_world_menu->add_entry(i, title);
180     }
181
182     contrib_world_menu->add_hl();
183     contrib_world_menu->add_back(_("Back"));
184
185     Menu::push_current(contrib_world_menu.get());
186   }
187 }
188
189 void
190 TitleScreen::check_contrib_world_menu()
191 {
192   int index = contrib_world_menu->check();
193   if (index != -1) {
194     if (contrib_world_menu->get_item_by_id(index).kind == MN_ACTION) {
195       sound_manager->stop_music();
196       GameSession* session =
197         new GameSession(current_world->get_level_filename(index));
198       g_main_loop->push_screen(session);
199     }
200   }
201 }
202
203 namespace {
204 bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
205 {
206   return a1->title < a2->title;
207 }
208
209 const int ADDON_LIST_START_ID = 10;
210 }
211
212 void
213 TitleScreen::generate_addons_menu()
214 {
215   AddonManager& adm = AddonManager::get_instance();
216
217   // refresh list of addons
218   addons = adm.get_addons();
219   
220   // sort list
221   std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
222
223   // (re)generate menu
224   free_addons_menu();
225   addons_menu.reset(new Menu());
226
227   addons_menu->add_label(_("Add-ons"));
228   addons_menu->add_hl();
229   
230 #ifdef HAVE_LIBCURL
231   addons_menu->add_entry(0, std::string(_("Check Online")));
232 #else
233   addons_menu->add_inactive(0, std::string(_("Check Online (disabled)")));
234 #endif
235
236   //addons_menu->add_hl();
237
238   for (unsigned int i = 0; i < addons.size(); i++) {
239     const Addon& addon = *addons[i];
240     std::string text = "";
241     if (addon.kind != "") text += addon.kind + " ";
242     text += std::string("\"") + addon.title + "\"";
243     if (addon.author != "") text += " by \"" + addon.author + "\"";
244     addons_menu->add_toggle(ADDON_LIST_START_ID + i, text, addon.loaded);
245   }
246
247   addons_menu->add_hl();
248   addons_menu->add_back(_("Back"));
249 }
250
251 void
252 TitleScreen::check_addons_menu()
253 {
254   int index = addons_menu->check();
255   if (index == -1) return;
256
257   // check if "Check Online" was chosen
258   if (index == 0) {
259     try {
260       AddonManager::get_instance().check_online();
261       generate_addons_menu();
262       Menu::set_current(addons_menu.get());
263       addons_menu->set_active_item(index);
264     } 
265     catch (std::runtime_error e) {
266       log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
267     }
268     return;
269   }
270
271   // if one of the Addons listed was chosen, take appropriate action
272   if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
273     Addon& addon = *addons[index - ADDON_LIST_START_ID];
274     if (!addon.installed) {
275       try {
276         AddonManager::get_instance().install(&addon);
277       } 
278       catch (std::runtime_error e) {
279         log_warning << "Installing Add-on failed: " << e.what() << std::endl;
280       }
281       addons_menu->set_toggled(index, addon.loaded);
282     } else if (!addon.loaded) {
283       try {
284         AddonManager::get_instance().enable(&addon);
285       } 
286       catch (std::runtime_error e) {
287         log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
288       }
289       addons_menu->set_toggled(index, addon.loaded);
290     } else {
291       try {
292         AddonManager::get_instance().disable(&addon);
293       } 
294       catch (std::runtime_error e) {
295         log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
296       }
297       addons_menu->set_toggled(index, addon.loaded);
298     }
299   }
300 }
301
302 void
303 TitleScreen::free_addons_menu()
304 {
305 }
306
307 void
308 TitleScreen::make_tux_jump()
309 {
310   static bool jumpWasReleased = true;
311   Sector* sector  = titlesession->get_current_sector();
312   Player* tux = sector->player;
313
314   controller->update();
315   controller->press(Controller::RIGHT);
316
317   // Check if we should press the jump button
318   Rect lookahead = tux->get_bbox();
319   lookahead.p2.x += 96;
320   bool pathBlocked = !sector->is_free_of_statics(lookahead);
321   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
322     controller->press(Controller::JUMP);
323     jumpWasReleased = false;
324   } else {
325     jumpWasReleased = true;
326   }
327
328   // Wrap around at the end of the level back to the beginning
329   if(sector->get_width() - 320 < tux->get_pos().x) {
330     sector->activate("main");
331     sector->camera->reset(tux->get_pos());
332   }
333 }
334
335 void
336 TitleScreen::generate_main_menu()
337 {
338   main_menu.reset(new Menu());
339   main_menu->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
340   main_menu->add_entry(MNID_STARTGAME, _("Start Game"));
341   main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
342   main_menu->add_entry(MNID_ADDONS, _("Add-ons"));
343   main_menu->add_submenu(_("Options"), MenuManager::get_options_menu());
344   main_menu->add_entry(MNID_CREDITS, _("Credits"));
345   main_menu->add_entry(MNID_QUITMAINMENU, _("Quit"));
346 }
347
348 TitleScreen::~TitleScreen()
349 {
350 }
351
352 void
353 TitleScreen::setup()
354 {
355   player_status->reset();
356
357   Sector* sector = titlesession->get_current_sector();
358   if(Sector::current() != sector) {
359     sector->play_music(LEVEL_MUSIC);
360     sector->activate(sector->player->get_pos());
361   }
362
363   Menu::set_current(main_menu.get());
364 }
365
366 void
367 TitleScreen::leave()
368 {
369   Sector* sector = titlesession->get_current_sector();
370   sector->deactivate();
371   Menu::set_current(NULL);
372 }
373
374 void
375 TitleScreen::draw(DrawingContext& context)
376 {
377   Sector* sector  = titlesession->get_current_sector();
378   sector->draw(context);
379
380   // FIXME: Add something to scale the frame to the resolution of the screen
381   context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
382
383   context.draw_text(small_font, "SuperTux " PACKAGE_VERSION "\n",
384                     Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
385   context.draw_text(small_font,
386                     _(
387                       "Copyright (c) 2007 SuperTux Devel Team\n"
388                       "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
389                       "redistribute it under certain conditions; see the file COPYING for details.\n"
390                       ),
391                     Vector(5, SCREEN_HEIGHT - 50 + small_font->get_height() + 5),
392                     ALIGN_LEFT, LAYER_FOREGROUND1);
393 }
394
395 void
396 TitleScreen::update(float elapsed_time)
397 {
398   g_main_loop->set_speed(0.6f);
399   Sector* sector  = titlesession->get_current_sector();
400   sector->update(elapsed_time);
401
402   make_tux_jump();
403
404   Menu* menu = Menu::current();
405   if(menu) {
406     if(menu == main_menu.get()) {
407       switch (main_menu->check()) {
408         case MNID_STARTGAME:
409           // Start Game, ie. goto the slots menu
410           if(main_world.get() == NULL) {
411             main_world.reset(new World());
412             main_world->load("levels/world1/info");
413           }
414           current_world = main_world.get();
415           start_game();
416           break;
417
418         case MNID_LEVELS_CONTRIB:
419           // Contrib Menu
420           generate_contrib_menu();
421           Menu::push_current(contrib_menu.get());
422           break;
423
424         case MNID_ADDONS:
425           // Add-ons Menu
426           generate_addons_menu();
427           Menu::push_current(addons_menu.get());
428           break;
429
430         case MNID_CREDITS:
431           Menu::set_current(NULL);
432           g_main_loop->push_screen(new TextScroller("credits.txt"),
433                                    new FadeOut(0.5));
434           break;
435
436         case MNID_QUITMAINMENU:
437           g_main_loop->quit(new FadeOut(0.25));
438           sound_manager->stop_music(0.25);
439           break;
440       }
441     } else if(menu == contrib_menu.get()) {
442       check_levels_contrib_menu();
443     } else if(menu == addons_menu.get()) {
444       check_addons_menu();
445     } else if (menu == contrib_world_menu.get()) {
446       check_contrib_world_menu();
447     }
448   }
449
450   // reopen menu if user closed it (so that the app doesn't close when user
451   // accidently hit ESC)
452   if(Menu::current() == 0 && g_main_loop->has_no_pending_fadeout()) {
453     generate_main_menu();
454     Menu::set_current(main_menu.get());
455   }
456 }
457
458 void
459 TitleScreen::start_game()
460 {
461   Menu::set_current(NULL);
462   std::string basename = current_world->get_basedir();
463   basename = basename.substr(0, basename.length()-1);
464   std::string worlddirname = FileSystem::basename(basename);
465   std::ostringstream stream;
466   stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
467   std::string slotfile = stream.str();
468
469   try {
470     current_world->set_savegame_filename(slotfile);
471     current_world->run();
472   } catch(std::exception& e) {
473     log_fatal << "Couldn't start world: " << e.what() << std::endl;
474   }
475 }
476
477 /* EOF */