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.
23 #include <sys/types.h>
34 #include "../app/globals.h"
35 #include "../gui/menu.h"
36 #include "../video/screen.h"
37 #include "../video/drawing_context.h"
38 #include "../app/setup.h"
39 #include "../app/gettext.h"
40 #include "../math/vector.h"
42 using namespace SuperTux;
44 #define FLICK_CURSOR_TIME 500
46 Surface* SuperTux::checkbox;
47 Surface* SuperTux::checkbox_checked;
48 Surface* SuperTux::back;
49 Surface* SuperTux::arrow_left;
50 Surface* SuperTux::arrow_right;
52 std::vector<Menu*> Menu::last_menus;
53 Menu* Menu::current_ = 0;
54 Font* Menu::default_font;
55 Font* Menu::active_font;
56 Font* Menu::deactive_font;
57 Font* Menu::label_font;
58 Font* Menu::field_font;
60 /* just displays a Yes/No text that can be used to confirm stuff */
61 bool SuperTux::confirm_dialog(Surface *background, std::string text)
63 //Surface* cap_screen = Surface::CaptureScreen();
65 Menu* dialog = new Menu;
66 dialog->additem(MN_DEACTIVE, text,0,0);
67 dialog->additem(MN_HL,"",0,0);
68 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
69 dialog->additem(MN_ACTION,_("No"),0,0,false);
70 dialog->additem(MN_HL,"",0,0);
72 Menu::set_current(dialog);
74 DrawingContext context;
80 while (SDL_PollEvent(&event))
85 if(background == NULL)
86 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
88 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
90 dialog->draw(context);
93 switch (dialog->check())
103 Menu::set_current(0);
111 mouse_cursor->draw(context);
112 context.do_drawing();
120 Menu::push_current(Menu* pmenu)
123 last_menus.push_back(current_);
126 current_->effect.start(500);
132 if (!last_menus.empty())
134 current_ = last_menus.back();
135 current_->effect.start(500);
137 last_menus.pop_back();
146 Menu::set_current(Menu* menu)
151 menu->effect.start(500);
156 MenuItem::MenuItem(MenuItemKind _kind, int _id)
157 : kind(_kind) , id(_id)
161 MenuItem::MenuItem(MenuItemKind _kind, int _id, const std::string& _text)
162 : kind(_kind) , id(_id) , text(_text)
166 /* Return a pointer to a new menu item */
168 MenuItem::create(MenuItemKind kind_, const std::string& text_, int init_toggle_, Menu* target_menu_, int id_, int* int_p_)
170 MenuItem *pnew_item = new MenuItem(kind_,id_);
172 pnew_item->text = text_;
174 if(kind_ == MN_TOGGLE)
175 pnew_item->toggled = init_toggle_;
177 pnew_item->toggled = false;
179 pnew_item->target_menu = target_menu_;
181 pnew_item->int_p = int_p_;
183 pnew_item->selected = 0;
185 pnew_item->input_flickering = false;
186 pnew_item->input_flickering_timer.init(true);
187 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
193 MenuItem::change_text(const std::string& text_)
199 MenuItem::change_input(const std::string& text_)
204 std::string MenuItem::get_input_with_symbol(bool active_item)
207 input_flickering = true;
210 if(input_flickering_timer.get_left() < 0)
213 input_flickering = false;
215 input_flickering = true;
216 input_flickering_timer.start(FLICK_CURSOR_TIME);
222 sprintf(str,"%s ",input.c_str());
224 sprintf(str,"%s_",input.c_str());
226 std::string string = str;
231 /* Set ControlField for keyboard key */
232 void Menu::get_controlfield_key_into_input(MenuItem *item)
237 item->change_input(_("Up cursor"));
240 item->change_input(_("Down cursor"));
243 item->change_input(_("Left cursor"));
246 item->change_input(_("Right cursor"));
249 item->change_input(_("Return"));
252 item->change_input(_("Space"));
255 item->change_input(_("Right Shift"));
258 item->change_input(_("Left Shift"));
261 item->change_input(_("Right Control"));
264 item->change_input(_("Left Control"));
267 item->change_input(_("Right Alt"));
270 item->change_input(_("Left Alt"));
275 snprintf(tmp, 64, "%d", *item->int_p);
276 item->change_input(tmp);
282 /* Set ControlField for joystick button */
283 void Menu::get_controlfield_js_into_input(MenuItem *item)
285 std::ostringstream oss;
286 oss << "Button " << *item->int_p;
287 item->change_input(oss.str().c_str());
297 menuaction = MENU_ACTION_NONE;
298 delete_character = 0;
299 mn_input_char = '\0';
307 joystick_timer.init(true);
310 void Menu::set_pos(int x, int y, float rw, float rh)
312 pos_x = x + (int)((float)get_width() * rw);
313 pos_y = y + (int)((float)get_height() * rh);
317 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
319 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
321 /* If a new menu is being built, the active item shouldn't be set to something
322 that isnt selectable. Keep setting the active item to the most recently
323 added item until a selectable entry is found.
325 if (item[active_item].kind == MN_HL
326 || item[active_item].kind == MN_LABEL
327 || item[active_item].kind == MN_DEACTIVE)
328 active_item = item.size() - 1;
331 /* Add an item to a menu */
333 Menu::additem(MenuItem* pmenu_item)
335 item.push_back(*pmenu_item);
339 /* Add an item to a menu */
341 Menu::additem(const MenuItem& pmenu_item)
343 item.push_back(pmenu_item);
352 /* Process actions done on the menu */
359 int last_active_item = active_item;
367 active_item = int(item.size())-1;
368 } while ((item[active_item].kind == MN_HL
369 || item[active_item].kind == MN_LABEL
370 || item[active_item].kind == MN_DEACTIVE)
371 && (active_item != last_active_item));
375 case MENU_ACTION_DOWN:
377 if(active_item < int(item.size())-1 )
381 } while ((item[active_item].kind == MN_HL
382 || item[active_item].kind == MN_LABEL
383 || item[active_item].kind == MN_DEACTIVE)
384 && (active_item != last_active_item));
388 case MENU_ACTION_LEFT:
389 if(item[active_item].kind == MN_STRINGSELECT) {
390 if(item[active_item].selected > 0)
391 item[active_item].selected--;
393 item[active_item].selected = item[active_item].list.size()-1;
397 case MENU_ACTION_RIGHT:
398 if(item[active_item].kind == MN_STRINGSELECT) {
399 if(item[active_item].selected+1 < item[active_item].list.size())
400 item[active_item].selected++;
402 item[active_item].selected = 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.empty())
447 int i = item[active_item].input.size();
449 while(delete_character > 0) /* remove charactes */
451 item[active_item].input.resize(i-1);
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 item[active_item].input.push_back(mn_input_char);
465 case MENU_ACTION_NONE:
471 menuaction = MENU_ACTION_NONE;
473 if (active_item >= int(item.size()))
474 active_item = int(item.size()) - 1;
481 return item[hit_item].id;
487 Menu::draw_item(DrawingContext& context,
488 int index, // Position of the current item in the menu
489 int menu_width, int menu_height)
491 MenuItem& pitem = item[index];
493 int effect_offset = 0;
498 effect_time = effect.get_left() / 4;
500 effect_offset = (index % 2) ? effect_time : -effect_time;
503 Font* text_font = default_font;
505 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
507 int text_width = int(text_font->get_text_width(pitem.text));
508 int input_width = int(text_font->get_text_width(pitem.input) + 10);
510 if(pitem.list.size() > 0) {
511 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
515 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
517 if(index == active_item)
520 text_font = active_font;
527 context.draw_text(deactive_font, pitem.text,
528 Vector(screen->w/2, y_pos - int(deactive_font->get_height()/2)),
529 CENTER_ALLIGN, LAYER_GUI);
536 int x = pos_x - menu_width/2;
537 int y = y_pos - 12 - effect_offset;
538 /* Draw a horizontal line with a little 3d effect */
539 context.draw_filled_rect(Vector(x, y + 6),
540 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
541 context.draw_filled_rect(Vector(x, y + 6),
542 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
547 context.draw_text(label_font, pitem.text,
548 Vector(screen->w/2, y_pos - int(label_font->get_height()/2)),
549 CENTER_ALLIGN, LAYER_GUI);
554 case MN_CONTROLFIELD_KB:
555 case MN_CONTROLFIELD_JS:
557 int width = text_width + input_width + 5;
558 int text_pos = screen->w/2 - width/2;
559 int input_pos = text_pos + text_width + 10;
561 context.draw_filled_rect(
562 Vector(input_pos - 5, y_pos - 10),
563 Vector(input_width + 10, 20),
564 Color(255,255,255,255), LAYER_GUI-5);
565 context.draw_filled_rect(
566 Vector(input_pos - 4, y_pos - 9),
567 Vector(input_width + 8, 18),
568 Color(0,0,0,128), LAYER_GUI-4);
570 if(pitem.kind == MN_CONTROLFIELD_KB)
571 get_controlfield_key_into_input(&pitem);
572 else if (pitem.kind == MN_CONTROLFIELD_JS)
573 get_controlfield_js_into_input(&pitem);
575 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
577 if(active_item == index)
578 context.draw_text(field_font,
579 pitem.get_input_with_symbol(true),
580 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
581 LEFT_ALLIGN, LAYER_GUI);
583 context.draw_text(field_font,
584 pitem.get_input_with_symbol(false),
585 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
586 LEFT_ALLIGN, LAYER_GUI);
589 context.draw_text(field_font, pitem.input,
590 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
591 LEFT_ALLIGN, LAYER_GUI);
593 context.draw_text(text_font, pitem.text,
594 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
595 LEFT_ALLIGN, LAYER_GUI);
598 case MN_STRINGSELECT:
600 int list_pos_2 = list_width + 16;
601 int list_pos = list_width/2;
602 int text_pos = (text_width + 16)/2;
605 context.draw_surface(arrow_left,
606 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
608 context.draw_surface(arrow_right,
609 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
612 /* Draw input background */
613 context.draw_filled_rect(
614 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
615 Vector(list_pos_2 + 2, 20),
616 Color(255,255,255,255), LAYER_GUI - 4);
617 context.draw_filled_rect(
618 Vector(x_pos - list_pos + text_pos, y_pos - 9),
619 Vector(list_pos_2, 18),
620 Color(0,0,0,128), LAYER_GUI - 5);
622 context.draw_text(text_font, pitem.list[pitem.selected],
623 Vector(screen->w/2 + text_pos, y_pos - int(text_font->get_height()/2)),
624 CENTER_ALLIGN, LAYER_GUI);
625 context.draw_text(text_font, pitem.text,
626 Vector(screen->w/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
627 CENTER_ALLIGN, LAYER_GUI);
632 context.draw_text(text_font, pitem.text,
633 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
634 CENTER_ALLIGN, LAYER_GUI);
635 context.draw_surface(back,
636 Vector(x_pos + text_width/2 + 16, y_pos - 8),
643 context.draw_text(text_font, pitem.text,
644 Vector(screen->w/2, y_pos - (text_font->get_height()/2)),
645 CENTER_ALLIGN, LAYER_GUI);
648 context.draw_surface(checkbox_checked,
649 Vector(x_pos + (text_width+16)/2, y_pos - 8),
652 context.draw_surface(checkbox,
653 Vector(x_pos + (text_width+16)/2, y_pos - 8),
658 context.draw_text(text_font, pitem.text,
659 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
660 CENTER_ALLIGN, LAYER_GUI);
664 context.draw_text(text_font, pitem.text,
665 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
666 CENTER_ALLIGN, LAYER_GUI);
671 int Menu::get_width() const
673 /* The width of the menu has to be more than the width of the text
674 with the most characters */
676 for(unsigned int i = 0; i < item.size(); ++i)
678 int w = item[i].text.size() + item[i].input.size() + 1;
682 if( item[i].kind == MN_TOGGLE)
687 return (menu_width * 16 + 24);
690 int Menu::get_height() const
692 return item.size() * 24;
695 /* Draw the current menu. */
697 Menu::draw(DrawingContext& context)
700 int menu_height = get_height();
701 int menu_width = get_width();
703 /* Draw a transparent background */
704 context.draw_filled_rect(
705 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
706 Vector(menu_width,menu_height + 20),
707 Color(150,180,200,125), LAYER_GUI-10);
709 for(unsigned int i = 0; i < item.size(); ++i)
711 draw_item(context, i, menu_width, menu_height);
716 Menu::get_item_by_id(int id)
718 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
725 static MenuItem dummyitem;
729 int Menu::get_active_item_id()
731 return item[active_item].id;
735 Menu::isToggled(int id)
737 return get_item_by_id(id).toggled;
740 /* Check for menu event */
742 Menu::event(SDL_Event& event)
748 SDLKey key = key = event.key.keysym.sym;
751 keymod = SDL_GetModState();
753 /* If the current unicode character is an ASCII character,
755 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
757 ch[0] = event.key.keysym.unicode & 0x7F;
762 /* An International Character. */
765 if(item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_KB)
767 if(key == SDLK_ESCAPE)
772 *item[active_item].int_p = key;
773 menuaction = MENU_ACTION_DOWN;
780 case SDLK_UP: /* Menu Up */
781 menuaction = MENU_ACTION_UP;
783 case SDLK_DOWN: /* Menu Down */
784 menuaction = MENU_ACTION_DOWN;
786 case SDLK_LEFT: /* Menu Up */
787 menuaction = MENU_ACTION_LEFT;
789 case SDLK_RIGHT: /* Menu Down */
790 menuaction = MENU_ACTION_RIGHT;
793 if(item.size() > 0 && item[active_item].kind == MN_TEXTFIELD)
795 menuaction = MENU_ACTION_INPUT;
799 case SDLK_RETURN: /* Menu Hit */
800 menuaction = MENU_ACTION_HIT;
804 menuaction = MENU_ACTION_REMOVE;
811 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
813 menuaction = MENU_ACTION_INPUT;
818 mn_input_char = '\0';
825 case SDL_JOYAXISMOTION:
826 if(event.jaxis.axis == joystick_keymap.y_axis)
828 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
830 menuaction = MENU_ACTION_DOWN;
831 joystick_timer.start(JOYSTICK_MENU_DELAY);
833 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
835 menuaction = MENU_ACTION_UP;
836 joystick_timer.start(JOYSTICK_MENU_DELAY);
839 joystick_timer.stop();
842 case SDL_JOYHATMOTION:
843 if(event.jhat.value & SDL_HAT_UP) {
844 menuaction = MENU_ACTION_UP;
845 } else if(event.jhat.value & SDL_HAT_DOWN) {
846 menuaction = MENU_ACTION_DOWN;
849 case SDL_JOYBUTTONDOWN:
850 if (item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_JS)
852 // FIXME: This next line does nothing useable, right?
853 // *item[active_item].int_p = key;
854 menuaction = MENU_ACTION_DOWN;
856 menuaction = MENU_ACTION_HIT;
859 case SDL_MOUSEBUTTONDOWN:
861 int x = event.motion.x;
862 int y = event.motion.y;
864 if(x > pos_x - get_width()/2 &&
865 x < pos_x + get_width()/2 &&
866 y > pos_y - get_height()/2 &&
867 y < pos_y + get_height()/2)
869 menuaction = MENU_ACTION_HIT;
874 case SDL_MOUSEMOTION:
876 int x = event.motion.x;
877 int y = event.motion.y;
879 if(x > pos_x - get_width()/2 &&
880 x < pos_x + get_width()/2 &&
881 y > pos_y - get_height()/2 &&
882 y < pos_y + get_height()/2)
884 int new_active_item = (y - (pos_y - get_height()/2)) / 24;
886 /* only change the mouse focus to a selectable item */
887 if ((item[new_active_item].kind != MN_HL)
888 && (item[new_active_item].kind != MN_LABEL)
889 && (item[new_active_item].kind != MN_DEACTIVE))
890 active_item = new_active_item;
892 if(MouseCursor::current())
893 MouseCursor::current()->set_state(MC_LINK);
897 if(MouseCursor::current())
898 MouseCursor::current()->set_state(MC_NORMAL);