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* worldmap_menu = 0;
42 Menu* options_menu = 0;
43 Menu* options_controls_menu = 0;
44 Menu* highscore_menu = 0;
45 Menu* load_game_menu = 0;
46 Menu* save_game_menu = 0;
47 Menu* contrib_menu = 0;
48 Menu* contrib_subset_menu = 0;
50 Menu* current_menu = 0;
52 /* input implementation variables */
56 /* Set the current menu */
58 Menu::set_current(Menu* pmenu)
60 if(pmenu != current_menu)
63 Menu* tmp = current_menu;
66 if(tmp->last_menu != pmenu)
67 current_menu->last_menu = tmp;
69 pmenu->effect.start(500);
73 /* Return a pointer to a new menu item */
75 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
77 MenuItem *pnew_item = new MenuItem;
79 pnew_item->kind = kind_;
80 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
81 strcpy(pnew_item->text, text_);
83 if(kind_ == MN_TOGGLE)
84 pnew_item->toggled = init_toggle_;
86 pnew_item->toggled = false;
88 pnew_item->target_menu = target_menu_;
89 pnew_item->input = (char*) malloc(sizeof(char));
90 pnew_item->input[0] = '\0';
92 if(kind_ == MN_STRINGSELECT)
94 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
95 string_list_init(pnew_item->list);
98 pnew_item->list = NULL;
103 MenuItem::change_text(const char *text_)
108 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
114 MenuItem::change_input(const char *text_)
119 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
120 strcpy(input, text_);
124 /* Free a menu and all its items */
129 for(unsigned int i = 0; i < item.size(); ++i)
133 string_list_free(item[i].list);
149 void Menu::set_pos(int x, int y, float rw, float rh)
151 pos_x = x + (int)((float)width() * rw);
152 pos_y = y + (int)((float)height() * rh);
156 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
158 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
161 /* Add an item to a menu */
163 Menu::additem(MenuItem* pmenu_item)
165 item.push_back(*pmenu_item);
175 /* Process actions done on the menu */
187 active_item = int(item.size())-1;
190 case MENU_ACTION_DOWN:
191 if(active_item < int(item.size())-1)
197 case MENU_ACTION_LEFT:
198 if(item[active_item].kind == MN_STRINGSELECT
199 && item[active_item].list->num_items != 0)
201 if(item[active_item].list->active_item > 0)
202 --item[active_item].list->active_item;
204 item[active_item].list->active_item = item[active_item].list->num_items-1;
208 case MENU_ACTION_RIGHT:
209 if(item[active_item].kind == MN_STRINGSELECT
210 && item[active_item].list->num_items != 0)
212 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
213 ++item[active_item].list->active_item;
215 item[active_item].list->active_item = 0;
219 case MENU_ACTION_HIT:
221 switch (item[active_item].kind)
224 if (item[active_item].target_menu != NULL)
225 Menu::set_current(item[active_item].target_menu);
231 item[active_item].toggled = !item[active_item].toggled;
238 case MN_CONTROLFIELD:
239 item[active_item].toggled = true;
243 if(last_menu != NULL)
244 Menu::set_current(last_menu);
252 case MENU_ACTION_REMOVE:
253 if(item[active_item].kind == MN_TEXTFIELD
254 || item[active_item].kind == MN_NUMFIELD)
256 if(item[active_item].input != NULL)
258 int i = strlen(item[active_item].input);
260 while(delete_character > 0) /* remove charactes */
262 item[active_item].input[i-1] = '\0';
269 case MENU_ACTION_INPUT:
270 if(item[active_item].kind == MN_TEXTFIELD
271 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
273 if(item[active_item].input != NULL)
275 int i = strlen(item[active_item].input);
276 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
277 item[active_item].input[i] = mn_input_char;
278 item[active_item].input[i+1] = '\0';
282 item[active_item].input = (char*) malloc(2*sizeof(char));
283 item[active_item].input[0] = mn_input_char;
284 item[active_item].input[1] = '\0';
289 case MENU_ACTION_NONE:
294 MenuItem& new_item = item[active_item];
295 if(new_item.kind == MN_DEACTIVE
296 || new_item.kind == MN_LABEL
297 || new_item.kind == MN_HL)
299 // Skip the horzontal line item
300 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
301 menuaction = MENU_ACTION_DOWN;
311 if (item.size() != 0)
313 if((item[active_item].kind == MN_ACTION
314 || item[active_item].kind == MN_TEXTFIELD
315 || item[active_item].kind == MN_NUMFIELD)
316 && item[active_item].toggled)
318 item[active_item].toggled = false;
322 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
334 Menu::draw_item(int index, // Position of the current item in the menu
338 const MenuItem& pitem = item[index];
341 int effect_offset = 0;
346 effect_time = effect.get_left() / 4;
348 effect_offset = (index % 2) ? effect_time : -effect_time;
352 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
354 int text_width = strlen(pitem.text) * font_width;
355 int input_width = strlen(pitem.input) * font_width;
356 int list_width = strlen(string_list_active(pitem.list)) * font_width;
357 text_type* text_font = &white_text;
360 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
362 if(index == active_item)
365 text_font = &blue_text;
372 text_draw_align(&black_text, pitem.text,
374 A_HMIDDLE, A_VMIDDLE, 2);
380 int x = pos_x - menu_width/2;
381 int y = y_pos - 12 - effect_offset;
382 /* Draw a horizontal line with a little 3d effect */
393 text_draw_align(&white_big_text, pitem.text,
395 A_HMIDDLE, A_VMIDDLE, 2);
400 case MN_CONTROLFIELD:
402 int input_pos = input_width/2;
403 int text_pos = (text_width + font_width)/2;
405 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
406 input_width + font_width + 2, 20,
408 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
409 input_width + font_width, 18,
412 text_draw_align(&gold_text, pitem.input,
413 x_pos + text_pos, y_pos,
414 A_HMIDDLE, A_VMIDDLE, 2);
416 text_draw_align(text_font, pitem.text,
417 x_pos - (input_width + font_width)/2, y_pos,
418 A_HMIDDLE, A_VMIDDLE, shadow_size);
421 case MN_STRINGSELECT:
423 int list_pos_2 = list_width + font_width;
424 int list_pos = list_width/2;
425 int text_pos = (text_width + font_width)/2;
428 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
429 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
431 /* Draw input background */
432 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
435 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
439 text_draw_align(&gold_text, string_list_active(pitem.list),
440 x_pos + text_pos, y_pos,
441 A_HMIDDLE, A_VMIDDLE,2);
443 text_draw_align(text_font, pitem.text,
444 x_pos - list_pos_2/2, y_pos,
445 A_HMIDDLE, A_VMIDDLE, shadow_size);
450 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
451 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
457 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
460 texture_draw(&checkbox_checked,
461 x_pos + (text_width+font_width)/2,
464 texture_draw(&checkbox,
465 x_pos + (text_width+font_width)/2,
470 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
474 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
481 /* The width of the menu has to be more than the width of the text
482 with the most characters */
484 for(unsigned int i = 0; i < item.size(); ++i)
486 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
490 if( item[i].kind == MN_TOGGLE)
495 return (menu_width * 16 + 24);
500 return item.size() * 24;
503 /* Draw the current menu. */
507 int menu_height = height();
508 int menu_width = width();
510 /* Draw a transparent background */
511 fillrect(pos_x - menu_width/2,
512 pos_y - 24*item.size()/2 - 10,
513 menu_width,menu_height + 20,
516 for(unsigned int i = 0; i < item.size(); ++i)
518 draw_item(i, menu_width, menu_height);
522 /* Reset/Set global defaults */
523 void menu_reset(void)
527 menuaction = MENU_ACTION_NONE;
530 delete_character = 0;
531 mn_input_char = '\0';
535 /* Draw the current menu and execute the (menu)events */
536 void menu_process_current(void)
540 if(current_menu != NULL)
542 current_menu->action();
543 current_menu->draw();
546 menuaction = MENU_ACTION_NONE;
549 /* Check for menu event */
551 Menu::event(SDL_Event& event)
557 key = event.key.keysym.sym;
560 keymod = SDL_GetModState();
563 /* If the current unicode character is an ASCII character,
565 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
567 ch[0] = event.key.keysym.unicode & 0x7F;
572 /* An International Character. */
577 case SDLK_UP: /* Menu Up */
578 menuaction = MENU_ACTION_UP;
581 case SDLK_DOWN: /* Menu Down */
582 menuaction = MENU_ACTION_DOWN;
585 case SDLK_LEFT: /* Menu Up */
586 menuaction = MENU_ACTION_LEFT;
589 case SDLK_RIGHT: /* Menu Down */
590 menuaction = MENU_ACTION_RIGHT;
594 if(item[active_item].kind == MN_TEXTFIELD)
596 menuaction = MENU_ACTION_INPUT;
601 case SDLK_RETURN: /* Menu Hit */
602 menuaction = MENU_ACTION_HIT;
607 menuaction = MENU_ACTION_REMOVE;
612 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
614 menuaction = MENU_ACTION_INPUT;
620 mn_input_char = '\0';
625 case SDL_JOYAXISMOTION:
626 if(event.jaxis.axis == JOY_Y)
628 if (event.jaxis.value > 1024)
629 menuaction = MENU_ACTION_DOWN;
630 else if (event.jaxis.value < -1024)
631 menuaction = MENU_ACTION_UP;
634 case SDL_JOYBUTTONDOWN:
635 menuaction = MENU_ACTION_HIT;
637 case SDL_MOUSEBUTTONDOWN:
640 if(x > pos_x - width()/2 &&
641 x < pos_x + width()/2 &&
642 y > pos_y - height()/2 &&
643 y < pos_y + height()/2)
645 menuaction = MENU_ACTION_HIT;
648 case SDL_MOUSEMOTION:
651 if(x > pos_x - width()/2 &&
652 x < pos_x + width()/2 &&
653 y > pos_y - height()/2 &&
654 y < pos_y + height()/2)
656 active_item = (y - (pos_y - height()/2)) / 24;
658 mouse_cursor->set_state(MC_LINK);
662 mouse_cursor->set_state(MC_NORMAL);