4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <sys/types.h>
37 #include "leveleditor.h"
39 #include "high_scores.h"
42 Surface* checkbox_checked;
49 Menu* worldmap_menu = 0;
50 Menu* options_menu = 0;
51 Menu* options_controls_menu = 0;
52 Menu* highscore_menu = 0;
53 Menu* load_game_menu = 0;
54 Menu* save_game_menu = 0;
55 Menu* contrib_menu = 0;
56 Menu* contrib_subset_menu = 0;
58 std::vector<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
62 Menu::push_current(Menu* pmenu)
65 last_menus.push_back(current_);
68 current_->effect.start(500);
74 if (!last_menus.empty())
76 current_ = last_menus.back();
77 current_->effect.start(500);
79 last_menus.pop_back();
88 Menu::set_current(Menu* menu)
93 menu->effect.start(500);
98 /* Return a pointer to a new menu item */
100 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
102 MenuItem *pnew_item = new MenuItem;
104 pnew_item->kind = kind_;
105 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
106 strcpy(pnew_item->text, text_);
108 if(kind_ == MN_TOGGLE)
109 pnew_item->toggled = init_toggle_;
111 pnew_item->toggled = false;
113 pnew_item->target_menu = target_menu_;
114 pnew_item->input = (char*) malloc(sizeof(char));
115 pnew_item->input[0] = '\0';
117 if(kind_ == MN_STRINGSELECT)
119 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
120 string_list_init(pnew_item->list);
123 pnew_item->list = NULL;
125 pnew_item->int_p = int_p_;
131 MenuItem::change_text(const char *text_)
136 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
142 MenuItem::change_input(const char *text_)
147 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
148 strcpy(input, text_);
152 /* Set ControlField a key */
153 void MenuItem::set_controlfield_key(SDLKey key, char ch[])
162 strcpy(input, "Up cursor");
165 strcpy(input, "Down cursor");
168 strcpy(input, "Left cursor");
171 strcpy(input, "Right cursor");
174 strcpy(input, "Return");
177 strcpy(input, "Space");
180 strcpy(input, "Right Shift");
183 strcpy(input, "Left Shift");
186 strcpy(input, "Right Control");
189 strcpy(input, "Left Control");
192 strcpy(input, "Right Alt");
195 strcpy(input, "Left Alt");
203 /* Free a menu and all its items */
208 for(unsigned int i = 0; i < item.size(); ++i)
212 string_list_free(item[i].list);
221 menuaction = MENU_ACTION_NONE;
222 delete_character = 0;
223 mn_input_char = '\0';
227 has_backitem = false;
233 void Menu::set_pos(int x, int y, float rw, float rh)
235 pos_x = x + (int)((float)get_width() * rw);
236 pos_y = y + (int)((float)get_height() * rh);
240 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int* int_p)
245 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, int_p));
248 /* Add an item to a menu */
250 Menu::additem(MenuItem* pmenu_item)
252 if(pmenu_item->kind == MN_BACK)
255 item.push_back(*pmenu_item);
265 /* Process actions done on the menu */
278 active_item = int(item.size())-1;
281 case MENU_ACTION_DOWN:
282 if(active_item < int(item.size())-1)
288 case MENU_ACTION_LEFT:
289 if(item[active_item].kind == MN_STRINGSELECT
290 && item[active_item].list->num_items != 0)
292 if(item[active_item].list->active_item > 0)
293 --item[active_item].list->active_item;
295 item[active_item].list->active_item = item[active_item].list->num_items-1;
299 case MENU_ACTION_RIGHT:
300 if(item[active_item].kind == MN_STRINGSELECT
301 && item[active_item].list->num_items != 0)
303 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
304 ++item[active_item].list->active_item;
306 item[active_item].list->active_item = 0;
310 case MENU_ACTION_HIT:
312 hit_item = active_item;
313 switch (item[active_item].kind)
316 if (item[active_item].target_menu != NULL)
317 Menu::push_current(item[active_item].target_menu);
323 item[active_item].toggled = !item[active_item].toggled;
329 Menu::set_current(0);
330 item[active_item].toggled = true;
342 case MENU_ACTION_REMOVE:
343 if(item[active_item].kind == MN_TEXTFIELD
344 || item[active_item].kind == MN_NUMFIELD)
346 if(item[active_item].input != NULL)
348 int i = strlen(item[active_item].input);
350 while(delete_character > 0) /* remove charactes */
352 item[active_item].input[i-1] = '\0';
359 case MENU_ACTION_INPUT:
360 if(item[active_item].kind == MN_TEXTFIELD
361 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
363 if(item[active_item].input != NULL)
365 int i = strlen(item[active_item].input);
366 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
367 item[active_item].input[i] = mn_input_char;
368 item[active_item].input[i+1] = '\0';
372 item[active_item].input = (char*) malloc(2*sizeof(char));
373 item[active_item].input[0] = mn_input_char;
374 item[active_item].input[1] = '\0';
378 case MENU_ACTION_NONE:
383 MenuItem& new_item = item[active_item];
384 if(new_item.kind == MN_DEACTIVE
385 || new_item.kind == MN_LABEL
386 || new_item.kind == MN_HL)
388 // Skip the horzontal line item
389 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
390 menuaction = MENU_ACTION_DOWN;
396 menuaction = MENU_ACTION_NONE;
404 if (item.size() != 0)
406 if((item[active_item].kind == MN_ACTION
407 || item[active_item].kind == MN_TEXTFIELD
408 || item[active_item].kind == MN_NUMFIELD)
409 && item[active_item].toggled)
411 item[active_item].toggled = false;
412 Menu::set_current(0);
415 else if(item[active_item].kind == MN_TOGGLE
416 || item[active_item].kind == MN_GOTO)
429 Menu::draw_item(int index, // Position of the current item in the menu
433 const MenuItem& pitem = item[index];
436 int effect_offset = 0;
441 effect_time = effect.get_left() / 4;
443 effect_offset = (index % 2) ? effect_time : -effect_time;
447 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
449 int text_width = strlen(pitem.text) * font_width;
450 int input_width = strlen(pitem.input) * font_width;
451 int list_width = strlen(string_list_active(pitem.list)) * font_width;
452 Text* text_font = white_text;
455 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
457 if(index == active_item)
460 text_font = blue_text;
467 black_text->draw_align(pitem.text,
469 A_HMIDDLE, A_VMIDDLE, 2);
475 int x = pos_x - menu_width/2;
476 int y = y_pos - 12 - effect_offset;
477 /* Draw a horizontal line with a little 3d effect */
488 white_big_text->draw_align(pitem.text,
490 A_HMIDDLE, A_VMIDDLE, 2);
495 case MN_CONTROLFIELD:
497 int input_pos = input_width/2;
498 int text_pos = (text_width + font_width)/2;
500 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
501 input_width + font_width + 2, 20,
503 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
504 input_width + font_width, 18,
507 gold_text->draw_align(pitem.input,
508 x_pos + text_pos, y_pos,
509 A_HMIDDLE, A_VMIDDLE, 2);
511 text_font->draw_align(pitem.text,
512 x_pos - (input_width + font_width)/2, y_pos,
513 A_HMIDDLE, A_VMIDDLE, shadow_size);
516 case MN_STRINGSELECT:
518 int list_pos_2 = list_width + font_width;
519 int list_pos = list_width/2;
520 int text_pos = (text_width + font_width)/2;
523 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
524 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
526 /* Draw input background */
527 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
530 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
534 gold_text->draw_align(string_list_active(pitem.list),
535 x_pos + text_pos, y_pos,
536 A_HMIDDLE, A_VMIDDLE,2);
538 text_font->draw_align(pitem.text,
539 x_pos - list_pos_2/2, y_pos,
540 A_HMIDDLE, A_VMIDDLE, shadow_size);
545 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
546 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
552 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
555 checkbox_checked->draw(
556 x_pos + (text_width+font_width)/2,
560 x_pos + (text_width+font_width)/2,
565 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
569 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
574 int Menu::get_width() const
576 /* The width of the menu has to be more than the width of the text
577 with the most characters */
579 for(unsigned int i = 0; i < item.size(); ++i)
581 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
585 if( item[i].kind == MN_TOGGLE)
590 return (menu_width * 16 + 24);
593 int Menu::get_height() const
595 return item.size() * 24;
598 /* Draw the current menu. */
602 int menu_height = get_height();
603 int menu_width = get_width();
605 /* Draw a transparent background */
606 fillrect(pos_x - menu_width/2,
607 pos_y - 24*item.size()/2 - 10,
608 menu_width,menu_height + 20,
611 for(unsigned int i = 0; i < item.size(); ++i)
613 draw_item(i, menu_width, menu_height);
617 /* Check for menu event */
619 Menu::event(SDL_Event& event)
625 key = event.key.keysym.sym;
628 keymod = SDL_GetModState();
631 /* If the current unicode character is an ASCII character,
633 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
635 ch[0] = event.key.keysym.unicode & 0x7F;
640 /* An International Character. */
643 if(item[active_item].kind == MN_CONTROLFIELD)
645 item[active_item].set_controlfield_key(key, ch);
646 menuaction = MENU_ACTION_DOWN;
653 case SDLK_UP: /* Menu Up */
654 menuaction = MENU_ACTION_UP;
656 case SDLK_DOWN: /* Menu Down */
657 menuaction = MENU_ACTION_DOWN;
659 case SDLK_LEFT: /* Menu Up */
660 menuaction = MENU_ACTION_LEFT;
662 case SDLK_RIGHT: /* Menu Down */
663 menuaction = MENU_ACTION_RIGHT;
666 if(item[active_item].kind == MN_TEXTFIELD)
668 menuaction = MENU_ACTION_INPUT;
672 case SDLK_RETURN: /* Menu Hit */
673 menuaction = MENU_ACTION_HIT;
677 menuaction = MENU_ACTION_REMOVE;
684 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
686 menuaction = MENU_ACTION_INPUT;
691 mn_input_char = '\0';
696 case SDL_JOYAXISMOTION:
697 if(event.jaxis.axis == joystick_keymap.y_axis)
699 if (event.jaxis.value > 1024)
700 menuaction = MENU_ACTION_DOWN;
701 else if (event.jaxis.value < -1024)
702 menuaction = MENU_ACTION_UP;
705 case SDL_JOYBUTTONDOWN:
706 menuaction = MENU_ACTION_HIT;
708 case SDL_MOUSEBUTTONDOWN:
711 if(x > pos_x - get_width()/2 &&
712 x < pos_x + get_width()/2 &&
713 y > pos_y - get_height()/2 &&
714 y < pos_y + get_height()/2)
716 menuaction = MENU_ACTION_HIT;
719 case SDL_MOUSEMOTION:
722 if(x > pos_x - get_width()/2 &&
723 x < pos_x + get_width()/2 &&
724 y > pos_y - get_height()/2 &&
725 y < pos_y + get_height()/2)
727 active_item = (y - (pos_y - get_height()/2)) / 24;
728 mouse_cursor->set_state(MC_LINK);
732 mouse_cursor->set_state(MC_NORMAL);