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;
39 Surface* checkbox_checked;
46 Menu* worldmap_menu = 0;
47 Menu* options_menu = 0;
48 Menu* options_controls_menu = 0;
49 Menu* highscore_menu = 0;
50 Menu* load_game_menu = 0;
51 Menu* save_game_menu = 0;
52 Menu* contrib_menu = 0;
53 Menu* contrib_subset_menu = 0;
55 Menu* current_menu = 0;
57 /* input implementation variables */
61 /* Set the current menu */
63 Menu::set_current(Menu* pmenu)
65 if(pmenu != current_menu)
68 Menu* tmp = current_menu;
71 if(tmp->last_menu != pmenu)
72 current_menu->last_menu = tmp;
74 pmenu->effect.start(500);
78 /* Return a pointer to a new menu item */
80 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
82 MenuItem *pnew_item = new MenuItem;
84 pnew_item->kind = kind_;
85 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
86 strcpy(pnew_item->text, text_);
88 if(kind_ == MN_TOGGLE)
89 pnew_item->toggled = init_toggle_;
91 pnew_item->toggled = false;
93 pnew_item->target_menu = target_menu_;
94 pnew_item->input = (char*) malloc(sizeof(char));
95 pnew_item->input[0] = '\0';
97 if(kind_ == MN_STRINGSELECT)
99 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
100 string_list_init(pnew_item->list);
103 pnew_item->list = NULL;
108 MenuItem::change_text(const char *text_)
113 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
119 MenuItem::change_input(const char *text_)
124 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
125 strcpy(input, text_);
129 /* Free a menu and all its items */
134 for(unsigned int i = 0; i < item.size(); ++i)
138 string_list_free(item[i].list);
147 has_backitem = false;
155 void Menu::set_pos(int x, int y, float rw, float rh)
157 pos_x = x + (int)((float)width() * rw);
158 pos_y = y + (int)((float)height() * rh);
162 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
167 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
170 /* Add an item to a menu */
172 Menu::additem(MenuItem* pmenu_item)
174 if(pmenu_item->kind == MN_BACK)
177 item.push_back(*pmenu_item);
187 /* Process actions done on the menu */
199 active_item = int(item.size())-1;
202 case MENU_ACTION_DOWN:
203 if(active_item < int(item.size())-1)
209 case MENU_ACTION_LEFT:
210 if(item[active_item].kind == MN_STRINGSELECT
211 && item[active_item].list->num_items != 0)
213 if(item[active_item].list->active_item > 0)
214 --item[active_item].list->active_item;
216 item[active_item].list->active_item = item[active_item].list->num_items-1;
220 case MENU_ACTION_RIGHT:
221 if(item[active_item].kind == MN_STRINGSELECT
222 && item[active_item].list->num_items != 0)
224 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
225 ++item[active_item].list->active_item;
227 item[active_item].list->active_item = 0;
231 case MENU_ACTION_HIT:
233 switch (item[active_item].kind)
236 if (item[active_item].target_menu != NULL)
237 Menu::set_current(item[active_item].target_menu);
243 item[active_item].toggled = !item[active_item].toggled;
250 case MN_CONTROLFIELD:
251 item[active_item].toggled = true;
255 if(last_menu != NULL)
256 Menu::set_current(last_menu);
264 case MENU_ACTION_REMOVE:
265 if(item[active_item].kind == MN_TEXTFIELD
266 || item[active_item].kind == MN_NUMFIELD)
268 if(item[active_item].input != NULL)
270 int i = strlen(item[active_item].input);
272 while(delete_character > 0) /* remove charactes */
274 item[active_item].input[i-1] = '\0';
281 case MENU_ACTION_INPUT:
282 if(item[active_item].kind == MN_TEXTFIELD
283 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
285 if(item[active_item].input != NULL)
287 int i = strlen(item[active_item].input);
288 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
289 item[active_item].input[i] = mn_input_char;
290 item[active_item].input[i+1] = '\0';
294 item[active_item].input = (char*) malloc(2*sizeof(char));
295 item[active_item].input[0] = mn_input_char;
296 item[active_item].input[1] = '\0';
301 case MENU_ACTION_NONE:
306 MenuItem& new_item = item[active_item];
307 if(new_item.kind == MN_DEACTIVE
308 || new_item.kind == MN_LABEL
309 || new_item.kind == MN_HL)
311 // Skip the horzontal line item
312 if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
313 menuaction = MENU_ACTION_DOWN;
323 if (item.size() != 0)
325 if((item[active_item].kind == MN_ACTION
326 || item[active_item].kind == MN_TEXTFIELD
327 || item[active_item].kind == MN_NUMFIELD)
328 && item[active_item].toggled)
330 item[active_item].toggled = false;
334 else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
346 Menu::draw_item(int index, // Position of the current item in the menu
350 const MenuItem& pitem = item[index];
353 int effect_offset = 0;
358 effect_time = effect.get_left() / 4;
360 effect_offset = (index % 2) ? effect_time : -effect_time;
364 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
366 int text_width = strlen(pitem.text) * font_width;
367 int input_width = strlen(pitem.input) * font_width;
368 int list_width = strlen(string_list_active(pitem.list)) * font_width;
369 Text* text_font = white_text;
372 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
374 if(index == active_item)
377 text_font = blue_text;
384 black_text->draw_align(pitem.text,
386 A_HMIDDLE, A_VMIDDLE, 2);
392 int x = pos_x - menu_width/2;
393 int y = y_pos - 12 - effect_offset;
394 /* Draw a horizontal line with a little 3d effect */
405 white_big_text->draw_align(pitem.text,
407 A_HMIDDLE, A_VMIDDLE, 2);
412 case MN_CONTROLFIELD:
414 int input_pos = input_width/2;
415 int text_pos = (text_width + font_width)/2;
417 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
418 input_width + font_width + 2, 20,
420 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
421 input_width + font_width, 18,
424 gold_text->draw_align(pitem.input,
425 x_pos + text_pos, y_pos,
426 A_HMIDDLE, A_VMIDDLE, 2);
428 text_font->draw_align(pitem.text,
429 x_pos - (input_width + font_width)/2, y_pos,
430 A_HMIDDLE, A_VMIDDLE, shadow_size);
433 case MN_STRINGSELECT:
435 int list_pos_2 = list_width + font_width;
436 int list_pos = list_width/2;
437 int text_pos = (text_width + font_width)/2;
440 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
441 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
443 /* Draw input background */
444 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
447 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
451 gold_text->draw_align(string_list_active(pitem.list),
452 x_pos + text_pos, y_pos,
453 A_HMIDDLE, A_VMIDDLE,2);
455 text_font->draw_align(pitem.text,
456 x_pos - list_pos_2/2, y_pos,
457 A_HMIDDLE, A_VMIDDLE, shadow_size);
462 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
463 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
469 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
472 checkbox_checked->draw(
473 x_pos + (text_width+font_width)/2,
477 x_pos + (text_width+font_width)/2,
482 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
486 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
493 /* The width of the menu has to be more than the width of the text
494 with the most characters */
496 for(unsigned int i = 0; i < item.size(); ++i)
498 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
502 if( item[i].kind == MN_TOGGLE)
507 return (menu_width * 16 + 24);
512 return item.size() * 24;
515 /* Draw the current menu. */
519 int menu_height = height();
520 int menu_width = width();
522 /* Draw a transparent background */
523 fillrect(pos_x - menu_width/2,
524 pos_y - 24*item.size()/2 - 10,
525 menu_width,menu_height + 20,
528 for(unsigned int i = 0; i < item.size(); ++i)
530 draw_item(i, menu_width, menu_height);
534 /* Reset/Set global defaults */
535 void menu_reset(void)
539 menuaction = MENU_ACTION_NONE;
542 delete_character = 0;
543 mn_input_char = '\0';
547 /* Draw the current menu and execute the (menu)events */
548 void menu_process_current(void)
555 if(current_menu != NULL)
557 current_menu->action();
558 current_menu->draw();
561 menuaction = MENU_ACTION_NONE;
564 /* Check for menu event */
566 Menu::event(SDL_Event& event)
568 if(show_menu == false && event.key.keysym.sym != SDLK_ESCAPE)
575 key = event.key.keysym.sym;
578 keymod = SDL_GetModState();
581 /* If the current unicode character is an ASCII character,
583 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
585 ch[0] = event.key.keysym.unicode & 0x7F;
590 /* An International Character. */
595 case SDLK_UP: /* Menu Up */
596 menuaction = MENU_ACTION_UP;
599 case SDLK_DOWN: /* Menu Down */
600 menuaction = MENU_ACTION_DOWN;
603 case SDLK_LEFT: /* Menu Up */
604 menuaction = MENU_ACTION_LEFT;
607 case SDLK_RIGHT: /* Menu Down */
608 menuaction = MENU_ACTION_RIGHT;
612 if(item[active_item].kind == MN_TEXTFIELD)
614 menuaction = MENU_ACTION_INPUT;
619 case SDLK_RETURN: /* Menu Hit */
620 menuaction = MENU_ACTION_HIT;
625 menuaction = MENU_ACTION_REMOVE;
630 if(show_menu && has_backitem == true && last_menu != NULL)
631 Menu::set_current(last_menu);
637 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
639 menuaction = MENU_ACTION_INPUT;
645 mn_input_char = '\0';
650 case SDL_JOYAXISMOTION:
651 if(event.jaxis.axis == JOY_Y)
653 if (event.jaxis.value > 1024)
654 menuaction = MENU_ACTION_DOWN;
655 else if (event.jaxis.value < -1024)
656 menuaction = MENU_ACTION_UP;
659 case SDL_JOYBUTTONDOWN:
660 menuaction = MENU_ACTION_HIT;
662 case SDL_MOUSEBUTTONDOWN:
665 if(x > pos_x - width()/2 &&
666 x < pos_x + width()/2 &&
667 y > pos_y - height()/2 &&
668 y < pos_y + height()/2)
670 menuaction = MENU_ACTION_HIT;
673 case SDL_MOUSEMOTION:
676 if(x > pos_x - width()/2 &&
677 x < pos_x + width()/2 &&
678 y > pos_y - height()/2 &&
679 y < pos_y + height()/2)
681 active_item = (y - (pos_y - height()/2)) / 24;
683 mouse_cursor->set_state(MC_LINK);
687 mouse_cursor->set_state(MC_NORMAL);