if(Menu::current())
{
- Menu::current()->action();
Menu::current()->draw();
mouse_cursor->draw();
}
GameSession::ExitStatus
GameSession::run()
{
+ Menu::set_current(0);
Player* tux = world->get_tux();
current_ = this;
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())
{
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();
}
sprintf(str, "%d", hs_score);
yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
- Menu::current()->action();
Menu::current()->draw();
+ Menu::current()->action();
flipscreen();
if(Menu::current())
{
- Menu::current()->action();
Menu::current()->draw();
+ Menu::current()->action();
if(Menu::current() == leveleditor_menu)
{
Menu* contrib_menu = 0;
Menu* contrib_subset_menu = 0;
-std::stack<Menu*> Menu::last_menus;
+std::vector<Menu*> 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
{
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);
Menu::Menu()
{
+ hit_item = -1;
menuaction = MENU_ACTION_NONE;
delete_character = 0;
mn_input_char = '\0';
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
void
Menu::action()
{
+ hit_item = -1;
if(item.size() != 0)
{
switch(menuaction)
case MENU_ACTION_HIT:
{
+ hit_item = active_item;
switch (item[active_item].kind)
{
case MN_GOTO:
case MN_TEXTFIELD:
case MN_NUMFIELD:
case MN_CONTROLFIELD:
+ Menu::set_current(0);
item[active_item].toggled = true;
break;
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;
}
}
else
return -1;
+ */
}
void
}
}
-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 */
return (menu_width * 16 + 24);
}
-int Menu::height()
+int Menu::get_height() const
{
return item.size() * 24;
}
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,
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;
}
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
#include <SDL.h>
#include <vector>
-#include <stack>
#include "texture.h"
#include "timer.h"
#include "type.h"
class Menu
{
private:
- static std::stack<Menu*> last_menus;
+ static std::vector<Menu*> last_menus;
static Menu* current_;
static void push_current(Menu* pmenu);
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;
int delete_character;
char mn_input_char;
- int width();
- int height();
-
public:
Timer effect;
int arrange_left;
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;
std::vector<st_subset> 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()
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);
+ }
}
}
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();
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())
{
break;
}
}
+ else if(menu == options_menu)
+ {
+ process_options_menu();
+ }
}
}
if(Menu::current())
{
- Menu::current()->action();
Menu::current()->draw();
mouse_cursor->draw();
}