|
From: Tapio V. <aa...@us...> - 2010-08-21 18:03:21
|
Module: performous
Branch: master
Commit: fc87927320d7744ad5496702e395668e0512db24
Author: Tapio Vierros <tap...@gm...>
Date: Thu Jul 8 21:29:01 2010 +0300
Menu now remembers where it was previously when exiting submenu.
---
game/instrumentgraph.cc | 2 +-
game/menu.cc | 47 ++++++++++++++++++++++-------------------------
game/menu.hh | 11 ++++-------
game/screen_intro.cc | 6 +++---
4 files changed, 30 insertions(+), 36 deletions(-)
diff --git a/game/instrumentgraph.cc b/game/instrumentgraph.cc
index 7240bb7..3629f66 100644
--- a/game/instrumentgraph.cc
+++ b/game/instrumentgraph.cc
@@ -59,7 +59,7 @@ void InstrumentGraph::drawMenu(double offsetX) {
float y = -0.5 * m_menu.getOptions().size() * step;
// Some helper vars
ThemeInstrumentMenu& th = *m_menuTheme;
- MenuOptions::const_iterator cur = m_menu.current();
+ MenuOptions::const_iterator cur = static_cast<MenuOptions::const_iterator>(&m_menu.current());
// Loop through menu items
for (MenuOptions::const_iterator it = m_menu.begin(); it != m_menu.end(); ++it) {
SvgTxtTheme* txt = &th.option;
diff --git a/game/menu.cc b/game/menu.cc
index e1b78f6..8f08e63 100644
--- a/game/menu.cc
+++ b/game/menu.cc
@@ -61,50 +61,46 @@ MenuOption::MenuOption(const std::string& nm, const std::string& comm, const std
Menu::Menu():
- m_open(true),
- m_level(0)
+ m_open(true)
{
- menu_stack.push_back(&root_options);
- current_it = menu_stack.back()->end();
+ clear();
}
void Menu::add(MenuOption opt) {
root_options.push_back(opt);
- menu_stack.clear();
- menu_stack.push_back(&root_options);
- current_it = menu_stack.back()->begin(); // Reset iterator
+ clear(true); // Adding resets menu stack
}
void Menu::move(int dir) {
- if (dir > 0 && current_it != (--menu_stack.back()->end())) ++current_it;
- else if (dir < 0 && current_it != menu_stack.back()->begin()) --current_it;
+ if (dir > 0 && selection_stack.back() < menu_stack.back()->size() - 1) ++selection_stack.back();
+ else if (dir < 0 && selection_stack.back() > 0) --selection_stack.back();
}
void Menu::action(int dir) {
- switch (current_it->type) {
+ switch (current().type) {
case MenuOption::OPEN_SUBMENU:
- if (current_it->options.empty()) break;
- menu_stack.push_back(¤t_it->options);
- current_it = menu_stack.back()->begin();
- m_level++;
+ if (current().options.empty()) break;
+ menu_stack.push_back(¤t().options);
+ selection_stack.push_back(0);
break;
case MenuOption::CHANGE_VALUE:
- if (current_it->value) {
- if (dir > 0) ++(*(current_it->value));
- else if (dir < 0) --(*(current_it->value));
+ if (current().value) {
+ if (dir > 0) ++(*(current().value));
+ else if (dir < 0) --(*(current().value));
}
break;
case MenuOption::SET_AND_CLOSE:
- if (current_it->value) *(current_it->value) = current_it->newValue;
+ if (current().value) *(current().value) = current().newValue;
// Fall-through to closing
case MenuOption::CLOSE_SUBMENU:
- if (menu_stack.size() > 1) menu_stack.pop_back();
- else close();
- current_it = menu_stack.back()->begin();
+ if (menu_stack.size() > 1) {
+ menu_stack.pop_back();
+ selection_stack.pop_back();
+ } else close();
break;
case MenuOption::ACTIVATE_SCREEN:
ScreenManager* sm = ScreenManager::getSingletonPtr();
- std::string screen = current_it->newValue.s();
+ std::string screen = current().newValue.s();
clear();
if (screen.empty()) sm->finished();
else sm->activateScreen(screen);
@@ -112,9 +108,10 @@ void Menu::action(int dir) {
}
}
-void Menu::clear() {
+void Menu::clear(bool save_root) {
+ if (!save_root) root_options.clear();
menu_stack.clear();
- root_options.clear();
+ selection_stack.clear();
menu_stack.push_back(&root_options);
- current_it = menu_stack.back()->end();
+ selection_stack.push_back(0);
}
diff --git a/game/menu.hh b/game/menu.hh
index 9877e38..ab5e883 100644
--- a/game/menu.hh
+++ b/game/menu.hh
@@ -62,28 +62,25 @@ struct Menu {
/// adjust the selected value
void action(int dir = 1);
/// clear items
- void clear();
+ void clear(bool save_root = false);
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 = --(menu_stack.back()->end()); }
+ void moveToLast() { selection_stack.back() = menu_stack.back()->size() - 1; }
+ MenuOption& current() { return menu_stack.back()->at(selection_stack.back()); }
MenuOption& back() { return root_options.back(); }
- MenuOptions::iterator& currentRef() { return current_it; }
- const MenuOptions::const_iterator current() const { return current_it; }
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 root_options;
SubmenuStack menu_stack;
+ std::vector<size_t> selection_stack;
bool m_open;
- int m_level;
-
};
diff --git a/game/screen_intro.cc b/game/screen_intro.cc
index 7775457..5496a1b 100644
--- a/game/screen_intro.cc
+++ b/game/screen_intro.cc
@@ -49,7 +49,7 @@ void ScreenIntro::manageEvent(SDL_Event event) {
void ScreenIntro::draw_menu_options() {
int i = 0;
for (MenuOptions::const_iterator it = m_menu.begin(); it != m_menu.end(); ++it, ++i) {
- if (m_menu.current() == it) {
+ if (static_cast<MenuOptions::const_iterator>(&m_menu.current()) == it) {
theme->back_h.dimensions.left(-0.4).center(-0.097 + i*0.08);
theme->back_h.draw();
theme->option_selected.dimensions.left(-0.35).center(-0.1 + i*0.08);
@@ -63,11 +63,11 @@ void ScreenIntro::draw_menu_options() {
void ScreenIntro::draw() {
theme->bg.draw();
- m_menu.current()->image->draw();
+ m_menu.current().image->draw();
theme->comment_bg.dimensions.center().screenBottom(-0.01);
theme->comment_bg.draw();
theme->comment.dimensions.left(-0.48).screenBottom(-0.028);
- theme->comment.draw(m_menu.current()->getComment());
+ theme->comment.draw(m_menu.current().getComment());
draw_menu_options();
if (m_dialog) m_dialog->draw();
}
|