|
From: Tapio V. <aa...@us...> - 2010-08-21 18:03:16
|
Module: performous
Branch: master
Commit: 33de4c23cb98a3f7f4b50354fd1cd43853a87794
Author: Tapio Vierros <tap...@gm...>
Date: Thu Jul 8 19:21:29 2010 +0300
Support unlimited submenu levels.
Also fix a mistake in previous navigation commit.
---
game/joystick.cc | 2 +-
game/menu.cc | 34 +++++++++++++++++++++-------------
game/menu.hh | 15 ++++++++-------
3 files changed, 30 insertions(+), 21 deletions(-)
diff --git a/game/joystick.cc b/game/joystick.cc
index 82e65a1..d30b33e 100644
--- a/game/joystick.cc
+++ b/game/joystick.cc
@@ -474,7 +474,7 @@ bool input::SDL::pushEvent(SDL_Event _e) {
if (pickPressed[0]) return true; // repeating
pickPressed[0] = true;
event.type = input::Event::PICK;
- button = 1;
+ button = 0;
is_guitar_event = true;
break;
case SDLK_RSHIFT:
diff --git a/game/menu.cc b/game/menu.cc
index 3fab361..e71ec37 100644
--- a/game/menu.cc
+++ b/game/menu.cc
@@ -50,24 +50,32 @@ MenuOption::MenuOption(const std::string& nm, const std::string& comm, const std
}
+Menu::Menu():
+ m_open(true),
+ m_level(0)
+{
+ menu_stack.push_back(root_options);
+ current_it = menu_stack.back().end();
+}
void Menu::add(MenuOption opt) {
root_options.push_back(opt);
- options = root_options; // Set active menu to root
- current_it = options.begin(); // Reset iterator
+ menu_stack.clear();
+ menu_stack.push_back(root_options);
+ current_it = menu_stack.back().begin(); // Reset iterator
}
void Menu::move(int dir) {
- if (dir > 0 && current_it != (--options.end())) ++current_it;
- else if (dir < 0 && current_it != options.begin()) --current_it;
+ if (dir > 0 && current_it != (--menu_stack.back().end())) ++current_it;
+ else if (dir < 0 && current_it != menu_stack.back().begin()) --current_it;
}
void Menu::action(int dir) {
switch (current_it->type) {
case MenuOption::OPEN_SUBMENU:
if (current_it->options.empty()) break;
- options = current_it->options;
- current_it = options.begin();
+ menu_stack.push_back(current_it->options);
+ current_it = menu_stack.back().begin();
m_level++;
break;
case MenuOption::CHANGE_VALUE:
@@ -80,15 +88,14 @@ void Menu::action(int dir) {
if (current_it->value) *(current_it->value) = current_it->newValue;
// Fall-through to closing
case MenuOption::CLOSE_SUBMENU:
- // TODO: Handle more than one level of submenus
- if (m_level == 0) close();
- else m_level--;
- options = root_options;
- current_it = options.begin();
+ if (menu_stack.size() > 1) menu_stack.pop_back();
+ else close();
+ current_it = menu_stack.back().begin();
break;
case MenuOption::ACTIVATE_SCREEN:
ScreenManager* sm = ScreenManager::getSingletonPtr();
std::string screen = current_it->newValue.s();
+ clear();
if (screen.empty()) sm->finished();
else sm->activateScreen(screen);
break;
@@ -96,7 +103,8 @@ void Menu::action(int dir) {
}
void Menu::clear() {
- options.clear();
+ menu_stack.clear();
root_options.clear();
- current_it = options.end();
+ menu_stack.push_back(root_options);
+ current_it = menu_stack.back().end();
}
diff --git a/game/menu.hh b/game/menu.hh
index 21ba3d9..6b11de8 100644
--- a/game/menu.hh
+++ b/game/menu.hh
@@ -11,6 +11,7 @@ class Surface;
struct MenuOption;
typedef std::vector<MenuOption> MenuOptions;
+typedef std::vector<MenuOptions> SubmenuStack;
/// struct for menu options
struct MenuOption {
@@ -44,7 +45,7 @@ struct MenuOption {
/// Menu for selecting difficulty etc.
struct Menu {
/// constructor
- Menu(): current_it(options.end()), m_open(true), m_level(0) { }
+ Menu();
/// add a menu option
void add(MenuOption opt);
/// move the selection
@@ -54,23 +55,23 @@ struct Menu {
/// clear items
void clear();
- bool empty() const { return options.empty(); }
+ bool empty() const { return (menu_stack.empty() || (menu_stack.size() == 1 && menu_stack.back().empty())); }
bool isOpen() const { return m_open; }
void open() { m_open = true; }
void close() { m_open = false; }
void toggle() { m_open = !m_open; }
- void moveToLast() { current_it = --(options.end()); }
+ void moveToLast() { current_it = --(menu_stack.back().end()); }
MenuOptions::iterator& currentRef() { return current_it; }
const MenuOptions::const_iterator current() const { return current_it; }
- const MenuOptions::const_iterator begin() const { return options.begin(); }
- const MenuOptions::const_iterator end() const { return options.end(); }
- const MenuOptions getOptions() const { return options; }
+ const MenuOptions::const_iterator begin() const { return menu_stack.back().begin(); }
+ const MenuOptions::const_iterator end() const { return menu_stack.back().end(); }
+ const MenuOptions getOptions() const { return menu_stack.back(); }
private:
MenuOptions::iterator current_it;
- MenuOptions options;
MenuOptions root_options;
+ SubmenuStack menu_stack;
bool m_open;
int m_level;
|