From bc8d1cc5b511c8d1e6ed4d7d94f0362dc5b38bd0 Mon Sep 17 00:00:00 2001 From: grumbel Date: Thu, 19 Nov 2009 00:52:29 +0000 Subject: [PATCH] Added MenuManager to keep track of created Menus git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6038 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/supertux/game_session.cpp | 5 ++-- src/supertux/menu/menu_manager.cpp | 53 ++++++++++++++++++++++++++++++++++++++ src/supertux/menu/menu_manager.hpp | 44 +++++++++++++++++++++++++++++++ src/supertux/menu/options_menu.cpp | 24 ++++------------- src/supertux/menu/options_menu.hpp | 6 +---- src/supertux/menu/profile_menu.cpp | 50 ++++++++++++----------------------- src/supertux/menu/profile_menu.hpp | 12 ++++++--- src/supertux/title_screen.cpp | 3 ++- src/worldmap/worldmap.cpp | 3 ++- 9 files changed, 135 insertions(+), 65 deletions(-) create mode 100644 src/supertux/menu/menu_manager.cpp create mode 100644 src/supertux/menu/menu_manager.hpp diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 65336e3c6..79b5aca17 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -34,6 +34,7 @@ #include "supertux/levelintro.hpp" #include "supertux/globals.hpp" #include "supertux/mainloop.hpp" +#include "supertux/menu/menu_manager.hpp" #include "supertux/menu/options_menu.hpp" #include "supertux/sector.hpp" #include "util/file_system.hpp" @@ -83,7 +84,7 @@ GameSession::GameSession(const std::string& levelfile_, Statistics* statistics) game_menu->add_label(level->name); game_menu->add_hl(); game_menu->add_entry(MNID_CONTINUE, _("Continue")); - game_menu->add_submenu(_("Options"), get_options_menu()); + game_menu->add_submenu(_("Options"), MenuManager::get_options_menu()); game_menu->add_hl(); game_menu->add_entry(MNID_ABORTLEVEL, _("Abort Level")); } @@ -151,7 +152,7 @@ GameSession::~GameSession() delete capture_demo_stream; delete playback_demo_stream; delete demo_controller; - free_options_menu(); + MenuManager::free_options_menu(); } void diff --git a/src/supertux/menu/menu_manager.cpp b/src/supertux/menu/menu_manager.cpp new file mode 100644 index 000000000..1c9f0bc7c --- /dev/null +++ b/src/supertux/menu/menu_manager.cpp @@ -0,0 +1,53 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "supertux/menu/menu_manager.hpp" + +#include "supertux/menu/options_menu.hpp" +#include "supertux/menu/profile_menu.hpp" + +Menu* MenuManager::options_menu = 0; +Menu* MenuManager::profile_menu = 0; + +Menu* +MenuManager::get_options_menu() +{ + options_menu = new OptionsMenu(); + return options_menu; +} + +void +MenuManager::free_options_menu() +{ + delete options_menu; + options_menu = 0; +} + +Menu* +MenuManager::get_profile_menu() +{ + profile_menu = new ProfileMenu(); + return profile_menu; +} + +void +MenuManager::free_profile_menu() +{ + delete profile_menu; + profile_menu = 0; +} + +/* EOF */ diff --git a/src/supertux/menu/menu_manager.hpp b/src/supertux/menu/menu_manager.hpp new file mode 100644 index 000000000..435421d71 --- /dev/null +++ b/src/supertux/menu/menu_manager.hpp @@ -0,0 +1,44 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_SUPERTUX_MENU_MENU_MANAGER_HPP +#define HEADER_SUPERTUX_SUPERTUX_MENU_MENU_MANAGER_HPP + +class Menu; + +class MenuManager +{ +public: + MenuManager(); + + static Menu* get_options_menu(); + static void free_options_menu(); + + static Menu* get_profile_menu(); + static void free_profile_menu(); + +private: + static Menu* options_menu; + static Menu* profile_menu; + +private: + MenuManager(const MenuManager&); + MenuManager& operator=(const MenuManager&); +}; + +#endif + +/* EOF */ diff --git a/src/supertux/menu/options_menu.cpp b/src/supertux/menu/options_menu.cpp index 344b38d07..1bebe5938 100644 --- a/src/supertux/menu/options_menu.cpp +++ b/src/supertux/menu/options_menu.cpp @@ -26,11 +26,10 @@ #include "supertux/main.hpp" #include "supertux/menu/profile_menu.hpp" #include "supertux/menu/language_menu.hpp" +#include "supertux/menu/menu_manager.hpp" #include "util/gettext.hpp" #include "video/renderer.hpp" -Menu* options_menu = 0; - enum OptionsMenuIDs { MNID_FULLSCREEN, MNID_FULLSCREEN_RESOLUTION, @@ -54,7 +53,7 @@ OptionsMenu::OptionsMenu() : add_submenu(_("Select Language"), language_menu.get()) ->set_help(_("Select a different language to display text in")); - add_submenu(_("Select Profile"), get_profile_menu()) + add_submenu(_("Select Profile"), MenuManager::get_profile_menu()) ->set_help(_("Select a profile to play with")); add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled) @@ -212,7 +211,7 @@ OptionsMenu::menu_action(MenuItem* item) break; case MNID_FULLSCREEN: - if(g_config->use_fullscreen != options_menu->is_toggled(MNID_FULLSCREEN)) { + if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) { g_config->use_fullscreen = !g_config->use_fullscreen; init_video(); // FIXME: Should call apply_config instead Menu::recalc_pos(); @@ -221,7 +220,7 @@ OptionsMenu::menu_action(MenuItem* item) break; case MNID_SOUND: - if(g_config->sound_enabled != options_menu->is_toggled(MNID_SOUND)) { + if(g_config->sound_enabled != is_toggled(MNID_SOUND)) { g_config->sound_enabled = !g_config->sound_enabled; sound_manager->enable_sound(g_config->sound_enabled); g_config->save(); @@ -229,7 +228,7 @@ OptionsMenu::menu_action(MenuItem* item) break; case MNID_MUSIC: - if(g_config->music_enabled != options_menu->is_toggled(MNID_MUSIC)) { + if(g_config->music_enabled != is_toggled(MNID_MUSIC)) { g_config->music_enabled = !g_config->music_enabled; sound_manager->enable_music(g_config->music_enabled); g_config->save(); @@ -241,17 +240,4 @@ OptionsMenu::menu_action(MenuItem* item) } } -Menu* get_options_menu() -{ - //static OptionsMenu menu; - options_menu = new OptionsMenu(); - return options_menu; -} - -void free_options_menu() -{ - delete options_menu; - options_menu = 0; -} - /* EOF */ diff --git a/src/supertux/menu/options_menu.hpp b/src/supertux/menu/options_menu.hpp index be2264d71..4e70a64f6 100644 --- a/src/supertux/menu/options_menu.hpp +++ b/src/supertux/menu/options_menu.hpp @@ -24,9 +24,6 @@ class LanguageMenu; -Menu* get_options_menu(); -void free_options_menu(); - class OptionsMenu : public Menu { public: @@ -36,8 +33,7 @@ public: virtual void menu_action(MenuItem* item); protected: - std::auto_ptr language_menu; - + std::auto_ptr language_menu; }; #endif diff --git a/src/supertux/menu/profile_menu.cpp b/src/supertux/menu/profile_menu.cpp index e97a64013..39b0d954d 100644 --- a/src/supertux/menu/profile_menu.cpp +++ b/src/supertux/menu/profile_menu.cpp @@ -14,6 +14,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#include "supertux/menu/profile_menu.hpp" + #include #include "gui/menu.hpp" @@ -21,46 +23,26 @@ #include "supertux/gameconfig.hpp" #include "util/gettext.hpp" -enum ProfileMenuIDs { - -}; - -class ProfileMenu : public Menu +ProfileMenu::ProfileMenu() { -public: - ProfileMenu() { - add_label(_("Select Profile")); - add_hl(); - for(int i = 0; i < 5; ++i) - { - std::ostringstream out; - out << "Profile " << i+1; - add_entry(i+1, out.str()); - } - - add_hl(); - add_back(_("Back")); - } - - void menu_action(MenuItem* item) { - g_config->profile = item->id; - Menu::set_current(0); + add_label(_("Select Profile")); + add_hl(); + for(int i = 0; i < 5; ++i) + { + std::ostringstream out; + out << "Profile " << i+1; + add_entry(i+1, out.str()); } -}; -Menu* profile_menu = 0; - -Menu* get_profile_menu() -{ - //static ProfileMenu menu; - profile_menu = new ProfileMenu(); - return profile_menu; + add_hl(); + add_back(_("Back")); } -void free_profile_menu() +void +ProfileMenu::menu_action(MenuItem* item) { - delete profile_menu; - profile_menu = 0; + g_config->profile = item->id; + Menu::set_current(0); } /* diff --git a/src/supertux/menu/profile_menu.hpp b/src/supertux/menu/profile_menu.hpp index 89b965c62..ac301e0de 100644 --- a/src/supertux/menu/profile_menu.hpp +++ b/src/supertux/menu/profile_menu.hpp @@ -17,9 +17,15 @@ #ifndef HEADER_SUPERTUX_SUPERTUX_PROFILE_MENU_HPP #define HEADER_SUPERTUX_SUPERTUX_PROFILE_MENU_HPP -class Menu; -Menu* get_profile_menu(); -void free_profile_menu(); +#include "gui/menu.hpp" + +class ProfileMenu : public Menu +{ +public: + ProfileMenu(); + + void menu_action(MenuItem* item); +}; #endif diff --git a/src/supertux/title_screen.cpp b/src/supertux/title_screen.cpp index 2a5a2da12..5da0446ed 100644 --- a/src/supertux/title_screen.cpp +++ b/src/supertux/title_screen.cpp @@ -33,6 +33,7 @@ #include "supertux/gameconfig.hpp" #include "supertux/globals.hpp" #include "supertux/mainloop.hpp" +#include "supertux/menu/menu_manager.hpp" #include "supertux/menu/options_menu.hpp" #include "supertux/resources.hpp" #include "supertux/sector.hpp" @@ -339,7 +340,7 @@ TitleScreen::generate_main_menu() main_menu->add_entry(MNID_STARTGAME, _("Start Game")); main_menu->add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels")); main_menu->add_entry(MNID_ADDONS, _("Add-ons")); - main_menu->add_submenu(_("Options"), get_options_menu()); + main_menu->add_submenu(_("Options"), MenuManager::get_options_menu()); main_menu->add_entry(MNID_CREDITS, _("Credits")); main_menu->add_entry(MNID_QUITMAINMENU, _("Quit")); } diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index 3fd4ec501..b56c9f393 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -45,6 +45,7 @@ #include "supertux/game_session.hpp" #include "supertux/globals.hpp" #include "supertux/mainloop.hpp" +#include "supertux/menu/menu_manager.hpp" #include "supertux/menu/options_menu.hpp" #include "supertux/player_status.hpp" #include "supertux/resources.hpp" @@ -176,7 +177,7 @@ WorldMap::WorldMap(const std::string& filename, const std::string& force_spawnpo worldmap_menu->add_label(_("Pause")); worldmap_menu->add_hl(); worldmap_menu->add_entry(MNID_RETURNWORLDMAP, _("Continue")); - worldmap_menu->add_submenu(_("Options"), get_options_menu()); + worldmap_menu->add_submenu(_("Options"), MenuManager::get_options_menu()); worldmap_menu->add_hl(); worldmap_menu->add_entry(MNID_QUITWORLDMAP, _("Quit World")); -- 2.11.0