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"
42 #define FLICK_CURSOR_TIME 500
45 Surface* checkbox_checked;
52 Menu* worldmap_menu = 0;
53 Menu* options_menu = 0;
54 Menu* options_keys_menu = 0;
55 Menu* options_joystick_menu = 0;
56 Menu* highscore_menu = 0;
57 Menu* load_game_menu = 0;
58 Menu* save_game_menu = 0;
59 Menu* contrib_menu = 0;
60 Menu* contrib_subset_menu = 0;
62 std::vector<Menu*> Menu::last_menus;
63 Menu* Menu::current_ = 0;
65 /* just displays a Yes/No text that can be used to confirm stuff */
66 bool confirm_dialog(std::string text)
68 Surface* cap_screen = Surface::CaptureScreen();
70 Menu* dialog = new Menu;
71 dialog->additem(MN_DEACTIVE, text,0,0);
72 dialog->additem(MN_HL,"",0,0);
73 dialog->additem(MN_ACTION,"Yes",0,0,true);
74 dialog->additem(MN_ACTION,"No",0,0,false);
75 dialog->additem(MN_HL,"",0,0);
77 Menu::set_current(dialog);
83 while (SDL_PollEvent(&event))
88 cap_screen->draw(0,0);
93 switch (dialog->check())
103 Menu::set_current(0);
111 mouse_cursor->draw();
120 Menu::push_current(Menu* pmenu)
123 last_menus.push_back(current_);
126 current_->effect.start(500);
132 if (!last_menus.empty())
134 current_ = last_menus.back();
135 current_->effect.start(500);
137 last_menus.pop_back();
146 Menu::set_current(Menu* menu)
151 menu->effect.start(500);
156 /* Return a pointer to a new menu item */
158 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
160 MenuItem *pnew_item = new MenuItem;
162 pnew_item->kind = kind_;
163 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
164 strcpy(pnew_item->text, text_);
166 if(kind_ == MN_TOGGLE)
167 pnew_item->toggled = init_toggle_;
169 pnew_item->toggled = false;
171 pnew_item->target_menu = target_menu_;
172 pnew_item->input = (char*) malloc(sizeof(char));
173 pnew_item->input[0] = '\0';
175 if(kind_ == MN_STRINGSELECT)
177 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
178 string_list_init(pnew_item->list);
181 pnew_item->list = NULL;
184 pnew_item->int_p = int_p_;
186 pnew_item->input_flickering = false;
187 pnew_item->input_flickering_timer.init(true);
188 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
194 MenuItem::change_text(const char *text_)
199 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
205 MenuItem::change_input(const char *text_)
210 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
211 strcpy(input, text_);
215 std::string MenuItem::get_input_with_symbol(bool active_item)
218 input_flickering = true;
221 if(input_flickering_timer.get_left() < 0)
224 input_flickering = false;
226 input_flickering = true;
227 input_flickering_timer.start(FLICK_CURSOR_TIME);
233 sprintf(str,"%s_",input);
235 sprintf(str,"%s ",input);
237 std::string string = str;
242 /* Set ControlField a key */
243 void Menu::get_controlfield_key_into_input(MenuItem *item)
248 item->change_input("Up cursor");
251 item->change_input("Down cursor");
254 item->change_input("Left cursor");
257 item->change_input("Right cursor");
260 item->change_input("Return");
263 item->change_input("Space");
266 item->change_input("Right Shift");
269 item->change_input("Left Shift");
272 item->change_input("Right Control");
275 item->change_input("Left Control");
278 item->change_input("Right Alt");
281 item->change_input("Left Alt");
286 snprintf(tmp, 64, "%d", *item->int_p);
287 item->change_input(tmp);
293 /* Free a menu and all its items */
298 for(unsigned int i = 0; i < item.size(); ++i)
302 string_list_free(item[i].list);
311 menuaction = MENU_ACTION_NONE;
312 delete_character = 0;
313 mn_input_char = '\0';
322 void Menu::set_pos(int x, int y, float rw, float rh)
324 pos_x = x + (int)((float)get_width() * rw);
325 pos_y = y + (int)((float)get_height() * rh);
329 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
331 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
334 /* Add an item to a menu */
336 Menu::additem(MenuItem* pmenu_item)
338 item.push_back(*pmenu_item);
348 /* Process actions done on the menu */
361 active_item = int(item.size())-1;
364 case MENU_ACTION_DOWN:
365 if(active_item < int(item.size())-1)
371 case MENU_ACTION_LEFT:
372 if(item[active_item].kind == MN_STRINGSELECT
373 && item[active_item].list->num_items != 0)
375 if(item[active_item].list->active_item > 0)
376 --item[active_item].list->active_item;
378 item[active_item].list->active_item = item[active_item].list->num_items-1;
382 case MENU_ACTION_RIGHT:
383 if(item[active_item].kind == MN_STRINGSELECT
384 && item[active_item].list->num_items != 0)
386 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
387 ++item[active_item].list->active_item;
389 item[active_item].list->active_item = 0;
393 case MENU_ACTION_HIT:
395 hit_item = active_item;
396 switch (item[active_item].kind)
399 if (item[active_item].target_menu != NULL)
400 Menu::push_current(item[active_item].target_menu);
406 item[active_item].toggled = !item[active_item].toggled;
410 Menu::set_current(0);
411 item[active_item].toggled = true;
415 menuaction = MENU_ACTION_DOWN;
428 case MENU_ACTION_REMOVE:
429 if(item[active_item].kind == MN_TEXTFIELD
430 || item[active_item].kind == MN_NUMFIELD)
432 if(item[active_item].input != NULL)
434 int i = strlen(item[active_item].input);
436 while(delete_character > 0) /* remove charactes */
438 item[active_item].input[i-1] = '\0';
445 case MENU_ACTION_INPUT:
446 if(item[active_item].kind == MN_TEXTFIELD
447 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
449 if(item[active_item].input != NULL)
451 int i = strlen(item[active_item].input);
452 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
453 item[active_item].input[i] = mn_input_char;
454 item[active_item].input[i+1] = '\0';
458 item[active_item].input = (char*) malloc(2*sizeof(char));
459 item[active_item].input[0] = mn_input_char;
460 item[active_item].input[1] = '\0';
464 case MENU_ACTION_NONE:
469 MenuItem& new_item = item[active_item];
470 if(new_item.kind == MN_DEACTIVE
471 || new_item.kind == MN_LABEL
472 || new_item.kind == MN_HL)
474 // Skip the horzontal line item
475 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
476 menuaction = MENU_ACTION_DOWN;
482 menuaction = MENU_ACTION_NONE;
484 if (active_item >= item.size())
485 active_item = int(item.size()) - 1;
492 return item[hit_item].id;
498 Menu::draw_item(int index, // Position of the current item in the menu
502 MenuItem& pitem = item[index];
505 int effect_offset = 0;
510 effect_time = effect.get_left() / 4;
512 effect_offset = (index % 2) ? effect_time : -effect_time;
516 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
518 int text_width = strlen(pitem.text) * font_width;
519 int input_width = (strlen(pitem.input)+ 1) * font_width;
520 int list_width = strlen(string_list_active(pitem.list)) * font_width;
521 Text* text_font = white_text;
524 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
526 if(index == active_item)
529 text_font = blue_text;
536 black_text->draw_align(pitem.text,
538 A_HMIDDLE, A_VMIDDLE, 2);
544 int x = pos_x - menu_width/2;
545 int y = y_pos - 12 - effect_offset;
546 /* Draw a horizontal line with a little 3d effect */
557 white_big_text->draw_align(pitem.text,
559 A_HMIDDLE, A_VMIDDLE, 2);
564 case MN_CONTROLFIELD:
566 int input_pos = input_width/2;
567 int text_pos = (text_width + font_width)/2;
569 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
570 input_width + font_width + 2, 20,
572 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
573 input_width + font_width, 18,
576 if(pitem.kind == MN_CONTROLFIELD)
577 get_controlfield_key_into_input(&pitem);
579 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
581 if(active_item == index)
582 gold_text->draw_align((pitem.get_input_with_symbol(true)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
584 gold_text->draw_align((pitem.get_input_with_symbol(false)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
587 gold_text->draw_align(pitem.input,
588 x_pos + text_pos, y_pos,
589 A_HMIDDLE, A_VMIDDLE, 2);
591 text_font->draw_align(pitem.text,
592 x_pos - (input_width + font_width)/2, y_pos,
593 A_HMIDDLE, A_VMIDDLE, shadow_size);
596 case MN_STRINGSELECT:
598 int list_pos_2 = list_width + font_width;
599 int list_pos = list_width/2;
600 int text_pos = (text_width + font_width)/2;
603 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
604 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
606 /* Draw input background */
607 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
610 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
614 gold_text->draw_align(string_list_active(pitem.list),
615 x_pos + text_pos, y_pos,
616 A_HMIDDLE, A_VMIDDLE,2);
618 text_font->draw_align(pitem.text,
619 x_pos - list_pos_2/2, y_pos,
620 A_HMIDDLE, A_VMIDDLE, shadow_size);
625 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
626 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
632 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
635 checkbox_checked->draw(
636 x_pos + (text_width+font_width)/2,
640 x_pos + (text_width+font_width)/2,
645 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
649 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
654 int Menu::get_width() const
656 /* The width of the menu has to be more than the width of the text
657 with the most characters */
659 for(unsigned int i = 0; i < item.size(); ++i)
661 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
665 if( item[i].kind == MN_TOGGLE)
670 return (menu_width * 16 + 24);
673 int Menu::get_height() const
675 return item.size() * 24;
678 /* Draw the current menu. */
682 int menu_height = get_height();
683 int menu_width = get_width();
685 /* Draw a transparent background */
686 fillrect(pos_x - menu_width/2,
687 pos_y - 24*item.size()/2 - 10,
688 menu_width,menu_height + 20,
691 for(unsigned int i = 0; i < item.size(); ++i)
693 draw_item(i, menu_width, menu_height);
698 Menu::get_item_by_id(int id)
700 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
707 static MenuItem dummyitem;
711 int Menu::get_active_item_id()
713 return item[active_item].id;
717 Menu::isToggled(int id)
719 return get_item_by_id(id).toggled;
722 /* Check for menu event */
724 Menu::event(SDL_Event& event)
730 key = event.key.keysym.sym;
733 keymod = SDL_GetModState();
736 /* If the current unicode character is an ASCII character,
738 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
740 ch[0] = event.key.keysym.unicode & 0x7F;
745 /* An International Character. */
748 if(item[active_item].kind == MN_CONTROLFIELD)
750 if(key == SDLK_ESCAPE)
755 *item[active_item].int_p = key;
756 menuaction = MENU_ACTION_DOWN;
763 case SDLK_UP: /* Menu Up */
764 menuaction = MENU_ACTION_UP;
766 case SDLK_DOWN: /* Menu Down */
767 menuaction = MENU_ACTION_DOWN;
769 case SDLK_LEFT: /* Menu Up */
770 menuaction = MENU_ACTION_LEFT;
772 case SDLK_RIGHT: /* Menu Down */
773 menuaction = MENU_ACTION_RIGHT;
776 if(item[active_item].kind == MN_TEXTFIELD)
778 menuaction = MENU_ACTION_INPUT;
782 case SDLK_RETURN: /* Menu Hit */
783 menuaction = MENU_ACTION_HIT;
787 menuaction = MENU_ACTION_REMOVE;
794 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
796 menuaction = MENU_ACTION_INPUT;
801 mn_input_char = '\0';
806 case SDL_JOYAXISMOTION:
807 if(event.jaxis.axis == joystick_keymap.y_axis)
809 if (event.jaxis.value > 1024)
810 menuaction = MENU_ACTION_DOWN;
811 else if (event.jaxis.value < -1024)
812 menuaction = MENU_ACTION_UP;
815 case SDL_JOYBUTTONDOWN:
816 menuaction = MENU_ACTION_HIT;
818 case SDL_MOUSEBUTTONDOWN:
821 if(x > pos_x - get_width()/2 &&
822 x < pos_x + get_width()/2 &&
823 y > pos_y - get_height()/2 &&
824 y < pos_y + get_height()/2)
826 menuaction = MENU_ACTION_HIT;
829 case SDL_MOUSEMOTION:
832 if(x > pos_x - get_width()/2 &&
833 x < pos_x + get_width()/2 &&
834 y > pos_y - get_height()/2 &&
835 y < pos_y + get_height()/2)
837 active_item = (y - (pos_y - get_height()/2)) / 24;
838 mouse_cursor->set_state(MC_LINK);
842 mouse_cursor->set_state(MC_NORMAL);