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;
37 texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right;
41 Menu* options_menu = 0;
42 Menu* options_controls_menu = 0;
43 Menu* highscore_menu = 0;
44 Menu* load_game_menu = 0;
45 Menu* save_game_menu = 0;
47 Menu* current_menu = 0;
49 /* input implementation variables */
53 /* Set the current menu */
55 Menu::set_current(Menu* pmenu)
57 if(pmenu != current_menu)
60 Menu* tmp = current_menu;
63 if(tmp->last_menu != pmenu)
64 current_menu->last_menu = tmp;
66 timer_start(&pmenu->effect, 500);
70 /* Return a pointer to a new menu item */
71 menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu)
73 menu_item_type *pnew_item = (menu_item_type*) malloc(sizeof(menu_item_type));
74 pnew_item->kind = kind;
75 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text) + 1));
76 strcpy(pnew_item->text,text);
78 pnew_item->toggled = init_toggle;
80 pnew_item->toggled = false;
81 pnew_item->target_menu = target_menu;
82 pnew_item->input = (char*) malloc(sizeof(char));
83 pnew_item->input[0] = '\0';
84 if(kind == MN_STRINGSELECT)
86 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
87 string_list_init(pnew_item->list);
90 pnew_item->list = NULL;
94 void menu_item_change_text(menu_item_type* pmenu_item,const char *text)
98 free(pmenu_item->text);
99 pmenu_item->text = (char*) malloc(sizeof(char )*(strlen(text)+1));
100 strcpy(pmenu_item->text,text);
103 void menu_item_change_input(menu_item_type* pmenu_item,const char *text)
107 free(pmenu_item->input);
108 pmenu_item->input = (char*) malloc(sizeof(char )*(strlen(text)+1));
109 strcpy(pmenu_item->input,text);
113 /* Free a menu and all its items */
116 if(num_items != 0 && item != NULL)
118 for(int i = 0; i < num_items; ++i)
122 string_list_free(item[i].list);
136 timer_init(&effect,false);
140 Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
142 additem(menu_item_create(kind, text, toggle, menu));
145 /* Add an item to a menu */
147 Menu::additem(menu_item_type* pmenu_item)
150 item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items);
151 memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type));
155 /* Process actions done on the menu */
159 if(num_items != 0 && item != NULL)
167 active_item = num_items-1;
170 case MENU_ACTION_DOWN:
171 if(active_item < num_items-1)
177 case MENU_ACTION_LEFT:
178 if(item[active_item].kind == MN_STRINGSELECT
179 && item[active_item].list->num_items != 0)
181 if(item[active_item].list->active_item > 0)
182 --item[active_item].list->active_item;
184 item[active_item].list->active_item = item[active_item].list->num_items-1;
187 case MENU_ACTION_RIGHT:
188 if(item[active_item].kind == MN_STRINGSELECT
189 && item[active_item].list->num_items != 0)
191 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
192 ++item[active_item].list->active_item;
194 item[active_item].list->active_item = 0;
198 case MENU_ACTION_HIT:
200 switch (item[active_item].kind)
203 if (item[active_item].target_menu != NULL)
204 Menu::set_current(item[active_item].target_menu);
210 item[active_item].toggled = !item[active_item].toggled;
217 item[active_item].toggled = true;
221 if(last_menu != NULL)
222 Menu::set_current(last_menu);
230 case MENU_ACTION_REMOVE:
231 if(item[active_item].kind == MN_TEXTFIELD
232 || item[active_item].kind == MN_NUMFIELD)
234 if(item[active_item].input != NULL)
236 int i = strlen(item[active_item].input);
238 while(delete_character > 0) /* remove charactes */
240 item[active_item].input[i-1] = '\0';
247 case MENU_ACTION_INPUT:
248 if(item[active_item].kind == MN_TEXTFIELD
249 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
251 if(item[active_item].input != NULL)
253 int i = strlen(item[active_item].input);
254 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
255 item[active_item].input[i] = mn_input_char;
256 item[active_item].input[i+1] = '\0';
260 item[active_item].input = (char*) malloc(2*sizeof(char));
261 item[active_item].input[0] = mn_input_char;
262 item[active_item].input[1] = '\0';
267 case MENU_ACTION_NONE:
272 menu_item_type& new_item = item[active_item];
273 if(new_item.kind == MN_DEACTIVE
274 || new_item.kind == MN_LABEL
275 || new_item.kind == MN_HL)
277 // Skip the horzontal line item
278 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
279 menuaction = MENU_ACTION_DOWN;
286 /* Check, if the value of the active menu item has changed. */
290 if(num_items != 0 && item != NULL)
292 if((item[active_item].kind == MN_ACTION
293 || item[active_item].kind == MN_TEXTFIELD
294 || item[active_item].kind == MN_NUMFIELD)
295 && item[active_item].toggled)
297 item[active_item].toggled = false;
301 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
313 Menu::draw_item(int index, // Position of the current item in the menu
319 const menu_item_type& pitem = item[index];
321 int effect_offset = 0;
324 if(timer_check(&effect))
325 effect_time = timer_get_left(&effect) / 4;
327 effect_offset = (index % 2) ? effect_time : -effect_time;
331 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
333 int text_width = strlen(pitem.text) * font_width;
334 int input_width = strlen(pitem.input) * font_width;
335 int list_width = strlen(string_list_active(pitem.list)) * font_width;
336 text_type* text_font = &white_text;
339 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
341 if(index == active_item)
344 text_font = &blue_text;
351 text_draw_align(&black_text, pitem.text,
353 A_HMIDDLE, A_VMIDDLE, 2);
359 int x = pos_x - menu_width/2;
360 int y = y_pos - 12 - effect_offset;
361 /* Draw a horizontal line with a little 3d effect */
372 text_draw_align(&white_big_text, pitem.text,
374 A_HMIDDLE, A_VMIDDLE, 2);
380 int input_pos = input_width/2;
381 int text_pos = (text_width + font_width)/2;
383 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
384 input_width + font_width + 2, 20,
386 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
387 input_width + font_width, 18,
390 text_draw_align(&gold_text, pitem.input,
391 x_pos + text_pos, y_pos,
392 A_HMIDDLE, A_VMIDDLE, 2);
394 text_draw_align(text_font, pitem.text,
395 x_pos - (input_width + font_width)/2, y_pos,
396 A_HMIDDLE, A_VMIDDLE, shadow_size);
399 case MN_STRINGSELECT:
401 int list_pos_2 = list_width + font_width;
402 int list_pos = list_width/2;
403 int text_pos = (text_width + font_width)/2;
406 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
407 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
409 /* Draw input background */
410 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
413 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
417 text_draw_align(&gold_text, string_list_active(pitem.list),
418 x_pos + text_pos, y_pos,
419 A_HMIDDLE, A_VMIDDLE,2);
421 text_draw_align(text_font, pitem.text,
422 x_pos - list_pos_2/2, y_pos,
423 A_HMIDDLE, A_VMIDDLE, shadow_size);
428 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
429 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
435 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
438 texture_draw(&checkbox_checked,
439 x_pos + (text_width+font_width)/2,
442 texture_draw(&checkbox,
443 x_pos + (text_width+font_width)/2,
448 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
452 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
457 /* Draw the current menu. */
464 /* The width of the menu has to be more than the width of the text
465 with the most characters */
467 for(int i = 0; i < num_items; ++i)
469 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
473 if( item[i].kind == MN_TOGGLE)
478 menu_width = menu_width * 16 + 48;
479 menu_height = (num_items) * 24;
481 /* Draw a transparent background */
482 fillrect(pos_x - menu_width/2,
483 pos_y - 24*num_items/2,
484 menu_width,menu_height,150,150,150,100);
486 for(int i = 0; i < num_items; ++i)
488 draw_item(i, menu_width, menu_height);
492 /* Reset/Set global defaults */
493 void menu_reset(void)
497 menuaction = MENU_ACTION_NONE;
500 delete_character = 0;
501 mn_input_char = '\0';
505 /* Draw the current menu and execute the (menu)events */
506 void menu_process_current(void)
510 if(current_menu != NULL)
512 current_menu->action();
513 current_menu->draw();
516 menuaction = MENU_ACTION_NONE;
519 /* Check for menu event */
520 void menu_event(SDL_keysym* keysym)
522 SDLKey key = keysym->sym;
525 keymod = SDL_GetModState();
527 /* If the current unicode character is an ASCII character,
529 if ( (keysym->unicode & 0xFF80) == 0 )
531 ch[0] = keysym->unicode & 0x7F;
536 /* An International Character. */
541 case SDLK_UP: /* Menu Up */
542 menuaction = MENU_ACTION_UP;
545 case SDLK_DOWN: /* Menu Down */
546 menuaction = MENU_ACTION_DOWN;
549 case SDLK_LEFT: /* Menu Up */
550 menuaction = MENU_ACTION_LEFT;
553 case SDLK_RIGHT: /* Menu Down */
554 menuaction = MENU_ACTION_RIGHT;
558 if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
560 menuaction = MENU_ACTION_INPUT;
565 case SDLK_RETURN: /* Menu Hit */
566 menuaction = MENU_ACTION_HIT;
571 menuaction = MENU_ACTION_REMOVE;
576 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
578 menuaction = MENU_ACTION_INPUT;
584 mn_input_char = '\0';
590 /* FIXME: NO JOYSTICK SUPPORT */
592 else if (event.type == SDL_JOYBUTTONDOWN)
594 Joystick button: Continue: