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;
39 menu_type main_menu, game_menu, options_menu, highscore_menu, load_game_menu, save_game_menu;
40 menu_type* current_menu, * last_menu;
42 /* input implementation variables */
46 /* Set the current menu */
47 void menu_set_current(menu_type* pmenu)
49 if(pmenu != current_menu)
52 last_menu = current_menu;
54 timer_start(&pmenu->effect, 500);
58 /* Return a pointer to a new menu item */
59 menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, void* target_menu)
61 menu_item_type *pnew_item = (menu_item_type*) malloc(sizeof(menu_item_type));
62 pnew_item->kind = kind;
63 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text) + 1));
64 strcpy(pnew_item->text,text);
66 pnew_item->toggled = init_toggle;
68 pnew_item->toggled = NO;
69 pnew_item->target_menu = target_menu;
70 pnew_item->input = (char*) malloc(sizeof(char));
71 pnew_item->input[0] = '\0';
72 if(kind == MN_STRINGSELECT)
74 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
75 string_list_init(pnew_item->list);
78 pnew_item->list = NULL;
82 void menu_item_change_text(menu_item_type* pmenu_item,const char *text)
86 free(pmenu_item->text);
87 pmenu_item->text = (char*) malloc(sizeof(char )*(strlen(text)+1));
88 strcpy(pmenu_item->text,text);
91 void menu_item_change_input(menu_item_type* pmenu_item,const char *text)
95 free(pmenu_item->input);
96 pmenu_item->input = (char*) malloc(sizeof(char )*(strlen(text)+1));
97 strcpy(pmenu_item->input,text);
101 /* Free a menu and all its items */
102 void menu_free(menu_type* pmenu)
105 if(pmenu->num_items != 0 && pmenu->item != NULL)
107 for(i = 0; i < pmenu->num_items; ++i)
109 free(pmenu->item[i].text);
110 free(pmenu->item[i].input);
111 string_list_free(pmenu->item[i].list);
117 /* Initialize a menu */
118 void menu_init(menu_type* pmenu)
120 pmenu->arrange_left = 0;
121 pmenu->num_items = 0;
122 pmenu->active_item = 0;
124 timer_init(&pmenu->effect,NO);
127 /* Add an item to a menu */
128 void menu_additem(menu_type* pmenu, menu_item_type* pmenu_item)
131 pmenu->item = (menu_item_type*)realloc(pmenu->item, sizeof(menu_item_type) * pmenu->num_items);
132 memcpy(&pmenu->item[pmenu->num_items-1],pmenu_item,sizeof(menu_item_type));
136 /* Process actions done on the menu */
137 void menu_action(menu_type* pmenu)
139 menu_item_type& item = pmenu->item[pmenu->active_item];
141 if(pmenu->num_items != 0 && pmenu->item != NULL)
146 if(pmenu->active_item > 0)
147 --pmenu->active_item;
149 pmenu->active_item = pmenu->num_items-1;
152 case MENU_ACTION_DOWN:
153 if(pmenu->active_item < pmenu->num_items-1)
154 ++pmenu->active_item;
156 pmenu->active_item = 0;
159 case MENU_ACTION_LEFT:
160 if(item.kind == MN_STRINGSELECT
161 && item.list->num_items != 0)
163 if(item.list->active_item > 0)
164 --item.list->active_item;
166 item.list->active_item = item.list->num_items-1;
169 case MENU_ACTION_RIGHT:
170 if(item.kind == MN_STRINGSELECT
171 && item.list->num_items != 0)
173 if(item.list->active_item < item.list->num_items-1)
174 ++item.list->active_item;
176 item.list->active_item = 0;
180 case MENU_ACTION_HIT:
181 if(item.kind == MN_GOTO
182 && item.target_menu != NULL)
183 menu_set_current((menu_type*)item.target_menu);
185 else if(item.kind == MN_TOGGLE)
187 item.toggled = !item.toggled;
190 else if(item.kind == MN_ACTION || item.kind == MN_TEXTFIELD || item.kind == MN_NUMFIELD)
194 else if(item.kind == MN_BACK)
196 if(last_menu != NULL)
197 menu_set_current(last_menu);
201 case MENU_ACTION_REMOVE:
202 if(item.kind == MN_TEXTFIELD
203 || item.kind == MN_NUMFIELD)
205 if(item.input != NULL)
207 int i = strlen(item.input);
209 while(delete_character > 0) /* remove charactes */
211 item.input[i-1] = '\0';
218 case MENU_ACTION_INPUT:
219 if(item.kind == MN_TEXTFIELD
220 || (item.kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
222 if(item.input != NULL)
224 int i = strlen(item.input);
225 item.input = (char*) realloc(item.input,sizeof(char)*(i + 2));
226 item.input[i] = mn_input_char;
227 item.input[i+1] = '\0';
231 item.input = (char*) malloc(2*sizeof(char));
232 item.input[0] = mn_input_char;
233 item.input[1] = '\0';
238 case MENU_ACTION_NONE:
243 menu_item_type& new_item = pmenu->item[pmenu->active_item];
244 if(new_item.kind == MN_DEACTIVE
245 || new_item.kind == MN_LABEL
246 || new_item.kind == MN_HL)
248 // Skip the horzontal line item
249 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
250 menuaction = MENU_ACTION_DOWN;
252 if(pmenu->num_items > 1)
257 /* Check, if the value of the active menu item has changed. */
258 int menu_check(menu_type* pmenu)
260 menu_item_type& item = pmenu->item[pmenu->active_item];
262 if(pmenu->num_items != 0 && pmenu->item != NULL)
264 if((item.kind == MN_ACTION || item.kind == MN_TEXTFIELD || item.kind == MN_NUMFIELD) && item.toggled == YES)
268 return pmenu->active_item;
270 else if(item.kind == MN_TOGGLE || item.kind == MN_GOTO)
272 return pmenu->active_item;
281 void menu_draw_item(menu_type* pmenu,
282 int index, // Position of the current item in the menu
288 const menu_item_type& pitem = pmenu->item[index];
290 int effect_offset = 0;
293 if(timer_check(&pmenu->effect))
294 effect_time = timer_get_left(&pmenu->effect) / 4;
296 effect_offset = (index % 2) ? effect_time : -effect_time;
299 int x_pos = screen->w/2;
300 int y_pos = screen->h/2 + 24*index - menu_height/2 + 12 + effect_offset;
302 int text_width = strlen(pitem.text) * font_width;
303 int input_width = strlen(pitem.input) * font_width;
304 int list_width = strlen(string_list_active(pitem.list)) * font_width;
305 text_type* text_font = &white_text;
307 if(pmenu->arrange_left == YES)
308 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
310 if(index == pmenu->active_item)
313 text_font = &blue_text;
320 text_draw_align(&black_text, pitem.text,
322 A_HMIDDLE, A_VMIDDLE, 2);
328 int x = screen->w/2 - menu_width/2;
329 int y = y_pos - 12 - effect_offset;
330 /* Draw a horizontal line with a little 3d effect */
334 fillrect(x, y + 10 + 6,
341 text_draw_align(&white_big_text, pitem.text,
343 A_HMIDDLE, A_VMIDDLE, 2);
349 int input_pos = input_width/2;
350 int text_pos = (text_width + font_width)/2;
352 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
353 input_width + font_width + 2, 20,
355 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
356 input_width + font_width, 18,
359 text_draw_align(&gold_text, pitem.input,
360 x_pos + text_pos, y_pos,
361 A_HMIDDLE, A_VMIDDLE, 2);
363 text_draw_align(text_font, pitem.text,
364 x_pos - (input_width + font_width)/2, y_pos,
365 A_HMIDDLE, A_VMIDDLE, shadow_size);
368 case MN_STRINGSELECT:
370 int list_pos_2 = list_width + font_width;
371 int list_pos = list_width/2;
372 int text_pos = (text_width + font_width)/2;
375 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
376 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
378 /* Draw input background */
379 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
382 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
386 text_draw_align(&gold_text, string_list_active(pitem.list),
387 x_pos + text_pos, y_pos,
388 A_HMIDDLE, A_VMIDDLE,2);
390 text_draw_align(text_font, pitem.text,
391 x_pos - list_pos_2/2, y_pos,
392 A_HMIDDLE, A_VMIDDLE, shadow_size);
397 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
398 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
404 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
406 if(pitem.toggled == YES)
407 texture_draw(&checkbox_checked,
408 x_pos + (text_width+font_width)/2,
411 texture_draw(&checkbox,
412 x_pos + (text_width+font_width)/2,
417 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
421 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
426 /* Draw the current menu. */
427 void menu_draw(menu_type* pmenu)
433 /* The width of the menu has to be more than the width of the text with the most characters */
435 for(int i = 0; i < pmenu->num_items; ++i)
437 y = strlen(pmenu->item[i].text) + (pmenu->item[i].input ? strlen(pmenu->item[i].input) + 1 : 0) + strlen(string_list_active(pmenu->item[i].list));
441 if( pmenu->item[i].kind == MN_TOGGLE)
446 menu_width = menu_width * 16 + 48;
447 menu_height = (pmenu->num_items) * 24;
449 int center_x = screen->w/2;
450 /* Draw a transparent background */
451 fillrect(center_x - menu_width/2,screen->h/2-(((pmenu->num_items)*24)/2),menu_width,menu_height,150,150,150,100);
453 for(int i = 0; i < pmenu->num_items; ++i)
455 menu_draw_item(pmenu, i, menu_width, menu_height);
459 /* Reset/Set global defaults */
460 void menu_reset(void)
464 menuaction = MENU_ACTION_NONE;
468 delete_character = 0;
469 mn_input_char = '\0';
473 /* Draw the current menu and execute the (menu)events */
474 void menu_process_current(void)
478 if(current_menu != NULL)
480 menu_action(current_menu);
481 menu_draw(current_menu);
484 menuaction = MENU_ACTION_NONE;
487 /* Check for menu event */
488 void menu_event(SDL_keysym* keysym)
490 SDLKey key = keysym->sym;
493 keymod = SDL_GetModState();
495 /* If the current unicode character is an ASCII character,
497 if ( (keysym->unicode & 0xFF80) == 0 )
499 ch[0] = keysym->unicode & 0x7F;
504 /* An International Character. */
509 case SDLK_UP: /* Menu Up */
510 menuaction = MENU_ACTION_UP;
513 case SDLK_DOWN: /* Menu Down */
514 menuaction = MENU_ACTION_DOWN;
517 case SDLK_LEFT: /* Menu Up */
518 menuaction = MENU_ACTION_LEFT;
521 case SDLK_RIGHT: /* Menu Down */
522 menuaction = MENU_ACTION_RIGHT;
526 if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
528 menuaction = MENU_ACTION_INPUT;
533 case SDLK_RETURN: /* Menu Hit */
534 menuaction = MENU_ACTION_HIT;
539 menuaction = MENU_ACTION_REMOVE;
544 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
546 menuaction = MENU_ACTION_INPUT;
552 mn_input_char = '\0';
558 /* FIXME: NO JOYSTICK SUPPORT */
560 else if (event.type == SDL_JOYBUTTONDOWN)
562 Joystick button: Continue:
570 menu_additem(menu_type* pmenu, MenuItemKind kind, char *text, int init_toggle, void* target_menu)
572 menu_item_type* item = menu_item_create(kind, text, init_toggle, target_menu);
573 menu_additem(pmenu, item);