00001 // SuperTux 00002 // Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de> 00003 // 00004 // This program is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #include "supertux/menu/contrib_menu.hpp" 00018 00019 #include <physfs.h> 00020 00021 #include "gui/menu_manager.hpp" 00022 #include "supertux/menu/contrib_world_menu.hpp" 00023 #include "supertux/title_screen.hpp" 00024 #include "supertux/world.hpp" 00025 #include "util/gettext.hpp" 00026 00027 ContribMenu::ContribMenu() : 00028 m_contrib_world_menu(), 00029 m_contrib_worlds() 00030 { 00032 std::vector<std::string> level_worlds; 00033 char** files = PHYSFS_enumerateFiles("levels/"); 00034 for(const char* const* filename = files; *filename != 0; ++filename) { 00035 std::string filepath = std::string("levels/") + *filename; 00036 if(PHYSFS_isDirectory(filepath.c_str())) 00037 level_worlds.push_back(filepath); 00038 } 00039 PHYSFS_freeList(files); 00040 00041 add_label(_("Contrib Levels")); 00042 add_hl(); 00043 00044 int i = 0; 00045 for (std::vector<std::string>::const_iterator it = level_worlds.begin(); it != level_worlds.end(); ++it) 00046 { 00047 try 00048 { 00049 std::auto_ptr<World> world (new World()); 00050 world->load(*it + "/info"); 00051 if (!world->hide_from_contribs) 00052 { 00053 add_entry(i++, world->get_title()); 00054 m_contrib_worlds.push_back(world.release()); 00055 } 00056 } 00057 catch(std::exception& e) 00058 { 00059 log_info << "Couldn't parse levelset info for '" << *it << "': " << e.what() << std::endl; 00060 } 00061 } 00062 00063 add_hl(); 00064 add_back(_("Back")); 00065 } 00066 00067 ContribMenu::~ContribMenu() 00068 { 00069 for(std::vector<World*>::iterator i = m_contrib_worlds.begin(); i != m_contrib_worlds.end(); ++i) 00070 { 00071 delete *i; 00072 } 00073 m_contrib_worlds.clear(); 00074 } 00075 00076 void 00077 ContribMenu::check_menu() 00078 { 00079 int index = check(); 00080 if (index != -1) 00081 { 00082 World* world = m_contrib_worlds[index]; 00083 00084 if (!world->is_levelset) 00085 { 00086 TitleScreen::start_game(world); 00087 } 00088 else 00089 { 00090 m_contrib_world_menu.reset(new ContribWorldMenu(*world)); 00091 MenuManager::push_current(m_contrib_world_menu.get()); 00092 } 00093 } 00094 } 00095 00096 /* EOF */