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>
38 #include "leveleditor.h"
40 #include "high_scores.h"
43 Surface* checkbox_checked;
50 Menu* worldmap_menu = 0;
51 Menu* options_menu = 0;
52 Menu* options_keys_menu = 0;
53 Menu* options_joystick_menu = 0;
54 Menu* highscore_menu = 0;
55 Menu* load_game_menu = 0;
56 Menu* save_game_menu = 0;
57 Menu* contrib_menu = 0;
58 Menu* contrib_subset_menu = 0;
60 std::vector<Menu*> Menu::last_menus;
61 Menu* Menu::current_ = 0;
63 /* just displays a Yes/No text that can be used to confirm stuff */
64 bool confirm_dialog(char *text)
66 white_text->drawf(text,0,0,A_HMIDDLE,A_VMIDDLE,2);
67 red_text->drawf("(Y)es/(N)o",0,20,A_HMIDDLE,A_VMIDDLE,2);
74 while(SDL_PollEvent(&event))
77 case SDL_KEYDOWN: // key pressed
78 switch(event.key.keysym.sym)
91 case SDL_QUIT: // quit signal
102 Menu::push_current(Menu* pmenu)
105 last_menus.push_back(current_);
108 current_->effect.start(500);
114 if (!last_menus.empty())
116 current_ = last_menus.back();
117 current_->effect.start(500);
119 last_menus.pop_back();
128 Menu::set_current(Menu* menu)
133 menu->effect.start(500);
138 /* Return a pointer to a new menu item */
140 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
142 MenuItem *pnew_item = new MenuItem;
144 pnew_item->kind = kind_;
145 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
146 strcpy(pnew_item->text, text_);
148 if(kind_ == MN_TOGGLE)
149 pnew_item->toggled = init_toggle_;
151 pnew_item->toggled = false;
153 pnew_item->target_menu = target_menu_;
154 pnew_item->input = (char*) malloc(sizeof(char));
155 pnew_item->input[0] = '\0';
157 if(kind_ == MN_STRINGSELECT)
159 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
160 string_list_init(pnew_item->list);
163 pnew_item->list = NULL;
166 pnew_item->int_p = int_p_;
172 MenuItem::change_text(const char *text_)
177 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
183 MenuItem::change_input(const char *text_)
188 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
189 strcpy(input, text_);
193 /* Set ControlField a key */
194 void Menu::get_controlfield_key_into_input(MenuItem *item)
199 item->change_input("Up cursor");
202 item->change_input("Down cursor");
205 item->change_input("Left cursor");
208 item->change_input("Right cursor");
211 item->change_input("Return");
214 item->change_input("Space");
217 item->change_input("Right Shift");
220 item->change_input("Left Shift");
223 item->change_input("Right Control");
226 item->change_input("Left Control");
229 item->change_input("Right Alt");
232 item->change_input("Left Alt");
237 snprintf(tmp, 64, "%d", *item->int_p);
238 item->change_input(tmp);
244 /* Free a menu and all its items */
249 for(unsigned int i = 0; i < item.size(); ++i)
253 string_list_free(item[i].list);
262 menuaction = MENU_ACTION_NONE;
263 delete_character = 0;
264 mn_input_char = '\0';
273 void Menu::set_pos(int x, int y, float rw, float rh)
275 pos_x = x + (int)((float)get_width() * rw);
276 pos_y = y + (int)((float)get_height() * rh);
280 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
282 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
285 /* Add an item to a menu */
287 Menu::additem(MenuItem* pmenu_item)
289 item.push_back(*pmenu_item);
299 /* Process actions done on the menu */
312 active_item = int(item.size())-1;
315 case MENU_ACTION_DOWN:
316 if(active_item < int(item.size())-1)
322 case MENU_ACTION_LEFT:
323 if(item[active_item].kind == MN_STRINGSELECT
324 && item[active_item].list->num_items != 0)
326 if(item[active_item].list->active_item > 0)
327 --item[active_item].list->active_item;
329 item[active_item].list->active_item = item[active_item].list->num_items-1;
333 case MENU_ACTION_RIGHT:
334 if(item[active_item].kind == MN_STRINGSELECT
335 && item[active_item].list->num_items != 0)
337 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
338 ++item[active_item].list->active_item;
340 item[active_item].list->active_item = 0;
344 case MENU_ACTION_HIT:
346 hit_item = active_item;
347 switch (item[active_item].kind)
350 if (item[active_item].target_menu != NULL)
351 Menu::push_current(item[active_item].target_menu);
357 item[active_item].toggled = !item[active_item].toggled;
363 Menu::set_current(0);
364 item[active_item].toggled = true;
376 case MENU_ACTION_REMOVE:
377 if(item[active_item].kind == MN_TEXTFIELD
378 || item[active_item].kind == MN_NUMFIELD)
380 if(item[active_item].input != NULL)
382 int i = strlen(item[active_item].input);
384 while(delete_character > 0) /* remove charactes */
386 item[active_item].input[i-1] = '\0';
393 case MENU_ACTION_INPUT:
394 if(item[active_item].kind == MN_TEXTFIELD
395 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
397 if(item[active_item].input != NULL)
399 int i = strlen(item[active_item].input);
400 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
401 item[active_item].input[i] = mn_input_char;
402 item[active_item].input[i+1] = '\0';
406 item[active_item].input = (char*) malloc(2*sizeof(char));
407 item[active_item].input[0] = mn_input_char;
408 item[active_item].input[1] = '\0';
412 case MENU_ACTION_NONE:
417 MenuItem& new_item = item[active_item];
418 if(new_item.kind == MN_DEACTIVE
419 || new_item.kind == MN_LABEL
420 || new_item.kind == MN_HL)
422 // Skip the horzontal line item
423 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
424 menuaction = MENU_ACTION_DOWN;
430 menuaction = MENU_ACTION_NONE;
437 return item[hit_item].id;
443 Menu::draw_item(int index, // Position of the current item in the menu
447 MenuItem& pitem = item[index];
450 int effect_offset = 0;
455 effect_time = effect.get_left() / 4;
457 effect_offset = (index % 2) ? effect_time : -effect_time;
461 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
463 int text_width = strlen(pitem.text) * font_width;
464 int input_width = strlen(pitem.input) * font_width;
465 int list_width = strlen(string_list_active(pitem.list)) * font_width;
466 Text* text_font = white_text;
469 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
471 if(index == active_item)
474 text_font = blue_text;
481 black_text->draw_align(pitem.text,
483 A_HMIDDLE, A_VMIDDLE, 2);
489 int x = pos_x - menu_width/2;
490 int y = y_pos - 12 - effect_offset;
491 /* Draw a horizontal line with a little 3d effect */
502 white_big_text->draw_align(pitem.text,
504 A_HMIDDLE, A_VMIDDLE, 2);
509 case MN_CONTROLFIELD:
511 int input_pos = input_width/2;
512 int text_pos = (text_width + font_width)/2;
514 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
515 input_width + font_width + 2, 20,
517 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
518 input_width + font_width, 18,
521 if(pitem.kind == MN_CONTROLFIELD)
522 get_controlfield_key_into_input(&pitem);
524 gold_text->draw_align(pitem.input,
525 x_pos + text_pos, y_pos,
526 A_HMIDDLE, A_VMIDDLE, 2);
528 text_font->draw_align(pitem.text,
529 x_pos - (input_width + font_width)/2, y_pos,
530 A_HMIDDLE, A_VMIDDLE, shadow_size);
533 case MN_STRINGSELECT:
535 int list_pos_2 = list_width + font_width;
536 int list_pos = list_width/2;
537 int text_pos = (text_width + font_width)/2;
540 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
541 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
543 /* Draw input background */
544 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
547 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
551 gold_text->draw_align(string_list_active(pitem.list),
552 x_pos + text_pos, y_pos,
553 A_HMIDDLE, A_VMIDDLE,2);
555 text_font->draw_align(pitem.text,
556 x_pos - list_pos_2/2, y_pos,
557 A_HMIDDLE, A_VMIDDLE, shadow_size);
562 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
563 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
569 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
572 checkbox_checked->draw(
573 x_pos + (text_width+font_width)/2,
577 x_pos + (text_width+font_width)/2,
582 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
586 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
591 int Menu::get_width() const
593 /* The width of the menu has to be more than the width of the text
594 with the most characters */
596 for(unsigned int i = 0; i < item.size(); ++i)
598 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
602 if( item[i].kind == MN_TOGGLE)
607 return (menu_width * 16 + 24);
610 int Menu::get_height() const
612 return item.size() * 24;
615 /* Draw the current menu. */
619 int menu_height = get_height();
620 int menu_width = get_width();
622 /* Draw a transparent background */
623 fillrect(pos_x - menu_width/2,
624 pos_y - 24*item.size()/2 - 10,
625 menu_width,menu_height + 20,
628 for(unsigned int i = 0; i < item.size(); ++i)
630 draw_item(i, menu_width, menu_height);
635 Menu::get_item_by_id(int id)
637 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
643 static MenuItem dummyitem;
648 Menu::isToggled(int id)
650 return get_item_by_id(id).toggled;
653 /* Check for menu event */
655 Menu::event(SDL_Event& event)
661 key = event.key.keysym.sym;
664 keymod = SDL_GetModState();
667 /* If the current unicode character is an ASCII character,
669 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
671 ch[0] = event.key.keysym.unicode & 0x7F;
676 /* An International Character. */
679 if(item[active_item].kind == MN_CONTROLFIELD)
681 if(key == SDLK_ESCAPE)
686 *item[active_item].int_p = key;
687 menuaction = MENU_ACTION_DOWN;
694 case SDLK_UP: /* Menu Up */
695 menuaction = MENU_ACTION_UP;
697 case SDLK_DOWN: /* Menu Down */
698 menuaction = MENU_ACTION_DOWN;
700 case SDLK_LEFT: /* Menu Up */
701 menuaction = MENU_ACTION_LEFT;
703 case SDLK_RIGHT: /* Menu Down */
704 menuaction = MENU_ACTION_RIGHT;
707 if(item[active_item].kind == MN_TEXTFIELD)
709 menuaction = MENU_ACTION_INPUT;
713 case SDLK_RETURN: /* Menu Hit */
714 menuaction = MENU_ACTION_HIT;
718 menuaction = MENU_ACTION_REMOVE;
725 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
727 menuaction = MENU_ACTION_INPUT;
732 mn_input_char = '\0';
737 case SDL_JOYAXISMOTION:
738 if(event.jaxis.axis == joystick_keymap.y_axis)
740 if (event.jaxis.value > 1024)
741 menuaction = MENU_ACTION_DOWN;
742 else if (event.jaxis.value < -1024)
743 menuaction = MENU_ACTION_UP;
746 case SDL_JOYBUTTONDOWN:
747 menuaction = MENU_ACTION_HIT;
749 case SDL_MOUSEBUTTONDOWN:
752 if(x > pos_x - get_width()/2 &&
753 x < pos_x + get_width()/2 &&
754 y > pos_y - get_height()/2 &&
755 y < pos_y + get_height()/2)
757 menuaction = MENU_ACTION_HIT;
760 case SDL_MOUSEMOTION:
763 if(x > pos_x - get_width()/2 &&
764 x < pos_x + get_width()/2 &&
765 y > pos_y - get_height()/2 &&
766 y < pos_y + get_height()/2)
768 active_item = (y - (pos_y - get_height()/2)) / 24;
769 mouse_cursor->set_state(MC_LINK);
773 mouse_cursor->set_state(MC_NORMAL);