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* highscore_menu = 0;
43 Menu* load_game_menu = 0;
44 Menu* save_game_menu = 0;
46 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 last_menu = current_menu;
62 timer_start(&pmenu->effect, 500);
66 /* Return a pointer to a new menu item */
67 menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu)
69 menu_item_type *pnew_item = (menu_item_type*) malloc(sizeof(menu_item_type));
70 pnew_item->kind = kind;
71 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text) + 1));
72 strcpy(pnew_item->text,text);
74 pnew_item->toggled = init_toggle;
76 pnew_item->toggled = false;
77 pnew_item->target_menu = target_menu;
78 pnew_item->input = (char*) malloc(sizeof(char));
79 pnew_item->input[0] = '\0';
80 if(kind == MN_STRINGSELECT)
82 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
83 string_list_init(pnew_item->list);
86 pnew_item->list = NULL;
90 void menu_item_change_text(menu_item_type* pmenu_item,const char *text)
94 free(pmenu_item->text);
95 pmenu_item->text = (char*) malloc(sizeof(char )*(strlen(text)+1));
96 strcpy(pmenu_item->text,text);
99 void menu_item_change_input(menu_item_type* pmenu_item,const char *text)
103 free(pmenu_item->input);
104 pmenu_item->input = (char*) malloc(sizeof(char )*(strlen(text)+1));
105 strcpy(pmenu_item->input,text);
109 /* Free a menu and all its items */
112 if(num_items != 0 && item != NULL)
114 for(int i = 0; i < num_items; ++i)
118 string_list_free(item[i].list);
132 timer_init(&effect,false);
136 Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
138 additem(menu_item_create(kind, text, toggle, menu));
141 /* Add an item to a menu */
143 Menu::additem(menu_item_type* pmenu_item)
146 item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items);
147 memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type));
151 /* Process actions done on the menu */
155 if(num_items != 0 && item != NULL)
163 active_item = num_items-1;
166 case MENU_ACTION_DOWN:
167 if(active_item < num_items-1)
173 case MENU_ACTION_LEFT:
174 if(item[active_item].kind == MN_STRINGSELECT
175 && item[active_item].list->num_items != 0)
177 if(item[active_item].list->active_item > 0)
178 --item[active_item].list->active_item;
180 item[active_item].list->active_item = item[active_item].list->num_items-1;
183 case MENU_ACTION_RIGHT:
184 if(item[active_item].kind == MN_STRINGSELECT
185 && item[active_item].list->num_items != 0)
187 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
188 ++item[active_item].list->active_item;
190 item[active_item].list->active_item = 0;
194 case MENU_ACTION_HIT:
196 switch (item[active_item].kind)
199 if (item[active_item].target_menu != NULL)
200 Menu::set_current(item[active_item].target_menu);
206 item[active_item].toggled = !item[active_item].toggled;
213 item[active_item].toggled = true;
217 if(last_menu != NULL)
218 Menu::set_current(last_menu);
226 case MENU_ACTION_REMOVE:
227 if(item[active_item].kind == MN_TEXTFIELD
228 || item[active_item].kind == MN_NUMFIELD)
230 if(item[active_item].input != NULL)
232 int i = strlen(item[active_item].input);
234 while(delete_character > 0) /* remove charactes */
236 item[active_item].input[i-1] = '\0';
243 case MENU_ACTION_INPUT:
244 if(item[active_item].kind == MN_TEXTFIELD
245 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
247 if(item[active_item].input != NULL)
249 int i = strlen(item[active_item].input);
250 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
251 item[active_item].input[i] = mn_input_char;
252 item[active_item].input[i+1] = '\0';
256 item[active_item].input = (char*) malloc(2*sizeof(char));
257 item[active_item].input[0] = mn_input_char;
258 item[active_item].input[1] = '\0';
263 case MENU_ACTION_NONE:
268 menu_item_type& new_item = item[active_item];
269 if(new_item.kind == MN_DEACTIVE
270 || new_item.kind == MN_LABEL
271 || new_item.kind == MN_HL)
273 // Skip the horzontal line item
274 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
275 menuaction = MENU_ACTION_DOWN;
282 /* Check, if the value of the active menu item has changed. */
286 if(num_items != 0 && item != NULL)
288 if((item[active_item].kind == MN_ACTION || item[active_item].kind == MN_TEXTFIELD || item[active_item].kind == MN_NUMFIELD) && item[active_item].toggled == true)
290 item[active_item].toggled = false;
294 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
306 Menu::draw_item(int index, // Position of the current item in the menu
312 const menu_item_type& pitem = item[index];
314 int effect_offset = 0;
317 if(timer_check(&effect))
318 effect_time = timer_get_left(&effect) / 4;
320 effect_offset = (index % 2) ? effect_time : -effect_time;
324 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
326 int text_width = strlen(pitem.text) * font_width;
327 int input_width = strlen(pitem.input) * font_width;
328 int list_width = strlen(string_list_active(pitem.list)) * font_width;
329 text_type* text_font = &white_text;
331 if(arrange_left == true)
332 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
334 if(index == active_item)
337 text_font = &blue_text;
344 text_draw_align(&black_text, pitem.text,
346 A_HMIDDLE, A_VMIDDLE, 2);
352 int x = pos_x - menu_width/2;
353 int y = y_pos - 12 - effect_offset;
354 /* Draw a horizontal line with a little 3d effect */
365 text_draw_align(&white_big_text, pitem.text,
367 A_HMIDDLE, A_VMIDDLE, 2);
373 int input_pos = input_width/2;
374 int text_pos = (text_width + font_width)/2;
376 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
377 input_width + font_width + 2, 20,
379 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
380 input_width + font_width, 18,
383 text_draw_align(&gold_text, pitem.input,
384 x_pos + text_pos, y_pos,
385 A_HMIDDLE, A_VMIDDLE, 2);
387 text_draw_align(text_font, pitem.text,
388 x_pos - (input_width + font_width)/2, y_pos,
389 A_HMIDDLE, A_VMIDDLE, shadow_size);
392 case MN_STRINGSELECT:
394 int list_pos_2 = list_width + font_width;
395 int list_pos = list_width/2;
396 int text_pos = (text_width + font_width)/2;
399 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
400 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
402 /* Draw input background */
403 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
406 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
410 text_draw_align(&gold_text, string_list_active(pitem.list),
411 x_pos + text_pos, y_pos,
412 A_HMIDDLE, A_VMIDDLE,2);
414 text_draw_align(text_font, pitem.text,
415 x_pos - list_pos_2/2, y_pos,
416 A_HMIDDLE, A_VMIDDLE, shadow_size);
421 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
422 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
428 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
430 if(pitem.toggled == true)
431 texture_draw(&checkbox_checked,
432 x_pos + (text_width+font_width)/2,
435 texture_draw(&checkbox,
436 x_pos + (text_width+font_width)/2,
441 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
445 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
450 /* Draw the current menu. */
457 /* The width of the menu has to be more than the width of the text
458 with the most characters */
460 for(int i = 0; i < num_items; ++i)
462 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
466 if( item[i].kind == MN_TOGGLE)
471 menu_width = menu_width * 16 + 48;
472 menu_height = (num_items) * 24;
474 /* Draw a transparent background */
475 fillrect(pos_x - menu_width/2,
476 pos_y - 24*num_items/2,
477 menu_width,menu_height,150,150,150,100);
479 for(int i = 0; i < num_items; ++i)
481 draw_item(i, menu_width, menu_height);
485 /* Reset/Set global defaults */
486 void menu_reset(void)
490 menuaction = MENU_ACTION_NONE;
494 delete_character = 0;
495 mn_input_char = '\0';
499 /* Draw the current menu and execute the (menu)events */
500 void menu_process_current(void)
504 if(current_menu != NULL)
506 current_menu->action();
507 current_menu->draw();
510 menuaction = MENU_ACTION_NONE;
513 /* Check for menu event */
514 void menu_event(SDL_keysym* keysym)
516 SDLKey key = keysym->sym;
519 keymod = SDL_GetModState();
521 /* If the current unicode character is an ASCII character,
523 if ( (keysym->unicode & 0xFF80) == 0 )
525 ch[0] = keysym->unicode & 0x7F;
530 /* An International Character. */
535 case SDLK_UP: /* Menu Up */
536 menuaction = MENU_ACTION_UP;
539 case SDLK_DOWN: /* Menu Down */
540 menuaction = MENU_ACTION_DOWN;
543 case SDLK_LEFT: /* Menu Up */
544 menuaction = MENU_ACTION_LEFT;
547 case SDLK_RIGHT: /* Menu Down */
548 menuaction = MENU_ACTION_RIGHT;
552 if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
554 menuaction = MENU_ACTION_INPUT;
559 case SDLK_RETURN: /* Menu Hit */
560 menuaction = MENU_ACTION_HIT;
565 menuaction = MENU_ACTION_REMOVE;
570 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
572 menuaction = MENU_ACTION_INPUT;
578 mn_input_char = '\0';
584 /* FIXME: NO JOYSTICK SUPPORT */
586 else if (event.type == SDL_JOYBUTTONDOWN)
588 Joystick button: Continue: