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 case MN_CONTROLFIELD:
218 item[active_item].toggled = true;
222 if(last_menu != NULL)
223 Menu::set_current(last_menu);
231 case MENU_ACTION_REMOVE:
232 if(item[active_item].kind == MN_TEXTFIELD
233 || item[active_item].kind == MN_NUMFIELD)
235 if(item[active_item].input != NULL)
237 int i = strlen(item[active_item].input);
239 while(delete_character > 0) /* remove charactes */
241 item[active_item].input[i-1] = '\0';
248 case MENU_ACTION_INPUT:
249 if(item[active_item].kind == MN_TEXTFIELD
250 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
252 if(item[active_item].input != NULL)
254 int i = strlen(item[active_item].input);
255 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
256 item[active_item].input[i] = mn_input_char;
257 item[active_item].input[i+1] = '\0';
261 item[active_item].input = (char*) malloc(2*sizeof(char));
262 item[active_item].input[0] = mn_input_char;
263 item[active_item].input[1] = '\0';
268 case MENU_ACTION_NONE:
273 menu_item_type& new_item = item[active_item];
274 if(new_item.kind == MN_DEACTIVE
275 || new_item.kind == MN_LABEL
276 || new_item.kind == MN_HL)
278 // Skip the horzontal line item
279 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
280 menuaction = MENU_ACTION_DOWN;
287 /* Check, if the value of the active menu item has changed. */
291 if(num_items != 0 && item != NULL)
293 if((item[active_item].kind == MN_ACTION
294 || item[active_item].kind == MN_TEXTFIELD
295 || item[active_item].kind == MN_NUMFIELD)
296 && item[active_item].toggled)
298 item[active_item].toggled = false;
302 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
314 Menu::draw_item(int index, // Position of the current item in the menu
320 const menu_item_type& pitem = item[index];
322 int effect_offset = 0;
325 if(timer_check(&effect))
326 effect_time = timer_get_left(&effect) / 4;
328 effect_offset = (index % 2) ? effect_time : -effect_time;
332 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
334 int text_width = strlen(pitem.text) * font_width;
335 int input_width = strlen(pitem.input) * font_width;
336 int list_width = strlen(string_list_active(pitem.list)) * font_width;
337 text_type* text_font = &white_text;
340 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
342 if(index == active_item)
345 text_font = &blue_text;
352 text_draw_align(&black_text, pitem.text,
354 A_HMIDDLE, A_VMIDDLE, 2);
360 int x = pos_x - menu_width/2;
361 int y = y_pos - 12 - effect_offset;
362 /* Draw a horizontal line with a little 3d effect */
373 text_draw_align(&white_big_text, pitem.text,
375 A_HMIDDLE, A_VMIDDLE, 2);
380 case MN_CONTROLFIELD:
382 int input_pos = input_width/2;
383 int text_pos = (text_width + font_width)/2;
385 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
386 input_width + font_width + 2, 20,
388 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
389 input_width + font_width, 18,
392 text_draw_align(&gold_text, pitem.input,
393 x_pos + text_pos, y_pos,
394 A_HMIDDLE, A_VMIDDLE, 2);
396 text_draw_align(text_font, pitem.text,
397 x_pos - (input_width + font_width)/2, y_pos,
398 A_HMIDDLE, A_VMIDDLE, shadow_size);
401 case MN_STRINGSELECT:
403 int list_pos_2 = list_width + font_width;
404 int list_pos = list_width/2;
405 int text_pos = (text_width + font_width)/2;
408 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
409 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
411 /* Draw input background */
412 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
415 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
419 text_draw_align(&gold_text, string_list_active(pitem.list),
420 x_pos + text_pos, y_pos,
421 A_HMIDDLE, A_VMIDDLE,2);
423 text_draw_align(text_font, pitem.text,
424 x_pos - list_pos_2/2, y_pos,
425 A_HMIDDLE, A_VMIDDLE, shadow_size);
430 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
431 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
437 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
440 texture_draw(&checkbox_checked,
441 x_pos + (text_width+font_width)/2,
444 texture_draw(&checkbox,
445 x_pos + (text_width+font_width)/2,
450 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
454 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
461 /* The width of the menu has to be more than the width of the text
462 with the most characters */
464 for(int i = 0; i < num_items; ++i)
466 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
470 if( item[i].kind == MN_TOGGLE)
475 return (menu_width * 16 + 48);
480 return ((num_items) * 24);
483 /* Draw the current menu. */
487 int menu_height = height();
488 int menu_width = width();
490 /* Draw a transparent background */
491 fillrect(pos_x - menu_width/2,
492 pos_y - 24*num_items/2,
493 menu_width,menu_height,150,150,150,100);
495 for(int i = 0; i < num_items; ++i)
497 draw_item(i, menu_width, menu_height);
501 /* Reset/Set global defaults */
502 void menu_reset(void)
506 menuaction = MENU_ACTION_NONE;
509 delete_character = 0;
510 mn_input_char = '\0';
514 /* Draw the current menu and execute the (menu)events */
515 void menu_process_current(void)
519 if(current_menu != NULL)
521 current_menu->action();
522 current_menu->draw();
525 menuaction = MENU_ACTION_NONE;
528 /* Check for menu event */
529 void menu_event(SDL_Event& event)
535 key = event.key.keysym.sym;
538 keymod = SDL_GetModState();
541 /* If the current unicode character is an ASCII character,
543 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
545 ch[0] = event.key.keysym.unicode & 0x7F;
550 /* An International Character. */
555 case SDLK_UP: /* Menu Up */
556 menuaction = MENU_ACTION_UP;
559 case SDLK_DOWN: /* Menu Down */
560 menuaction = MENU_ACTION_DOWN;
563 case SDLK_LEFT: /* Menu Up */
564 menuaction = MENU_ACTION_LEFT;
567 case SDLK_RIGHT: /* Menu Down */
568 menuaction = MENU_ACTION_RIGHT;
572 if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
574 menuaction = MENU_ACTION_INPUT;
579 case SDLK_RETURN: /* Menu Hit */
580 menuaction = MENU_ACTION_HIT;
585 menuaction = MENU_ACTION_REMOVE;
590 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
592 menuaction = MENU_ACTION_INPUT;
598 mn_input_char = '\0';
603 case SDL_JOYAXISMOTION:
604 if(event.jaxis.axis == JOY_Y)
606 if (event.jaxis.value > 1024)
607 menuaction = MENU_ACTION_DOWN;
608 else if (event.jaxis.value < -1024)
609 menuaction = MENU_ACTION_UP;
612 case SDL_JOYBUTTONDOWN:
613 menuaction = MENU_ACTION_HIT;
615 case SDL_MOUSEBUTTONDOWN:
618 if(x > current_menu->pos_x - current_menu->width()/2 &&
619 x < current_menu->pos_x + current_menu->width()/2 &&
620 y > current_menu->pos_y - current_menu->height()/2 &&
621 y < current_menu->pos_y + current_menu->height()/2)
623 menuaction = MENU_ACTION_HIT;
626 case SDL_MOUSEMOTION:
629 if(x > current_menu->pos_x - current_menu->width()/2 &&
630 x < current_menu->pos_x + current_menu->width()/2 &&
631 y > current_menu->pos_y - current_menu->height()/2 &&
632 y < current_menu->pos_y + current_menu->height()/2)
634 current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24;
636 mouse_cursor->set_state(MC_LINK);
640 mouse_cursor->set_state(MC_NORMAL);