From: Ingo Ruhnke Date: Thu, 19 Nov 2009 15:10:26 +0000 (+0000) Subject: Move some code from TitleScreen to ContribMenu X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=c73a530130ab85f54446517dca9d80713cbaaf7e;p=supertux.git Move some code from TitleScreen to ContribMenu SVN-Revision: 6047 --- diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 5761e19e3..3a7867944 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -428,9 +428,11 @@ Menu::update() int Menu::check() { - if (hit_item != -1) { + if (hit_item != -1) + { int id = items[hit_item]->id; - hit_item = -1; //Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib) + // Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib) + hit_item = -1; return id; } else diff --git a/src/supertux/menu/contrib_menu.cpp b/src/supertux/menu/contrib_menu.cpp index 39c8c817a..092a94759 100644 --- a/src/supertux/menu/contrib_menu.cpp +++ b/src/supertux/menu/contrib_menu.cpp @@ -16,12 +16,24 @@ #include "supertux/menu/contrib_menu.hpp" +#include + #include "supertux/world.hpp" #include "util/gettext.hpp" -ContribMenu::ContribMenu(const std::vector& level_worlds, - std::vector& contrib_worlds) +ContribMenu::ContribMenu() { + /** Generating contrib levels list by making use of Level Subset */ + std::vector level_worlds; + char** files = PHYSFS_enumerateFiles("levels/"); + for(const char* const* filename = files; *filename != 0; ++filename) { + std::string filepath = std::string("levels/") + *filename; + if(PHYSFS_isDirectory(filepath.c_str())) + level_worlds.push_back(filepath); + } + PHYSFS_freeList(files); + + add_label(_("Contrib Levels")); add_hl(); @@ -35,7 +47,7 @@ ContribMenu::ContribMenu(const std::vector& level_worlds, if (!world->hide_from_contribs) { add_entry(i++, world->title); - contrib_worlds.push_back(world.release()); + m_contrib_worlds.push_back(world.release()); } } catch(std::exception& e) @@ -48,4 +60,27 @@ ContribMenu::ContribMenu(const std::vector& level_worlds, add_back(_("Back")); } +ContribMenu::~ContribMenu() +{ + for(std::vector::iterator i = m_contrib_worlds.begin(); i != m_contrib_worlds.end(); ++i) + { + delete *i; + } + m_contrib_worlds.clear(); +} + +World* +ContribMenu::get_current_world() +{ + int index = check(); + if (index == -1) + { + return 0; + } + else + { + return m_contrib_worlds[index]; + } +} + /* EOF */ diff --git a/src/supertux/menu/contrib_menu.hpp b/src/supertux/menu/contrib_menu.hpp index 1cfbccc87..b25380b51 100644 --- a/src/supertux/menu/contrib_menu.hpp +++ b/src/supertux/menu/contrib_menu.hpp @@ -27,10 +27,15 @@ class World; class ContribMenu : public Menu { private: + std::vector m_contrib_worlds; + public: - ContribMenu(const std::vector& level_worlds, - std::vector& contrib_worlds); + ContribMenu(); + ~ContribMenu(); + /** FIXME: the method returns the current world only once, as it uses Menu::check() */ + World* get_current_world(); + private: ContribMenu(const ContribMenu&); ContribMenu& operator=(const ContribMenu&); diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index 1dedf8a5b..b1db0f7b0 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -74,39 +74,6 @@ TitleScreen::TitleScreen() : frame = std::auto_ptr(new Surface("images/engine/menu/frame.png")); } -void -TitleScreen::update_load_game_menu() -{ -} - -void -TitleScreen::free_contrib_menu() -{ - for(std::vector::iterator i = contrib_worlds.begin(); - i != contrib_worlds.end(); ++i) - delete *i; - - contrib_worlds.clear(); -} - -void -TitleScreen::generate_contrib_menu() -{ - /** Generating contrib levels list by making use of Level Subset */ - std::vector level_worlds; - char** files = PHYSFS_enumerateFiles("levels/"); - for(const char* const* filename = files; *filename != 0; ++filename) { - std::string filepath = std::string("levels/") + *filename; - if(PHYSFS_isDirectory(filepath.c_str())) - level_worlds.push_back(filepath); - } - PHYSFS_freeList(files); - - free_contrib_menu(); - contrib_menu.reset(new ContribMenu(level_worlds, - contrib_worlds)); -} - std::string TitleScreen::get_level_name(const std::string& filename) { @@ -131,17 +98,19 @@ TitleScreen::get_level_name(const std::string& filename) void TitleScreen::check_levels_contrib_menu() { - int index = contrib_menu->check(); - if (index == -1) - return; - - current_world = contrib_worlds[index]; - - if(!current_world->is_levelset) { - start_game(); - } else { - contrib_world_menu.reset(new ContribWorldMenu(*current_world)); - MenuManager::push_current(contrib_world_menu.get()); + current_world = contrib_menu->get_current_world(); + + if (current_world) + { + if (!current_world->is_levelset) + { + start_game(); + } + else + { + contrib_world_menu.reset(new ContribWorldMenu(*current_world)); + MenuManager::push_current(contrib_world_menu.get()); + } } } @@ -164,7 +133,6 @@ TitleScreen::generate_addons_menu() std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter); // (re)generate menu - free_addons_menu(); addons_menu.reset(new AddonMenu(addons)); } @@ -220,11 +188,6 @@ TitleScreen::check_addons_menu() } void -TitleScreen::free_addons_menu() -{ -} - -void TitleScreen::make_tux_jump() { static bool jumpWasReleased = true; @@ -330,7 +293,7 @@ TitleScreen::update(float elapsed_time) case MNID_LEVELS_CONTRIB: // Contrib Menu - generate_contrib_menu(); + contrib_menu.reset(new ContribMenu()); MenuManager::push_current(contrib_menu.get()); break; diff --git a/src/supertux/title_screen.hpp b/src/supertux/title_screen.hpp index 99f2f72cf..f955d7aab 100644 --- a/src/supertux/title_screen.hpp +++ b/src/supertux/title_screen.hpp @@ -21,10 +21,12 @@ #include "addon/addon.hpp" #include "supertux/game_session.hpp" -class Menu; +class CodeController; +class ContribMenu; class ContribWorldMenu; +class MainMenu; +class Menu; class World; -class CodeController; /** * Screen that displays the SuperTux logo, lets players start a new game, etc. @@ -48,19 +50,16 @@ public: private: void start_game(); void make_tux_jump(); - void update_load_game_menu(); void generate_main_menu(); - void generate_contrib_menu(); void check_levels_contrib_menu(); - void free_contrib_menu(); void generate_addons_menu(); void check_addons_menu(); - void free_addons_menu(); private: - std::auto_ptr main_menu; - std::auto_ptr contrib_menu; + std::auto_ptr main_menu; + std::auto_ptr contrib_menu; std::auto_ptr contrib_world_menu; + std::auto_ptr main_world; std::vector contrib_worlds; std::auto_ptr addons_menu;