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(char *text)
68 white_text->drawf(text,0,0,A_HMIDDLE,A_VMIDDLE,2);
69 red_text->drawf("(Y)es/(N)o",0,20,A_HMIDDLE,A_VMIDDLE,2);
76 while(SDL_PollEvent(&event))
79 case SDL_KEYDOWN: // key pressed
80 switch(event.key.keysym.sym)
93 case SDL_QUIT: // quit signal
104 Menu::push_current(Menu* pmenu)
107 last_menus.push_back(current_);
110 current_->effect.start(500);
116 if (!last_menus.empty())
118 current_ = last_menus.back();
119 current_->effect.start(500);
121 last_menus.pop_back();
130 Menu::set_current(Menu* menu)
135 menu->effect.start(500);
140 /* Return a pointer to a new menu item */
142 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
144 MenuItem *pnew_item = new MenuItem;
146 pnew_item->kind = kind_;
147 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
148 strcpy(pnew_item->text, text_);
150 if(kind_ == MN_TOGGLE)
151 pnew_item->toggled = init_toggle_;
153 pnew_item->toggled = false;
155 pnew_item->target_menu = target_menu_;
156 pnew_item->input = (char*) malloc(sizeof(char));
157 pnew_item->input[0] = '\0';
159 if(kind_ == MN_STRINGSELECT)
161 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
162 string_list_init(pnew_item->list);
165 pnew_item->list = NULL;
168 pnew_item->int_p = int_p_;
170 pnew_item->input_flickering = false;
171 pnew_item->input_flickering_timer.init(true);
172 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
178 MenuItem::change_text(const char *text_)
183 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
189 MenuItem::change_input(const char *text_)
194 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
195 strcpy(input, text_);
199 char* MenuItem::get_input_with_symbol(bool active_item)
202 input_flickering = true;
205 if(input_flickering_timer.get_left() < 0)
208 input_flickering = false;
210 input_flickering = true;
211 input_flickering_timer.start(FLICK_CURSOR_TIME);
217 sprintf(str,"%s-",input);
219 sprintf(str,"%s ",input);
224 /* Set ControlField a key */
225 void Menu::get_controlfield_key_into_input(MenuItem *item)
230 item->change_input("Up cursor");
233 item->change_input("Down cursor");
236 item->change_input("Left cursor");
239 item->change_input("Right cursor");
242 item->change_input("Return");
245 item->change_input("Space");
248 item->change_input("Right Shift");
251 item->change_input("Left Shift");
254 item->change_input("Right Control");
257 item->change_input("Left Control");
260 item->change_input("Right Alt");
263 item->change_input("Left Alt");
268 snprintf(tmp, 64, "%d", *item->int_p);
269 item->change_input(tmp);
275 /* Free a menu and all its items */
280 for(unsigned int i = 0; i < item.size(); ++i)
284 string_list_free(item[i].list);
293 menuaction = MENU_ACTION_NONE;
294 delete_character = 0;
295 mn_input_char = '\0';
304 void Menu::set_pos(int x, int y, float rw, float rh)
306 pos_x = x + (int)((float)get_width() * rw);
307 pos_y = y + (int)((float)get_height() * rh);
311 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
313 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
316 /* Add an item to a menu */
318 Menu::additem(MenuItem* pmenu_item)
320 item.push_back(*pmenu_item);
330 /* Process actions done on the menu */
343 active_item = int(item.size())-1;
346 case MENU_ACTION_DOWN:
347 if(active_item < int(item.size())-1)
353 case MENU_ACTION_LEFT:
354 if(item[active_item].kind == MN_STRINGSELECT
355 && item[active_item].list->num_items != 0)
357 if(item[active_item].list->active_item > 0)
358 --item[active_item].list->active_item;
360 item[active_item].list->active_item = item[active_item].list->num_items-1;
364 case MENU_ACTION_RIGHT:
365 if(item[active_item].kind == MN_STRINGSELECT
366 && item[active_item].list->num_items != 0)
368 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
369 ++item[active_item].list->active_item;
371 item[active_item].list->active_item = 0;
375 case MENU_ACTION_HIT:
377 hit_item = active_item;
378 switch (item[active_item].kind)
381 if (item[active_item].target_menu != NULL)
382 Menu::push_current(item[active_item].target_menu);
388 item[active_item].toggled = !item[active_item].toggled;
394 Menu::set_current(0);
395 item[active_item].toggled = true;
407 case MENU_ACTION_REMOVE:
408 if(item[active_item].kind == MN_TEXTFIELD
409 || item[active_item].kind == MN_NUMFIELD)
411 if(item[active_item].input != NULL)
413 int i = strlen(item[active_item].input);
415 while(delete_character > 0) /* remove charactes */
417 item[active_item].input[i-1] = '\0';
424 case MENU_ACTION_INPUT:
425 if(item[active_item].kind == MN_TEXTFIELD
426 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
428 if(item[active_item].input != NULL)
430 int i = strlen(item[active_item].input);
431 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
432 item[active_item].input[i] = mn_input_char;
433 item[active_item].input[i+1] = '\0';
437 item[active_item].input = (char*) malloc(2*sizeof(char));
438 item[active_item].input[0] = mn_input_char;
439 item[active_item].input[1] = '\0';
443 case MENU_ACTION_NONE:
448 MenuItem& new_item = item[active_item];
449 if(new_item.kind == MN_DEACTIVE
450 || new_item.kind == MN_LABEL
451 || new_item.kind == MN_HL)
453 // Skip the horzontal line item
454 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
455 menuaction = MENU_ACTION_DOWN;
461 menuaction = MENU_ACTION_NONE;
468 return item[hit_item].id;
474 Menu::draw_item(int index, // Position of the current item in the menu
478 MenuItem& pitem = item[index];
481 int effect_offset = 0;
486 effect_time = effect.get_left() / 4;
488 effect_offset = (index % 2) ? effect_time : -effect_time;
492 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
494 int text_width = strlen(pitem.text) * font_width;
495 int input_width = (strlen(pitem.input)+ 1) * font_width;
496 int list_width = strlen(string_list_active(pitem.list)) * font_width;
497 Text* text_font = white_text;
500 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
502 if(index == active_item)
505 text_font = blue_text;
512 black_text->draw_align(pitem.text,
514 A_HMIDDLE, A_VMIDDLE, 2);
520 int x = pos_x - menu_width/2;
521 int y = y_pos - 12 - effect_offset;
522 /* Draw a horizontal line with a little 3d effect */
533 white_big_text->draw_align(pitem.text,
535 A_HMIDDLE, A_VMIDDLE, 2);
540 case MN_CONTROLFIELD:
542 int input_pos = input_width/2;
543 int text_pos = (text_width + font_width)/2;
545 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
546 input_width + font_width + 2, 20,
548 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
549 input_width + font_width, 18,
552 if(pitem.kind == MN_CONTROLFIELD)
553 get_controlfield_key_into_input(&pitem);
555 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
557 if(active_item == index)
558 gold_text->draw_align(pitem.get_input_with_symbol(true), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
560 gold_text->draw_align(pitem.get_input_with_symbol(false), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
563 gold_text->draw_align(pitem.input,
564 x_pos + text_pos, y_pos,
565 A_HMIDDLE, A_VMIDDLE, 2);
567 text_font->draw_align(pitem.text,
568 x_pos - (input_width + font_width)/2, y_pos,
569 A_HMIDDLE, A_VMIDDLE, shadow_size);
572 case MN_STRINGSELECT:
574 int list_pos_2 = list_width + font_width;
575 int list_pos = list_width/2;
576 int text_pos = (text_width + font_width)/2;
579 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
580 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
582 /* Draw input background */
583 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
586 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
590 gold_text->draw_align(string_list_active(pitem.list),
591 x_pos + text_pos, y_pos,
592 A_HMIDDLE, A_VMIDDLE,2);
594 text_font->draw_align(pitem.text,
595 x_pos - list_pos_2/2, y_pos,
596 A_HMIDDLE, A_VMIDDLE, shadow_size);
601 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
602 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
608 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
611 checkbox_checked->draw(
612 x_pos + (text_width+font_width)/2,
616 x_pos + (text_width+font_width)/2,
621 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
625 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
630 int Menu::get_width() const
632 /* The width of the menu has to be more than the width of the text
633 with the most characters */
635 for(unsigned int i = 0; i < item.size(); ++i)
637 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
641 if( item[i].kind == MN_TOGGLE)
646 return (menu_width * 16 + 24);
649 int Menu::get_height() const
651 return item.size() * 24;
654 /* Draw the current menu. */
658 int menu_height = get_height();
659 int menu_width = get_width();
661 /* Draw a transparent background */
662 fillrect(pos_x - menu_width/2,
663 pos_y - 24*item.size()/2 - 10,
664 menu_width,menu_height + 20,
667 for(unsigned int i = 0; i < item.size(); ++i)
669 draw_item(i, menu_width, menu_height);
674 Menu::get_item_by_id(int id)
676 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
682 static MenuItem dummyitem;
686 int Menu::get_active_item_id()
688 return item[active_item].id;
692 Menu::isToggled(int id)
694 return get_item_by_id(id).toggled;
697 /* Check for menu event */
699 Menu::event(SDL_Event& event)
705 key = event.key.keysym.sym;
708 keymod = SDL_GetModState();
711 /* If the current unicode character is an ASCII character,
713 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
715 ch[0] = event.key.keysym.unicode & 0x7F;
720 /* An International Character. */
723 if(item[active_item].kind == MN_CONTROLFIELD)
725 if(key == SDLK_ESCAPE)
730 *item[active_item].int_p = key;
731 menuaction = MENU_ACTION_DOWN;
738 case SDLK_UP: /* Menu Up */
739 menuaction = MENU_ACTION_UP;
741 case SDLK_DOWN: /* Menu Down */
742 menuaction = MENU_ACTION_DOWN;
744 case SDLK_LEFT: /* Menu Up */
745 menuaction = MENU_ACTION_LEFT;
747 case SDLK_RIGHT: /* Menu Down */
748 menuaction = MENU_ACTION_RIGHT;
751 if(item[active_item].kind == MN_TEXTFIELD)
753 menuaction = MENU_ACTION_INPUT;
757 case SDLK_RETURN: /* Menu Hit */
758 menuaction = MENU_ACTION_HIT;
762 menuaction = MENU_ACTION_REMOVE;
769 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
771 menuaction = MENU_ACTION_INPUT;
776 mn_input_char = '\0';
781 case SDL_JOYAXISMOTION:
782 if(event.jaxis.axis == joystick_keymap.y_axis)
784 if (event.jaxis.value > 1024)
785 menuaction = MENU_ACTION_DOWN;
786 else if (event.jaxis.value < -1024)
787 menuaction = MENU_ACTION_UP;
790 case SDL_JOYBUTTONDOWN:
791 menuaction = MENU_ACTION_HIT;
793 case SDL_MOUSEBUTTONDOWN:
796 if(x > pos_x - get_width()/2 &&
797 x < pos_x + get_width()/2 &&
798 y > pos_y - get_height()/2 &&
799 y < pos_y + get_height()/2)
801 menuaction = MENU_ACTION_HIT;
804 case SDL_MOUSEMOTION:
807 if(x > pos_x - get_width()/2 &&
808 x < pos_x + get_width()/2 &&
809 y > pos_y - get_height()/2 &&
810 y < pos_y + get_height()/2)
812 active_item = (y - (pos_y - get_height()/2)) / 24;
813 mouse_cursor->set_state(MC_LINK);
817 mouse_cursor->set_state(MC_NORMAL);