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>
35 #include "screen/screen.h"
36 #include "screen/drawing_context.h"
40 #include "leveleditor.h"
42 #include "high_scores.h"
45 #define FLICK_CURSOR_TIME 500
48 Surface* checkbox_checked;
55 Menu* worldmap_menu = 0;
56 Menu* options_menu = 0;
57 Menu* options_keys_menu = 0;
58 Menu* options_joystick_menu = 0;
59 Menu* highscore_menu = 0;
60 Menu* load_game_menu = 0;
61 Menu* save_game_menu = 0;
62 Menu* contrib_worldmap_menu = 0;
63 Menu* contrib_menu = 0;
64 Menu* contrib_subset_menu = 0;
66 std::vector<Menu*> Menu::last_menus;
67 Menu* Menu::current_ = 0;
69 /* just displays a Yes/No text that can be used to confirm stuff */
70 bool confirm_dialog(Surface *background, std::string text)
72 //Surface* cap_screen = Surface::CaptureScreen();
74 Menu* dialog = new Menu;
75 dialog->additem(MN_DEACTIVE, text,0,0);
76 dialog->additem(MN_HL,"",0,0);
77 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
78 dialog->additem(MN_ACTION,_("No"),0,0,false);
79 dialog->additem(MN_HL,"",0,0);
81 Menu::set_current(dialog);
83 DrawingContext context;
89 while (SDL_PollEvent(&event))
94 if(background == NULL)
95 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
97 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
99 dialog->draw(context);
102 switch (dialog->check())
106 Menu::set_current(0);
112 Menu::set_current(0);
120 mouse_cursor->draw(context);
121 context.do_drawing();
129 Menu::push_current(Menu* pmenu)
132 last_menus.push_back(current_);
135 current_->effect.start(500);
141 if (!last_menus.empty())
143 current_ = last_menus.back();
144 current_->effect.start(500);
146 last_menus.pop_back();
155 Menu::set_current(Menu* menu)
160 menu->effect.start(500);
165 /* Return a pointer to a new menu item */
167 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
169 MenuItem *pnew_item = new MenuItem;
171 pnew_item->kind = kind_;
172 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
173 strcpy(pnew_item->text, text_);
175 if(kind_ == MN_TOGGLE)
176 pnew_item->toggled = init_toggle_;
178 pnew_item->toggled = false;
180 pnew_item->target_menu = target_menu_;
181 pnew_item->input = (char*) malloc(sizeof(char));
182 pnew_item->input[0] = '\0';
184 if(kind_ == MN_STRINGSELECT)
186 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
187 string_list_init(pnew_item->list);
190 pnew_item->list = NULL;
193 pnew_item->int_p = int_p_;
195 pnew_item->input_flickering = false;
196 pnew_item->input_flickering_timer.init(true);
197 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
203 MenuItem::change_text(const char *text_)
208 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
214 MenuItem::change_input(const char *text_)
219 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
220 strcpy(input, text_);
224 std::string MenuItem::get_input_with_symbol(bool active_item)
227 input_flickering = true;
230 if(input_flickering_timer.get_left() < 0)
233 input_flickering = false;
235 input_flickering = true;
236 input_flickering_timer.start(FLICK_CURSOR_TIME);
242 sprintf(str,"%s ",input);
244 sprintf(str,"%s_",input);
246 std::string string = str;
251 /* Set ControlField for keyboard key */
252 void Menu::get_controlfield_key_into_input(MenuItem *item)
257 item->change_input(_("Up cursor"));
260 item->change_input(_("Down cursor"));
263 item->change_input(_("Left cursor"));
266 item->change_input(_("Right cursor"));
269 item->change_input(_("Return"));
272 item->change_input(_("Space"));
275 item->change_input(_("Right Shift"));
278 item->change_input(_("Left Shift"));
281 item->change_input(_("Right Control"));
284 item->change_input(_("Left Control"));
287 item->change_input(_("Right Alt"));
290 item->change_input(_("Left Alt"));
295 snprintf(tmp, 64, "%d", *item->int_p);
296 item->change_input(tmp);
302 /* Set ControlField for joystick button */
303 void Menu::get_controlfield_js_into_input(MenuItem *item)
305 std::ostringstream oss;
306 oss << "Button " << *item->int_p;
307 item->change_input(oss.str().c_str());
310 /* Free a menu and all its items */
315 for(unsigned int i = 0; i < item.size(); ++i)
319 string_list_free(item[i].list);
328 menuaction = MENU_ACTION_NONE;
329 delete_character = 0;
330 mn_input_char = '\0';
338 joystick_timer.init(true);
341 void Menu::set_pos(int x, int y, float rw, float rh)
343 pos_x = x + (int)((float)get_width() * rw);
344 pos_y = y + (int)((float)get_height() * rh);
348 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
350 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
353 /* Add an item to a menu */
355 Menu::additem(MenuItem* pmenu_item)
357 item.push_back(*pmenu_item);
367 /* Process actions done on the menu */
380 active_item = int(item.size())-1;
383 case MENU_ACTION_DOWN:
384 if(active_item < int(item.size())-1)
390 case MENU_ACTION_LEFT:
391 if(item[active_item].kind == MN_STRINGSELECT
392 && item[active_item].list->num_items != 0)
394 if(item[active_item].list->active_item > 0)
395 --item[active_item].list->active_item;
397 item[active_item].list->active_item = item[active_item].list->num_items-1;
401 case MENU_ACTION_RIGHT:
402 if(item[active_item].kind == MN_STRINGSELECT
403 && item[active_item].list->num_items != 0)
405 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
406 ++item[active_item].list->active_item;
408 item[active_item].list->active_item = 0;
412 case MENU_ACTION_HIT:
414 hit_item = active_item;
415 switch (item[active_item].kind)
418 if (item[active_item].target_menu != NULL)
419 Menu::push_current(item[active_item].target_menu);
425 item[active_item].toggled = !item[active_item].toggled;
429 Menu::set_current(0);
430 item[active_item].toggled = true;
434 menuaction = MENU_ACTION_DOWN;
447 case MENU_ACTION_REMOVE:
448 if(item[active_item].kind == MN_TEXTFIELD
449 || item[active_item].kind == MN_NUMFIELD)
451 if(item[active_item].input != NULL)
453 int i = strlen(item[active_item].input);
455 while(delete_character > 0) /* remove charactes */
457 item[active_item].input[i-1] = '\0';
464 case MENU_ACTION_INPUT:
465 if(item[active_item].kind == MN_TEXTFIELD
466 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
468 if(item[active_item].input != NULL)
470 int i = strlen(item[active_item].input);
471 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
472 item[active_item].input[i] = mn_input_char;
473 item[active_item].input[i+1] = '\0';
477 item[active_item].input = (char*) malloc(2*sizeof(char));
478 item[active_item].input[0] = mn_input_char;
479 item[active_item].input[1] = '\0';
483 case MENU_ACTION_NONE:
488 MenuItem& new_item = item[active_item];
489 if(new_item.kind == MN_DEACTIVE
490 || new_item.kind == MN_LABEL
491 || new_item.kind == MN_HL)
493 // Skip the horzontal line item
494 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
495 menuaction = MENU_ACTION_DOWN;
501 menuaction = MENU_ACTION_NONE;
503 if (active_item >= int(item.size()))
504 active_item = int(item.size()) - 1;
511 return item[hit_item].id;
517 Menu::draw_item(DrawingContext& context,
518 int index, // Position of the current item in the menu
519 int menu_width, int menu_height)
521 MenuItem& pitem = item[index];
523 int effect_offset = 0;
528 effect_time = effect.get_left() / 4;
530 effect_offset = (index % 2) ? effect_time : -effect_time;
533 Font* text_font = white_text;
535 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
537 int text_width = int(text_font->get_text_width(pitem.text));
538 int input_width = int(text_font->get_text_width(pitem.input) + 10);
540 int(text_font->get_text_width(string_list_active(pitem.list)));
543 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
545 if(index == active_item)
548 text_font = blue_text;
555 context.draw_text_center(gray_text, pitem.text,
556 Vector(0, y_pos - int(blue_text->get_height()/2)),
564 int x = pos_x - menu_width/2;
565 int y = y_pos - 12 - effect_offset;
566 /* Draw a horizontal line with a little 3d effect */
567 context.draw_filled_rect(Vector(x, y + 6),
568 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
569 context.draw_filled_rect(Vector(x, y + 6),
570 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
575 context.draw_text_center(white_big_text,
576 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
582 case MN_CONTROLFIELD_KB:
583 case MN_CONTROLFIELD_JS:
585 int width = text_width + input_width + 5;
586 int text_pos = screen->w/2 - width/2;
587 int input_pos = text_pos + text_width + 10;
589 context.draw_filled_rect(
590 Vector(input_pos - 5, y_pos - 10),
591 Vector(input_width + 10, 20),
592 Color(255,255,255,255), LAYER_GUI-5);
593 context.draw_filled_rect(
594 Vector(input_pos - 4, y_pos - 9),
595 Vector(input_width + 8, 18),
596 Color(0,0,0,128), LAYER_GUI-4);
598 if(pitem.kind == MN_CONTROLFIELD_KB)
599 get_controlfield_key_into_input(&pitem);
600 else if (pitem.kind == MN_CONTROLFIELD_JS)
601 get_controlfield_js_into_input(&pitem);
603 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
605 if(active_item == index)
606 context.draw_text(gold_text,
607 pitem.get_input_with_symbol(true),
608 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
611 context.draw_text(gold_text,
612 pitem.get_input_with_symbol(false),
613 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
617 context.draw_text(gold_text, pitem.input,
618 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
621 context.draw_text(text_font, pitem.text,
622 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
626 case MN_STRINGSELECT:
628 int list_pos_2 = list_width + 16;
629 int list_pos = list_width/2;
630 int text_pos = (text_width + 16)/2;
633 context.draw_surface(arrow_left,
634 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
636 context.draw_surface(arrow_right,
637 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
640 /* Draw input background */
641 context.draw_filled_rect(
642 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
643 Vector(list_pos_2 + 2, 20),
644 Color(255,255,255,255), LAYER_GUI - 4);
645 context.draw_filled_rect(
646 Vector(x_pos - list_pos + text_pos, y_pos - 9),
647 Vector(list_pos_2, 18),
648 Color(0,0,0,128), LAYER_GUI - 5);
650 context.draw_text_center(text_font, string_list_active(pitem.list),
651 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
653 context.draw_text_center(text_font, pitem.text,
654 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
660 context.draw_text_center(text_font, pitem.text,
661 Vector(0, y_pos - int(text_font->get_height()/2)),
663 context.draw_surface(back,
664 Vector(x_pos + text_width/2 + 16, y_pos - 8),
671 context.draw_text_center(text_font, pitem.text,
672 Vector(0, y_pos - (text_font->get_height()/2)),
676 context.draw_surface(checkbox_checked,
677 Vector(x_pos + (text_width+16)/2, y_pos - 8),
680 context.draw_surface(checkbox,
681 Vector(x_pos + (text_width+16)/2, y_pos - 8),
686 context.draw_text_center(text_font, pitem.text,
687 Vector(0, y_pos - int(text_font->get_height()/2)),
692 context.draw_text_center(text_font, pitem.text,
693 Vector(0, y_pos - int(text_font->get_height()/2)),
699 int Menu::get_width() const
701 /* The width of the menu has to be more than the width of the text
702 with the most characters */
704 for(unsigned int i = 0; i < item.size(); ++i)
706 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
710 if( item[i].kind == MN_TOGGLE)
715 return (menu_width * 16 + 24);
718 int Menu::get_height() const
720 return item.size() * 24;
723 /* Draw the current menu. */
725 Menu::draw(DrawingContext& context)
727 int menu_height = get_height();
728 int menu_width = get_width();
730 /* Draw a transparent background */
731 context.draw_filled_rect(
732 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
733 Vector(menu_width,menu_height + 20),
734 Color(150,180,200,125), LAYER_GUI-10);
736 for(unsigned int i = 0; i < item.size(); ++i)
738 draw_item(context, i, menu_width, menu_height);
743 Menu::get_item_by_id(int id)
745 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
752 static MenuItem dummyitem;
756 int Menu::get_active_item_id()
758 return item[active_item].id;
762 Menu::isToggled(int id)
764 return get_item_by_id(id).toggled;
767 /* Check for menu event */
769 Menu::event(SDL_Event& event)
775 SDLKey key = key = event.key.keysym.sym;
778 keymod = SDL_GetModState();
780 /* If the current unicode character is an ASCII character,
782 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
784 ch[0] = event.key.keysym.unicode & 0x7F;
789 /* An International Character. */
792 if(item[active_item].kind == MN_CONTROLFIELD_KB)
794 if(key == SDLK_ESCAPE)
799 *item[active_item].int_p = key;
800 menuaction = MENU_ACTION_DOWN;
807 case SDLK_UP: /* Menu Up */
808 menuaction = MENU_ACTION_UP;
810 case SDLK_DOWN: /* Menu Down */
811 menuaction = MENU_ACTION_DOWN;
813 case SDLK_LEFT: /* Menu Up */
814 menuaction = MENU_ACTION_LEFT;
816 case SDLK_RIGHT: /* Menu Down */
817 menuaction = MENU_ACTION_RIGHT;
820 if(item[active_item].kind == MN_TEXTFIELD)
822 menuaction = MENU_ACTION_INPUT;
826 case SDLK_RETURN: /* Menu Hit */
827 menuaction = MENU_ACTION_HIT;
831 menuaction = MENU_ACTION_REMOVE;
838 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
840 menuaction = MENU_ACTION_INPUT;
845 mn_input_char = '\0';
852 case SDL_JOYAXISMOTION:
853 if(event.jaxis.axis == joystick_keymap.y_axis)
855 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
857 menuaction = MENU_ACTION_DOWN;
858 joystick_timer.start(JOYSTICK_MENU_DELAY);
860 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
862 menuaction = MENU_ACTION_UP;
863 joystick_timer.start(JOYSTICK_MENU_DELAY);
866 joystick_timer.stop();
869 case SDL_JOYBUTTONDOWN:
870 if (item[active_item].kind == MN_CONTROLFIELD_JS)
872 // FIXME: This next line does nothing useable, right?
873 // *item[active_item].int_p = key;
874 menuaction = MENU_ACTION_DOWN;
876 menuaction = MENU_ACTION_HIT;
879 case SDL_MOUSEBUTTONDOWN:
881 int x = event.motion.x;
882 int y = event.motion.y;
884 if(x > pos_x - get_width()/2 &&
885 x < pos_x + get_width()/2 &&
886 y > pos_y - get_height()/2 &&
887 y < pos_y + get_height()/2)
889 menuaction = MENU_ACTION_HIT;
894 case SDL_MOUSEMOTION:
896 int x = event.motion.x;
897 int y = event.motion.y;
899 if(x > pos_x - get_width()/2 &&
900 x < pos_x + get_width()/2 &&
901 y > pos_y - get_height()/2 &&
902 y < pos_y + get_height()/2)
904 active_item = (y - (pos_y - get_height()/2)) / 24;
905 mouse_cursor->set_state(MC_LINK);
909 mouse_cursor->set_state(MC_NORMAL);