Skip to content

Commit

Permalink
Turned MenuStorage into a proper class
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 8, 2014
1 parent f05308a commit 972088a
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 37 deletions.
6 changes: 3 additions & 3 deletions src/control/joystick_manager.cpp
Expand Up @@ -143,7 +143,7 @@ JoystickManager::process_hat_event(const SDL_JoyHatEvent& jhat)
if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT)
bind_joyhat(jhat.which, SDL_HAT_RIGHT, Controller::Control(wait_for_joystick));

MenuStorage::get_joystick_options_menu()->update();
MenuStorage::instance().get_joystick_options_menu()->update();
wait_for_joystick = -1;
}
else
Expand Down Expand Up @@ -191,7 +191,7 @@ JoystickManager::process_axis_event(const SDL_JoyAxisEvent& jaxis)
else
bind_joyaxis(jaxis.which, jaxis.axis + 1, Controller::Control(wait_for_joystick));

MenuStorage::get_joystick_options_menu()->update();
MenuStorage::instance().get_joystick_options_menu()->update();
wait_for_joystick = -1;
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ JoystickManager::process_button_event(const SDL_JoyButtonEvent& jbutton)
if(jbutton.state == SDL_PRESSED)
{
bind_joybutton(jbutton.which, jbutton.button, (Controller::Control)wait_for_joystick);
MenuStorage::get_joystick_options_menu()->update();
MenuStorage::instance().get_joystick_options_menu()->update();
parent->reset();
wait_for_joystick = -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/control/keyboard_manager.cpp
Expand Up @@ -168,7 +168,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
}
m_parent->reset();
MenuStorage::get_key_options_menu()->update();
MenuStorage::instance().get_key_options_menu()->update();
wait_for_key = -1;
return;
}
Expand All @@ -178,7 +178,7 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
if (event.keysym.sym == SDLK_ESCAPE)
{
m_parent->reset();
MenuStorage::get_joystick_options_menu()->update();
MenuStorage::instance().get_joystick_options_menu()->update();
m_parent->joystick_manager->wait_for_joystick = -1;
}
return;
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/menu/game_menu.cpp
Expand Up @@ -26,7 +26,7 @@ GameMenu::GameMenu(const Level& level)
add_label(level.name);
add_hl();
add_entry(MNID_CONTINUE, _("Continue"));
add_submenu(_("Options"), MenuStorage::get_options_menu());
add_submenu(_("Options"), MenuStorage::instance().get_options_menu());
add_hl();
add_entry(MNID_ABORTLEVEL, _("Abort Level"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/menu/main_menu.cpp
Expand Up @@ -39,7 +39,7 @@ MainMenu::MainMenu() :
add_entry(MNID_STARTGAME, _("Start Game"));
add_entry(MNID_LEVELS_CONTRIB, _("Contrib Levels"));
add_entry(MNID_ADDONS, _("Add-ons"));
add_submenu(_("Options"), MenuStorage::get_options_menu());
add_submenu(_("Options"), MenuStorage::instance().get_options_menu());
add_entry(MNID_CREDITS, _("Credits"));
add_entry(MNID_QUITMAINMENU, _("Quit"));
}
Expand Down
55 changes: 39 additions & 16 deletions src/supertux/menu/menu_storage.cpp
Expand Up @@ -22,45 +22,68 @@
#include "supertux/menu/keyboard_menu.hpp"
#include "supertux/globals.hpp"

OptionsMenu* MenuStorage::options_menu = 0;
ProfileMenu* MenuStorage::profile_menu = 0;
KeyboardMenu* MenuStorage::key_options_menu = 0;
JoystickMenu* MenuStorage::joystick_options_menu = 0;
MenuStorage* MenuStorage::s_instance = 0;

MenuStorage&
MenuStorage::instance()
{
assert(s_instance);
return *s_instance;
}

MenuStorage::MenuStorage()
{
assert(!s_instance);
s_instance = this;
}

MenuStorage::~MenuStorage()
{
s_instance = nullptr;
}

OptionsMenu*
MenuStorage::get_options_menu()
{
options_menu = new OptionsMenu();
return options_menu;
if (!m_options_menu)
{
m_options_menu.reset(new OptionsMenu());
}

return m_options_menu.get();
}

ProfileMenu*
MenuStorage::get_profile_menu()
{
profile_menu = new ProfileMenu();
return profile_menu;
if (!m_profile_menu)
{
m_profile_menu.reset(new ProfileMenu());
}

return m_profile_menu.get();
}

KeyboardMenu*
MenuStorage::get_key_options_menu()
{
if (!key_options_menu)
{ // FIXME: this in never freed
key_options_menu = new KeyboardMenu(g_input_manager);
if (!m_key_options_menu)
{
m_key_options_menu.reset(new KeyboardMenu(g_input_manager));
}

return key_options_menu;
return m_key_options_menu.get();
}

JoystickMenu*
MenuStorage::get_joystick_options_menu()
{
if (!joystick_options_menu)
{ // FIXME: this in never freed
joystick_options_menu = new JoystickMenu(g_input_manager);
if (!m_joystick_options_menu)
{
m_joystick_options_menu.reset(new JoystickMenu(g_input_manager));
}

return joystick_options_menu;
return m_joystick_options_menu.get();
}

/* EOF */
24 changes: 16 additions & 8 deletions src/supertux/menu/menu_storage.hpp
Expand Up @@ -17,6 +17,8 @@
#ifndef HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP
#define HEADER_SUPERTUX_SUPERTUX_MENU_MENU_STORAGE_HPP

#include <memory>

class JoystickMenu;
class KeyboardMenu;
class Menu;
Expand All @@ -25,19 +27,25 @@ class ProfileMenu;

class MenuStorage
{
private:
static MenuStorage* s_instance;
public:
static MenuStorage& instance();

public:
MenuStorage();
~MenuStorage();

static OptionsMenu* get_options_menu();
static ProfileMenu* get_profile_menu();
static KeyboardMenu* get_key_options_menu();
static JoystickMenu* get_joystick_options_menu();
OptionsMenu* get_options_menu();
ProfileMenu* get_profile_menu();
KeyboardMenu* get_key_options_menu();
JoystickMenu* get_joystick_options_menu();

private:
static OptionsMenu* options_menu;
static ProfileMenu* profile_menu;
static KeyboardMenu* key_options_menu;
static JoystickMenu* joystick_options_menu;
std::unique_ptr<OptionsMenu> m_options_menu;
std::unique_ptr<ProfileMenu> m_profile_menu;
std::unique_ptr<KeyboardMenu> m_key_options_menu;
std::unique_ptr<JoystickMenu> m_joystick_options_menu;

private:
MenuStorage(const MenuStorage&);
Expand Down
6 changes: 3 additions & 3 deletions src/supertux/menu/options_menu.cpp
Expand Up @@ -55,7 +55,7 @@ OptionsMenu::OptionsMenu() :
add_submenu(_("Select Language"), language_menu.get())
->set_help(_("Select a different language to display text in"));

add_submenu(_("Select Profile"), MenuStorage::get_profile_menu())
add_submenu(_("Select Profile"), MenuStorage::instance().get_profile_menu())
->set_help(_("Select a profile to play with"));

add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
Expand Down Expand Up @@ -192,10 +192,10 @@ OptionsMenu::OptionsMenu() :
add_inactive(MNID_MUSIC, _("Music (disabled)"));
}

add_submenu(_("Setup Keyboard"), MenuStorage::get_key_options_menu())
add_submenu(_("Setup Keyboard"), MenuStorage::instance().get_key_options_menu())
->set_help(_("Configure key-action mappings"));

add_submenu(_("Setup Joystick"), MenuStorage::get_joystick_options_menu())
add_submenu(_("Setup Joystick"), MenuStorage::instance().get_joystick_options_menu())
->set_help(_("Configure joystick control-action mappings"));
add_hl();
add_back(_("Back"));
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/menu/worldmap_menu.cpp
Expand Up @@ -25,7 +25,7 @@ WorldmapMenu::WorldmapMenu()
add_label(_("Pause"));
add_hl();
add_entry(MNID_RETURNWORLDMAP, _("Continue"));
add_submenu(_("Options"), MenuStorage::get_options_menu());
add_submenu(_("Options"), MenuStorage::instance().get_options_menu());
add_hl();
add_entry(MNID_QUITWORLDMAP, _("Quit World"));
}
Expand Down
6 changes: 4 additions & 2 deletions src/supertux/screen_manager.cpp
Expand Up @@ -22,15 +22,16 @@
#include "gui/menu_manager.hpp"
#include "scripting/squirrel_util.hpp"
#include "scripting/time_scheduler.hpp"
#include "supertux/constants.hpp"
#include "supertux/console.hpp"
#include "supertux/constants.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "supertux/main.hpp"
#include "supertux/menu/menu_storage.hpp"
#include "supertux/player_status.hpp"
#include "supertux/resources.hpp"
#include "supertux/screen_fade.hpp"
#include "supertux/screen.hpp"
#include "supertux/screen_fade.hpp"
#include "supertux/timer.hpp"
#include "video/drawing_context.hpp"
#include "video/renderer.hpp"
Expand All @@ -43,6 +44,7 @@ static const int MAX_FRAME_SKIP = 2;

ScreenManager::ScreenManager() :
waiting_threads(),
m_menu_storage(new MenuStorage),
m_menu_manager(new MenuManager),
running(),
speed(1.0),
Expand Down
2 changes: 2 additions & 0 deletions src/supertux/screen_manager.hpp
Expand Up @@ -25,6 +25,7 @@
class Console;
class DrawingContext;
class MenuManager;
class MenuStorage;
class Screen;
class ScreenFade;

Expand Down Expand Up @@ -64,6 +65,7 @@ class ScreenManager
void handle_screen_switch();

private:
std::unique_ptr<MenuStorage> m_menu_storage;
std::unique_ptr<MenuManager> m_menu_manager;
bool running;
float speed;
Expand Down

0 comments on commit 972088a

Please sign in to comment.