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 */
127 for(unsigned int i = 0; i < item.size(); ++i)
131 string_list_free(item[i].list);
147 void Menu::set_pos(int x, int y, float rw, float rh)
149 pos_x = x + (int)((float)width() * rw);
150 pos_y = y + (int)((float)height() * rh);
154 Menu::additem(MenuItemKind kind_, char *text_, int toggle_, Menu* menu_)
156 additem(MenuItem::create(kind_, text_, toggle_, menu_));
159 /* Add an item to a menu */
161 Menu::additem(MenuItem* pmenu_item)
163 item.push_back(*pmenu_item);
167 /* Process actions done on the menu */
179 active_item = int(item.size())-1;
182 case MENU_ACTION_DOWN:
183 if(active_item < int(item.size())-1)
189 case MENU_ACTION_LEFT:
190 if(item[active_item].kind == MN_STRINGSELECT
191 && item[active_item].list->num_items != 0)
193 if(item[active_item].list->active_item > 0)
194 --item[active_item].list->active_item;
196 item[active_item].list->active_item = item[active_item].list->num_items-1;
200 case MENU_ACTION_RIGHT:
201 if(item[active_item].kind == MN_STRINGSELECT
202 && item[active_item].list->num_items != 0)
204 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
205 ++item[active_item].list->active_item;
207 item[active_item].list->active_item = 0;
211 case MENU_ACTION_HIT:
213 switch (item[active_item].kind)
216 if (item[active_item].target_menu != NULL)
217 Menu::set_current(item[active_item].target_menu);
223 item[active_item].toggled = !item[active_item].toggled;
230 case MN_CONTROLFIELD:
231 item[active_item].toggled = true;
235 if(last_menu != NULL)
236 Menu::set_current(last_menu);
244 case MENU_ACTION_REMOVE:
245 if(item[active_item].kind == MN_TEXTFIELD
246 || item[active_item].kind == MN_NUMFIELD)
248 if(item[active_item].input != NULL)
250 int i = strlen(item[active_item].input);
252 while(delete_character > 0) /* remove charactes */
254 item[active_item].input[i-1] = '\0';
261 case MENU_ACTION_INPUT:
262 if(item[active_item].kind == MN_TEXTFIELD
263 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
265 if(item[active_item].input != NULL)
267 int i = strlen(item[active_item].input);
268 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
269 item[active_item].input[i] = mn_input_char;
270 item[active_item].input[i+1] = '\0';
274 item[active_item].input = (char*) malloc(2*sizeof(char));
275 item[active_item].input[0] = mn_input_char;
276 item[active_item].input[1] = '\0';
281 case MENU_ACTION_NONE:
286 MenuItem& new_item = item[active_item];
287 if(new_item.kind == MN_DEACTIVE
288 || new_item.kind == MN_LABEL
289 || new_item.kind == MN_HL)
291 // Skip the horzontal line item
292 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
293 menuaction = MENU_ACTION_DOWN;
303 if (item.size() != 0)
305 if((item[active_item].kind == MN_ACTION
306 || item[active_item].kind == MN_TEXTFIELD
307 || item[active_item].kind == MN_NUMFIELD)
308 && item[active_item].toggled)
310 item[active_item].toggled = false;
314 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
326 Menu::draw_item(int index, // Position of the current item in the menu
330 const MenuItem& pitem = item[index];
333 int effect_offset = 0;
337 effect_time = effect.get_left() / 4;
339 effect_offset = (index % 2) ? effect_time : -effect_time;
343 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
345 int text_width = strlen(pitem.text) * font_width;
346 int input_width = strlen(pitem.input) * font_width;
347 int list_width = strlen(string_list_active(pitem.list)) * font_width;
348 text_type* text_font = &white_text;
351 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
353 if(index == active_item)
356 text_font = &blue_text;
363 text_draw_align(&black_text, pitem.text,
365 A_HMIDDLE, A_VMIDDLE, 2);
371 int x = pos_x - menu_width/2;
372 int y = y_pos - 12 - effect_offset;
373 /* Draw a horizontal line with a little 3d effect */
384 text_draw_align(&white_big_text, pitem.text,
386 A_HMIDDLE, A_VMIDDLE, 2);
391 case MN_CONTROLFIELD:
393 int input_pos = input_width/2;
394 int text_pos = (text_width + font_width)/2;
396 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
397 input_width + font_width + 2, 20,
399 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
400 input_width + font_width, 18,
403 text_draw_align(&gold_text, pitem.input,
404 x_pos + text_pos, y_pos,
405 A_HMIDDLE, A_VMIDDLE, 2);
407 text_draw_align(text_font, pitem.text,
408 x_pos - (input_width + font_width)/2, y_pos,
409 A_HMIDDLE, A_VMIDDLE, shadow_size);
412 case MN_STRINGSELECT:
414 int list_pos_2 = list_width + font_width;
415 int list_pos = list_width/2;
416 int text_pos = (text_width + font_width)/2;
419 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
420 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
422 /* Draw input background */
423 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
426 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
430 text_draw_align(&gold_text, string_list_active(pitem.list),
431 x_pos + text_pos, y_pos,
432 A_HMIDDLE, A_VMIDDLE,2);
434 text_draw_align(text_font, pitem.text,
435 x_pos - list_pos_2/2, y_pos,
436 A_HMIDDLE, A_VMIDDLE, shadow_size);
441 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
442 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
448 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
451 texture_draw(&checkbox_checked,
452 x_pos + (text_width+font_width)/2,
455 texture_draw(&checkbox,
456 x_pos + (text_width+font_width)/2,
461 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
465 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
472 /* The width of the menu has to be more than the width of the text
473 with the most characters */
475 for(unsigned int i = 0; i < item.size(); ++i)
477 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
481 if( item[i].kind == MN_TOGGLE)
486 return (menu_width * 16 + 24);
491 return item.size() * 24;
494 /* Draw the current menu. */
498 int menu_height = height();
499 int menu_width = width();
501 /* Draw a transparent background */
502 fillrect(pos_x - menu_width/2,
503 pos_y - 24*item.size()/2 - 10,
504 menu_width,menu_height + 20,
507 for(unsigned int i = 0; i < item.size(); ++i)
509 draw_item(i, menu_width, menu_height);
513 /* Reset/Set global defaults */
514 void menu_reset(void)
518 menuaction = MENU_ACTION_NONE;
521 delete_character = 0;
522 mn_input_char = '\0';
526 /* Draw the current menu and execute the (menu)events */
527 void menu_process_current(void)
531 if(current_menu != NULL)
533 current_menu->action();
534 current_menu->draw();
537 menuaction = MENU_ACTION_NONE;
540 /* Check for menu event */
542 Menu::event(SDL_Event& event)
548 key = event.key.keysym.sym;
551 keymod = SDL_GetModState();
554 /* If the current unicode character is an ASCII character,
556 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
558 ch[0] = event.key.keysym.unicode & 0x7F;
563 /* An International Character. */
568 case SDLK_UP: /* Menu Up */
569 menuaction = MENU_ACTION_UP;
572 case SDLK_DOWN: /* Menu Down */
573 menuaction = MENU_ACTION_DOWN;
576 case SDLK_LEFT: /* Menu Up */
577 menuaction = MENU_ACTION_LEFT;
580 case SDLK_RIGHT: /* Menu Down */
581 menuaction = MENU_ACTION_RIGHT;
585 if(item[active_item].kind == MN_TEXTFIELD)
587 menuaction = MENU_ACTION_INPUT;
592 case SDLK_RETURN: /* Menu Hit */
593 menuaction = MENU_ACTION_HIT;
598 menuaction = MENU_ACTION_REMOVE;
603 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
605 menuaction = MENU_ACTION_INPUT;
611 mn_input_char = '\0';
616 case SDL_JOYAXISMOTION:
617 if(event.jaxis.axis == JOY_Y)
619 if (event.jaxis.value > 1024)
620 menuaction = MENU_ACTION_DOWN;
621 else if (event.jaxis.value < -1024)
622 menuaction = MENU_ACTION_UP;
625 case SDL_JOYBUTTONDOWN:
626 menuaction = MENU_ACTION_HIT;
628 case SDL_MOUSEBUTTONDOWN:
631 if(x > pos_x - width()/2 &&
632 x < pos_x + width()/2 &&
633 y > pos_y - height()/2 &&
634 y < pos_y + height()/2)
636 menuaction = MENU_ACTION_HIT;
639 case SDL_MOUSEMOTION:
642 if(x > pos_x - width()/2 &&
643 x < pos_x + width()/2 &&
644 y > pos_y - height()/2 &&
645 y < pos_y + height()/2)
647 active_item = (y - (pos_y - height()/2)) / 24;
649 mouse_cursor->set_state(MC_LINK);
653 mouse_cursor->set_state(MC_NORMAL);