From 3cdf2556213f08d021be1c39da9771f301cfc543 Mon Sep 17 00:00:00 2001 From: grumbel Date: Thu, 19 Nov 2009 22:27:34 +0000 Subject: [PATCH] Moved more menu related code into the Menu classes git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6059 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/gui/menu.hpp | 2 + src/supertux/game_session.cpp | 1 - src/supertux/menu/contrib_menu.cpp | 25 ++++++--- src/supertux/menu/contrib_menu.hpp | 6 +-- src/supertux/menu/game_menu.cpp | 6 +++ src/supertux/menu/game_menu.hpp | 2 + src/supertux/menu/joystick_menu.hpp | 1 + src/supertux/menu/keyboard_menu.hpp | 2 +- src/supertux/menu/language_menu.hpp | 4 +- src/supertux/menu/main_menu.cpp | 53 ++++++++++++++++++- src/supertux/menu/main_menu.hpp | 10 ++++ src/supertux/menu/menu_storage.cpp | 23 ++------ src/supertux/menu/menu_storage.hpp | 16 +++--- src/supertux/menu/options_menu.cpp | 6 +++ src/supertux/menu/options_menu.hpp | 3 +- src/supertux/menu/profile_menu.hpp | 2 + src/supertux/menu/worldmap_menu.cpp | 6 +++ src/supertux/menu/worldmap_menu.hpp | 2 + src/supertux/title_screen.cpp | 102 ++++++------------------------------ src/supertux/title_screen.hpp | 15 ++---- 20 files changed, 145 insertions(+), 142 deletions(-) diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index 7aae4f781..13f7e2206 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -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]); diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 94eedd53b..4cded8f6f 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -143,7 +143,6 @@ GameSession::~GameSession() delete capture_demo_stream; delete playback_demo_stream; delete demo_controller; - MenuStorage::free_options_menu(); } void diff --git a/src/supertux/menu/contrib_menu.cpp b/src/supertux/menu/contrib_menu.cpp index 092a94759..ada2e116f 100644 --- a/src/supertux/menu/contrib_menu.cpp +++ b/src/supertux/menu/contrib_menu.cpp @@ -18,6 +18,9 @@ #include +#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()); + } } } diff --git a/src/supertux/menu/contrib_menu.hpp b/src/supertux/menu/contrib_menu.hpp index 3c62d8d77..25e9fd308 100644 --- a/src/supertux/menu/contrib_menu.hpp +++ b/src/supertux/menu/contrib_menu.hpp @@ -19,20 +19,20 @@ #include "gui/menu.hpp" - +class ContribWorldMenu; class World; class ContribMenu : public Menu { private: + std::auto_ptr m_contrib_world_menu; std::vector 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&); diff --git a/src/supertux/menu/game_menu.cpp b/src/supertux/menu/game_menu.cpp index cb260032e..7b1bf6d3e 100644 --- a/src/supertux/menu/game_menu.cpp +++ b/src/supertux/menu/game_menu.cpp @@ -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 */ diff --git a/src/supertux/menu/game_menu.hpp b/src/supertux/menu/game_menu.hpp index b4f4799bd..0255f3721 100644 --- a/src/supertux/menu/game_menu.hpp +++ b/src/supertux/menu/game_menu.hpp @@ -32,6 +32,8 @@ private: public: GameMenu(const Level& level); + void check_menu(); + private: GameMenu(const GameMenu&); GameMenu& operator=(const GameMenu&); diff --git a/src/supertux/menu/joystick_menu.hpp b/src/supertux/menu/joystick_menu.hpp index d39fad787..6529563d8 100644 --- a/src/supertux/menu/joystick_menu.hpp +++ b/src/supertux/menu/joystick_menu.hpp @@ -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(); diff --git a/src/supertux/menu/keyboard_menu.hpp b/src/supertux/menu/keyboard_menu.hpp index f41204390..f4435d17a 100644 --- a/src/supertux/menu/keyboard_menu.hpp +++ b/src/supertux/menu/keyboard_menu.hpp @@ -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&); diff --git a/src/supertux/menu/language_menu.hpp b/src/supertux/menu/language_menu.hpp index cdaa6aa30..0c4fade6d 100644 --- a/src/supertux/menu/language_menu.hpp +++ b/src/supertux/menu/language_menu.hpp @@ -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 diff --git a/src/supertux/menu/main_menu.cpp b/src/supertux/menu/main_menu.cpp index 59b18321b..e498739ec 100644 --- a/src/supertux/menu/main_menu.cpp +++ b/src/supertux/menu/main_menu.cpp @@ -16,11 +16,23 @@ #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 */ diff --git a/src/supertux/menu/main_menu.hpp b/src/supertux/menu/main_menu.hpp index db065ccf6..0f900a4a5 100644 --- a/src/supertux/menu/main_menu.hpp +++ b/src/supertux/menu/main_menu.hpp @@ -19,6 +19,10 @@ #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 m_addon_menu; + std::auto_ptr m_contrib_menu; + std::auto_ptr m_main_world; + public: MainMenu(); + void check_menu(); + private: MainMenu(const MainMenu&); MainMenu& operator=(const MainMenu&); diff --git a/src/supertux/menu/menu_storage.cpp b/src/supertux/menu/menu_storage.cpp index a5025037e..ea1ae467c 100644 --- a/src/supertux/menu/menu_storage.cpp +++ b/src/supertux/menu/menu_storage.cpp @@ -22,40 +22,25 @@ #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() { diff --git a/src/supertux/menu/menu_storage.hpp b/src/supertux/menu/menu_storage.hpp index dabb47b4a..8ac6bf2a3 100644 --- a/src/supertux/menu/menu_storage.hpp +++ b/src/supertux/menu/menu_storage.hpp @@ -17,27 +17,25 @@ #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; diff --git a/src/supertux/menu/options_menu.cpp b/src/supertux/menu/options_menu.cpp index a930309dd..b43e5ce2d 100644 --- a/src/supertux/menu/options_menu.cpp +++ b/src/supertux/menu/options_menu.cpp @@ -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 */ diff --git a/src/supertux/menu/options_menu.hpp b/src/supertux/menu/options_menu.hpp index 78a3b42e1..473f3dd6d 100644 --- a/src/supertux/menu/options_menu.hpp +++ b/src/supertux/menu/options_menu.hpp @@ -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 language_menu; diff --git a/src/supertux/menu/profile_menu.hpp b/src/supertux/menu/profile_menu.hpp index ac301e0de..b98135de0 100644 --- a/src/supertux/menu/profile_menu.hpp +++ b/src/supertux/menu/profile_menu.hpp @@ -25,6 +25,8 @@ public: ProfileMenu(); void menu_action(MenuItem* item); + + void check_menu() {} }; #endif diff --git a/src/supertux/menu/worldmap_menu.cpp b/src/supertux/menu/worldmap_menu.cpp index 5dbef701d..4a751e460 100644 --- a/src/supertux/menu/worldmap_menu.cpp +++ b/src/supertux/menu/worldmap_menu.cpp @@ -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 */ diff --git a/src/supertux/menu/worldmap_menu.hpp b/src/supertux/menu/worldmap_menu.hpp index 142f7a148..6f0637242 100644 --- a/src/supertux/menu/worldmap_menu.hpp +++ b/src/supertux/menu/worldmap_menu.hpp @@ -30,6 +30,8 @@ private: public: WorldmapMenu(); + void check_menu(); + private: WorldmapMenu(const WorldmapMenu&); WorldmapMenu& operator=(const WorldmapMenu&); diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index 76598e7c8..d77e38169 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -42,13 +42,7 @@ #include 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(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; } } diff --git a/src/supertux/title_screen.hpp b/src/supertux/title_screen.hpp index 3758e222c..8156f3ec6 100644 --- a/src/supertux/title_screen.hpp +++ b/src/supertux/title_screen.hpp @@ -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 main_menu; - std::auto_ptr contrib_menu; - std::auto_ptr contrib_world_menu; - std::auto_ptr addons_menu; - - std::auto_ptr main_world; - std::vector contrib_worlds; - World* current_world; - std::auto_ptr frame; std::auto_ptr controller; std::auto_ptr titlesession; -- 2.11.0