|
From: Tapio V. <aa...@us...> - 2010-08-21 18:02:59
|
Module: performous
Branch: master
Commit: db8e0e4ab54c3fd063d036dbc8e388d0d4b77980
Author: Tapio Vierros <tap...@gm...>
Date: Wed Jul 7 20:03:20 2010 +0300
Use getters for Menu's current and options + add some helpers.
---
game/instrumentgraph.cc | 15 +++++++++------
game/menu.cc | 28 ++++++++++++++--------------
game/menu.hh | 14 +++++++++++---
game/screen_intro.cc | 10 +++++-----
4 files changed, 39 insertions(+), 28 deletions(-)
diff --git a/game/instrumentgraph.cc b/game/instrumentgraph.cc
index 72b4ebb..b6a71f4 100644
--- a/game/instrumentgraph.cc
+++ b/game/instrumentgraph.cc
@@ -56,13 +56,16 @@ void InstrumentGraph::toggleMenu(bool forceopen) {
void InstrumentGraph::drawMenu(double offsetX) {
- if (m_menu.options.empty()) return;
+ if (m_menu.empty()) return;
float step = 0.075;
- float y = -0.5 * m_menu.options.size() * step;
+ float y = -0.5 * m_menu.getOptions().size() * step;
+ // Some helper vars
ThemeInstrumentMenu& th = *m_menuTheme;
- for (MenuOptions::iterator it = m_menu.options.begin(); it != m_menu.options.end(); ++it) {
+ MenuOptions::const_iterator cur = m_menu.current();
+ // Loop through menu items
+ for (MenuOptions::const_iterator it = m_menu.begin(); it != m_menu.end(); ++it) {
SvgTxtTheme* txt = &th.option;
- if (m_menu.current == it) {
+ if (cur == it) {
//th.back_h.dimensions.middle(0.05 + offsetX).center(y);
//th.back_h.draw();
txt = &th.option_selected;
@@ -72,11 +75,11 @@ void InstrumentGraph::drawMenu(double offsetX) {
y += step;
}
- if (m_menu.current->comment != "") {
+ if (cur->comment != "") {
//th.comment_bg.dimensions.middle().screenBottom(-0.2);
//th.comment_bg.draw();
th.comment.dimensions.middle(-0.1 + offsetX).screenBottom(-0.2);
- th.comment.draw(m_menu.current->comment);
+ th.comment.draw(cur->comment);
}
}
diff --git a/game/menu.cc b/game/menu.cc
index 9d3d625..d01b578 100755
--- a/game/menu.cc
+++ b/game/menu.cc
@@ -53,41 +53,41 @@ MenuOption::MenuOption(const std::string& nm, const std::string& comm, const std
void Menu::add(MenuOption opt) {
root_options.push_back(opt);
- options = root_options; // Set current menu to root
- current = options.begin(); // Reset iterator
+ options = root_options; // Set active menu to root
+ current_it = options.begin(); // Reset iterator
}
void Menu::move(int dir) {
- if (dir > 0 && current != (--options.end())) ++current;
- else if (dir < 0 && current != options.begin()) --current;
+ if (dir > 0 && current_it != (--options.end())) ++current_it;
+ else if (dir < 0 && current_it != options.begin()) --current_it;
}
void Menu::action(int dir) {
- switch (current->type) {
+ switch (current_it->type) {
case MenuOption::OPEN_SUBMENU:
- options = current->options;
- current = options.begin();
+ options = current_it->options;
+ current_it = options.begin();
m_level++;
break;
case MenuOption::CHANGE_VALUE:
- if (current->value) {
- if (dir > 0) ++(*(current->value));
- else if (dir < 0) --(*(current->value));
+ if (current_it->value) {
+ if (dir > 0) ++(*(current_it->value));
+ else if (dir < 0) --(*(current_it->value));
}
break;
case MenuOption::SET_AND_CLOSE:
- if (current->value) *(current->value) = current->newValue;
+ 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 = options.begin();
+ current_it = options.begin();
break;
case MenuOption::ACTIVATE_SCREEN:
ScreenManager* sm = ScreenManager::getSingletonPtr();
- std::string screen = current->newValue.s();
+ std::string screen = current_it->newValue.s();
if (screen.empty()) sm->finished();
else sm->activateScreen(screen);
break;
@@ -97,5 +97,5 @@ void Menu::action(int dir) {
void Menu::clear() {
options.clear();
root_options.clear();
- current = options.end();
+ current_it = options.end();
}
diff --git a/game/menu.hh b/game/menu.hh
index 0df839e..947feef 100755
--- a/game/menu.hh
+++ b/game/menu.hh
@@ -44,7 +44,7 @@ struct MenuOption {
/// Menu for selecting difficulty etc.
struct Menu {
/// constructor
- Menu(): current(options.end()), m_open(true), m_level(0) { }
+ Menu(): current_it(options.end()), m_open(true), m_level(0) { }
/// add a menu option
void add(MenuOption opt);
/// move the selection
@@ -54,16 +54,24 @@ struct Menu {
/// clear items
void clear();
+ bool empty() const { return options.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()); }
+ 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; }
+
+ private:
+ MenuOptions::iterator current_it;
MenuOptions options;
MenuOptions root_options;
- MenuOptions::iterator current;
- private:
bool m_open;
int m_level;
diff --git a/game/screen_intro.cc b/game/screen_intro.cc
index 5fd406b..9cbff7e 100755
--- a/game/screen_intro.cc
+++ b/game/screen_intro.cc
@@ -38,7 +38,7 @@ void ScreenIntro::manageEvent(SDL_Event event) {
input::NavButton nav(input::getNav(event));
if (nav != input::NONE) {
if (m_dialog) { m_dialog.reset(); return; }
- if (nav == input::CANCEL) m_menu.current = --(m_menu.options.end()); // Move cursor to quit
+ if (nav == input::CANCEL) m_menu.moveToLast(); // Move cursor to quit
else if (nav == input::DOWN || nav == input::RIGHT || nav == input::MOREDOWN) m_menu.move(1);
else if (nav == input::UP || nav == input::LEFT || nav == input::MOREUP) m_menu.move(-1);
else if (nav == input::START) m_menu.action();
@@ -48,8 +48,8 @@ void ScreenIntro::manageEvent(SDL_Event event) {
void ScreenIntro::draw_menu_options() {
int i = 0;
- for (MenuOptions::iterator it = m_menu.options.begin(); it != m_menu.options.end(); ++it, ++i) {
- if (m_menu.current == it) {
+ for (MenuOptions::const_iterator it = m_menu.begin(); it != m_menu.end(); ++it, ++i) {
+ if (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->comment);
+ theme->comment.draw(m_menu.current()->comment);
draw_menu_options();
if (m_dialog) m_dialog->draw();
}
|