Skip to content

Commit

Permalink
Implemented MenuManager::refresh() so that joystick/keyboard_manager …
Browse files Browse the repository at this point in the history
…have something to call when key bindings change
  • Loading branch information
Grumbel committed Aug 9, 2014
1 parent 16a0bcb commit 3748bff
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/control/joystick_manager.cpp
Expand Up @@ -19,10 +19,10 @@
#include <iostream>
#include <algorithm>

#include "gui/menu_manager.hpp"
#include "control/input_manager.hpp"
#include "lisp/list_iterator.hpp"
#include "supertux/menu/joystick_menu.hpp"
#include "supertux/menu/menu_storage.hpp"
#include "util/gettext.hpp"
#include "util/log.hpp"
#include "util/writer.hpp"
Expand Down 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::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
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::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
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::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
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::instance().get_key_options_menu()->update();
MenuManager::instance().refresh();
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::instance().get_joystick_options_menu()->update();
MenuManager::instance().refresh();
m_parent->joystick_manager->wait_for_joystick = -1;
}
return;
Expand Down
3 changes: 3 additions & 0 deletions src/gui/menu.hpp
Expand Up @@ -71,6 +71,9 @@ class Menu

void update();

/** Perform actions to bring the menu up to date with configuration changes */
virtual void refresh() {}

/** Remove all entries from the menu */
void clear();

Expand Down
9 changes: 9 additions & 0 deletions src/gui/menu_manager.cpp
Expand Up @@ -140,6 +140,15 @@ MenuManager::~MenuManager()
s_instance = nullptr;
}

void
MenuManager::refresh()
{
for(auto i = m_menu_stack.begin(); i != m_menu_stack.end(); ++i)
{
(*i)->refresh();
}
}

void
MenuManager::update()
{
Expand Down
2 changes: 2 additions & 0 deletions src/gui/menu_manager.hpp
Expand Up @@ -46,6 +46,8 @@ class MenuManager

void event(const SDL_Event& event);
void update();
void refresh();

void draw(DrawingContext& context);
bool check_menu();

Expand Down
30 changes: 15 additions & 15 deletions src/supertux/menu/joystick_menu.cpp
Expand Up @@ -87,7 +87,7 @@ JoystickMenu::recreate_menu()

add_hl();
add_back(_("Back"));
update();
refresh();
}

std::string
Expand Down Expand Up @@ -131,7 +131,7 @@ JoystickMenu::menu_action(MenuItem* item)
}

void
JoystickMenu::update_menu_item(Controller::Control id)
JoystickMenu::refresh_menu_item(Controller::Control id)
{
int button = m_input_manager->joystick_manager->reversemap_joybutton(id);
int axis = m_input_manager->joystick_manager->reversemap_joyaxis(id);
Expand Down Expand Up @@ -201,22 +201,22 @@ JoystickMenu::update_menu_item(Controller::Control id)
}

void
JoystickMenu::update()
JoystickMenu::refresh()
{
if (m_joysticks_available)
{
update_menu_item(Controller::UP);
update_menu_item(Controller::DOWN);
update_menu_item(Controller::LEFT);
update_menu_item(Controller::RIGHT);

update_menu_item(Controller::JUMP);
update_menu_item(Controller::ACTION);
update_menu_item(Controller::PAUSE_MENU);
update_menu_item(Controller::PEEK_LEFT);
update_menu_item(Controller::PEEK_RIGHT);
update_menu_item(Controller::PEEK_UP);
update_menu_item(Controller::PEEK_DOWN);
refresh_menu_item(Controller::UP);
refresh_menu_item(Controller::DOWN);
refresh_menu_item(Controller::LEFT);
refresh_menu_item(Controller::RIGHT);

refresh_menu_item(Controller::JUMP);
refresh_menu_item(Controller::ACTION);
refresh_menu_item(Controller::PAUSE_MENU);
refresh_menu_item(Controller::PEEK_LEFT);
refresh_menu_item(Controller::PEEK_RIGHT);
refresh_menu_item(Controller::PEEK_UP);
refresh_menu_item(Controller::PEEK_DOWN);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/supertux/menu/joystick_menu.hpp
Expand Up @@ -27,9 +27,10 @@ class JoystickMenu : public Menu
JoystickMenu(InputManager* input_manager);
virtual ~JoystickMenu();

void update();
void refresh();
void refresh_menu_item(Controller::Control id);

std::string get_button_name(int button);
void update_menu_item(Controller::Control id);
virtual void menu_action(MenuItem* item);
void check_menu() {}

Expand Down
6 changes: 2 additions & 4 deletions src/supertux/menu/keyboard_menu.cpp
Expand Up @@ -43,7 +43,7 @@ KeyboardMenu::KeyboardMenu(InputManager* _controller) :
add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->keyboard_manager->jump_with_up_kbd);
add_hl();
add_back(_("Back"));
update();
refresh();
}

KeyboardMenu::~KeyboardMenu()
Expand Down Expand Up @@ -96,10 +96,8 @@ KeyboardMenu::menu_action(MenuItem* item)
}

void
KeyboardMenu::update()
KeyboardMenu::refresh()
{
// update menu

auto& kbd_mgr = controller->keyboard_manager;

get_item_by_id((int) Controller::UP).change_input(get_key_name(
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/menu/keyboard_menu.hpp
Expand Up @@ -27,7 +27,7 @@ class KeyboardMenu : public Menu
KeyboardMenu(InputManager* controller);
~KeyboardMenu();

void update();
void refresh();
std::string get_key_name(SDL_Keycode key);
virtual void menu_action(MenuItem* item);
InputManager* controller;
Expand Down
14 changes: 0 additions & 14 deletions src/supertux/menu/menu_storage.cpp
Expand Up @@ -98,18 +98,4 @@ MenuStorage::create(MenuId menu_id)
}
}

KeyboardMenu*
MenuStorage::get_key_options_menu()
{
assert(!"broken");
return new KeyboardMenu(g_input_manager);
}

JoystickMenu*
MenuStorage::get_joystick_options_menu()
{
assert(!"broken");
return new JoystickMenu(g_input_manager);
}

/* EOF */
6 changes: 0 additions & 6 deletions src/supertux/menu/menu_storage.hpp
Expand Up @@ -55,12 +55,6 @@ class MenuStorage

std::unique_ptr<Menu> create(MenuId menu_id);

// FIXME
#ifdef GRUMBEL
#endif
JoystickMenu* get_joystick_options_menu();
KeyboardMenu* get_key_options_menu();

private:
MenuStorage(const MenuStorage&);
MenuStorage& operator=(const MenuStorage&);
Expand Down

0 comments on commit 3748bff

Please sign in to comment.