8 http://www.newbreedsoftware.com/supertux/
10 December 20, 2003 - March 15, 2004
14 #include <sys/types.h>
29 #include "leveleditor.h"
31 #include "high_scores.h"
34 Surface* checkbox_checked;
41 Menu* worldmap_menu = 0;
42 Menu* options_menu = 0;
43 Menu* options_controls_menu = 0;
44 Menu* highscore_menu = 0;
45 Menu* load_game_menu = 0;
46 Menu* save_game_menu = 0;
47 Menu* contrib_menu = 0;
48 Menu* contrib_subset_menu = 0;
50 Menu* Menu::current_ = 0;
53 Menu::set_current(Menu* menu)
57 menu->last_menu = current_;
58 menu->effect.start(500);
64 /* Return a pointer to a new menu item */
66 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
68 MenuItem *pnew_item = new MenuItem;
70 pnew_item->kind = kind_;
71 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
72 strcpy(pnew_item->text, text_);
74 if(kind_ == MN_TOGGLE)
75 pnew_item->toggled = init_toggle_;
77 pnew_item->toggled = false;
79 pnew_item->target_menu = target_menu_;
80 pnew_item->input = (char*) malloc(sizeof(char));
81 pnew_item->input[0] = '\0';
83 if(kind_ == MN_STRINGSELECT)
85 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
86 string_list_init(pnew_item->list);
89 pnew_item->list = NULL;
94 MenuItem::change_text(const char *text_)
99 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
105 MenuItem::change_input(const char *text_)
110 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
111 strcpy(input, text_);
115 /* Free a menu and all its items */
120 for(unsigned int i = 0; i < item.size(); ++i)
124 string_list_free(item[i].list);
132 menuaction = MENU_ACTION_NONE;
133 delete_character = 0;
134 mn_input_char = '\0';
138 has_backitem = false;
146 void Menu::set_pos(int x, int y, float rw, float rh)
148 pos_x = x + (int)((float)width() * rw);
149 pos_y = y + (int)((float)height() * rh);
153 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
158 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
161 /* Add an item to a menu */
163 Menu::additem(MenuItem* pmenu_item)
165 if(pmenu_item->kind == MN_BACK)
168 item.push_back(*pmenu_item);
178 /* Process actions done on the menu */
190 active_item = int(item.size())-1;
193 case MENU_ACTION_DOWN:
194 if(active_item < int(item.size())-1)
200 case MENU_ACTION_LEFT:
201 if(item[active_item].kind == MN_STRINGSELECT
202 && item[active_item].list->num_items != 0)
204 if(item[active_item].list->active_item > 0)
205 --item[active_item].list->active_item;
207 item[active_item].list->active_item = item[active_item].list->num_items-1;
211 case MENU_ACTION_RIGHT:
212 if(item[active_item].kind == MN_STRINGSELECT
213 && item[active_item].list->num_items != 0)
215 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
216 ++item[active_item].list->active_item;
218 item[active_item].list->active_item = 0;
222 case MENU_ACTION_HIT:
224 switch (item[active_item].kind)
227 if (item[active_item].target_menu != NULL)
228 Menu::set_current(item[active_item].target_menu);
234 item[active_item].toggled = !item[active_item].toggled;
240 case MN_CONTROLFIELD:
241 item[active_item].toggled = true;
245 if(last_menu != NULL)
246 Menu::set_current(last_menu);
254 case MENU_ACTION_REMOVE:
255 if(item[active_item].kind == MN_TEXTFIELD
256 || item[active_item].kind == MN_NUMFIELD)
258 if(item[active_item].input != NULL)
260 int i = strlen(item[active_item].input);
262 while(delete_character > 0) /* remove charactes */
264 item[active_item].input[i-1] = '\0';
271 case MENU_ACTION_INPUT:
272 if(item[active_item].kind == MN_TEXTFIELD
273 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
275 if(item[active_item].input != NULL)
277 int i = strlen(item[active_item].input);
278 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
279 item[active_item].input[i] = mn_input_char;
280 item[active_item].input[i+1] = '\0';
284 item[active_item].input = (char*) malloc(2*sizeof(char));
285 item[active_item].input[0] = mn_input_char;
286 item[active_item].input[1] = '\0';
291 case MENU_ACTION_NONE:
296 MenuItem& new_item = item[active_item];
297 if(new_item.kind == MN_DEACTIVE
298 || new_item.kind == MN_LABEL
299 || new_item.kind == MN_HL)
301 // Skip the horzontal line item
302 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
303 menuaction = MENU_ACTION_DOWN;
309 menuaction = MENU_ACTION_NONE;
315 if (item.size() != 0)
317 if((item[active_item].kind == MN_ACTION
318 || item[active_item].kind == MN_TEXTFIELD
319 || item[active_item].kind == MN_NUMFIELD)
320 && item[active_item].toggled)
322 item[active_item].toggled = false;
323 Menu::set_current(0);
326 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
338 Menu::draw_item(int index, // Position of the current item in the menu
342 const MenuItem& pitem = item[index];
345 int effect_offset = 0;
350 effect_time = effect.get_left() / 4;
352 effect_offset = (index % 2) ? effect_time : -effect_time;
356 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
358 int text_width = strlen(pitem.text) * font_width;
359 int input_width = strlen(pitem.input) * font_width;
360 int list_width = strlen(string_list_active(pitem.list)) * font_width;
361 Text* text_font = white_text;
364 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
366 if(index == active_item)
369 text_font = blue_text;
376 black_text->draw_align(pitem.text,
378 A_HMIDDLE, A_VMIDDLE, 2);
384 int x = pos_x - menu_width/2;
385 int y = y_pos - 12 - effect_offset;
386 /* Draw a horizontal line with a little 3d effect */
397 white_big_text->draw_align(pitem.text,
399 A_HMIDDLE, A_VMIDDLE, 2);
404 case MN_CONTROLFIELD:
406 int input_pos = input_width/2;
407 int text_pos = (text_width + font_width)/2;
409 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
410 input_width + font_width + 2, 20,
412 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
413 input_width + font_width, 18,
416 gold_text->draw_align(pitem.input,
417 x_pos + text_pos, y_pos,
418 A_HMIDDLE, A_VMIDDLE, 2);
420 text_font->draw_align(pitem.text,
421 x_pos - (input_width + font_width)/2, y_pos,
422 A_HMIDDLE, A_VMIDDLE, shadow_size);
425 case MN_STRINGSELECT:
427 int list_pos_2 = list_width + font_width;
428 int list_pos = list_width/2;
429 int text_pos = (text_width + font_width)/2;
432 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
433 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
435 /* Draw input background */
436 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
439 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
443 gold_text->draw_align(string_list_active(pitem.list),
444 x_pos + text_pos, y_pos,
445 A_HMIDDLE, A_VMIDDLE,2);
447 text_font->draw_align(pitem.text,
448 x_pos - list_pos_2/2, y_pos,
449 A_HMIDDLE, A_VMIDDLE, shadow_size);
454 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
455 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
461 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
464 checkbox_checked->draw(
465 x_pos + (text_width+font_width)/2,
469 x_pos + (text_width+font_width)/2,
474 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
478 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
485 /* The width of the menu has to be more than the width of the text
486 with the most characters */
488 for(unsigned int i = 0; i < item.size(); ++i)
490 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
494 if( item[i].kind == MN_TOGGLE)
499 return (menu_width * 16 + 24);
504 return item.size() * 24;
507 /* Draw the current menu. */
511 int menu_height = height();
512 int menu_width = width();
514 /* Draw a transparent background */
515 fillrect(pos_x - menu_width/2,
516 pos_y - 24*item.size()/2 - 10,
517 menu_width,menu_height + 20,
520 for(unsigned int i = 0; i < item.size(); ++i)
522 draw_item(i, menu_width, menu_height);
527 /* Draw the current menu and execute the (menu)events */
528 void menu_process_current()
532 Menu::current()->action();
533 Menu::current()->draw();
537 /* Check for menu event */
539 Menu::event(SDL_Event& event)
545 key = event.key.keysym.sym;
548 keymod = SDL_GetModState();
551 /* If the current unicode character is an ASCII character,
553 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
555 ch[0] = event.key.keysym.unicode & 0x7F;
560 /* An International Character. */
565 case SDLK_UP: /* Menu Up */
566 menuaction = MENU_ACTION_UP;
568 case SDLK_DOWN: /* Menu Down */
569 menuaction = MENU_ACTION_DOWN;
571 case SDLK_LEFT: /* Menu Up */
572 menuaction = MENU_ACTION_LEFT;
574 case SDLK_RIGHT: /* Menu Down */
575 menuaction = MENU_ACTION_RIGHT;
578 if(item[active_item].kind == MN_TEXTFIELD)
580 menuaction = MENU_ACTION_INPUT;
584 case SDLK_RETURN: /* Menu Hit */
585 menuaction = MENU_ACTION_HIT;
589 menuaction = MENU_ACTION_REMOVE;
595 if (has_backitem == true && last_menu != NULL)
596 Menu::set_current(last_menu);
598 Menu::set_current(0);
601 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
603 menuaction = MENU_ACTION_INPUT;
608 mn_input_char = '\0';
613 case SDL_JOYAXISMOTION:
614 if(event.jaxis.axis == JOY_Y)
616 if (event.jaxis.value > 1024)
617 menuaction = MENU_ACTION_DOWN;
618 else if (event.jaxis.value < -1024)
619 menuaction = MENU_ACTION_UP;
622 case SDL_JOYBUTTONDOWN:
623 menuaction = MENU_ACTION_HIT;
625 case SDL_MOUSEBUTTONDOWN:
628 if(x > pos_x - width()/2 &&
629 x < pos_x + width()/2 &&
630 y > pos_y - height()/2 &&
631 y < pos_y + height()/2)
633 menuaction = MENU_ACTION_HIT;
636 case SDL_MOUSEMOTION:
639 if(x > pos_x - width()/2 &&
640 x < pos_x + width()/2 &&
641 y > pos_y - height()/2 &&
642 y < pos_y + height()/2)
644 active_item = (y - (pos_y - height()/2)) / 24;
645 mouse_cursor->set_state(MC_LINK);
649 mouse_cursor->set_state(MC_NORMAL);