d4ae7673fa9655dd94f59f27c7b5e6fc69c2c938
[supertux.git] / src / supertux / menu / contrib_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_menu.hpp"
18
19 #include <physfs.h>
20 #include <sstream>
21
22 #include "gui/menu_manager.hpp"
23 #include "supertux/game_manager.hpp"
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/menu/contrib_levelset_menu.hpp"
26 #include "supertux/menu/menu_storage.hpp"
27 #include "supertux/title_screen.hpp"
28 #include "supertux/world.hpp"
29 #include "util/file_system.hpp"
30 #include "util/gettext.hpp"
31
32 ContribMenu::ContribMenu() :
33   m_contrib_worlds()
34 {
35   /** Generating contrib levels list by making use of Level Subset  */
36   std::vector<std::string> level_worlds;
37   char** files = PHYSFS_enumerateFiles("levels/");
38   for(const char* const* filename = files; *filename != 0; ++filename) {
39     std::string filepath = std::string("levels/") + *filename;
40     if(PHYSFS_isDirectory(filepath.c_str()))
41       level_worlds.push_back(filepath);
42   }
43   PHYSFS_freeList(files);
44
45   add_label(_("Contrib Levels"));
46   add_hl();
47
48   int i = 0;
49   for (std::vector<std::string>::const_iterator it = level_worlds.begin(); it != level_worlds.end(); ++it)
50   {
51     try
52     {
53       std::unique_ptr<World> world = World::load(*it);
54
55       if (!world->hide_from_contribs())
56       {
57         Savegame savegame(world->get_savegame_filename());
58         savegame.load();
59
60         if (world->is_levelset())
61         {
62           int level_count = 0;
63           int solved_count = 0;
64
65           const auto& state = savegame.get_levelset_state(world->get_basedir());
66           for(const auto& level_state : state.level_states)
67           {
68             if (level_state.solved)
69             {
70               solved_count += 1;
71             }
72             level_count += 1;
73           }
74
75           std::ostringstream title;
76           title << "[" << world->get_title() << "]";
77           if (level_count == 0)
78           {
79             title << " *NEW*";
80           }
81           else
82           {
83             title << " (" << solved_count << "/" << level_count << ")";
84           }
85           add_entry(i++, title.str());
86           m_contrib_worlds.push_back(std::move(world));
87         }
88         else if (world->is_worldmap())
89         {
90           int level_count = 0;
91           int solved_count = 0;
92
93           const auto& state = savegame.get_worldmap_state(world->get_worldmap_filename());
94           for(const auto& level_state : state.level_states)
95           {
96             if (level_state.solved)
97             {
98               solved_count += 1;
99             }
100             level_count += 1;
101           }
102
103           std::ostringstream title;
104           title << world->get_title();
105           if (level_count == 0)
106           {
107             title << " *NEW*";
108           }
109           else
110           {
111             title << " (" << solved_count << "/" << level_count << ")";
112           }
113           add_entry(i++, title.str());
114           m_contrib_worlds.push_back(std::move(world));
115         }
116         else
117         {
118           log_warning << "unknown World type" << std::endl;
119         }
120       }
121     }
122     catch(std::exception& e)
123     {
124       log_info << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl;
125     }
126   }
127
128   add_hl();
129   add_back(_("Back"));
130 }
131
132 ContribMenu::~ContribMenu()
133 {
134 }
135
136 void
137 ContribMenu::check_menu()
138 {
139   int index = check();
140   if (index != -1)
141   {
142     // reload the World so that we have something that we can safely
143     // std::move() around without wreaking the ContribMenu
144     std::unique_ptr<World> world = World::load(m_contrib_worlds[index]->get_basedir());
145     if (!world->is_levelset())
146     {
147       GameManager::current()->start_worldmap(std::move(world));
148     }
149     else
150     {
151       MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribLevelsetMenu(std::move(world))));
152     }
153   }
154 }
155
156 /* EOF */