Moved more menu related code into the Menu classes
authorIngo Ruhnke <grumbel@gmx.de>
Thu, 19 Nov 2009 22:27:34 +0000 (22:27 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Thu, 19 Nov 2009 22:27:34 +0000 (22:27 +0000)
SVN-Revision: 6059

20 files changed:
src/gui/menu.hpp
src/supertux/game_session.cpp
src/supertux/menu/contrib_menu.cpp
src/supertux/menu/contrib_menu.hpp
src/supertux/menu/game_menu.cpp
src/supertux/menu/game_menu.hpp
src/supertux/menu/joystick_menu.hpp
src/supertux/menu/keyboard_menu.hpp
src/supertux/menu/language_menu.hpp
src/supertux/menu/main_menu.cpp
src/supertux/menu/main_menu.hpp
src/supertux/menu/menu_storage.cpp
src/supertux/menu/menu_storage.hpp
src/supertux/menu/options_menu.cpp
src/supertux/menu/options_menu.hpp
src/supertux/menu/profile_menu.hpp
src/supertux/menu/worldmap_menu.cpp
src/supertux/menu/worldmap_menu.hpp
src/supertux/title_screen.cpp
src/supertux/title_screen.hpp

index 7aae4f7..13f7e22 100644 (file)
@@ -76,6 +76,8 @@ public:
       clicked on it) in the last event() call */
   int check ();
 
+  virtual void check_menu() =0;
+
   MenuItem& get_item(int index)
   {
     return *(items[index]);
index 94eedd5..4cded8f 100644 (file)
@@ -143,7 +143,6 @@ GameSession::~GameSession()
   delete capture_demo_stream;
   delete playback_demo_stream;
   delete demo_controller;
-  MenuStorage::free_options_menu();
 }
 
 void
index 092a947..ada2e11 100644 (file)
@@ -18,6 +18,9 @@
 
 #include <physfs.h>
 
+#include "gui/menu_manager.hpp"
+#include "supertux/menu/contrib_world_menu.hpp"
+#include "supertux/title_screen.hpp"
 #include "supertux/world.hpp"
 #include "util/gettext.hpp"
 
@@ -69,17 +72,23 @@ ContribMenu::~ContribMenu()
   m_contrib_worlds.clear();
 }
 
-World*
-ContribMenu::get_current_world()
+void
+ContribMenu::check_menu()
 {
   int index = check();
-  if (index == -1)
+  if (index != -1)
   {
-    return 0;
-  }
-  else
-  {
-    return m_contrib_worlds[index];
+    World* world = m_contrib_worlds[index];
+    
+    if (!world->is_levelset) 
+    {
+      TitleScreen::start_game(world);
+    }
+    else 
+    {
+      m_contrib_world_menu.reset(new ContribWorldMenu(*world));
+      MenuManager::push_current(m_contrib_world_menu.get());
+    }
   }
 }
 
index 3c62d8d..25e9fd3 100644 (file)
 
 #include "gui/menu.hpp"
 
-
+class ContribWorldMenu;
 class World;
 
 class ContribMenu : public Menu
 {
 private:
+  std::auto_ptr<ContribWorldMenu> m_contrib_world_menu;
   std::vector<World*> m_contrib_worlds;
 
 public:
   ContribMenu();
   ~ContribMenu();
   
-  /** FIXME: the method returns the current world only once, as it uses Menu::check() */
-  World* get_current_world();
+  void check_menu();
 
 private:
   ContribMenu(const ContribMenu&);
index cb26003..7b1bf6d 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "supertux/level.hpp"
 #include "supertux/menu/menu_storage.hpp"
+#include "supertux/menu/options_menu.hpp"
 #include "util/gettext.hpp"
 
 GameMenu::GameMenu(const Level& level)
@@ -30,4 +31,9 @@ GameMenu::GameMenu(const Level& level)
   add_entry(MNID_ABORTLEVEL, _("Abort Level"));
 }
 
+void
+GameMenu::check_menu()
+{
+}
+
 /* EOF */
index b4f4799..0255f37 100644 (file)
@@ -32,6 +32,8 @@ private:
 public:
   GameMenu(const Level& level);
 
+  void check_menu();
+
 private:
   GameMenu(const GameMenu&);
   GameMenu& operator=(const GameMenu&);
index d39fad7..6529563 100644 (file)
@@ -32,6 +32,7 @@ public:
   void update_menu_item(Controller::Control id);
   virtual void menu_action(MenuItem* item);
   JoystickKeyboardController* controller;
+  void check_menu() {}
 
 private:
   void recreateMenu();
index f412043..f4435d1 100644 (file)
@@ -31,7 +31,7 @@ public:
   std::string get_key_name(SDLKey key);
   virtual void menu_action(MenuItem* item);
   JoystickKeyboardController* controller;
-
+  void check_menu() {}
 private:
   KeyboardMenu(const KeyboardMenu&);
   KeyboardMenu& operator=(const KeyboardMenu&);
index cdaa6aa..0c4fade 100644 (file)
@@ -18,7 +18,6 @@
 #ifndef HEADER_SUPERTUX_SUPERTUX_LANGUAGE_MENU_HPP
 #define HEADER_SUPERTUX_SUPERTUX_LANGUAGE_MENU_HPP
 
-
 #include "util/gettext.hpp"
 #include "gui/menu.hpp"
 
@@ -27,7 +26,8 @@ class LanguageMenu : public Menu
 public:
   LanguageMenu();
 
-  virtual void menu_action(MenuItem* item);
+  void menu_action(MenuItem* item);
+  void check_menu() {}
 };
 
 #endif
index 59b1832..e498739 100644 (file)
 
 #include "supertux/menu/main_menu.hpp"
 
+#include "audio/sound_manager.hpp"
+#include "gui/menu_manager.hpp"
+#include "supertux/fadeout.hpp"
 #include "supertux/globals.hpp"
 #include "supertux/menu/menu_storage.hpp"
+#include "supertux/menu/addon_menu.hpp"
+#include "supertux/menu/options_menu.hpp"
+#include "supertux/menu/contrib_menu.hpp"
+#include "supertux/screen_manager.hpp"
+#include "supertux/textscroller.hpp"
+#include "supertux/title_screen.hpp"
+#include "supertux/world.hpp"
 #include "util/gettext.hpp"
 
-MainMenu::MainMenu()
+MainMenu::MainMenu() :
+  m_addon_menu(),
+  m_contrib_menu()
 {
   set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 35);
   add_entry(MNID_STARTGAME, _("Start Game"));
@@ -31,4 +43,43 @@ MainMenu::MainMenu()
   add_entry(MNID_QUITMAINMENU, _("Quit"));
 }
 
+void
+MainMenu::check_menu()
+{
+  switch (check())
+  {
+    case MNID_STARTGAME:
+      if (m_main_world.get() == NULL)
+      {
+        m_main_world.reset(new World());
+        m_main_world->load("levels/world1/info");
+      }
+      TitleScreen::start_game(m_main_world.get());
+      break;
+
+    case MNID_LEVELS_CONTRIB:
+      // Contrib Menu
+      m_contrib_menu.reset(new ContribMenu());
+      MenuManager::push_current(m_contrib_menu.get());
+      break;
+
+    case MNID_ADDONS:
+      // Add-ons Menu
+      m_addon_menu.reset(new AddonMenu());
+      MenuManager::push_current(m_addon_menu.get());
+      break;
+
+    case MNID_CREDITS:
+      MenuManager::set_current(NULL);
+      g_screen_manager->push_screen(new TextScroller("credits.txt"),
+                                    new FadeOut(0.5));
+      break;
+
+    case MNID_QUITMAINMENU:
+      g_screen_manager->quit(new FadeOut(0.25));
+      sound_manager->stop_music(0.25);
+      break;
+  }
+}
+
 /* EOF */
index db065cc..0f900a4 100644 (file)
 
 #include "gui/menu.hpp"
 
+class AddonMenu;
+class ContribMenu;
+class World;
+
 enum MainMenuIDs {
   MNID_STARTGAME,
   MNID_LEVELS_CONTRIB,
@@ -32,9 +36,15 @@ enum MainMenuIDs {
 class MainMenu : public Menu
 {
 private:
+  std::auto_ptr<AddonMenu>   m_addon_menu;
+  std::auto_ptr<ContribMenu> m_contrib_menu;
+  std::auto_ptr<World>       m_main_world;
+
 public:
   MainMenu();
 
+  void check_menu();
+
 private:
   MainMenu(const MainMenu&);
   MainMenu& operator=(const MainMenu&);
index a502503..ea1ae46 100644 (file)
 #include "supertux/menu/keyboard_menu.hpp"
 #include "supertux/globals.hpp"
 
-Menu* MenuStorage::options_menu = 0;
-Menu* MenuStorage::profile_menu = 0;
+OptionsMenu*  MenuStorage::options_menu = 0;
+ProfileMenu*  MenuStorage::profile_menu = 0;
 KeyboardMenu* MenuStorage::key_options_menu = 0;
 JoystickMenu* MenuStorage::joystick_options_menu = 0;
 
-Menu*
+OptionsMenu*
 MenuStorage::get_options_menu()
 {
   options_menu = new OptionsMenu();
   return options_menu;
 }
 
-void
-MenuStorage::free_options_menu()
-{
-  delete options_menu;
-  options_menu = 0;
-}
-
-Menu*
+ProfileMenu*
 MenuStorage::get_profile_menu()
 {
   profile_menu = new ProfileMenu();
   return profile_menu;
 }
 
-void
-MenuStorage::free_profile_menu()
-{
-  delete profile_menu;
-  profile_menu = 0;
-}
-
-
 KeyboardMenu*
 MenuStorage::get_key_options_menu()
 {
index dabb47b..8ac6bf2 100644 (file)
 #ifndef HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP
 #define HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP
 
-class Menu;
 class JoystickMenu;
 class KeyboardMenu;
+class Menu;
+class OptionsMenu;
+class ProfileMenu;
 
 class MenuStorage
 {
 public:
   MenuStorage();
 
-  static Menu* get_options_menu();
-  static void  free_options_menu();
-
-  static Menu* get_profile_menu();
-  static void  free_profile_menu();
-
+  static OptionsMenu*  get_options_menu();
+  static ProfileMenu*  get_profile_menu();
   static KeyboardMenu* get_key_options_menu();
   static JoystickMenu* get_joystick_options_menu();
 
 private:
-  static Menu* options_menu;
-  static Menu* profile_menu;
+  static OptionsMenu*  options_menu;
+  static ProfileMenu*  profile_menu;
   static KeyboardMenu* key_options_menu;
   static JoystickMenu* joystick_options_menu;
 
index a930309..b43e5ce 100644 (file)
@@ -24,6 +24,7 @@
 #include "supertux/menu/keyboard_menu.hpp"
 #include "supertux/menu/language_menu.hpp"
 #include "supertux/menu/menu_storage.hpp"
+#include "supertux/menu/profile_menu.hpp"
 #include "video/renderer.hpp"
 
 enum OptionsMenuIDs {
@@ -236,4 +237,9 @@ OptionsMenu::menu_action(MenuItem* item)
   }
 }
 
+void
+OptionsMenu::check_menu()
+{
+}
+
 /* EOF */
index 78a3b42..473f3dd 100644 (file)
@@ -29,7 +29,8 @@ public:
   OptionsMenu();
   virtual ~OptionsMenu();
 
-  virtual void menu_action(MenuItem* item);
+  void menu_action(MenuItem* item);
+  void check_menu();
 
 protected:
   std::auto_ptr<LanguageMenu> language_menu; 
index ac301e0..b98135d 100644 (file)
@@ -25,6 +25,8 @@ public:
   ProfileMenu();
 
   void menu_action(MenuItem* item);
+
+  void check_menu() {}
 };
 
 #endif
index 5dbef70..4a751e4 100644 (file)
@@ -17,6 +17,7 @@
 #include "supertux/menu/worldmap_menu.hpp"
 
 #include "supertux/menu/menu_storage.hpp"
+#include "supertux/menu/options_menu.hpp"
 #include "util/gettext.hpp"
 
 WorldmapMenu::WorldmapMenu()
@@ -29,4 +30,9 @@ WorldmapMenu::WorldmapMenu()
   add_entry(MNID_QUITWORLDMAP, _("Quit World"));
 }
 
+void
+WorldmapMenu::check_menu()
+{
+}
+
 /* EOF */
index 142f7a1..6f06372 100644 (file)
@@ -30,6 +30,8 @@ private:
 public:
   WorldmapMenu();
 
+  void check_menu();
+
 private:
   WorldmapMenu(const WorldmapMenu&);
   WorldmapMenu& operator=(const WorldmapMenu&);
index 76598e7..d77e381 100644 (file)
 #include <version.h>
 
 TitleScreen::TitleScreen() :
-  main_menu(),
-  contrib_menu(),
-  contrib_world_menu(),
-  addons_menu(),
-  main_world(),
-  contrib_worlds(),
-  current_world(),
+  main_menu(new MainMenu()),
   frame(),
   controller(),
   titlesession()
@@ -60,8 +54,6 @@ TitleScreen::TitleScreen() :
   player->set_controller(controller.get());
   player->set_speedlimit(230); //MAX_WALK_XM
 
-  generate_main_menu();
-
   frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
 }
 
@@ -87,25 +79,6 @@ TitleScreen::get_level_name(const std::string& filename)
 }
 
 void
-TitleScreen::check_levels_contrib_menu()
-{
-  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());
-    }
-  }
-}
-
-void
 TitleScreen::make_tux_jump()
 {
   static bool jumpWasReleased = true;
@@ -133,12 +106,6 @@ TitleScreen::make_tux_jump()
   }
 }
 
-void
-TitleScreen::generate_main_menu()
-{
-  main_menu.reset(new MainMenu());
-}
-
 TitleScreen::~TitleScreen()
 {
 }
@@ -195,75 +162,38 @@ TitleScreen::update(float elapsed_time)
 
   make_tux_jump();
 
-  Menu* menu = MenuManager::current();
-  if(menu) {
-    if(menu == main_menu.get()) {
-      switch (main_menu->check()) {
-        case MNID_STARTGAME:
-          // Start Game, ie. goto the slots menu
-          if(main_world.get() == NULL) {
-            main_world.reset(new World());
-            main_world->load("levels/world1/info");
-          }
-          current_world = main_world.get();
-          start_game();
-          break;
-
-        case MNID_LEVELS_CONTRIB:
-          // Contrib Menu
-          contrib_menu.reset(new ContribMenu());
-          MenuManager::push_current(contrib_menu.get());
-          break;
-
-        case MNID_ADDONS:
-          // Add-ons Menu
-          addons_menu.reset(new AddonMenu());
-          MenuManager::push_current(addons_menu.get());
-          break;
-
-        case MNID_CREDITS:
-          MenuManager::set_current(NULL);
-          g_screen_manager->push_screen(new TextScroller("credits.txt"),
-                                   new FadeOut(0.5));
-          break;
-
-        case MNID_QUITMAINMENU:
-          g_screen_manager->quit(new FadeOut(0.25));
-          sound_manager->stop_music(0.25);
-          break;
-      }
-    } else if(menu == contrib_menu.get()) {
-      check_levels_contrib_menu();
-    } else if(menu == addons_menu.get()) {
-      addons_menu->check_menu();
-    } else if (menu == contrib_world_menu.get()) {
-      contrib_world_menu->check_menu();
-    }
+  if (Menu* menu = MenuManager::current())
+  {
+    menu->check_menu();
   }
 
   // reopen menu if user closed it (so that the app doesn't close when user
   // accidently hit ESC)
-  if(MenuManager::current() == 0 && g_screen_manager->has_no_pending_fadeout()) {
-    generate_main_menu();
+  if(MenuManager::current() == 0 && g_screen_manager->has_no_pending_fadeout()) 
+  {
     MenuManager::set_current(main_menu.get());
   }
 }
 
 void
-TitleScreen::start_game()
+TitleScreen::start_game(World* world)
 {
   MenuManager::set_current(NULL);
-  std::string basename = current_world->get_basedir();
+
+  std::string basename = world->get_basedir();
   basename = basename.substr(0, basename.length()-1);
   std::string worlddirname = FileSystem::basename(basename);
   std::ostringstream stream;
   stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
   std::string slotfile = stream.str();
 
-  try {
-    current_world->set_savegame_filename(slotfile);
-    current_world->run();
-  } catch(std::exception& e) {
+  try 
+  {
+    world->set_savegame_filename(slotfile);
+    world->run();
+  } 
+  catch(std::exception& e) 
+  {
     log_fatal << "Couldn't start world: " << e.what() << std::endl;
   }
 }
index 3758e22..8156f3e 100644 (file)
@@ -47,22 +47,15 @@ public:
 
   virtual void update(float elapsed_time);
 
+public:
+  static void start_game(World* world);
+
 private:
-  void start_game();
   void make_tux_jump();
   void generate_main_menu();
-  void check_levels_contrib_menu();
-
+  
 private:
   std::auto_ptr<MainMenu>         main_menu;
-  std::auto_ptr<ContribMenu>      contrib_menu;
-  std::auto_ptr<ContribWorldMenu> contrib_world_menu;
-  std::auto_ptr<AddonMenu>        addons_menu;
-
-  std::auto_ptr<World> main_world;
-  std::vector<World*> contrib_worlds;
-  World* current_world;
-
   std::auto_ptr<Surface> frame;
   std::auto_ptr<CodeController> controller;
   std::auto_ptr<GameSession> titlesession;