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);
137 timer_init(&effect,false);
140 void Menu::set_pos(int x, int y, float rw, float rh)
142 pos_x = x + (int)((float)width() * rw);
143 pos_y = y + (int)((float)height() * rh);
147 Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
149 additem(menu_item_create(kind, text, toggle, menu));
152 /* Add an item to a menu */
154 Menu::additem(menu_item_type* pmenu_item)
157 item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items);
158 memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type));
162 /* Process actions done on the menu */
166 if(num_items != 0 && item != NULL)
174 active_item = num_items-1;
177 case MENU_ACTION_DOWN:
178 if(active_item < num_items-1)
184 case MENU_ACTION_LEFT:
185 if(item[active_item].kind == MN_STRINGSELECT
186 && item[active_item].list->num_items != 0)
188 if(item[active_item].list->active_item > 0)
189 --item[active_item].list->active_item;
191 item[active_item].list->active_item = item[active_item].list->num_items-1;
194 case MENU_ACTION_RIGHT:
195 if(item[active_item].kind == MN_STRINGSELECT
196 && item[active_item].list->num_items != 0)
198 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
199 ++item[active_item].list->active_item;
201 item[active_item].list->active_item = 0;
205 case MENU_ACTION_HIT:
207 switch (item[active_item].kind)
210 if (item[active_item].target_menu != NULL)
211 Menu::set_current(item[active_item].target_menu);
217 item[active_item].toggled = !item[active_item].toggled;
224 case MN_CONTROLFIELD:
225 item[active_item].toggled = true;
229 if(last_menu != NULL)
230 Menu::set_current(last_menu);
238 case MENU_ACTION_REMOVE:
239 if(item[active_item].kind == MN_TEXTFIELD
240 || item[active_item].kind == MN_NUMFIELD)
242 if(item[active_item].input != NULL)
244 int i = strlen(item[active_item].input);
246 while(delete_character > 0) /* remove charactes */
248 item[active_item].input[i-1] = '\0';
255 case MENU_ACTION_INPUT:
256 if(item[active_item].kind == MN_TEXTFIELD
257 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
259 if(item[active_item].input != NULL)
261 int i = strlen(item[active_item].input);
262 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
263 item[active_item].input[i] = mn_input_char;
264 item[active_item].input[i+1] = '\0';
268 item[active_item].input = (char*) malloc(2*sizeof(char));
269 item[active_item].input[0] = mn_input_char;
270 item[active_item].input[1] = '\0';
275 case MENU_ACTION_NONE:
280 menu_item_type& new_item = item[active_item];
281 if(new_item.kind == MN_DEACTIVE
282 || new_item.kind == MN_LABEL
283 || new_item.kind == MN_HL)
285 // Skip the horzontal line item
286 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
287 menuaction = MENU_ACTION_DOWN;
294 /* Check, if the value of the active menu item has changed. */
298 if(num_items != 0 && item != NULL)
300 if((item[active_item].kind == MN_ACTION
301 || item[active_item].kind == MN_TEXTFIELD
302 || item[active_item].kind == MN_NUMFIELD)
303 && item[active_item].toggled)
305 item[active_item].toggled = false;
309 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
321 Menu::draw_item(int index, // Position of the current item in the menu
327 const menu_item_type& pitem = item[index];
329 int effect_offset = 0;
332 if(timer_check(&effect))
333 effect_time = timer_get_left(&effect) / 4;
335 effect_offset = (index % 2) ? effect_time : -effect_time;
339 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
341 int text_width = strlen(pitem.text) * font_width;
342 int input_width = strlen(pitem.input) * font_width;
343 int list_width = strlen(string_list_active(pitem.list)) * font_width;
344 text_type* text_font = &white_text;
347 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
349 if(index == active_item)
352 text_font = &blue_text;
359 text_draw_align(&black_text, pitem.text,
361 A_HMIDDLE, A_VMIDDLE, 2);
367 int x = pos_x - menu_width/2;
368 int y = y_pos - 12 - effect_offset;
369 /* Draw a horizontal line with a little 3d effect */
380 text_draw_align(&white_big_text, pitem.text,
382 A_HMIDDLE, A_VMIDDLE, 2);
387 case MN_CONTROLFIELD:
389 int input_pos = input_width/2;
390 int text_pos = (text_width + font_width)/2;
392 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
393 input_width + font_width + 2, 20,
395 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
396 input_width + font_width, 18,
399 text_draw_align(&gold_text, pitem.input,
400 x_pos + text_pos, y_pos,
401 A_HMIDDLE, A_VMIDDLE, 2);
403 text_draw_align(text_font, pitem.text,
404 x_pos - (input_width + font_width)/2, y_pos,
405 A_HMIDDLE, A_VMIDDLE, shadow_size);
408 case MN_STRINGSELECT:
410 int list_pos_2 = list_width + font_width;
411 int list_pos = list_width/2;
412 int text_pos = (text_width + font_width)/2;
415 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
416 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
418 /* Draw input background */
419 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
422 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
426 text_draw_align(&gold_text, string_list_active(pitem.list),
427 x_pos + text_pos, y_pos,
428 A_HMIDDLE, A_VMIDDLE,2);
430 text_draw_align(text_font, pitem.text,
431 x_pos - list_pos_2/2, y_pos,
432 A_HMIDDLE, A_VMIDDLE, shadow_size);
437 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
438 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
444 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
447 texture_draw(&checkbox_checked,
448 x_pos + (text_width+font_width)/2,
451 texture_draw(&checkbox,
452 x_pos + (text_width+font_width)/2,
457 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
461 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
468 /* The width of the menu has to be more than the width of the text
469 with the most characters */
471 for(int i = 0; i < num_items; ++i)
473 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
477 if( item[i].kind == MN_TOGGLE)
482 return (menu_width * 16 + 48);
487 return ((num_items) * 24);
490 /* Draw the current menu. */
494 int menu_height = height();
495 int menu_width = width();
497 /* Draw a transparent background */
498 fillrect(pos_x - menu_width/2,
499 pos_y - 24*num_items/2,
500 menu_width,menu_height,150,150,150,100);
502 for(int i = 0; i < num_items; ++i)
504 draw_item(i, menu_width, menu_height);
508 /* Reset/Set global defaults */
509 void menu_reset(void)
513 menuaction = MENU_ACTION_NONE;
516 delete_character = 0;
517 mn_input_char = '\0';
521 /* Draw the current menu and execute the (menu)events */
522 void menu_process_current(void)
526 if(current_menu != NULL)
528 current_menu->action();
529 current_menu->draw();
532 menuaction = MENU_ACTION_NONE;
535 /* Check for menu event */
536 void menu_event(SDL_Event& event)
542 key = event.key.keysym.sym;
545 keymod = SDL_GetModState();
548 /* If the current unicode character is an ASCII character,
550 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
552 ch[0] = event.key.keysym.unicode & 0x7F;
557 /* An International Character. */
562 case SDLK_UP: /* Menu Up */
563 menuaction = MENU_ACTION_UP;
566 case SDLK_DOWN: /* Menu Down */
567 menuaction = MENU_ACTION_DOWN;
570 case SDLK_LEFT: /* Menu Up */
571 menuaction = MENU_ACTION_LEFT;
574 case SDLK_RIGHT: /* Menu Down */
575 menuaction = MENU_ACTION_RIGHT;
579 if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
581 menuaction = MENU_ACTION_INPUT;
586 case SDLK_RETURN: /* Menu Hit */
587 menuaction = MENU_ACTION_HIT;
592 menuaction = MENU_ACTION_REMOVE;
597 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
599 menuaction = MENU_ACTION_INPUT;
605 mn_input_char = '\0';
610 case SDL_JOYAXISMOTION:
611 if(event.jaxis.axis == JOY_Y)
613 if (event.jaxis.value > 1024)
614 menuaction = MENU_ACTION_DOWN;
615 else if (event.jaxis.value < -1024)
616 menuaction = MENU_ACTION_UP;
619 case SDL_JOYBUTTONDOWN:
620 menuaction = MENU_ACTION_HIT;
622 case SDL_MOUSEBUTTONDOWN:
625 if(x > current_menu->pos_x - current_menu->width()/2 &&
626 x < current_menu->pos_x + current_menu->width()/2 &&
627 y > current_menu->pos_y - current_menu->height()/2 &&
628 y < current_menu->pos_y + current_menu->height()/2)
630 menuaction = MENU_ACTION_HIT;
633 case SDL_MOUSEMOTION:
636 if(x > current_menu->pos_x - current_menu->width()/2 &&
637 x < current_menu->pos_x + current_menu->width()/2 &&
638 y > current_menu->pos_y - current_menu->height()/2 &&
639 y < current_menu->pos_y + current_menu->height()/2)
641 current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24;
643 mouse_cursor->set_state(MC_LINK);
647 mouse_cursor->set_state(MC_NORMAL);