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;
47 Menu* contrib_subset_menu = 0;
49 Menu* current_menu = 0;
51 /* input implementation variables */
55 /* Set the current menu */
57 Menu::set_current(Menu* pmenu)
59 if(pmenu != current_menu)
62 Menu* tmp = current_menu;
65 if(tmp->last_menu != pmenu)
66 current_menu->last_menu = tmp;
68 pmenu->effect.start(500);
72 /* Return a pointer to a new menu item */
74 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
76 MenuItem *pnew_item = new MenuItem;
78 pnew_item->kind = kind_;
79 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
80 strcpy(pnew_item->text, text_);
82 if(kind_ == MN_TOGGLE)
83 pnew_item->toggled = init_toggle_;
85 pnew_item->toggled = false;
87 pnew_item->target_menu = target_menu_;
88 pnew_item->input = (char*) malloc(sizeof(char));
89 pnew_item->input[0] = '\0';
91 if(kind_ == MN_STRINGSELECT)
93 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
94 string_list_init(pnew_item->list);
97 pnew_item->list = NULL;
102 MenuItem::change_text(const char *text_)
107 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
113 MenuItem::change_input(const char *text_)
118 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
119 strcpy(input, text_);
123 /* Free a menu and all its items */
128 for(unsigned int i = 0; i < item.size(); ++i)
132 string_list_free(item[i].list);
148 void Menu::set_pos(int x, int y, float rw, float rh)
150 pos_x = x + (int)((float)width() * rw);
151 pos_y = y + (int)((float)height() * rh);
155 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
157 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
160 /* Add an item to a menu */
162 Menu::additem(MenuItem* pmenu_item)
164 item.push_back(*pmenu_item);
174 /* Process actions done on the menu */
186 active_item = int(item.size())-1;
189 case MENU_ACTION_DOWN:
190 if(active_item < int(item.size())-1)
196 case MENU_ACTION_LEFT:
197 if(item[active_item].kind == MN_STRINGSELECT
198 && item[active_item].list->num_items != 0)
200 if(item[active_item].list->active_item > 0)
201 --item[active_item].list->active_item;
203 item[active_item].list->active_item = item[active_item].list->num_items-1;
207 case MENU_ACTION_RIGHT:
208 if(item[active_item].kind == MN_STRINGSELECT
209 && item[active_item].list->num_items != 0)
211 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
212 ++item[active_item].list->active_item;
214 item[active_item].list->active_item = 0;
218 case MENU_ACTION_HIT:
220 switch (item[active_item].kind)
223 if (item[active_item].target_menu != NULL)
224 Menu::set_current(item[active_item].target_menu);
230 item[active_item].toggled = !item[active_item].toggled;
237 case MN_CONTROLFIELD:
238 item[active_item].toggled = true;
242 if(last_menu != NULL)
243 Menu::set_current(last_menu);
251 case MENU_ACTION_REMOVE:
252 if(item[active_item].kind == MN_TEXTFIELD
253 || item[active_item].kind == MN_NUMFIELD)
255 if(item[active_item].input != NULL)
257 int i = strlen(item[active_item].input);
259 while(delete_character > 0) /* remove charactes */
261 item[active_item].input[i-1] = '\0';
268 case MENU_ACTION_INPUT:
269 if(item[active_item].kind == MN_TEXTFIELD
270 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
272 if(item[active_item].input != NULL)
274 int i = strlen(item[active_item].input);
275 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
276 item[active_item].input[i] = mn_input_char;
277 item[active_item].input[i+1] = '\0';
281 item[active_item].input = (char*) malloc(2*sizeof(char));
282 item[active_item].input[0] = mn_input_char;
283 item[active_item].input[1] = '\0';
288 case MENU_ACTION_NONE:
293 MenuItem& new_item = item[active_item];
294 if(new_item.kind == MN_DEACTIVE
295 || new_item.kind == MN_LABEL
296 || new_item.kind == MN_HL)
298 // Skip the horzontal line item
299 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
300 menuaction = MENU_ACTION_DOWN;
310 if (item.size() != 0)
312 if((item[active_item].kind == MN_ACTION
313 || item[active_item].kind == MN_TEXTFIELD
314 || item[active_item].kind == MN_NUMFIELD)
315 && item[active_item].toggled)
317 item[active_item].toggled = false;
321 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
333 Menu::draw_item(int index, // Position of the current item in the menu
337 const MenuItem& pitem = item[index];
340 int effect_offset = 0;
345 effect_time = effect.get_left() / 4;
347 effect_offset = (index % 2) ? effect_time : -effect_time;
351 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
353 int text_width = strlen(pitem.text) * font_width;
354 int input_width = strlen(pitem.input) * font_width;
355 int list_width = strlen(string_list_active(pitem.list)) * font_width;
356 text_type* text_font = &white_text;
359 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
361 if(index == active_item)
364 text_font = &blue_text;
371 text_draw_align(&black_text, pitem.text,
373 A_HMIDDLE, A_VMIDDLE, 2);
379 int x = pos_x - menu_width/2;
380 int y = y_pos - 12 - effect_offset;
381 /* Draw a horizontal line with a little 3d effect */
392 text_draw_align(&white_big_text, pitem.text,
394 A_HMIDDLE, A_VMIDDLE, 2);
399 case MN_CONTROLFIELD:
401 int input_pos = input_width/2;
402 int text_pos = (text_width + font_width)/2;
404 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
405 input_width + font_width + 2, 20,
407 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
408 input_width + font_width, 18,
411 text_draw_align(&gold_text, pitem.input,
412 x_pos + text_pos, y_pos,
413 A_HMIDDLE, A_VMIDDLE, 2);
415 text_draw_align(text_font, pitem.text,
416 x_pos - (input_width + font_width)/2, y_pos,
417 A_HMIDDLE, A_VMIDDLE, shadow_size);
420 case MN_STRINGSELECT:
422 int list_pos_2 = list_width + font_width;
423 int list_pos = list_width/2;
424 int text_pos = (text_width + font_width)/2;
427 texture_draw(&arrow_left, x_pos - list_pos + text_pos - 17, y_pos - 8);
428 texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
430 /* Draw input background */
431 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
434 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
438 text_draw_align(&gold_text, string_list_active(pitem.list),
439 x_pos + text_pos, y_pos,
440 A_HMIDDLE, A_VMIDDLE,2);
442 text_draw_align(text_font, pitem.text,
443 x_pos - list_pos_2/2, y_pos,
444 A_HMIDDLE, A_VMIDDLE, shadow_size);
449 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
450 texture_draw(&back, x_pos + text_width/2 + font_width, y_pos - 8);
456 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
459 texture_draw(&checkbox_checked,
460 x_pos + (text_width+font_width)/2,
463 texture_draw(&checkbox,
464 x_pos + (text_width+font_width)/2,
469 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
473 text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
480 /* The width of the menu has to be more than the width of the text
481 with the most characters */
483 for(unsigned int i = 0; i < item.size(); ++i)
485 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
489 if( item[i].kind == MN_TOGGLE)
494 return (menu_width * 16 + 24);
499 return item.size() * 24;
502 /* Draw the current menu. */
506 int menu_height = height();
507 int menu_width = width();
509 /* Draw a transparent background */
510 fillrect(pos_x - menu_width/2,
511 pos_y - 24*item.size()/2 - 10,
512 menu_width,menu_height + 20,
515 for(unsigned int i = 0; i < item.size(); ++i)
517 draw_item(i, menu_width, menu_height);
521 /* Reset/Set global defaults */
522 void menu_reset(void)
526 menuaction = MENU_ACTION_NONE;
529 delete_character = 0;
530 mn_input_char = '\0';
534 /* Draw the current menu and execute the (menu)events */
535 void menu_process_current(void)
539 if(current_menu != NULL)
541 current_menu->action();
542 current_menu->draw();
545 menuaction = MENU_ACTION_NONE;
548 /* Check for menu event */
550 Menu::event(SDL_Event& event)
556 key = event.key.keysym.sym;
559 keymod = SDL_GetModState();
562 /* If the current unicode character is an ASCII character,
564 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
566 ch[0] = event.key.keysym.unicode & 0x7F;
571 /* An International Character. */
576 case SDLK_UP: /* Menu Up */
577 menuaction = MENU_ACTION_UP;
580 case SDLK_DOWN: /* Menu Down */
581 menuaction = MENU_ACTION_DOWN;
584 case SDLK_LEFT: /* Menu Up */
585 menuaction = MENU_ACTION_LEFT;
588 case SDLK_RIGHT: /* Menu Down */
589 menuaction = MENU_ACTION_RIGHT;
593 if(item[active_item].kind == MN_TEXTFIELD)
595 menuaction = MENU_ACTION_INPUT;
600 case SDLK_RETURN: /* Menu Hit */
601 menuaction = MENU_ACTION_HIT;
606 menuaction = MENU_ACTION_REMOVE;
611 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
613 menuaction = MENU_ACTION_INPUT;
619 mn_input_char = '\0';
624 case SDL_JOYAXISMOTION:
625 if(event.jaxis.axis == JOY_Y)
627 if (event.jaxis.value > 1024)
628 menuaction = MENU_ACTION_DOWN;
629 else if (event.jaxis.value < -1024)
630 menuaction = MENU_ACTION_UP;
633 case SDL_JOYBUTTONDOWN:
634 menuaction = MENU_ACTION_HIT;
636 case SDL_MOUSEBUTTONDOWN:
639 if(x > pos_x - width()/2 &&
640 x < pos_x + width()/2 &&
641 y > pos_y - height()/2 &&
642 y < pos_y + height()/2)
644 menuaction = MENU_ACTION_HIT;
647 case SDL_MOUSEMOTION:
650 if(x > pos_x - width()/2 &&
651 x < pos_x + width()/2 &&
652 y > pos_y - height()/2 &&
653 y < pos_y + height()/2)
655 active_item = (y - (pos_y - height()/2)) / 24;
657 mouse_cursor->set_state(MC_LINK);
661 mouse_cursor->set_state(MC_NORMAL);