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"
33 /* (global) menu variables */
34 MenuAction menuaction = MENU_ACTION_NONE;
39 Surface* checkbox_checked;
46 Menu* worldmap_menu = 0;
47 Menu* options_menu = 0;
48 Menu* options_controls_menu = 0;
49 Menu* highscore_menu = 0;
50 Menu* load_game_menu = 0;
51 Menu* save_game_menu = 0;
52 Menu* contrib_menu = 0;
53 Menu* contrib_subset_menu = 0;
55 Menu* current_menu = 0;
57 /* input implementation variables */
61 /* Set the current menu */
63 Menu::set_current(Menu* pmenu)
65 if(pmenu != current_menu)
68 Menu* tmp = current_menu;
71 if(tmp->last_menu != pmenu)
72 current_menu->last_menu = tmp;
74 pmenu->effect.start(500);
78 /* Return a pointer to a new menu item */
80 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
82 MenuItem *pnew_item = new MenuItem;
84 pnew_item->kind = kind_;
85 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
86 strcpy(pnew_item->text, text_);
88 if(kind_ == MN_TOGGLE)
89 pnew_item->toggled = init_toggle_;
91 pnew_item->toggled = false;
93 pnew_item->target_menu = target_menu_;
94 pnew_item->input = (char*) malloc(sizeof(char));
95 pnew_item->input[0] = '\0';
97 if(kind_ == MN_STRINGSELECT)
99 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
100 string_list_init(pnew_item->list);
103 pnew_item->list = NULL;
108 MenuItem::change_text(const char *text_)
113 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
119 MenuItem::change_input(const char *text_)
124 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
125 strcpy(input, text_);
129 /* Free a menu and all its items */
134 for(unsigned int i = 0; i < item.size(); ++i)
138 string_list_free(item[i].list);
154 void Menu::set_pos(int x, int y, float rw, float rh)
156 pos_x = x + (int)((float)width() * rw);
157 pos_y = y + (int)((float)height() * rh);
161 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
163 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
166 /* Add an item to a menu */
168 Menu::additem(MenuItem* pmenu_item)
170 item.push_back(*pmenu_item);
180 /* Process actions done on the menu */
192 active_item = int(item.size())-1;
195 case MENU_ACTION_DOWN:
196 if(active_item < int(item.size())-1)
202 case MENU_ACTION_LEFT:
203 if(item[active_item].kind == MN_STRINGSELECT
204 && item[active_item].list->num_items != 0)
206 if(item[active_item].list->active_item > 0)
207 --item[active_item].list->active_item;
209 item[active_item].list->active_item = item[active_item].list->num_items-1;
213 case MENU_ACTION_RIGHT:
214 if(item[active_item].kind == MN_STRINGSELECT
215 && item[active_item].list->num_items != 0)
217 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
218 ++item[active_item].list->active_item;
220 item[active_item].list->active_item = 0;
224 case MENU_ACTION_HIT:
226 switch (item[active_item].kind)
229 if (item[active_item].target_menu != NULL)
230 Menu::set_current(item[active_item].target_menu);
236 item[active_item].toggled = !item[active_item].toggled;
243 case MN_CONTROLFIELD:
244 item[active_item].toggled = true;
248 if(last_menu != NULL)
249 Menu::set_current(last_menu);
257 case MENU_ACTION_REMOVE:
258 if(item[active_item].kind == MN_TEXTFIELD
259 || item[active_item].kind == MN_NUMFIELD)
261 if(item[active_item].input != NULL)
263 int i = strlen(item[active_item].input);
265 while(delete_character > 0) /* remove charactes */
267 item[active_item].input[i-1] = '\0';
274 case MENU_ACTION_INPUT:
275 if(item[active_item].kind == MN_TEXTFIELD
276 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
278 if(item[active_item].input != NULL)
280 int i = strlen(item[active_item].input);
281 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
282 item[active_item].input[i] = mn_input_char;
283 item[active_item].input[i+1] = '\0';
287 item[active_item].input = (char*) malloc(2*sizeof(char));
288 item[active_item].input[0] = mn_input_char;
289 item[active_item].input[1] = '\0';
294 case MENU_ACTION_NONE:
299 MenuItem& new_item = item[active_item];
300 if(new_item.kind == MN_DEACTIVE
301 || new_item.kind == MN_LABEL
302 || new_item.kind == MN_HL)
304 // Skip the horzontal line item
305 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
306 menuaction = MENU_ACTION_DOWN;
316 if (item.size() != 0)
318 if((item[active_item].kind == MN_ACTION
319 || item[active_item].kind == MN_TEXTFIELD
320 || item[active_item].kind == MN_NUMFIELD)
321 && item[active_item].toggled)
323 item[active_item].toggled = false;
327 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
339 Menu::draw_item(int index, // Position of the current item in the menu
343 const MenuItem& pitem = item[index];
346 int effect_offset = 0;
351 effect_time = effect.get_left() / 4;
353 effect_offset = (index % 2) ? effect_time : -effect_time;
357 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
359 int text_width = strlen(pitem.text) * font_width;
360 int input_width = strlen(pitem.input) * font_width;
361 int list_width = strlen(string_list_active(pitem.list)) * font_width;
362 Text* text_font = white_text;
365 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
367 if(index == active_item)
370 text_font = blue_text;
377 black_text->draw_align(pitem.text,
379 A_HMIDDLE, A_VMIDDLE, 2);
385 int x = pos_x - menu_width/2;
386 int y = y_pos - 12 - effect_offset;
387 /* Draw a horizontal line with a little 3d effect */
398 white_big_text->draw_align(pitem.text,
400 A_HMIDDLE, A_VMIDDLE, 2);
405 case MN_CONTROLFIELD:
407 int input_pos = input_width/2;
408 int text_pos = (text_width + font_width)/2;
410 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
411 input_width + font_width + 2, 20,
413 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
414 input_width + font_width, 18,
417 gold_text->draw_align(pitem.input,
418 x_pos + text_pos, y_pos,
419 A_HMIDDLE, A_VMIDDLE, 2);
421 text_font->draw_align(pitem.text,
422 x_pos - (input_width + font_width)/2, y_pos,
423 A_HMIDDLE, A_VMIDDLE, shadow_size);
426 case MN_STRINGSELECT:
428 int list_pos_2 = list_width + font_width;
429 int list_pos = list_width/2;
430 int text_pos = (text_width + font_width)/2;
433 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
434 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
436 /* Draw input background */
437 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
440 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
444 gold_text->draw_align(string_list_active(pitem.list),
445 x_pos + text_pos, y_pos,
446 A_HMIDDLE, A_VMIDDLE,2);
448 text_font->draw_align(pitem.text,
449 x_pos - list_pos_2/2, y_pos,
450 A_HMIDDLE, A_VMIDDLE, shadow_size);
455 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
456 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
462 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
465 checkbox_checked->draw(
466 x_pos + (text_width+font_width)/2,
470 x_pos + (text_width+font_width)/2,
475 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
479 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
486 /* The width of the menu has to be more than the width of the text
487 with the most characters */
489 for(unsigned int i = 0; i < item.size(); ++i)
491 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
495 if( item[i].kind == MN_TOGGLE)
500 return (menu_width * 16 + 24);
505 return item.size() * 24;
508 /* Draw the current menu. */
512 int menu_height = height();
513 int menu_width = width();
515 /* Draw a transparent background */
516 fillrect(pos_x - menu_width/2,
517 pos_y - 24*item.size()/2 - 10,
518 menu_width,menu_height + 20,
521 for(unsigned int i = 0; i < item.size(); ++i)
523 draw_item(i, menu_width, menu_height);
527 /* Reset/Set global defaults */
528 void menu_reset(void)
532 menuaction = MENU_ACTION_NONE;
535 delete_character = 0;
536 mn_input_char = '\0';
540 /* Draw the current menu and execute the (menu)events */
541 void menu_process_current(void)
545 if(current_menu != NULL)
547 current_menu->action();
548 current_menu->draw();
551 menuaction = MENU_ACTION_NONE;
554 /* Check for menu event */
556 Menu::event(SDL_Event& event)
562 key = event.key.keysym.sym;
565 keymod = SDL_GetModState();
568 /* If the current unicode character is an ASCII character,
570 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
572 ch[0] = event.key.keysym.unicode & 0x7F;
577 /* An International Character. */
582 case SDLK_UP: /* Menu Up */
583 menuaction = MENU_ACTION_UP;
586 case SDLK_DOWN: /* Menu Down */
587 menuaction = MENU_ACTION_DOWN;
590 case SDLK_LEFT: /* Menu Up */
591 menuaction = MENU_ACTION_LEFT;
594 case SDLK_RIGHT: /* Menu Down */
595 menuaction = MENU_ACTION_RIGHT;
599 if(item[active_item].kind == MN_TEXTFIELD)
601 menuaction = MENU_ACTION_INPUT;
606 case SDLK_RETURN: /* Menu Hit */
607 menuaction = MENU_ACTION_HIT;
612 menuaction = MENU_ACTION_REMOVE;
617 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
619 menuaction = MENU_ACTION_INPUT;
625 mn_input_char = '\0';
630 case SDL_JOYAXISMOTION:
631 if(event.jaxis.axis == JOY_Y)
633 if (event.jaxis.value > 1024)
634 menuaction = MENU_ACTION_DOWN;
635 else if (event.jaxis.value < -1024)
636 menuaction = MENU_ACTION_UP;
639 case SDL_JOYBUTTONDOWN:
640 menuaction = MENU_ACTION_HIT;
642 case SDL_MOUSEBUTTONDOWN:
645 if(x > pos_x - width()/2 &&
646 x < pos_x + width()/2 &&
647 y > pos_y - height()/2 &&
648 y < pos_y + height()/2)
650 menuaction = MENU_ACTION_HIT;
653 case SDL_MOUSEMOTION:
656 if(x > pos_x - width()/2 &&
657 x < pos_x + width()/2 &&
658 y > pos_y - height()/2 &&
659 y < pos_y + height()/2)
661 active_item = (y - (pos_y - height()/2)) / 24;
663 mouse_cursor->set_state(MC_LINK);
667 mouse_cursor->set_state(MC_NORMAL);