Move some code from TitleScreen to ContribMenu
authorgrumbel <grumbel@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Thu, 19 Nov 2009 15:10:26 +0000 (15:10 +0000)
committergrumbel <grumbel@837edb03-e0f3-0310-88ca-d4d4e8b29345>
Thu, 19 Nov 2009 15:10:26 +0000 (15:10 +0000)
git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6047 837edb03-e0f3-0310-88ca-d4d4e8b29345

src/gui/menu.cpp
src/supertux/menu/contrib_menu.cpp
src/supertux/menu/contrib_menu.hpp
src/supertux/title_screen.cpp
src/supertux/title_screen.hpp

index 5761e19..3a78679 100644 (file)
@@ -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
index 39c8c81..092a947 100644 (file)
 
 #include "supertux/menu/contrib_menu.hpp"
 
+#include <physfs.h>
+
 #include "supertux/world.hpp"
 #include "util/gettext.hpp"
 
-ContribMenu::ContribMenu(const std::vector<std::string>& level_worlds,
-                         std::vector<World*>& contrib_worlds)
+ContribMenu::ContribMenu()
 {
+  /** Generating contrib levels list by making use of Level Subset  */
+  std::vector<std::string> 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<std::string>& 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<std::string>& level_worlds,
   add_back(_("Back"));
 }
 
+ContribMenu::~ContribMenu()
+{
+  for(std::vector<World*>::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 */
index 1cfbccc..b25380b 100644 (file)
@@ -27,10 +27,15 @@ class World;
 class ContribMenu : public Menu
 {
 private:
+  std::vector<World*> m_contrib_worlds;
+
 public:
-  ContribMenu(const std::vector<std::string>& level_worlds,
-              std::vector<World*>& 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&);
index 1dedf8a..b1db0f7 100644 (file)
@@ -74,39 +74,6 @@ TitleScreen::TitleScreen() :
   frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
 }
 
-void
-TitleScreen::update_load_game_menu()
-{
-}
-
-void
-TitleScreen::free_contrib_menu()
-{
-  for(std::vector<World*>::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<std::string> 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;
 
index 99f2f72..f955d7a 100644 (file)
 #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<Menu> main_menu;
-  std::auto_ptr<Menu> contrib_menu;
+  std::auto_ptr<MainMenu>         main_menu;
+  std::auto_ptr<ContribMenu>      contrib_menu;
   std::auto_ptr<ContribWorldMenu> contrib_world_menu;
+
   std::auto_ptr<World> main_world;
   std::vector<World*> contrib_worlds;
   std::auto_ptr<Menu> addons_menu;