From 749b9fb1fc17480be99154e72815ac368c131d36 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Sun, 11 Apr 2004 17:39:47 +0000 Subject: [PATCH] - turned menu_item_type into a class SVN-Revision: 487 --- src/button.cpp | 2 +- src/gameloop.cpp | 2 +- src/high_scores.cpp | 2 +- src/leveleditor.cpp | 22 +++++++------- src/menu.cpp | 84 +++++++++++++++++++++++++++++------------------------ src/menu.h | 45 +++++++++++++++------------- src/setup.cpp | 2 +- src/title.cpp | 2 +- 8 files changed, 87 insertions(+), 74 deletions(-) diff --git a/src/button.cpp b/src/button.cpp index 82d048937..d2c184fc9 100644 --- a/src/button.cpp +++ b/src/button.cpp @@ -68,7 +68,7 @@ Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x bkgd = NULL; } -void Button::change_icon(std::string icon_file, int mw, int mh) +void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/) { char filename[1024]; diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 7fbe920bb..834f98ffc 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -170,7 +170,7 @@ GameSession::process_events() { /* Check for menu-events, if the menu is shown */ if(show_menu) - menu_event(event); + current_menu->event(event); switch(event.type) { diff --git a/src/high_scores.cpp b/src/high_scores.cpp index 09bec1bc4..97108e0ae 100644 --- a/src/high_scores.cpp +++ b/src/high_scores.cpp @@ -115,7 +115,7 @@ void save_hs(int score) while(SDL_PollEvent(&event)) if(event.type == SDL_KEYDOWN) - menu_event(event); + current_menu->event(event); switch (highscore_menu->check()) { diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index bca421d4f..d7dbdb817 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -282,7 +282,7 @@ int leveleditor(int levelnb) le_set_defaults(); le_current_level->load_gfx(); le_world.activate_bad_guys(); - menu_item_change_input(&subset_new_menu->item[2],""); + subset_new_menu->item[2].change_input(""); show_menu = true; break; } @@ -463,7 +463,7 @@ void update_level_settings_menu() char str[80]; int i; - menu_item_change_input(&level_settings_menu->item[2], le_current_level->name.c_str()); + level_settings_menu->item[2].change_input(le_current_level->name.c_str()); sprintf(str,"%d",le_current_level->width); string_list_copy(level_settings_menu->item[3].list, dsubdirs("images/themes", "solid0.png")); @@ -477,23 +477,23 @@ void update_level_settings_menu() if((i = string_list_find(level_settings_menu->item[5].list,le_current_level->bkgd_image.c_str())) != -1) level_settings_menu->item[5].list->active_item = i; - menu_item_change_input(&level_settings_menu->item[6], str); + level_settings_menu->item[6].change_input(str); sprintf(str,"%d",le_current_level->time_left); - menu_item_change_input(&level_settings_menu->item[7], str); + level_settings_menu->item[7].change_input(str); sprintf(str,"%2.0f",le_current_level->gravity); - menu_item_change_input(&level_settings_menu->item[8], str); + level_settings_menu->item[8].change_input(str); sprintf(str,"%d",le_current_level->bkgd_red); - menu_item_change_input(&level_settings_menu->item[9], str); + level_settings_menu->item[9].change_input(str); sprintf(str,"%d",le_current_level->bkgd_green); - menu_item_change_input(&level_settings_menu->item[10], str); + level_settings_menu->item[10].change_input(str); sprintf(str,"%d",le_current_level->bkgd_blue); - menu_item_change_input(&level_settings_menu->item[11], str); + level_settings_menu->item[11].change_input(str); } void update_subset_settings_menu() { - menu_item_change_input(&subset_settings_menu->item[2], le_level_subset.title.c_str()); - menu_item_change_input(&subset_settings_menu->item[3], le_level_subset.description.c_str()); + subset_settings_menu->item[2].change_input(le_level_subset.title.c_str()); + subset_settings_menu->item[3].change_input(le_level_subset.description.c_str()); } void apply_level_settings_menu() @@ -750,7 +750,7 @@ void le_checkevents() while(SDL_PollEvent(&event)) { if(show_menu) - menu_event(event); + current_menu->event(event); else mouse_cursor->set_state(MC_NORMAL); diff --git a/src/menu.cpp b/src/menu.cpp index f70453ea4..e2b2e513f 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -69,20 +69,25 @@ Menu::set_current(Menu* pmenu) } /* Return a pointer to a new menu item */ -menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu) +MenuItem* +MenuItem::create(MenuItemKind kind_, char *text_, int init_toggle_, Menu* target_menu_) { - menu_item_type *pnew_item = (menu_item_type*) malloc(sizeof(menu_item_type)); - pnew_item->kind = kind; - pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text) + 1)); - strcpy(pnew_item->text,text); - if(kind == MN_TOGGLE) - pnew_item->toggled = init_toggle; + MenuItem *pnew_item = new MenuItem; + + pnew_item->kind = kind_; + pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1)); + strcpy(pnew_item->text, text_); + + if(kind_ == MN_TOGGLE) + pnew_item->toggled = init_toggle_; else pnew_item->toggled = false; - pnew_item->target_menu = target_menu; + + pnew_item->target_menu = target_menu_; pnew_item->input = (char*) malloc(sizeof(char)); pnew_item->input[0] = '\0'; - if(kind == MN_STRINGSELECT) + + if(kind_ == MN_STRINGSELECT) { pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type)); string_list_init(pnew_item->list); @@ -92,22 +97,25 @@ menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, return pnew_item; } -void menu_item_change_text(menu_item_type* pmenu_item,const char *text) +void +MenuItem::change_text(const char *text_) { - if(text) + if (text_) { - free(pmenu_item->text); - pmenu_item->text = (char*) malloc(sizeof(char )*(strlen(text)+1)); - strcpy(pmenu_item->text,text); + free(text); + text = (char*) malloc(sizeof(char )*(strlen(text_)+1)); + strcpy(text, text_); } } -void menu_item_change_input(menu_item_type* pmenu_item,const char *text) + +void +MenuItem::change_input(const char *text_) { if(text) { - free(pmenu_item->input); - pmenu_item->input = (char*) malloc(sizeof(char )*(strlen(text)+1)); - strcpy(pmenu_item->input,text); + free(input); + input = (char*) malloc(sizeof(char )*(strlen(text_)+1)); + strcpy(input, text_); } } @@ -146,18 +154,18 @@ void Menu::set_pos(int x, int y, float rw, float rh) } void -Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu) +Menu::additem(MenuItemKind kind_, char *text_, int toggle_, Menu* menu_) { - additem(menu_item_create(kind, text, toggle, menu)); + additem(MenuItem::create(kind_, text_, toggle_, menu_)); } /* Add an item to a menu */ void -Menu::additem(menu_item_type* pmenu_item) +Menu::additem(MenuItem* pmenu_item) { ++num_items; - item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items); - memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type)); + item = (MenuItem*)realloc(item, sizeof(MenuItem) * num_items); + memcpy(&item[num_items-1], pmenu_item, sizeof(MenuItem)); free(pmenu_item); } @@ -280,7 +288,7 @@ Menu::action() } } - menu_item_type& new_item = item[active_item]; + MenuItem& new_item = item[active_item]; if(new_item.kind == MN_DEACTIVE || new_item.kind == MN_LABEL || new_item.kind == MN_HL) @@ -324,10 +332,9 @@ Menu::draw_item(int index, // Position of the current item in the menu int menu_width, int menu_height) { - int font_width = 16; - - const menu_item_type& pitem = item[index]; + const MenuItem& pitem = item[index]; + int font_width = 16; int effect_offset = 0; { int effect_time = 0; @@ -536,7 +543,8 @@ void menu_process_current(void) } /* Check for menu event */ -void menu_event(SDL_Event& event) +void +Menu::event(SDL_Event& event) { SDLKey key; switch(event.type) @@ -579,7 +587,7 @@ void menu_event(SDL_Event& event) menu_change = true; break; case SDLK_SPACE: - if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD) + if(item[active_item].kind == MN_TEXTFIELD) { menuaction = MENU_ACTION_INPUT; menu_change = true; @@ -625,10 +633,10 @@ void menu_event(SDL_Event& event) case SDL_MOUSEBUTTONDOWN: x = event.motion.x; y = event.motion.y; - if(x > current_menu->pos_x - current_menu->width()/2 && - x < current_menu->pos_x + current_menu->width()/2 && - y > current_menu->pos_y - current_menu->height()/2 && - y < current_menu->pos_y + current_menu->height()/2) + if(x > pos_x - width()/2 && + x < pos_x + width()/2 && + y > pos_y - height()/2 && + y < pos_y + height()/2) { menuaction = MENU_ACTION_HIT; } @@ -636,12 +644,12 @@ void menu_event(SDL_Event& event) case SDL_MOUSEMOTION: x = event.motion.x; y = event.motion.y; - if(x > current_menu->pos_x - current_menu->width()/2 && - x < current_menu->pos_x + current_menu->width()/2 && - y > current_menu->pos_y - current_menu->height()/2 && - y < current_menu->pos_y + current_menu->height()/2) + if(x > pos_x - width()/2 && + x < pos_x + width()/2 && + y > pos_y - height()/2 && + y < pos_y + height()/2) { - current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24; + active_item = (y - (pos_y - height()/2)) / 24; menu_change = true; mouse_cursor->set_state(MC_LINK); } diff --git a/src/menu.h b/src/menu.h index 4d9f00423..46f41b590 100644 --- a/src/menu.h +++ b/src/menu.h @@ -36,24 +36,24 @@ enum MenuItemKind { class Menu; -struct menu_item_type - { - MenuItemKind kind; - int toggled; - char *text; - char *input; - string_list_type* list; - Menu* target_menu; -}; +class MenuItem +{ +public: + MenuItemKind kind; + int toggled; + char *text; + char *input; + string_list_type* list; + Menu* target_menu; -menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu); -void menu_item_change_text (menu_item_type* pmenu_item, const char *text); -void menu_item_change_input(menu_item_type* pmenu_item, const char *text); + void change_text (const char *text); + void change_input(const char *text); + + static MenuItem* create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu); +}; class Menu { -friend void menu_event(SDL_Event& event); - private: // position of the menu (ie. center of the menu, not top/left) int pos_x; @@ -68,14 +68,14 @@ public: Timer effect; int arrange_left; int active_item; - menu_item_type *item; + MenuItem* item; static void set_current(Menu* pmenu); Menu(); ~Menu(); - void additem(menu_item_type* pmenu_item); + void additem(MenuItem* pmenu_item); void additem(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu); void action (); @@ -84,6 +84,9 @@ public: 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); + + /* Check for a menu event */ + void event(SDL_Event& event); }; @@ -103,7 +106,12 @@ enum MenuAction { extern MenuAction menuaction; extern bool show_menu; extern bool menu_change; -extern texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right; + +extern texture_type checkbox; +extern texture_type checkbox_checked; +extern texture_type back; +extern texture_type arrow_left; +extern texture_type arrow_right; extern Menu* contrib_menu; extern Menu* main_menu; @@ -125,9 +133,6 @@ void menu_reset(void); /* "Calculate" and draw the menu */ void menu_process_current(void); -/* Check for a menu event */ -void menu_event(SDL_Event& event); - #endif /*SUPERTUX_MENU_H*/ /* Local Variables: */ diff --git a/src/setup.cpp b/src/setup.cpp index 155b5ab41..589d129e8 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -445,7 +445,7 @@ void update_load_save_game_menu(Menu* pmenu, int load) pmenu->item[i].kind = MN_ACTION; else pmenu->item[i].kind = MN_ACTION; - menu_item_change_text(&pmenu->item[i], tmp.c_str()); + pmenu->item[i].change_text(tmp.c_str()); } } diff --git a/src/title.cpp b/src/title.cpp index 39f55a24e..10b7c4c3a 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -187,7 +187,7 @@ bool title(void) while (SDL_PollEvent(&event)) { - menu_event(event); + current_menu->event(event); if (event.type == SDL_QUIT) { done = true; -- 2.11.0