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;
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);
138 timer_init(&effect,false);
141 void Menu::set_pos(int x, int y, float rw, float rh)
143 pos_x = x + (int)((float)width() * rw);
144 pos_y = y + (int)((float)height() * rh);
148 Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
150 additem(menu_item_create(kind, text, toggle, menu));
153 /* Add an item to a menu */
155 Menu::additem(menu_item_type* pmenu_item)
158 item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items);
159 memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type));
163 /* Process actions done on the menu */
167 if(num_items != 0 && item != NULL)
175 active_item = num_items-1;
178 case MENU_ACTION_DOWN:
179 if(active_item < num_items-1)
185 case MENU_ACTION_LEFT:
186 if(item[active_item].kind == MN_STRINGSELECT
187 && item[active_item].list->num_items != 0)
189 if(item[active_item].list->active_item > 0)
190 --item[active_item].list->active_item;
192 item[active_item].list->active_item = item[active_item].list->num_items-1;
196 case MENU_ACTION_RIGHT:
197 if(item[active_item].kind == MN_STRINGSELECT
198 && item[active_item].list->num_items != 0)
200 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
201 ++item[active_item].list->active_item;
203 item[active_item].list->active_item = 0;
207 case MENU_ACTION_HIT:
209 switch (item[active_item].kind)
212 if (item[active_item].target_menu != NULL)
213 Menu::set_current(item[active_item].target_menu);
219 item[active_item].toggled = !item[active_item].toggled;
226 case MN_CONTROLFIELD:
227 item[active_item].toggled = true;
231 if(last_menu != NULL)
232 Menu::set_current(last_menu);
240 case MENU_ACTION_REMOVE:
241 if(item[active_item].kind == MN_TEXTFIELD
242 || item[active_item].kind == MN_NUMFIELD)
244 if(item[active_item].input != NULL)
246 int i = strlen(item[active_item].input);
248 while(delete_character > 0) /* remove charactes */
250 item[active_item].input[i-1] = '\0';
257 case MENU_ACTION_INPUT:
258 if(item[active_item].kind == MN_TEXTFIELD
259 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
261 if(item[active_item].input != NULL)
263 int i = strlen(item[active_item].input);
264 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
265 item[active_item].input[i] = mn_input_char;
266 item[active_item].input[i+1] = '\0';
270 item[active_item].input = (char*) malloc(2*sizeof(char));
271 item[active_item].input[0] = mn_input_char;
272 item[active_item].input[1] = '\0';
277 case MENU_ACTION_NONE:
282 menu_item_type& new_item = item[active_item];
283 if(new_item.kind == MN_DEACTIVE
284 || new_item.kind == MN_LABEL
285 || new_item.kind == MN_HL)
287 // Skip the horzontal line item
288 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
289 menuaction = MENU_ACTION_DOWN;
296 /* Check, if the value of the active menu item has changed. */
300 if(num_items != 0 && item != NULL)
302 if((item[active_item].kind == MN_ACTION
303 || item[active_item].kind == MN_TEXTFIELD
304 || item[active_item].kind == MN_NUMFIELD)
305 && item[active_item].toggled)
307 item[active_item].toggled = false;
311 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
323 Menu::draw_item(int index, // Position of the current item in the menu
329 const menu_item_type& pitem = item[index];
331 int effect_offset = 0;
334 if(timer_check(&effect))
335 effect_time = timer_get_left(&effect) / 4;
337 effect_offset = (index % 2) ? effect_time : -effect_time;
341 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
343 int text_width = strlen(pitem.text) * font_width;
344 int input_width = strlen(pitem.input) * font_width;
345 int list_width = strlen(string_list_active(pitem.list)) * font_width;
346 text_type* text_font = &white_text;
349 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
351 if(index == active_item)
354 text_font = &blue_text;
361 text_draw_align(&black_text, pitem.text,
363 A_HMIDDLE, A_VMIDDLE, 2);
369 int x = pos_x - menu_width/2;
370 int y = y_pos - 12 - effect_offset;
371 /* Draw a horizontal line with a little 3d effect */
382 text_draw_align(&white_big_text, pitem.text,
384 A_HMIDDLE, A_VMIDDLE, 2);
389 case MN_CONTROLFIELD:
391 int input_pos = input_width/2;
392 int text_pos = (text_width + font_width)/2;
394 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
395 input_width + font_width + 2, 20,
397 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
398 input_width + font_width, 18,
401 text_draw_align(&gold_text, pitem.input,
402 x_pos + text_pos, y_pos,
403 A_HMIDDLE, A_VMIDDLE, 2);
405 text_draw_align(text_font, pitem.text,
406 x_pos - (input_width + font_width)/2, y_pos,
407 A_HMIDDLE, A_VMIDDLE, shadow_size);
410 case MN_STRINGSELECT:
412 int list_pos_2 = list_width + font_width;
413 int list_pos = list_width/2;
414 int text_pos = (text_width + font_width)/2;
417 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
418 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
420 /* Draw input background */
421 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
424 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
428 text_draw_align(&gold_text, string_list_active(pitem.list),
429 x_pos + text_pos, y_pos,
430 A_HMIDDLE, A_VMIDDLE,2);
432 text_draw_align(text_font, pitem.text,
433 x_pos - list_pos_2/2, y_pos,
434 A_HMIDDLE, A_VMIDDLE, shadow_size);
439 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
440 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
446 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
449 texture_draw(&checkbox_checked,
450 x_pos + (text_width+font_width)/2,
453 texture_draw(&checkbox,
454 x_pos + (text_width+font_width)/2,
459 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
463 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
470 /* The width of the menu has to be more than the width of the text
471 with the most characters */
473 for(int i = 0; i < num_items; ++i)
475 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
479 if( item[i].kind == MN_TOGGLE)
484 return (menu_width * 16 + 48);
489 return ((num_items) * 24);
492 /* Draw the current menu. */
496 int menu_height = height();
497 int menu_width = width();
499 /* Draw a transparent background */
500 fillrect(pos_x - menu_width/2,
501 pos_y - 24*num_items/2,
502 menu_width,menu_height,
505 for(int i = 0; i < num_items; ++i)
507 draw_item(i, menu_width, menu_height);
511 /* Reset/Set global defaults */
512 void menu_reset(void)
516 menuaction = MENU_ACTION_NONE;
519 delete_character = 0;
520 mn_input_char = '\0';
524 /* Draw the current menu and execute the (menu)events */
525 void menu_process_current(void)
529 if(current_menu != NULL)
531 current_menu->action();
532 current_menu->draw();
535 menuaction = MENU_ACTION_NONE;
538 /* Check for menu event */
539 void menu_event(SDL_Event& event)
545 key = event.key.keysym.sym;
548 keymod = SDL_GetModState();
551 /* If the current unicode character is an ASCII character,
553 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
555 ch[0] = event.key.keysym.unicode & 0x7F;
560 /* An International Character. */
565 case SDLK_UP: /* Menu Up */
566 menuaction = MENU_ACTION_UP;
569 case SDLK_DOWN: /* Menu Down */
570 menuaction = MENU_ACTION_DOWN;
573 case SDLK_LEFT: /* Menu Up */
574 menuaction = MENU_ACTION_LEFT;
577 case SDLK_RIGHT: /* Menu Down */
578 menuaction = MENU_ACTION_RIGHT;
582 if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
584 menuaction = MENU_ACTION_INPUT;
589 case SDLK_RETURN: /* Menu Hit */
590 menuaction = MENU_ACTION_HIT;
595 menuaction = MENU_ACTION_REMOVE;
600 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
602 menuaction = MENU_ACTION_INPUT;
608 mn_input_char = '\0';
613 case SDL_JOYAXISMOTION:
614 if(event.jaxis.axis == JOY_Y)
616 if (event.jaxis.value > 1024)
617 menuaction = MENU_ACTION_DOWN;
618 else if (event.jaxis.value < -1024)
619 menuaction = MENU_ACTION_UP;
622 case SDL_JOYBUTTONDOWN:
623 menuaction = MENU_ACTION_HIT;
625 case SDL_MOUSEBUTTONDOWN:
628 if(x > current_menu->pos_x - current_menu->width()/2 &&
629 x < current_menu->pos_x + current_menu->width()/2 &&
630 y > current_menu->pos_y - current_menu->height()/2 &&
631 y < current_menu->pos_y + current_menu->height()/2)
633 menuaction = MENU_ACTION_HIT;
636 case SDL_MOUSEMOTION:
639 if(x > current_menu->pos_x - current_menu->width()/2 &&
640 x < current_menu->pos_x + current_menu->width()/2 &&
641 y > current_menu->pos_y - current_menu->height()/2 &&
642 y < current_menu->pos_y + current_menu->height()/2)
644 current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24;
646 mouse_cursor->set_state(MC_LINK);
650 mouse_cursor->set_state(MC_NORMAL);