[Super-tux-commit] supertux/lib/gui menu.cpp,1.7,1.8 menu.h,1.5,1.6
Brought to you by:
wkendrick
From: Tobias G. <to...@us...> - 2004-07-28 23:06:20
|
Update of /cvsroot/super-tux/supertux/lib/gui In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18640/lib/gui Modified Files: menu.cpp menu.h Log Message: - Various fixes and cleanups. - Added FrameRate class to SuperTux library - More menu code C++ing. Index: menu.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/gui/menu.cpp,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- menu.cpp 27 Jul 2004 19:31:37 -0000 1.7 +++ menu.cpp 28 Jul 2004 23:06:11 -0000 1.8 @@ -152,15 +152,23 @@ current_ = menu; } +MenuItem::MenuItem(MenuItemKind _kind, int _id) : kind(_kind) , id(_id) +{ + list.second = 0; +} + +MenuItem::MenuItem(MenuItemKind _kind, int _id, const std::string& _text) : kind(_kind) , id(_id) , text(_text) +{ + list.second = 0; +} + /* Return a pointer to a new menu item */ MenuItem* -MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_) +MenuItem::create(MenuItemKind kind_, const std::string& text_, int init_toggle_, Menu* target_menu_, int id_, int* int_p_) { - MenuItem *pnew_item = new MenuItem; + MenuItem *pnew_item = new MenuItem(kind_,id_); - pnew_item->kind = kind_; - pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1)); - strcpy(pnew_item->text, text_); + pnew_item->text = text_; if(kind_ == MN_TOGGLE) pnew_item->toggled = init_toggle_; @@ -168,10 +176,7 @@ pnew_item->toggled = false; pnew_item->target_menu = target_menu_; - pnew_item->input = (char*) malloc(sizeof(char)); - pnew_item->input[0] = '\0'; - pnew_item->id = id; pnew_item->int_p = int_p_; pnew_item->list.second = 0; @@ -184,25 +189,15 @@ } void -MenuItem::change_text(const char *text_) +MenuItem::change_text(const std::string& text_) { - if (text_) - { - free(text); - text = (char*) malloc(sizeof(char )*(strlen(text_)+1)); - strcpy(text, text_); - } + text = text_; } void -MenuItem::change_input(const char *text_) +MenuItem::change_input(const std::string& text_) { - if(text) - { - free(input); - input = (char*) malloc(sizeof(char )*(strlen(text_)+1)); - strcpy(input, text_); - } + input = text_; } std::string MenuItem::get_input_with_symbol(bool active_item) @@ -223,9 +218,9 @@ char str[1024]; if(input_flickering) - sprintf(str,"%s ",input); + sprintf(str,"%s ",input.c_str()); else - sprintf(str,"%s_",input); + sprintf(str,"%s_",input.c_str()); std::string string = str; @@ -298,8 +293,6 @@ { for(unsigned int i = 0; i < item.size(); ++i) { - free(item[i].text); - free(item[i].input); item[i].list.first.clear(); } } @@ -342,6 +335,13 @@ delete pmenu_item; } +/* Add an item to a menu */ +void +Menu::additem(const MenuItem& pmenu_item) +{ + item.push_back(pmenu_item); +} + void Menu::clear() { @@ -432,13 +432,13 @@ if(item[active_item].kind == MN_TEXTFIELD || item[active_item].kind == MN_NUMFIELD) { - if(item[active_item].input != NULL) + if(!item[active_item].input.empty()) { - int i = strlen(item[active_item].input); + int i = item[active_item].input.size(); while(delete_character > 0) /* remove charactes */ { - item[active_item].input[i-1] = '\0'; + item[active_item].input.resize(i-1); delete_character--; } } @@ -449,19 +449,7 @@ if(item[active_item].kind == MN_TEXTFIELD || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9')) { - if(item[active_item].input != NULL) - { - int i = strlen(item[active_item].input); - item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2)); - item[active_item].input[i] = mn_input_char; - item[active_item].input[i+1] = '\0'; - } - else - { - item[active_item].input = (char*) malloc(2*sizeof(char)); - item[active_item].input[0] = mn_input_char; - item[active_item].input[1] = '\0'; - } + item[active_item].input.push_back(mn_input_char); } case MENU_ACTION_NONE: @@ -688,7 +676,7 @@ int menu_width = 0; for(unsigned int i = 0; i < item.size(); ++i) { - int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0); //+ ((item[i].list.second != item[i].list.first.end()) ? (strlen((*(item[i].list.second)).c_str())) : 0); + int w = item[i].text.size() + item[i].input.size() + 1; //+ ((item[i].list.second != item[i].list.first.end()) ? (strlen((*(item[i].list.second)).c_str())) : 0); if( w > menu_width ) { menu_width = w; Index: menu.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/gui/menu.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- menu.h 27 Jul 2004 19:31:38 -0000 1.5 +++ menu.h 28 Jul 2004 23:06:11 -0000 1.6 @@ -64,19 +64,22 @@ class MenuItem { public: + MenuItem() {}; + MenuItem(MenuItemKind kind, int id = -1); + MenuItem(MenuItemKind kind, int id, const std::string& text); MenuItemKind kind; + int id; // item id int toggled; - char *text; - char *input; + std::string text; + std::string input; int *int_p; // used for setting keys (can be used for more stuff...) - int id; // item id std::pair<std::set<std::string>, std::set<std::string>::iterator> list; Menu* target_menu; - void change_text (const char *text); - void change_input(const char *text); + void change_text (const std::string& text); + void change_input(const std::string& text); - static MenuItem* create(MenuItemKind kind, const char *text, int init_toggle, Menu* target_menu, int id, int* int_p); + static MenuItem* create(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int id, int* int_p); std::string get_input_with_symbol(bool active_item); // returns the text with an input symbol private: @@ -148,6 +151,7 @@ Menu(); ~Menu(); + void additem(const MenuItem& menu_item); void additem(MenuItem* pmenu_item); void additem(MenuItemKind kind, const std::string& text, int init_toggle, Menu* target_menu, int id = -1, int *int_p = NULL); |