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);
215 static char str[1024];
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;
392 Menu::set_current(0);
393 item[active_item].toggled = true;
397 menuaction = MENU_ACTION_DOWN;
410 case MENU_ACTION_REMOVE:
411 if(item[active_item].kind == MN_TEXTFIELD
412 || item[active_item].kind == MN_NUMFIELD)
414 if(item[active_item].input != NULL)
416 int i = strlen(item[active_item].input);
418 while(delete_character > 0) /* remove charactes */
420 item[active_item].input[i-1] = '\0';
427 case MENU_ACTION_INPUT:
428 if(item[active_item].kind == MN_TEXTFIELD
429 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
431 if(item[active_item].input != NULL)
433 int i = strlen(item[active_item].input);
434 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
435 item[active_item].input[i] = mn_input_char;
436 item[active_item].input[i+1] = '\0';
440 item[active_item].input = (char*) malloc(2*sizeof(char));
441 item[active_item].input[0] = mn_input_char;
442 item[active_item].input[1] = '\0';
446 case MENU_ACTION_NONE:
451 MenuItem& new_item = item[active_item];
452 if(new_item.kind == MN_DEACTIVE
453 || new_item.kind == MN_LABEL
454 || new_item.kind == MN_HL)
456 // Skip the horzontal line item
457 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
458 menuaction = MENU_ACTION_DOWN;
464 menuaction = MENU_ACTION_NONE;
471 return item[hit_item].id;
477 Menu::draw_item(int index, // Position of the current item in the menu
481 MenuItem& pitem = item[index];
484 int effect_offset = 0;
489 effect_time = effect.get_left() / 4;
491 effect_offset = (index % 2) ? effect_time : -effect_time;
495 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
497 int text_width = strlen(pitem.text) * font_width;
498 int input_width = (strlen(pitem.input)+ 1) * font_width;
499 int list_width = strlen(string_list_active(pitem.list)) * font_width;
500 Text* text_font = white_text;
503 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
505 if(index == active_item)
508 text_font = blue_text;
515 black_text->draw_align(pitem.text,
517 A_HMIDDLE, A_VMIDDLE, 2);
523 int x = pos_x - menu_width/2;
524 int y = y_pos - 12 - effect_offset;
525 /* Draw a horizontal line with a little 3d effect */
536 white_big_text->draw_align(pitem.text,
538 A_HMIDDLE, A_VMIDDLE, 2);
543 case MN_CONTROLFIELD:
545 int input_pos = input_width/2;
546 int text_pos = (text_width + font_width)/2;
548 fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
549 input_width + font_width + 2, 20,
551 fillrect(x_pos - input_pos + text_pos, y_pos - 9,
552 input_width + font_width, 18,
555 if(pitem.kind == MN_CONTROLFIELD)
556 get_controlfield_key_into_input(&pitem);
558 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
560 if(active_item == index)
561 gold_text->draw_align(pitem.get_input_with_symbol(true), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
563 gold_text->draw_align(pitem.get_input_with_symbol(false), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
566 gold_text->draw_align(pitem.input,
567 x_pos + text_pos, y_pos,
568 A_HMIDDLE, A_VMIDDLE, 2);
570 text_font->draw_align(pitem.text,
571 x_pos - (input_width + font_width)/2, y_pos,
572 A_HMIDDLE, A_VMIDDLE, shadow_size);
575 case MN_STRINGSELECT:
577 int list_pos_2 = list_width + font_width;
578 int list_pos = list_width/2;
579 int text_pos = (text_width + font_width)/2;
582 arrow_left->draw( x_pos - list_pos + text_pos - 17, y_pos - 8);
583 arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
585 /* Draw input background */
586 fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
589 fillrect(x_pos - list_pos + text_pos, y_pos - 9,
593 gold_text->draw_align(string_list_active(pitem.list),
594 x_pos + text_pos, y_pos,
595 A_HMIDDLE, A_VMIDDLE,2);
597 text_font->draw_align(pitem.text,
598 x_pos - list_pos_2/2, y_pos,
599 A_HMIDDLE, A_VMIDDLE, shadow_size);
604 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
605 back->draw( x_pos + text_width/2 + font_width, y_pos - 8);
611 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
614 checkbox_checked->draw(
615 x_pos + (text_width+font_width)/2,
619 x_pos + (text_width+font_width)/2,
624 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
628 text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
633 int Menu::get_width() const
635 /* The width of the menu has to be more than the width of the text
636 with the most characters */
638 for(unsigned int i = 0; i < item.size(); ++i)
640 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
644 if( item[i].kind == MN_TOGGLE)
649 return (menu_width * 16 + 24);
652 int Menu::get_height() const
654 return item.size() * 24;
657 /* Draw the current menu. */
661 int menu_height = get_height();
662 int menu_width = get_width();
664 /* Draw a transparent background */
665 fillrect(pos_x - menu_width/2,
666 pos_y - 24*item.size()/2 - 10,
667 menu_width,menu_height + 20,
670 for(unsigned int i = 0; i < item.size(); ++i)
672 draw_item(i, menu_width, menu_height);
677 Menu::get_item_by_id(int id)
679 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
685 static MenuItem dummyitem;
689 int Menu::get_active_item_id()
691 return item[active_item].id;
695 Menu::isToggled(int id)
697 return get_item_by_id(id).toggled;
700 /* Check for menu event */
702 Menu::event(SDL_Event& event)
708 key = event.key.keysym.sym;
711 keymod = SDL_GetModState();
714 /* If the current unicode character is an ASCII character,
716 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
718 ch[0] = event.key.keysym.unicode & 0x7F;
723 /* An International Character. */
726 if(item[active_item].kind == MN_CONTROLFIELD)
728 if(key == SDLK_ESCAPE)
733 *item[active_item].int_p = key;
734 menuaction = MENU_ACTION_DOWN;
741 case SDLK_UP: /* Menu Up */
742 menuaction = MENU_ACTION_UP;
744 case SDLK_DOWN: /* Menu Down */
745 menuaction = MENU_ACTION_DOWN;
747 case SDLK_LEFT: /* Menu Up */
748 menuaction = MENU_ACTION_LEFT;
750 case SDLK_RIGHT: /* Menu Down */
751 menuaction = MENU_ACTION_RIGHT;
754 if(item[active_item].kind == MN_TEXTFIELD)
756 menuaction = MENU_ACTION_INPUT;
760 case SDLK_RETURN: /* Menu Hit */
761 menuaction = MENU_ACTION_HIT;
765 menuaction = MENU_ACTION_REMOVE;
772 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
774 menuaction = MENU_ACTION_INPUT;
779 mn_input_char = '\0';
784 case SDL_JOYAXISMOTION:
785 if(event.jaxis.axis == joystick_keymap.y_axis)
787 if (event.jaxis.value > 1024)
788 menuaction = MENU_ACTION_DOWN;
789 else if (event.jaxis.value < -1024)
790 menuaction = MENU_ACTION_UP;
793 case SDL_JOYBUTTONDOWN:
794 menuaction = MENU_ACTION_HIT;
796 case SDL_MOUSEBUTTONDOWN:
799 if(x > pos_x - get_width()/2 &&
800 x < pos_x + get_width()/2 &&
801 y > pos_y - get_height()/2 &&
802 y < pos_y + get_height()/2)
804 menuaction = MENU_ACTION_HIT;
807 case SDL_MOUSEMOTION:
810 if(x > pos_x - get_width()/2 &&
811 x < pos_x + get_width()/2 &&
812 y > pos_y - get_height()/2 &&
813 y < pos_y + get_height()/2)
815 active_item = (y - (pos_y - get_height()/2)) / 24;
816 mouse_cursor->set_state(MC_LINK);
820 mouse_cursor->set_state(MC_NORMAL);