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;
46 Menu* contrib_menu = 0;
48 Menu* current_menu = 0;
50 /* input implementation variables */
54 /* Set the current menu */
56 Menu::set_current(Menu* pmenu)
58 if(pmenu != current_menu)
61 Menu* tmp = current_menu;
64 if(tmp->last_menu != pmenu)
65 current_menu->last_menu = tmp;
67 pmenu->effect.start(500);
71 /* Return a pointer to a new menu item */
73 MenuItem::create(MenuItemKind kind_, char *text_, int init_toggle_, Menu* target_menu_)
75 MenuItem *pnew_item = new MenuItem;
77 pnew_item->kind = kind_;
78 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
79 strcpy(pnew_item->text, text_);
81 if(kind_ == MN_TOGGLE)
82 pnew_item->toggled = init_toggle_;
84 pnew_item->toggled = false;
86 pnew_item->target_menu = target_menu_;
87 pnew_item->input = (char*) malloc(sizeof(char));
88 pnew_item->input[0] = '\0';
90 if(kind_ == MN_STRINGSELECT)
92 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
93 string_list_init(pnew_item->list);
96 pnew_item->list = NULL;
101 MenuItem::change_text(const char *text_)
106 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
112 MenuItem::change_input(const char *text_)
117 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
118 strcpy(input, text_);
122 /* Free a menu and all its items */
125 if(num_items != 0 && item != NULL)
127 for(int i = 0; i < num_items; ++i)
131 string_list_free(item[i].list);
150 void Menu::set_pos(int x, int y, float rw, float rh)
152 pos_x = x + (int)((float)width() * rw);
153 pos_y = y + (int)((float)height() * rh);
157 Menu::additem(MenuItemKind kind_, char *text_, int toggle_, Menu* menu_)
159 additem(MenuItem::create(kind_, text_, toggle_, menu_));
162 /* Add an item to a menu */
164 Menu::additem(MenuItem* pmenu_item)
167 item = (MenuItem*)realloc(item, sizeof(MenuItem) * num_items);
168 memcpy(&item[num_items-1], pmenu_item, sizeof(MenuItem));
172 /* Process actions done on the menu */
176 if(num_items != 0 && item != NULL)
184 active_item = num_items-1;
187 case MENU_ACTION_DOWN:
188 if(active_item < num_items-1)
194 case MENU_ACTION_LEFT:
195 if(item[active_item].kind == MN_STRINGSELECT
196 && item[active_item].list->num_items != 0)
198 if(item[active_item].list->active_item > 0)
199 --item[active_item].list->active_item;
201 item[active_item].list->active_item = item[active_item].list->num_items-1;
205 case MENU_ACTION_RIGHT:
206 if(item[active_item].kind == MN_STRINGSELECT
207 && item[active_item].list->num_items != 0)
209 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
210 ++item[active_item].list->active_item;
212 item[active_item].list->active_item = 0;
216 case MENU_ACTION_HIT:
218 switch (item[active_item].kind)
221 if (item[active_item].target_menu != NULL)
222 Menu::set_current(item[active_item].target_menu);
228 item[active_item].toggled = !item[active_item].toggled;
235 case MN_CONTROLFIELD:
236 item[active_item].toggled = true;
240 if(last_menu != NULL)
241 Menu::set_current(last_menu);
249 case MENU_ACTION_REMOVE:
250 if(item[active_item].kind == MN_TEXTFIELD
251 || item[active_item].kind == MN_NUMFIELD)
253 if(item[active_item].input != NULL)
255 int i = strlen(item[active_item].input);
257 while(delete_character > 0) /* remove charactes */
259 item[active_item].input[i-1] = '\0';
266 case MENU_ACTION_INPUT:
267 if(item[active_item].kind == MN_TEXTFIELD
268 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
270 if(item[active_item].input != NULL)
272 int i = strlen(item[active_item].input);
273 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
274 item[active_item].input[i] = mn_input_char;
275 item[active_item].input[i+1] = '\0';
279 item[active_item].input = (char*) malloc(2*sizeof(char));
280 item[active_item].input[0] = mn_input_char;
281 item[active_item].input[1] = '\0';
286 case MENU_ACTION_NONE:
291 MenuItem& new_item = item[active_item];
292 if(new_item.kind == MN_DEACTIVE
293 || new_item.kind == MN_LABEL
294 || new_item.kind == MN_HL)
296 // Skip the horzontal line item
297 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
298 menuaction = MENU_ACTION_DOWN;
308 if(num_items != 0 && item != NULL)
310 if((item[active_item].kind == MN_ACTION
311 || item[active_item].kind == MN_TEXTFIELD
312 || item[active_item].kind == MN_NUMFIELD)
313 && item[active_item].toggled)
315 item[active_item].toggled = false;
319 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
331 Menu::draw_item(int index, // Position of the current item in the menu
335 const MenuItem& pitem = item[index];
338 int effect_offset = 0;
342 effect_time = effect.get_left() / 4;
344 effect_offset = (index % 2) ? effect_time : -effect_time;
348 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
350 int text_width = strlen(pitem.text) * font_width;
351 int input_width = strlen(pitem.input) * font_width;
352 int list_width = strlen(string_list_active(pitem.list)) * font_width;
353 text_type* text_font = &white_text;
356 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
358 if(index == active_item)
361 text_font = &blue_text;
368 text_draw_align(&black_text, pitem.text,
370 A_HMIDDLE, A_VMIDDLE, 2);
376 int x = pos_x - menu_width/2;
377 int y = y_pos - 12 - effect_offset;
378 /* Draw a horizontal line with a little 3d effect */
389 text_draw_align(&white_big_text, pitem.text,
391 A_HMIDDLE, A_VMIDDLE, 2);
396 case MN_CONTROLFIELD:
398 int input_pos = input_width/2;
399 int text_pos = (text_width + font_width)/2;
401 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
402 input_width + font_width + 2, 20,
404 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
405 input_width + font_width, 18,
408 text_draw_align(&gold_text, pitem.input,
409 x_pos + text_pos, y_pos,
410 A_HMIDDLE, A_VMIDDLE, 2);
412 text_draw_align(text_font, pitem.text,
413 x_pos - (input_width + font_width)/2, y_pos,
414 A_HMIDDLE, A_VMIDDLE, shadow_size);
417 case MN_STRINGSELECT:
419 int list_pos_2 = list_width + font_width;
420 int list_pos = list_width/2;
421 int text_pos = (text_width + font_width)/2;
424 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
425 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
427 /* Draw input background */
428 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
431 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
435 text_draw_align(&gold_text, string_list_active(pitem.list),
436 x_pos + text_pos, y_pos,
437 A_HMIDDLE, A_VMIDDLE,2);
439 text_draw_align(text_font, pitem.text,
440 x_pos - list_pos_2/2, y_pos,
441 A_HMIDDLE, A_VMIDDLE, shadow_size);
446 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
447 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
453 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
456 texture_draw(&checkbox_checked,
457 x_pos + (text_width+font_width)/2,
460 texture_draw(&checkbox,
461 x_pos + (text_width+font_width)/2,
466 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
470 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
477 /* The width of the menu has to be more than the width of the text
478 with the most characters */
480 for(int i = 0; i < num_items; ++i)
482 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
486 if( item[i].kind == MN_TOGGLE)
491 return (menu_width * 16 + 24);
496 return ((num_items) * 24);
499 /* Draw the current menu. */
503 int menu_height = height();
504 int menu_width = width();
506 /* Draw a transparent background */
507 fillrect(pos_x - menu_width/2,
508 pos_y - 24*num_items/2 - 10,
509 menu_width,menu_height + 20,
512 for(int i = 0; i < num_items; ++i)
514 draw_item(i, menu_width, menu_height);
518 /* Reset/Set global defaults */
519 void menu_reset(void)
523 menuaction = MENU_ACTION_NONE;
526 delete_character = 0;
527 mn_input_char = '\0';
531 /* Draw the current menu and execute the (menu)events */
532 void menu_process_current(void)
536 if(current_menu != NULL)
538 current_menu->action();
539 current_menu->draw();
542 menuaction = MENU_ACTION_NONE;
545 /* Check for menu event */
547 Menu::event(SDL_Event& event)
553 key = event.key.keysym.sym;
556 keymod = SDL_GetModState();
559 /* If the current unicode character is an ASCII character,
561 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
563 ch[0] = event.key.keysym.unicode & 0x7F;
568 /* An International Character. */
573 case SDLK_UP: /* Menu Up */
574 menuaction = MENU_ACTION_UP;
577 case SDLK_DOWN: /* Menu Down */
578 menuaction = MENU_ACTION_DOWN;
581 case SDLK_LEFT: /* Menu Up */
582 menuaction = MENU_ACTION_LEFT;
585 case SDLK_RIGHT: /* Menu Down */
586 menuaction = MENU_ACTION_RIGHT;
590 if(item[active_item].kind == MN_TEXTFIELD)
592 menuaction = MENU_ACTION_INPUT;
597 case SDLK_RETURN: /* Menu Hit */
598 menuaction = MENU_ACTION_HIT;
603 menuaction = MENU_ACTION_REMOVE;
608 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
610 menuaction = MENU_ACTION_INPUT;
616 mn_input_char = '\0';
621 case SDL_JOYAXISMOTION:
622 if(event.jaxis.axis == JOY_Y)
624 if (event.jaxis.value > 1024)
625 menuaction = MENU_ACTION_DOWN;
626 else if (event.jaxis.value < -1024)
627 menuaction = MENU_ACTION_UP;
630 case SDL_JOYBUTTONDOWN:
631 menuaction = MENU_ACTION_HIT;
633 case SDL_MOUSEBUTTONDOWN:
636 if(x > pos_x - width()/2 &&
637 x < pos_x + width()/2 &&
638 y > pos_y - height()/2 &&
639 y < pos_y + height()/2)
641 menuaction = MENU_ACTION_HIT;
644 case SDL_MOUSEMOTION:
647 if(x > pos_x - width()/2 &&
648 x < pos_x + width()/2 &&
649 y > pos_y - height()/2 &&
650 y < pos_y + height()/2)
652 active_item = (y - (pos_y - height()/2)) / 24;
654 mouse_cursor->set_state(MC_LINK);
658 mouse_cursor->set_state(MC_NORMAL);