Turned a lot of other global objects into Currentons
[supertux.git] / src / supertux / menu / contrib_levelset_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/menu/contrib_levelset_menu.hpp"
18
19 #include <sstream>
20
21 #include "audio/sound_manager.hpp"
22 #include "gui/menu_item.hpp"
23 #include "supertux/game_manager.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/levelset.hpp"
26 #include "supertux/screen_fade.hpp"
27 #include "supertux/screen_manager.hpp"
28 #include "supertux/title_screen.hpp"
29 #include "supertux/world.hpp"
30 #include "util/file_system.hpp"
31 #include "util/gettext.hpp"
32
33 ContribLevelsetMenu::ContribLevelsetMenu(std::unique_ptr<World> world) :
34   m_world(std::move(world)),
35   m_levelset()
36 {
37   assert(m_world->is_levelset());
38
39   m_levelset = std::unique_ptr<Levelset>(new Levelset(m_world->get_basedir()));
40
41   Savegame savegame(m_world->get_savegame_filename());
42   savegame.load();
43   LevelsetState state = savegame.get_levelset_state(m_world->get_basedir());
44
45   add_label(m_world->get_title());
46   add_hl();
47
48   for (int i = 0; i < m_levelset->get_num_levels(); ++i)
49   {
50     std::string filename = m_levelset->get_level_filename(i);
51     std::string full_filename = FileSystem::join(m_world->get_basedir(), filename);
52     std::string title = GameManager::current()->get_level_name(full_filename);
53     LevelState level_state = state.get_level_state(filename);
54
55     std::ostringstream out;
56     if (level_state.solved)
57     {
58       out << title << " [*]";
59     }
60     else
61     {
62       out << title << " [ ]";
63     }
64     add_entry(i, out.str());
65   }
66
67   add_hl();
68   add_back(_("Back"));
69 }
70
71 void
72 ContribLevelsetMenu::menu_action(MenuItem* item)
73 {
74   if (item->kind == MN_ACTION)
75   {
76     SoundManager::current()->stop_music();
77
78     // reload the World so that we have something that we can safely
79     // std::move() around without wreaking the ContribMenu
80     std::unique_ptr<World> world = World::load(m_world->get_basedir());
81     GameManager::current()->start_level(std::move(world), m_levelset->get_level_filename(item->id));
82   }
83 }
84
85 /* EOF */