From f7be36d821fb67ece9bdc1a4003016e9e4431e63 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Fri, 8 Aug 2014 04:02:52 +0200 Subject: [PATCH] Moved menu code frome GameSession and Worldmap into the proper Menu::check_menu() calls --- src/gui/menu.hpp | 8 +++---- src/supertux/game_session.cpp | 42 +++++++++++++++---------------------- src/supertux/game_session.hpp | 2 +- src/supertux/menu/game_menu.cpp | 14 +++++++++++++ src/supertux/menu/game_menu.hpp | 2 +- src/supertux/menu/worldmap_menu.cpp | 12 +++++++++++ src/supertux/menu/worldmap_menu.hpp | 2 +- src/worldmap/worldmap.cpp | 16 +++----------- 8 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/gui/menu.hpp b/src/gui/menu.hpp index 49405b61c..302750684 100644 --- a/src/gui/menu.hpp +++ b/src/gui/menu.hpp @@ -74,10 +74,6 @@ public: /** Remove all entries from the menu */ void clear(); - /** Return the index of the menu item that was 'hit' (ie. the user - clicked on it) in the last event() call */ - int check (); - virtual void check_menu() =0; MenuItem& get_item(int index) @@ -100,6 +96,10 @@ public: void set_toggled(int id, bool toggled); protected: + /** Return the index of the menu item that was 'hit' (ie. the user + clicked on it) in the last event() call */ + int check (); + MenuItem* add_item(std::unique_ptr menu_item); float get_width() const; float get_height() const; diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 0a83e9d8e..2b57b040c 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -256,6 +256,18 @@ GameSession::toggle_pause() } void +GameSession::abort_level() +{ + MenuManager::instance().set_current(0); + g_screen_manager->exit_screen(); + currentsector->player->set_bonus(bonus_at_start); + PlayerStatus *currentStatus = get_player_status(); + currentStatus->coins = coins_at_start; + currentStatus->max_fire_bullets = max_fire_bullets_at_start; + currentStatus->max_ice_bullets = max_ice_bullets_at_start; +} + +void GameSession::set_editmode(bool edit_mode) { if (this->edit_mode == edit_mode) return; @@ -391,30 +403,6 @@ GameSession::draw_pause(DrawingContext& context) } void -GameSession::process_menu() -{ - Menu* menu = MenuManager::instance().current(); - if(menu) { - if(menu == game_menu.get()) { - switch (game_menu->check()) { - case MNID_CONTINUE: - MenuManager::instance().set_current(0); - toggle_pause(); - break; - case MNID_ABORTLEVEL: - MenuManager::instance().set_current(0); - g_screen_manager->exit_screen(); - currentsector->player->set_bonus(bonus_at_start); - PlayerStatus *currentStatus = get_player_status(); - currentStatus->coins = coins_at_start; - currentStatus->max_fire_bullets = max_fire_bullets_at_start; - currentStatus->max_ice_bullets = max_ice_bullets_at_start; - } - } - } -} - -void GameSession::setup() { if (currentsector == NULL) @@ -440,7 +428,11 @@ GameSession::update(float elapsed_time) on_escape_press(); process_events(); - process_menu(); + Menu* menu = MenuManager::instance().current(); + if (menu && menu == game_menu.get()) + { + menu->check_menu(); + } // Unpause the game if the menu has been closed if (game_pause && !MenuManager::instance().current()) { diff --git a/src/supertux/game_session.hpp b/src/supertux/game_session.hpp index cdad31935..87a419272 100644 --- a/src/supertux/game_session.hpp +++ b/src/supertux/game_session.hpp @@ -83,6 +83,7 @@ public: int restart_level(); void toggle_pause(); + void abort_level(); /** * Enters or leaves level editor mode @@ -104,7 +105,6 @@ private: HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename); void on_escape_press(); - void process_menu(); std::unique_ptr level; SurfacePtr statistics_backdrop; diff --git a/src/supertux/menu/game_menu.cpp b/src/supertux/menu/game_menu.cpp index 1bdc59ba8..13deef988 100644 --- a/src/supertux/menu/game_menu.cpp +++ b/src/supertux/menu/game_menu.cpp @@ -16,6 +16,9 @@ #include "supertux/menu/game_menu.hpp" +#include "gui/menu_manager.hpp" +#include "supertux/game_session.hpp" +#include "supertux/screen_manager.hpp" #include "supertux/level.hpp" #include "supertux/menu/menu_storage.hpp" #include "supertux/menu/options_menu.hpp" @@ -34,6 +37,17 @@ GameMenu::GameMenu(const Level& level) void GameMenu::check_menu() { + switch (check()) + { + case MNID_CONTINUE: + MenuManager::instance().set_current(0); + GameSession::current()->toggle_pause(); + break; + + case MNID_ABORTLEVEL: + GameSession::current()->abort_level(); + break; + } } /* EOF */ diff --git a/src/supertux/menu/game_menu.hpp b/src/supertux/menu/game_menu.hpp index 0255f3721..c325926c6 100644 --- a/src/supertux/menu/game_menu.hpp +++ b/src/supertux/menu/game_menu.hpp @@ -32,7 +32,7 @@ private: public: GameMenu(const Level& level); - void check_menu(); + void check_menu() override; private: GameMenu(const GameMenu&); diff --git a/src/supertux/menu/worldmap_menu.cpp b/src/supertux/menu/worldmap_menu.cpp index 2334edbd7..29b877700 100644 --- a/src/supertux/menu/worldmap_menu.cpp +++ b/src/supertux/menu/worldmap_menu.cpp @@ -16,8 +16,10 @@ #include "supertux/menu/worldmap_menu.hpp" +#include "gui/menu_manager.hpp" #include "supertux/menu/menu_storage.hpp" #include "supertux/menu/options_menu.hpp" +#include "supertux/screen_manager.hpp" #include "util/gettext.hpp" WorldmapMenu::WorldmapMenu() @@ -33,6 +35,16 @@ WorldmapMenu::WorldmapMenu() void WorldmapMenu::check_menu() { + switch (check()) + { + case MNID_RETURNWORLDMAP: // Return to game + MenuManager::instance().set_current(0); + break; + + case MNID_QUITWORLDMAP: // Quit Worldmap + g_screen_manager->exit_screen(); + break; + } } /* EOF */ diff --git a/src/supertux/menu/worldmap_menu.hpp b/src/supertux/menu/worldmap_menu.hpp index 6f0637242..219abc278 100644 --- a/src/supertux/menu/worldmap_menu.hpp +++ b/src/supertux/menu/worldmap_menu.hpp @@ -30,7 +30,7 @@ private: public: WorldmapMenu(); - void check_menu(); + void check_menu() override; private: WorldmapMenu(const WorldmapMenu&); diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index b974db816..f21f854cc 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -570,19 +570,9 @@ WorldMap::update(float delta) { if(!in_level) { Menu* menu = MenuManager::instance().current(); - if(menu != NULL) { - if(menu == worldmap_menu.get()) { - switch (worldmap_menu->check()) - { - case MNID_RETURNWORLDMAP: // Return to game - MenuManager::instance().set_current(0); - break; - case MNID_QUITWORLDMAP: // Quit Worldmap - g_screen_manager->exit_screen(); - break; - } - } - + if (menu && menu == worldmap_menu.get()) + { + menu->check_menu(); return; } -- 2.11.0