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"
33 #include "../gui/menu.h"
34 #include "../video/screen.h"
35 #include "../video/drawing_context.h"
36 #include "../app/setup.h"
37 #include "../special/timer.h"
38 #include "../app/gettext.h"
39 #include "../math/vector.h"
41 using namespace SuperTux;
43 #define FLICK_CURSOR_TIME 500
45 Surface* SuperTux::checkbox;
46 Surface* SuperTux::checkbox_checked;
47 Surface* SuperTux::back;
48 Surface* SuperTux::arrow_left;
49 Surface* SuperTux::arrow_right;
51 std::vector<Menu*> Menu::last_menus;
52 Menu* Menu::current_ = 0;
54 /* just displays a Yes/No text that can be used to confirm stuff */
55 bool SuperTux::confirm_dialog(Surface *background, std::string text)
57 //Surface* cap_screen = Surface::CaptureScreen();
59 Menu* dialog = new Menu;
60 dialog->additem(MN_DEACTIVE, text,0,0);
61 dialog->additem(MN_HL,"",0,0);
62 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
63 dialog->additem(MN_ACTION,_("No"),0,0,false);
64 dialog->additem(MN_HL,"",0,0);
66 Menu::set_current(dialog);
68 DrawingContext context;
74 while (SDL_PollEvent(&event))
79 if(background == NULL)
80 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
82 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
84 dialog->draw(context);
87 switch (dialog->check())
105 mouse_cursor->draw(context);
106 context.do_drawing();
114 Menu::push_current(Menu* pmenu)
117 last_menus.push_back(current_);
120 current_->effect.start(500);
126 if (!last_menus.empty())
128 current_ = last_menus.back();
129 current_->effect.start(500);
131 last_menus.pop_back();
140 Menu::set_current(Menu* menu)
145 menu->effect.start(500);
150 /* Return a pointer to a new menu item */
152 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
154 MenuItem *pnew_item = new MenuItem;
156 pnew_item->kind = kind_;
157 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
158 strcpy(pnew_item->text, text_);
160 if(kind_ == MN_TOGGLE)
161 pnew_item->toggled = init_toggle_;
163 pnew_item->toggled = false;
165 pnew_item->target_menu = target_menu_;
166 pnew_item->input = (char*) malloc(sizeof(char));
167 pnew_item->input[0] = '\0';
169 if(kind_ == MN_STRINGSELECT)
171 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
172 string_list_init(pnew_item->list);
175 pnew_item->list = NULL;
178 pnew_item->int_p = int_p_;
180 pnew_item->input_flickering = false;
181 pnew_item->input_flickering_timer.init(true);
182 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
188 MenuItem::change_text(const char *text_)
193 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
199 MenuItem::change_input(const char *text_)
204 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
205 strcpy(input, text_);
209 std::string MenuItem::get_input_with_symbol(bool active_item)
212 input_flickering = true;
215 if(input_flickering_timer.get_left() < 0)
218 input_flickering = false;
220 input_flickering = true;
221 input_flickering_timer.start(FLICK_CURSOR_TIME);
227 sprintf(str,"%s ",input);
229 sprintf(str,"%s_",input);
231 std::string string = str;
236 /* Set ControlField for keyboard key */
237 void Menu::get_controlfield_key_into_input(MenuItem *item)
242 item->change_input(_("Up cursor"));
245 item->change_input(_("Down cursor"));
248 item->change_input(_("Left cursor"));
251 item->change_input(_("Right cursor"));
254 item->change_input(_("Return"));
257 item->change_input(_("Space"));
260 item->change_input(_("Right Shift"));
263 item->change_input(_("Left Shift"));
266 item->change_input(_("Right Control"));
269 item->change_input(_("Left Control"));
272 item->change_input(_("Right Alt"));
275 item->change_input(_("Left Alt"));
280 snprintf(tmp, 64, "%d", *item->int_p);
281 item->change_input(tmp);
287 /* Set ControlField for joystick button */
288 void Menu::get_controlfield_js_into_input(MenuItem *item)
290 std::ostringstream oss;
291 oss << "Button " << *item->int_p;
292 item->change_input(oss.str().c_str());
295 /* Free a menu and all its items */
300 for(unsigned int i = 0; i < item.size(); ++i)
304 string_list_free(item[i].list);
313 menuaction = MENU_ACTION_NONE;
314 delete_character = 0;
315 mn_input_char = '\0';
323 joystick_timer.init(true);
326 void Menu::set_pos(int x, int y, float rw, float rh)
328 pos_x = x + (int)((float)get_width() * rw);
329 pos_y = y + (int)((float)get_height() * rh);
333 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
335 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
338 /* Add an item to a menu */
340 Menu::additem(MenuItem* pmenu_item)
342 item.push_back(*pmenu_item);
352 /* Process actions done on the menu */
365 active_item = int(item.size())-1;
368 case MENU_ACTION_DOWN:
369 if(active_item < int(item.size())-1)
375 case MENU_ACTION_LEFT:
376 if(item[active_item].kind == MN_STRINGSELECT
377 && item[active_item].list->num_items != 0)
379 if(item[active_item].list->active_item > 0)
380 --item[active_item].list->active_item;
382 item[active_item].list->active_item = item[active_item].list->num_items-1;
386 case MENU_ACTION_RIGHT:
387 if(item[active_item].kind == MN_STRINGSELECT
388 && item[active_item].list->num_items != 0)
390 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
391 ++item[active_item].list->active_item;
393 item[active_item].list->active_item = 0;
397 case MENU_ACTION_HIT:
399 hit_item = active_item;
400 switch (item[active_item].kind)
403 if (item[active_item].target_menu != NULL)
404 Menu::push_current(item[active_item].target_menu);
410 item[active_item].toggled = !item[active_item].toggled;
414 Menu::set_current(0);
415 item[active_item].toggled = true;
419 menuaction = MENU_ACTION_DOWN;
432 case MENU_ACTION_REMOVE:
433 if(item[active_item].kind == MN_TEXTFIELD
434 || item[active_item].kind == MN_NUMFIELD)
436 if(item[active_item].input != NULL)
438 int i = strlen(item[active_item].input);
440 while(delete_character > 0) /* remove charactes */
442 item[active_item].input[i-1] = '\0';
449 case MENU_ACTION_INPUT:
450 if(item[active_item].kind == MN_TEXTFIELD
451 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
453 if(item[active_item].input != NULL)
455 int i = strlen(item[active_item].input);
456 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
457 item[active_item].input[i] = mn_input_char;
458 item[active_item].input[i+1] = '\0';
462 item[active_item].input = (char*) malloc(2*sizeof(char));
463 item[active_item].input[0] = mn_input_char;
464 item[active_item].input[1] = '\0';
468 case MENU_ACTION_NONE:
473 MenuItem& new_item = item[active_item];
474 if(new_item.kind == MN_DEACTIVE
475 || new_item.kind == MN_LABEL
476 || new_item.kind == MN_HL)
478 // Skip the horzontal line item
479 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
480 menuaction = MENU_ACTION_DOWN;
486 menuaction = MENU_ACTION_NONE;
488 if (active_item >= int(item.size()))
489 active_item = int(item.size()) - 1;
496 return item[hit_item].id;
502 Menu::draw_item(DrawingContext& context,
503 int index, // Position of the current item in the menu
504 int menu_width, int menu_height)
506 MenuItem& pitem = item[index];
508 int effect_offset = 0;
513 effect_time = effect.get_left() / 4;
515 effect_offset = (index % 2) ? effect_time : -effect_time;
518 Font* text_font = white_text;
520 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
522 int text_width = int(text_font->get_text_width(pitem.text));
523 int input_width = int(text_font->get_text_width(pitem.input) + 10);
525 int(text_font->get_text_width(string_list_active(pitem.list)));
528 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
530 if(index == active_item)
533 text_font = blue_text;
540 context.draw_text_center(gray_text, pitem.text,
541 Vector(0, y_pos - int(blue_text->get_height()/2)),
549 int x = pos_x - menu_width/2;
550 int y = y_pos - 12 - effect_offset;
551 /* Draw a horizontal line with a little 3d effect */
552 context.draw_filled_rect(Vector(x, y + 6),
553 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
554 context.draw_filled_rect(Vector(x, y + 6),
555 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
560 context.draw_text_center(white_big_text,
561 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
567 case MN_CONTROLFIELD_KB:
568 case MN_CONTROLFIELD_JS:
570 int width = text_width + input_width + 5;
571 int text_pos = screen->w/2 - width/2;
572 int input_pos = text_pos + text_width + 10;
574 context.draw_filled_rect(
575 Vector(input_pos - 5, y_pos - 10),
576 Vector(input_width + 10, 20),
577 Color(255,255,255,255), LAYER_GUI-5);
578 context.draw_filled_rect(
579 Vector(input_pos - 4, y_pos - 9),
580 Vector(input_width + 8, 18),
581 Color(0,0,0,128), LAYER_GUI-4);
583 if(pitem.kind == MN_CONTROLFIELD_KB)
584 get_controlfield_key_into_input(&pitem);
585 else if (pitem.kind == MN_CONTROLFIELD_JS)
586 get_controlfield_js_into_input(&pitem);
588 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
590 if(active_item == index)
591 context.draw_text(gold_text,
592 pitem.get_input_with_symbol(true),
593 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
596 context.draw_text(gold_text,
597 pitem.get_input_with_symbol(false),
598 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
602 context.draw_text(gold_text, pitem.input,
603 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
606 context.draw_text(text_font, pitem.text,
607 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
611 case MN_STRINGSELECT:
613 int list_pos_2 = list_width + 16;
614 int list_pos = list_width/2;
615 int text_pos = (text_width + 16)/2;
618 context.draw_surface(arrow_left,
619 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
621 context.draw_surface(arrow_right,
622 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
625 /* Draw input background */
626 context.draw_filled_rect(
627 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
628 Vector(list_pos_2 + 2, 20),
629 Color(255,255,255,255), LAYER_GUI - 4);
630 context.draw_filled_rect(
631 Vector(x_pos - list_pos + text_pos, y_pos - 9),
632 Vector(list_pos_2, 18),
633 Color(0,0,0,128), LAYER_GUI - 5);
635 context.draw_text_center(text_font, string_list_active(pitem.list),
636 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
638 context.draw_text_center(text_font, pitem.text,
639 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
645 context.draw_text_center(text_font, pitem.text,
646 Vector(0, y_pos - int(text_font->get_height()/2)),
648 context.draw_surface(back,
649 Vector(x_pos + text_width/2 + 16, y_pos - 8),
656 context.draw_text_center(text_font, pitem.text,
657 Vector(0, y_pos - (text_font->get_height()/2)),
661 context.draw_surface(checkbox_checked,
662 Vector(x_pos + (text_width+16)/2, y_pos - 8),
665 context.draw_surface(checkbox,
666 Vector(x_pos + (text_width+16)/2, y_pos - 8),
671 context.draw_text_center(text_font, pitem.text,
672 Vector(0, y_pos - int(text_font->get_height()/2)),
677 context.draw_text_center(text_font, pitem.text,
678 Vector(0, y_pos - int(text_font->get_height()/2)),
684 int Menu::get_width() const
686 /* The width of the menu has to be more than the width of the text
687 with the most characters */
689 for(unsigned int i = 0; i < item.size(); ++i)
691 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
695 if( item[i].kind == MN_TOGGLE)
700 return (menu_width * 16 + 24);
703 int Menu::get_height() const
705 return item.size() * 24;
708 /* Draw the current menu. */
710 Menu::draw(DrawingContext& context)
712 int menu_height = get_height();
713 int menu_width = get_width();
715 /* Draw a transparent background */
716 context.draw_filled_rect(
717 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
718 Vector(menu_width,menu_height + 20),
719 Color(150,180,200,125), LAYER_GUI-10);
721 for(unsigned int i = 0; i < item.size(); ++i)
723 draw_item(context, i, menu_width, menu_height);
728 Menu::get_item_by_id(int id)
730 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
737 static MenuItem dummyitem;
741 int Menu::get_active_item_id()
743 return item[active_item].id;
747 Menu::isToggled(int id)
749 return get_item_by_id(id).toggled;
752 /* Check for menu event */
754 Menu::event(SDL_Event& event)
760 SDLKey key = key = event.key.keysym.sym;
763 keymod = SDL_GetModState();
765 /* If the current unicode character is an ASCII character,
767 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
769 ch[0] = event.key.keysym.unicode & 0x7F;
774 /* An International Character. */
777 if(item[active_item].kind == MN_CONTROLFIELD_KB)
779 if(key == SDLK_ESCAPE)
784 *item[active_item].int_p = key;
785 menuaction = MENU_ACTION_DOWN;
792 case SDLK_UP: /* Menu Up */
793 menuaction = MENU_ACTION_UP;
795 case SDLK_DOWN: /* Menu Down */
796 menuaction = MENU_ACTION_DOWN;
798 case SDLK_LEFT: /* Menu Up */
799 menuaction = MENU_ACTION_LEFT;
801 case SDLK_RIGHT: /* Menu Down */
802 menuaction = MENU_ACTION_RIGHT;
805 if(item[active_item].kind == MN_TEXTFIELD)
807 menuaction = MENU_ACTION_INPUT;
811 case SDLK_RETURN: /* Menu Hit */
812 menuaction = MENU_ACTION_HIT;
816 menuaction = MENU_ACTION_REMOVE;
823 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
825 menuaction = MENU_ACTION_INPUT;
830 mn_input_char = '\0';
837 case SDL_JOYAXISMOTION:
838 if(event.jaxis.axis == joystick_keymap.y_axis)
840 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
842 menuaction = MENU_ACTION_DOWN;
843 joystick_timer.start(JOYSTICK_MENU_DELAY);
845 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
847 menuaction = MENU_ACTION_UP;
848 joystick_timer.start(JOYSTICK_MENU_DELAY);
851 joystick_timer.stop();
854 case SDL_JOYBUTTONDOWN:
855 if (item[active_item].kind == MN_CONTROLFIELD_JS)
857 // FIXME: This next line does nothing useable, right?
858 // *item[active_item].int_p = key;
859 menuaction = MENU_ACTION_DOWN;
861 menuaction = MENU_ACTION_HIT;
864 case SDL_MOUSEBUTTONDOWN:
866 int x = event.motion.x;
867 int y = event.motion.y;
869 if(x > pos_x - get_width()/2 &&
870 x < pos_x + get_width()/2 &&
871 y > pos_y - get_height()/2 &&
872 y < pos_y + get_height()/2)
874 menuaction = MENU_ACTION_HIT;
879 case SDL_MOUSEMOTION:
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 active_item = (y - (pos_y - get_height()/2)) / 24;
890 mouse_cursor->set_state(MC_LINK);
894 mouse_cursor->set_state(MC_NORMAL);