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>
32 #include "app/globals.h"
34 #include "video/screen.h"
35 #include "video/drawing_context.h"
36 #include "app/setup.h"
37 #include "audio/sound.h"
38 #include "special/timer.h"
39 #include "app/gettext.h"
41 #define FLICK_CURSOR_TIME 500
44 Surface* checkbox_checked;
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(Surface *background, std::string text)
66 //Surface* cap_screen = Surface::CaptureScreen();
68 Menu* dialog = new Menu;
69 dialog->additem(MN_DEACTIVE, text,0,0);
70 dialog->additem(MN_HL,"",0,0);
71 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
72 dialog->additem(MN_ACTION,_("No"),0,0,false);
73 dialog->additem(MN_HL,"",0,0);
75 Menu::set_current(dialog);
77 DrawingContext context;
83 while (SDL_PollEvent(&event))
88 if(background == NULL)
89 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
91 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
93 dialog->draw(context);
96 switch (dialog->check())
100 Menu::set_current(0);
106 Menu::set_current(0);
114 mouse_cursor->draw(context);
115 context.do_drawing();
123 Menu::push_current(Menu* pmenu)
126 last_menus.push_back(current_);
129 current_->effect.start(500);
135 if (!last_menus.empty())
137 current_ = last_menus.back();
138 current_->effect.start(500);
140 last_menus.pop_back();
149 Menu::set_current(Menu* menu)
154 menu->effect.start(500);
159 /* Return a pointer to a new menu item */
161 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
163 MenuItem *pnew_item = new MenuItem;
165 pnew_item->kind = kind_;
166 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
167 strcpy(pnew_item->text, text_);
169 if(kind_ == MN_TOGGLE)
170 pnew_item->toggled = init_toggle_;
172 pnew_item->toggled = false;
174 pnew_item->target_menu = target_menu_;
175 pnew_item->input = (char*) malloc(sizeof(char));
176 pnew_item->input[0] = '\0';
178 if(kind_ == MN_STRINGSELECT)
180 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
181 string_list_init(pnew_item->list);
184 pnew_item->list = NULL;
187 pnew_item->int_p = int_p_;
189 pnew_item->input_flickering = false;
190 pnew_item->input_flickering_timer.init(true);
191 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
197 MenuItem::change_text(const char *text_)
202 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
208 MenuItem::change_input(const char *text_)
213 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
214 strcpy(input, text_);
218 std::string MenuItem::get_input_with_symbol(bool active_item)
221 input_flickering = true;
224 if(input_flickering_timer.get_left() < 0)
227 input_flickering = false;
229 input_flickering = true;
230 input_flickering_timer.start(FLICK_CURSOR_TIME);
236 sprintf(str,"%s ",input);
238 sprintf(str,"%s_",input);
240 std::string string = str;
245 /* Set ControlField for keyboard key */
246 void Menu::get_controlfield_key_into_input(MenuItem *item)
251 item->change_input(_("Up cursor"));
254 item->change_input(_("Down cursor"));
257 item->change_input(_("Left cursor"));
260 item->change_input(_("Right cursor"));
263 item->change_input(_("Return"));
266 item->change_input(_("Space"));
269 item->change_input(_("Right Shift"));
272 item->change_input(_("Left Shift"));
275 item->change_input(_("Right Control"));
278 item->change_input(_("Left Control"));
281 item->change_input(_("Right Alt"));
284 item->change_input(_("Left Alt"));
289 snprintf(tmp, 64, "%d", *item->int_p);
290 item->change_input(tmp);
296 /* Set ControlField for joystick button */
297 void Menu::get_controlfield_js_into_input(MenuItem *item)
299 std::ostringstream oss;
300 oss << "Button " << *item->int_p;
301 item->change_input(oss.str().c_str());
304 /* Free a menu and all its items */
309 for(unsigned int i = 0; i < item.size(); ++i)
313 string_list_free(item[i].list);
322 menuaction = MENU_ACTION_NONE;
323 delete_character = 0;
324 mn_input_char = '\0';
332 joystick_timer.init(true);
335 void Menu::set_pos(int x, int y, float rw, float rh)
337 pos_x = x + (int)((float)get_width() * rw);
338 pos_y = y + (int)((float)get_height() * rh);
342 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
344 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
347 /* Add an item to a menu */
349 Menu::additem(MenuItem* pmenu_item)
351 item.push_back(*pmenu_item);
361 /* Process actions done on the menu */
374 active_item = int(item.size())-1;
377 case MENU_ACTION_DOWN:
378 if(active_item < int(item.size())-1)
384 case MENU_ACTION_LEFT:
385 if(item[active_item].kind == MN_STRINGSELECT
386 && item[active_item].list->num_items != 0)
388 if(item[active_item].list->active_item > 0)
389 --item[active_item].list->active_item;
391 item[active_item].list->active_item = item[active_item].list->num_items-1;
395 case MENU_ACTION_RIGHT:
396 if(item[active_item].kind == MN_STRINGSELECT
397 && item[active_item].list->num_items != 0)
399 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
400 ++item[active_item].list->active_item;
402 item[active_item].list->active_item = 0;
406 case MENU_ACTION_HIT:
408 hit_item = active_item;
409 switch (item[active_item].kind)
412 if (item[active_item].target_menu != NULL)
413 Menu::push_current(item[active_item].target_menu);
419 item[active_item].toggled = !item[active_item].toggled;
423 Menu::set_current(0);
424 item[active_item].toggled = true;
428 menuaction = MENU_ACTION_DOWN;
441 case MENU_ACTION_REMOVE:
442 if(item[active_item].kind == MN_TEXTFIELD
443 || item[active_item].kind == MN_NUMFIELD)
445 if(item[active_item].input != NULL)
447 int i = strlen(item[active_item].input);
449 while(delete_character > 0) /* remove charactes */
451 item[active_item].input[i-1] = '\0';
458 case MENU_ACTION_INPUT:
459 if(item[active_item].kind == MN_TEXTFIELD
460 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
462 if(item[active_item].input != NULL)
464 int i = strlen(item[active_item].input);
465 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
466 item[active_item].input[i] = mn_input_char;
467 item[active_item].input[i+1] = '\0';
471 item[active_item].input = (char*) malloc(2*sizeof(char));
472 item[active_item].input[0] = mn_input_char;
473 item[active_item].input[1] = '\0';
477 case MENU_ACTION_NONE:
482 MenuItem& new_item = item[active_item];
483 if(new_item.kind == MN_DEACTIVE
484 || new_item.kind == MN_LABEL
485 || new_item.kind == MN_HL)
487 // Skip the horzontal line item
488 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
489 menuaction = MENU_ACTION_DOWN;
495 menuaction = MENU_ACTION_NONE;
497 if (active_item >= int(item.size()))
498 active_item = int(item.size()) - 1;
505 return item[hit_item].id;
511 Menu::draw_item(DrawingContext& context,
512 int index, // Position of the current item in the menu
513 int menu_width, int menu_height)
515 MenuItem& pitem = item[index];
517 int effect_offset = 0;
522 effect_time = effect.get_left() / 4;
524 effect_offset = (index % 2) ? effect_time : -effect_time;
527 Font* text_font = white_text;
529 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
531 int text_width = int(text_font->get_text_width(pitem.text));
532 int input_width = int(text_font->get_text_width(pitem.input) + 10);
534 int(text_font->get_text_width(string_list_active(pitem.list)));
537 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
539 if(index == active_item)
542 text_font = blue_text;
549 context.draw_text_center(gray_text, pitem.text,
550 Vector(0, y_pos - int(blue_text->get_height()/2)),
558 int x = pos_x - menu_width/2;
559 int y = y_pos - 12 - effect_offset;
560 /* Draw a horizontal line with a little 3d effect */
561 context.draw_filled_rect(Vector(x, y + 6),
562 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
563 context.draw_filled_rect(Vector(x, y + 6),
564 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
569 context.draw_text_center(white_big_text,
570 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
576 case MN_CONTROLFIELD_KB:
577 case MN_CONTROLFIELD_JS:
579 int width = text_width + input_width + 5;
580 int text_pos = screen->w/2 - width/2;
581 int input_pos = text_pos + text_width + 10;
583 context.draw_filled_rect(
584 Vector(input_pos - 5, y_pos - 10),
585 Vector(input_width + 10, 20),
586 Color(255,255,255,255), LAYER_GUI-5);
587 context.draw_filled_rect(
588 Vector(input_pos - 4, y_pos - 9),
589 Vector(input_width + 8, 18),
590 Color(0,0,0,128), LAYER_GUI-4);
592 if(pitem.kind == MN_CONTROLFIELD_KB)
593 get_controlfield_key_into_input(&pitem);
594 else if (pitem.kind == MN_CONTROLFIELD_JS)
595 get_controlfield_js_into_input(&pitem);
597 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
599 if(active_item == index)
600 context.draw_text(gold_text,
601 pitem.get_input_with_symbol(true),
602 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
605 context.draw_text(gold_text,
606 pitem.get_input_with_symbol(false),
607 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
611 context.draw_text(gold_text, pitem.input,
612 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
615 context.draw_text(text_font, pitem.text,
616 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
620 case MN_STRINGSELECT:
622 int list_pos_2 = list_width + 16;
623 int list_pos = list_width/2;
624 int text_pos = (text_width + 16)/2;
627 context.draw_surface(arrow_left,
628 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
630 context.draw_surface(arrow_right,
631 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
634 /* Draw input background */
635 context.draw_filled_rect(
636 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
637 Vector(list_pos_2 + 2, 20),
638 Color(255,255,255,255), LAYER_GUI - 4);
639 context.draw_filled_rect(
640 Vector(x_pos - list_pos + text_pos, y_pos - 9),
641 Vector(list_pos_2, 18),
642 Color(0,0,0,128), LAYER_GUI - 5);
644 context.draw_text_center(text_font, string_list_active(pitem.list),
645 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
647 context.draw_text_center(text_font, pitem.text,
648 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
654 context.draw_text_center(text_font, pitem.text,
655 Vector(0, y_pos - int(text_font->get_height()/2)),
657 context.draw_surface(back,
658 Vector(x_pos + text_width/2 + 16, y_pos - 8),
665 context.draw_text_center(text_font, pitem.text,
666 Vector(0, y_pos - (text_font->get_height()/2)),
670 context.draw_surface(checkbox_checked,
671 Vector(x_pos + (text_width+16)/2, y_pos - 8),
674 context.draw_surface(checkbox,
675 Vector(x_pos + (text_width+16)/2, y_pos - 8),
680 context.draw_text_center(text_font, pitem.text,
681 Vector(0, y_pos - int(text_font->get_height()/2)),
686 context.draw_text_center(text_font, pitem.text,
687 Vector(0, y_pos - int(text_font->get_height()/2)),
693 int Menu::get_width() const
695 /* The width of the menu has to be more than the width of the text
696 with the most characters */
698 for(unsigned int i = 0; i < item.size(); ++i)
700 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
704 if( item[i].kind == MN_TOGGLE)
709 return (menu_width * 16 + 24);
712 int Menu::get_height() const
714 return item.size() * 24;
717 /* Draw the current menu. */
719 Menu::draw(DrawingContext& context)
721 int menu_height = get_height();
722 int menu_width = get_width();
724 /* Draw a transparent background */
725 context.draw_filled_rect(
726 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
727 Vector(menu_width,menu_height + 20),
728 Color(150,180,200,125), LAYER_GUI-10);
730 for(unsigned int i = 0; i < item.size(); ++i)
732 draw_item(context, i, menu_width, menu_height);
737 Menu::get_item_by_id(int id)
739 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
746 static MenuItem dummyitem;
750 int Menu::get_active_item_id()
752 return item[active_item].id;
756 Menu::isToggled(int id)
758 return get_item_by_id(id).toggled;
761 /* Check for menu event */
763 Menu::event(SDL_Event& event)
769 SDLKey key = key = event.key.keysym.sym;
772 keymod = SDL_GetModState();
774 /* If the current unicode character is an ASCII character,
776 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
778 ch[0] = event.key.keysym.unicode & 0x7F;
783 /* An International Character. */
786 if(item[active_item].kind == MN_CONTROLFIELD_KB)
788 if(key == SDLK_ESCAPE)
793 *item[active_item].int_p = key;
794 menuaction = MENU_ACTION_DOWN;
801 case SDLK_UP: /* Menu Up */
802 menuaction = MENU_ACTION_UP;
804 case SDLK_DOWN: /* Menu Down */
805 menuaction = MENU_ACTION_DOWN;
807 case SDLK_LEFT: /* Menu Up */
808 menuaction = MENU_ACTION_LEFT;
810 case SDLK_RIGHT: /* Menu Down */
811 menuaction = MENU_ACTION_RIGHT;
814 if(item[active_item].kind == MN_TEXTFIELD)
816 menuaction = MENU_ACTION_INPUT;
820 case SDLK_RETURN: /* Menu Hit */
821 menuaction = MENU_ACTION_HIT;
825 menuaction = MENU_ACTION_REMOVE;
832 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
834 menuaction = MENU_ACTION_INPUT;
839 mn_input_char = '\0';
846 case SDL_JOYAXISMOTION:
847 if(event.jaxis.axis == joystick_keymap.y_axis)
849 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
851 menuaction = MENU_ACTION_DOWN;
852 joystick_timer.start(JOYSTICK_MENU_DELAY);
854 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
856 menuaction = MENU_ACTION_UP;
857 joystick_timer.start(JOYSTICK_MENU_DELAY);
860 joystick_timer.stop();
863 case SDL_JOYBUTTONDOWN:
864 if (item[active_item].kind == MN_CONTROLFIELD_JS)
866 // FIXME: This next line does nothing useable, right?
867 // *item[active_item].int_p = key;
868 menuaction = MENU_ACTION_DOWN;
870 menuaction = MENU_ACTION_HIT;
873 case SDL_MOUSEBUTTONDOWN:
875 int x = event.motion.x;
876 int y = event.motion.y;
878 if(x > pos_x - get_width()/2 &&
879 x < pos_x + get_width()/2 &&
880 y > pos_y - get_height()/2 &&
881 y < pos_y + get_height()/2)
883 menuaction = MENU_ACTION_HIT;
888 case SDL_MOUSEMOTION:
890 int x = event.motion.x;
891 int y = event.motion.y;
893 if(x > pos_x - get_width()/2 &&
894 x < pos_x + get_width()/2 &&
895 y > pos_y - get_height()/2 &&
896 y < pos_y + get_height()/2)
898 active_item = (y - (pos_y - get_height()/2)) / 24;
899 mouse_cursor->set_state(MC_LINK);
903 mouse_cursor->set_state(MC_NORMAL);