From b6543ef22bd7e4193d03cadac0c6e0dc75e3bd66 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Tue, 20 Apr 2004 10:42:05 +0000 Subject: [PATCH] - changed Menu::check() semantics a bit - speeded up the contrib menu a lot - fixed a few other menu issues - might have broken editor a bit, needs more testing SVN-Revision: 584 --- src/gameloop.cpp | 15 +++--- src/high_scores.cpp | 2 +- src/leveleditor.cpp | 2 +- src/menu.cpp | 59 +++++++++++++-------- src/menu.h | 23 ++++---- src/title.cpp | 148 ++++++++++++++++++++++++++-------------------------- src/worldmap.cpp | 12 +++-- 7 files changed, 143 insertions(+), 118 deletions(-) diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 00c26bd36..d47ab3334 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -393,7 +393,6 @@ GameSession::draw() if(Menu::current()) { - Menu::current()->action(); Menu::current()->draw(); mouse_cursor->draw(); } @@ -405,6 +404,7 @@ GameSession::draw() GameSession::ExitStatus GameSession::run() { + Menu::set_current(0); Player* tux = world->get_tux(); current_ = this; @@ -448,10 +448,13 @@ GameSession::run() tux->input.old_fire = tux->input.fire; process_events(); - - if(Menu::current()) + + Menu* menu = Menu::current(); + if(menu) { - if(Menu::current() == game_menu) + menu->action(); + + if(menu == game_menu) { switch (game_menu->check()) { @@ -464,11 +467,11 @@ GameSession::run() break; } } - else if(Menu::current() == options_menu) + else if(menu == options_menu) { process_options_menu(); } - else if(Menu::current() == load_game_menu ) + else if(menu == load_game_menu ) { process_load_game_menu(); } diff --git a/src/high_scores.cpp b/src/high_scores.cpp index 5b2d827a6..c32b48cbe 100644 --- a/src/high_scores.cpp +++ b/src/high_scores.cpp @@ -94,8 +94,8 @@ void save_hs(int score) sprintf(str, "%d", hs_score); yellow_nums->draw(str, 350, 170, 1, NO_UPDATE); - Menu::current()->action(); Menu::current()->draw(); + Menu::current()->action(); flipscreen(); diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index 358462d3e..eb9d2a5eb 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -210,8 +210,8 @@ int leveleditor(int levelnb) if(Menu::current()) { - Menu::current()->action(); Menu::current()->draw(); + Menu::current()->action(); if(Menu::current() == leveleditor_menu) { diff --git a/src/menu.cpp b/src/menu.cpp index 8c5bc22d6..841fdbe61 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -55,16 +55,17 @@ Menu* save_game_menu = 0; Menu* contrib_menu = 0; Menu* contrib_subset_menu = 0; -std::stack Menu::last_menus; +std::vector Menu::last_menus; Menu* Menu::current_ = 0; void Menu::push_current(Menu* pmenu) { if (current_) - last_menus.push(current_); + last_menus.push_back(current_); - set_current(pmenu); + current_ = pmenu; + current_->effect.start(500); } void @@ -72,18 +73,22 @@ Menu::pop_current() { if (!last_menus.empty()) { - set_current(last_menus.top()); - last_menus.pop(); + current_ = last_menus.back(); + current_->effect.start(500); + + last_menus.pop_back(); } else { - set_current(0); + current_ = 0; } } void Menu::set_current(Menu* menu) { + last_menus.clear(); + if (menu) menu->effect.start(500); @@ -158,6 +163,7 @@ Menu::~Menu() Menu::Menu() { + hit_item = -1; menuaction = MENU_ACTION_NONE; delete_character = 0; mn_input_char = '\0'; @@ -172,8 +178,8 @@ Menu::Menu() void Menu::set_pos(int x, int y, float rw, float rh) { - pos_x = x + (int)((float)width() * rw); - pos_y = y + (int)((float)height() * rh); + pos_x = x + (int)((float)get_width() * rw); + pos_y = y + (int)((float)get_height() * rh); } void @@ -206,6 +212,7 @@ Menu::clear() void Menu::action() { + hit_item = -1; if(item.size() != 0) { switch(menuaction) @@ -248,6 +255,7 @@ Menu::action() case MENU_ACTION_HIT: { + hit_item = active_item; switch (item[active_item].kind) { case MN_GOTO: @@ -265,6 +273,7 @@ Menu::action() case MN_TEXTFIELD: case MN_NUMFIELD: case MN_CONTROLFIELD: + Menu::set_current(0); item[active_item].toggled = true; break; @@ -338,18 +347,21 @@ Menu::action() int Menu::check() { + return hit_item; + /* if (item.size() != 0) { if((item[active_item].kind == MN_ACTION || item[active_item].kind == MN_TEXTFIELD || item[active_item].kind == MN_NUMFIELD) && item[active_item].toggled) - { + { item[active_item].toggled = false; Menu::set_current(0); return active_item; } - else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO) + else if(item[active_item].kind == MN_TOGGLE + || item[active_item].kind == MN_GOTO) { return active_item; } @@ -358,6 +370,7 @@ Menu::check() } else return -1; + */ } void @@ -506,7 +519,7 @@ Menu::draw_item(int index, // Position of the current item in the menu } } -int Menu::width() +int Menu::get_width() const { /* The width of the menu has to be more than the width of the text with the most characters */ @@ -525,7 +538,7 @@ int Menu::width() return (menu_width * 16 + 24); } -int Menu::height() +int Menu::get_height() const { return item.size() * 24; } @@ -534,8 +547,8 @@ int Menu::height() void Menu::draw() { - int menu_height = height(); - int menu_width = width(); + int menu_height = get_height(); + int menu_width = get_width(); /* Draw a transparent background */ fillrect(pos_x - menu_width/2, @@ -635,10 +648,10 @@ Menu::event(SDL_Event& event) case SDL_MOUSEBUTTONDOWN: x = event.motion.x; y = event.motion.y; - if(x > pos_x - width()/2 && - x < pos_x + width()/2 && - y > pos_y - height()/2 && - y < pos_y + height()/2) + if(x > pos_x - get_width()/2 && + x < pos_x + get_width()/2 && + y > pos_y - get_height()/2 && + y < pos_y + get_height()/2) { menuaction = MENU_ACTION_HIT; } @@ -646,12 +659,12 @@ Menu::event(SDL_Event& event) case SDL_MOUSEMOTION: x = event.motion.x; y = event.motion.y; - if(x > pos_x - width()/2 && - x < pos_x + width()/2 && - y > pos_y - height()/2 && - y < pos_y + height()/2) + if(x > pos_x - get_width()/2 && + x < pos_x + get_width()/2 && + y > pos_y - get_height()/2 && + y < pos_y + get_height()/2) { - active_item = (y - (pos_y - height()/2)) / 24; + active_item = (y - (pos_y - get_height()/2)) / 24; mouse_cursor->set_state(MC_LINK); } else diff --git a/src/menu.h b/src/menu.h index 36e533fc1..56e1a360f 100644 --- a/src/menu.h +++ b/src/menu.h @@ -22,7 +22,6 @@ #include #include -#include #include "texture.h" #include "timer.h" #include "type.h" @@ -64,7 +63,7 @@ public: class Menu { private: - static std::stack last_menus; + static std::vector last_menus; static Menu* current_; static void push_current(Menu* pmenu); @@ -89,6 +88,10 @@ private: MENU_ACTION_REMOVE }; + /** Number of the item that got 'hit' (ie. pressed) in the last + event()/action() call, -1 if none */ + int hit_item; + // position of the menu (ie. center of the menu, not top/left) int pos_x; int pos_y; @@ -101,9 +104,6 @@ private: int delete_character; char mn_input_char; - int width(); - int height(); - public: Timer effect; int arrange_left; @@ -116,22 +116,27 @@ public: void additem(MenuItem* pmenu_item); void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu); - void action (); + + void action (); /** Remove all entries from the menu */ void clear(); - /** Check, if the value of the active menu item has changed. FIXME: - Somebody should document the exact meaning of this function a - bit more */ + /** Return the index of the menu item that was 'hit' (ie. the user + clicked on it) in the last event() call */ int check (); + MenuItem& get_item(int index) { return item[index]; } + void draw (); void draw_item(int index, int menu_width, int menu_height); void set_pos(int x, int y, float rw = 0, float rh = 0); /** translate a SDL_Event into a menu_action */ void event(SDL_Event& event); + + int get_width() const; + int get_height() const; }; extern Surface* checkbox; diff --git a/src/title.cpp b/src/title.cpp index eaebd1248..738732462 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -58,33 +58,26 @@ void display_credits(); std::vector contrib_subsets; std::string current_contrib_subset; -void update_contrib_menu() +void generate_contrib_menu() { - // FIXME: Hack to update only once - static bool up_to_date = false; + string_list_type level_subsets = dsubdirs("/levels", "info"); - if (!up_to_date) - { - string_list_type level_subsets = dsubdirs("/levels", "info"); - - contrib_menu->clear(); - contrib_menu->additem(MN_LABEL,"Contrib Levels",0,0); - contrib_menu->additem(MN_HL,"",0,0); + contrib_menu->clear(); + contrib_menu->additem(MN_LABEL,"Contrib Levels",0,0); + contrib_menu->additem(MN_HL,"",0,0); - for (int i = 0; i < level_subsets.num_items; ++i) - { - st_subset subset; - subset.load(level_subsets.item[i]); - contrib_menu->additem(MN_GOTO, subset.title.c_str(), i, contrib_subset_menu); - contrib_subsets.push_back(subset); - } + for (int i = 0; i < level_subsets.num_items; ++i) + { + st_subset subset; + subset.load(level_subsets.item[i]); + contrib_menu->additem(MN_GOTO, subset.title.c_str(), i, contrib_subset_menu); + contrib_subsets.push_back(subset); + } - contrib_menu->additem(MN_HL,"",0,0); - contrib_menu->additem(MN_BACK,"Back",0,0); + contrib_menu->additem(MN_HL,"",0,0); + contrib_menu->additem(MN_BACK,"Back",0,0); - string_list_free(&level_subsets); - up_to_date = true; - } + string_list_free(&level_subsets); } void check_contrib_menu() @@ -133,11 +126,14 @@ void check_contrib_subset_menu() int index = contrib_subset_menu->check(); if (index != -1) { - index -= 1; // FIXME: Hack - std::cout << "Sarting level: " << index << std::endl; - GameSession session(current_contrib_subset, index, ST_GL_PLAY); - session.run(); - Menu::set_current(main_menu); + if (contrib_subset_menu->get_item(index).kind == MN_ACTION) + { + index -= 1; // FIXME: Hack + std::cout << "Sarting level: " << index << std::endl; + GameSession session(current_contrib_subset, index, ST_GL_PLAY); + session.run(); + Menu::set_current(main_menu); + } } } @@ -283,59 +279,61 @@ void title(void) 0, 420, 0); /* Don't draw menu, if quit is true */ - if(Menu::current()) - { - Menu::current()->action(); - Menu::current()->draw(); - } - - if(Menu::current() == main_menu) + Menu* menu = Menu::current(); + if(menu) { - switch (main_menu->check()) + menu->draw(); + menu->action(); + + if(menu == main_menu) { - case 0: - // Start Game, ie. goto the slots menu - update_load_save_game_menu(load_game_menu); - break; - case 1: - // Contrib Menu - update_contrib_menu(); - break; - case 3: - leveleditor(1); - Menu::set_current(main_menu); - break; - case 4: - display_credits(); - Menu::set_current(main_menu); - break; - case 5: - Menu::set_current(0); - break; + switch (main_menu->check()) + { + case 0: + // Start Game, ie. goto the slots menu + update_load_save_game_menu(load_game_menu); + break; + case 1: + // Contrib Menu + puts("Entering contrib menu"); + generate_contrib_menu(); + break; + case 3: + leveleditor(1); + Menu::set_current(main_menu); + break; + case 4: + display_credits(); + Menu::set_current(main_menu); + break; + case 5: + Menu::set_current(0); + break; + } } - } - else if(Menu::current() == options_menu) - { - process_options_menu(); - } - else if(Menu::current() == load_game_menu) - { - if (process_load_game_menu()) + else if(menu == options_menu) { - // FIXME: shouldn't be needed if GameSession doesn't relay on global variables - // reset tux - scroll_x = 0; - //titletux.level_begin(); - update_time = st_get_ticks(); + process_options_menu(); + } + else if(menu == load_game_menu) + { + if (process_load_game_menu()) + { + // FIXME: shouldn't be needed if GameSession doesn't relay on global variables + // reset tux + scroll_x = 0; + //titletux.level_begin(); + update_time = st_get_ticks(); + } + } + else if(menu == contrib_menu) + { + check_contrib_menu(); + } + else if (menu == contrib_subset_menu) + { + check_contrib_subset_menu(); } - } - else if(Menu::current() == contrib_menu) - { - check_contrib_menu(); - } - else if (Menu::current() == contrib_subset_menu) - { - check_contrib_subset_menu(); } mouse_cursor->draw(); diff --git a/src/worldmap.cpp b/src/worldmap.cpp index a89b0286c..cd460ace8 100644 --- a/src/worldmap.cpp +++ b/src/worldmap.cpp @@ -567,9 +567,12 @@ WorldMap::update() tux->update(0.33f); } - if(Menu::current()) + Menu* menu = Menu::current(); + if(menu) { - if(Menu::current() == worldmap_menu) + menu->action(); + + if(menu == worldmap_menu) { switch (worldmap_menu->check()) { @@ -585,6 +588,10 @@ WorldMap::update() break; } } + else if(menu == options_menu) + { + process_options_menu(); + } } } @@ -707,7 +714,6 @@ WorldMap::display() if(Menu::current()) { - Menu::current()->action(); Menu::current()->draw(); mouse_cursor->draw(); } -- 2.11.0