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_keys_menu = 0;
52 Menu* options_joystick_menu = 0;
53 Menu* highscore_menu = 0;
54 Menu* load_game_menu = 0;
55 Menu* save_game_menu = 0;
56 Menu* contrib_menu = 0;
57 Menu* contrib_subset_menu = 0;
59 std::vector<Menu*> Menu::last_menus;
60 Menu* Menu::current_ = 0;
63 Menu::push_current(Menu* pmenu)
66 last_menus.push_back(current_);
69 current_->effect.start(500);
75 if (!last_menus.empty())
77 current_ = last_menus.back();
78 current_->effect.start(500);
80 last_menus.pop_back();
89 Menu::set_current(Menu* menu)
94 menu->effect.start(500);
99 /* Return a pointer to a new menu item */
101 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
103 MenuItem *pnew_item = new MenuItem;
105 pnew_item->kind = kind_;
106 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
107 strcpy(pnew_item->text, text_);
109 if(kind_ == MN_TOGGLE)
110 pnew_item->toggled = init_toggle_;
112 pnew_item->toggled = false;
114 pnew_item->target_menu = target_menu_;
115 pnew_item->input = (char*) malloc(sizeof(char));
116 pnew_item->input[0] = '\0';
118 if(kind_ == MN_STRINGSELECT)
120 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
121 string_list_init(pnew_item->list);
124 pnew_item->list = NULL;
127 pnew_item->int_p = int_p_;
133 MenuItem::change_text(const char *text_)
138 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
144 MenuItem::change_input(const char *text_)
149 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
150 strcpy(input, text_);
154 /* Set ControlField a key */
155 void Menu::get_controlfield_key_into_input(MenuItem *item)
160 strcpy(item->input, "Up cursor");
163 strcpy(item->input, "Down cursor");
166 strcpy(item->input, "Left cursor");
169 strcpy(item->input, "Right cursor");
172 strcpy(item->input, "Return");
175 strcpy(item->input, "Space");
178 strcpy(item->input, "Right Shift");
181 strcpy(item->input, "Left Shift");
184 strcpy(item->input, "Right Control");
187 strcpy(item->input, "Left Control");
190 strcpy(item->input, "Right Alt");
193 strcpy(item->input, "Left Alt");
196 strcpy(item->input, (char*)item->int_p);
201 /* Free a menu and all its items */
206 for(unsigned int i = 0; i < item.size(); ++i)
210 string_list_free(item[i].list);
219 menuaction = MENU_ACTION_NONE;
220 delete_character = 0;
221 mn_input_char = '\0';
225 has_backitem = false;
231 void Menu::set_pos(int x, int y, float rw, float rh)
233 pos_x = x + (int)((float)get_width() * rw);
234 pos_y = y + (int)((float)get_height() * rh);
238 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
244 id = int(item.size());
246 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
249 /* Add an item to a menu */
251 Menu::additem(MenuItem* pmenu_item)
253 if(pmenu_item->kind == MN_BACK)
256 item.push_back(*pmenu_item);
266 /* Process actions done on the menu */
279 active_item = int(item.size())-1;
282 case MENU_ACTION_DOWN:
283 if(active_item < int(item.size())-1)
289 case MENU_ACTION_LEFT:
290 if(item[active_item].kind == MN_STRINGSELECT
291 && item[active_item].list->num_items != 0)
293 if(item[active_item].list->active_item > 0)
294 --item[active_item].list->active_item;
296 item[active_item].list->active_item = item[active_item].list->num_items-1;
300 case MENU_ACTION_RIGHT:
301 if(item[active_item].kind == MN_STRINGSELECT
302 && item[active_item].list->num_items != 0)
304 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
305 ++item[active_item].list->active_item;
307 item[active_item].list->active_item = 0;
311 case MENU_ACTION_HIT:
313 hit_item = active_item;
314 switch (item[active_item].kind)
317 if (item[active_item].target_menu != NULL)
318 Menu::push_current(item[active_item].target_menu);
324 item[active_item].toggled = !item[active_item].toggled;
330 Menu::set_current(0);
331 item[active_item].toggled = true;
343 case MENU_ACTION_REMOVE:
344 if(item[active_item].kind == MN_TEXTFIELD
345 || item[active_item].kind == MN_NUMFIELD)
347 if(item[active_item].input != NULL)
349 int i = strlen(item[active_item].input);
351 while(delete_character > 0) /* remove charactes */
353 item[active_item].input[i-1] = '\0';
360 case MENU_ACTION_INPUT:
361 if(item[active_item].kind == MN_TEXTFIELD
362 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
364 if(item[active_item].input != NULL)
366 int i = strlen(item[active_item].input);
367 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
368 item[active_item].input[i] = mn_input_char;
369 item[active_item].input[i+1] = '\0';
373 item[active_item].input = (char*) malloc(2*sizeof(char));
374 item[active_item].input[0] = mn_input_char;
375 item[active_item].input[1] = '\0';
379 case MENU_ACTION_NONE:
384 MenuItem& new_item = item[active_item];
385 if(new_item.kind == MN_DEACTIVE
386 || new_item.kind == MN_LABEL
387 || new_item.kind == MN_HL)
389 // Skip the horzontal line item
390 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
391 menuaction = MENU_ACTION_DOWN;
397 menuaction = MENU_ACTION_NONE;
404 return item[hit_item].id;
410 Menu::draw_item(int index, // Position of the current item in the menu
414 MenuItem& pitem = item[index];
417 int effect_offset = 0;
422 effect_time = effect.get_left() / 4;
424 effect_offset = (index % 2) ? effect_time : -effect_time;
428 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
430 int text_width = strlen(pitem.text) * font_width;
431 int input_width = strlen(pitem.input) * font_width;
432 int list_width = strlen(string_list_active(pitem.list)) * font_width;
433 Text* text_font = white_text;
436 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
438 if(index == active_item)
441 text_font = blue_text;
448 black_text->draw_align(pitem.text,
450 A_HMIDDLE, A_VMIDDLE, 2);
456 int x = pos_x - menu_width/2;
457 int y = y_pos - 12 - effect_offset;
458 /* Draw a horizontal line with a little 3d effect */
469 white_big_text->draw_align(pitem.text,
471 A_HMIDDLE, A_VMIDDLE, 2);
476 case MN_CONTROLFIELD:
478 int input_pos = input_width/2;
479 int text_pos = (text_width + font_width)/2;
481 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
482 input_width + font_width + 2, 20,
484 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
485 input_width + font_width, 18,
488 if(pitem.kind == MN_CONTROLFIELD)
489 get_controlfield_key_into_input(&pitem);
491 gold_text->draw_align(pitem.input,
492 x_pos + text_pos, y_pos,
493 A_HMIDDLE, A_VMIDDLE, 2);
495 text_font->draw_align(pitem.text,
496 x_pos - (input_width + font_width)/2, y_pos,
497 A_HMIDDLE, A_VMIDDLE, shadow_size);
500 case MN_STRINGSELECT:
502 int list_pos_2 = list_width + font_width;
503 int list_pos = list_width/2;
504 int text_pos = (text_width + font_width)/2;
507 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
508 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
510 /* Draw input background */
511 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
514 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
518 gold_text->draw_align(string_list_active(pitem.list),
519 x_pos + text_pos, y_pos,
520 A_HMIDDLE, A_VMIDDLE,2);
522 text_font->draw_align(pitem.text,
523 x_pos - list_pos_2/2, y_pos,
524 A_HMIDDLE, A_VMIDDLE, shadow_size);
529 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
530 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
536 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
539 checkbox_checked->draw(
540 x_pos + (text_width+font_width)/2,
544 x_pos + (text_width+font_width)/2,
549 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
553 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
558 int Menu::get_width() const
560 /* The width of the menu has to be more than the width of the text
561 with the most characters */
563 for(unsigned int i = 0; i < item.size(); ++i)
565 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
569 if( item[i].kind == MN_TOGGLE)
574 return (menu_width * 16 + 24);
577 int Menu::get_height() const
579 return item.size() * 24;
582 /* Draw the current menu. */
586 int menu_height = get_height();
587 int menu_width = get_width();
589 /* Draw a transparent background */
590 fillrect(pos_x - menu_width/2,
591 pos_y - 24*item.size()/2 - 10,
592 menu_width,menu_height + 20,
595 for(unsigned int i = 0; i < item.size(); ++i)
597 draw_item(i, menu_width, menu_height);
601 /* Check for menu event */
603 Menu::event(SDL_Event& event)
609 key = event.key.keysym.sym;
612 keymod = SDL_GetModState();
615 /* If the current unicode character is an ASCII character,
617 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
619 ch[0] = event.key.keysym.unicode & 0x7F;
624 /* An International Character. */
627 if(item[active_item].kind == MN_CONTROLFIELD)
629 if(key == SDLK_ESCAPE)
634 *item[active_item].int_p = key;
635 menuaction = MENU_ACTION_DOWN;
642 case SDLK_UP: /* Menu Up */
643 menuaction = MENU_ACTION_UP;
645 case SDLK_DOWN: /* Menu Down */
646 menuaction = MENU_ACTION_DOWN;
648 case SDLK_LEFT: /* Menu Up */
649 menuaction = MENU_ACTION_LEFT;
651 case SDLK_RIGHT: /* Menu Down */
652 menuaction = MENU_ACTION_RIGHT;
655 if(item[active_item].kind == MN_TEXTFIELD)
657 menuaction = MENU_ACTION_INPUT;
661 case SDLK_RETURN: /* Menu Hit */
662 menuaction = MENU_ACTION_HIT;
666 menuaction = MENU_ACTION_REMOVE;
673 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
675 menuaction = MENU_ACTION_INPUT;
680 mn_input_char = '\0';
685 case SDL_JOYAXISMOTION:
686 if(event.jaxis.axis == joystick_keymap.y_axis)
688 if (event.jaxis.value > 1024)
689 menuaction = MENU_ACTION_DOWN;
690 else if (event.jaxis.value < -1024)
691 menuaction = MENU_ACTION_UP;
694 case SDL_JOYBUTTONDOWN:
695 menuaction = MENU_ACTION_HIT;
697 case SDL_MOUSEBUTTONDOWN:
700 if(x > pos_x - get_width()/2 &&
701 x < pos_x + get_width()/2 &&
702 y > pos_y - get_height()/2 &&
703 y < pos_y + get_height()/2)
705 menuaction = MENU_ACTION_HIT;
708 case SDL_MOUSEMOTION:
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 active_item = (y - (pos_y - get_height()/2)) / 24;
717 mouse_cursor->set_state(MC_LINK);
721 mouse_cursor->set_state(MC_NORMAL);