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"
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);
353 /* Process actions done on the menu */
360 int last_active_item = active_item;
368 active_item = int(item.size())-1;
369 } while ((item[active_item].kind == MN_HL
370 || item[active_item].kind == MN_LABEL
371 || item[active_item].kind == MN_DEACTIVE)
372 && (active_item != last_active_item));
376 case MENU_ACTION_DOWN:
378 if(active_item < int(item.size())-1 )
382 } while ((item[active_item].kind == MN_HL
383 || item[active_item].kind == MN_LABEL
384 || item[active_item].kind == MN_DEACTIVE)
385 && (active_item != last_active_item));
389 case MENU_ACTION_LEFT:
390 if(item[active_item].kind == MN_STRINGSELECT) {
391 if(item[active_item].selected > 0)
392 item[active_item].selected--;
394 item[active_item].selected = item[active_item].list.size()-1;
398 case MENU_ACTION_RIGHT:
399 if(item[active_item].kind == MN_STRINGSELECT) {
400 if(item[active_item].selected+1 < item[active_item].list.size())
401 item[active_item].selected++;
403 item[active_item].selected = 0;
407 case MENU_ACTION_HIT:
409 hit_item = active_item;
410 switch (item[active_item].kind)
413 if (item[active_item].target_menu != NULL)
414 Menu::push_current(item[active_item].target_menu);
420 item[active_item].toggled = !item[active_item].toggled;
424 Menu::set_current(0);
425 item[active_item].toggled = true;
429 menuaction = MENU_ACTION_DOWN;
442 case MENU_ACTION_REMOVE:
443 if(item[active_item].kind == MN_TEXTFIELD
444 || item[active_item].kind == MN_NUMFIELD)
446 if(!item[active_item].input.empty())
448 int i = item[active_item].input.size();
450 while(delete_character > 0) /* remove charactes */
452 item[active_item].input.resize(i-1);
459 case MENU_ACTION_INPUT:
460 if(item[active_item].kind == MN_TEXTFIELD
461 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
463 item[active_item].input.push_back(mn_input_char);
466 case MENU_ACTION_NONE:
472 menuaction = MENU_ACTION_NONE;
474 if (active_item >= int(item.size()))
475 active_item = int(item.size()) - 1;
482 return item[hit_item].id;
488 Menu::draw_item(DrawingContext& context,
489 int index, // Position of the current item in the menu
490 int menu_width, int menu_height)
492 MenuItem& pitem = item[index];
494 int effect_offset = 0;
499 effect_time = effect.get_left() / 4;
501 effect_offset = (index % 2) ? effect_time : -effect_time;
504 Font* text_font = default_font;
506 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
508 int text_width = int(text_font->get_text_width(pitem.text));
509 int input_width = int(text_font->get_text_width(pitem.input) + 10);
511 if(pitem.list.size() > 0) {
512 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
516 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
518 if(index == active_item)
521 text_font = active_font;
528 context.draw_text(deactive_font, pitem.text,
529 Vector(screen->w/2, y_pos - int(deactive_font->get_height()/2)),
530 CENTER_ALLIGN, LAYER_GUI);
537 int x = pos_x - menu_width/2;
538 int y = y_pos - 12 - effect_offset;
539 /* Draw a horizontal line with a little 3d effect */
540 context.draw_filled_rect(Vector(x, y + 6),
541 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
542 context.draw_filled_rect(Vector(x, y + 6),
543 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
548 context.draw_text(label_font, pitem.text,
549 Vector(screen->w/2, y_pos - int(label_font->get_height()/2)),
550 CENTER_ALLIGN, LAYER_GUI);
555 case MN_CONTROLFIELD_KB:
556 case MN_CONTROLFIELD_JS:
558 int width = text_width + input_width + 5;
559 int text_pos = screen->w/2 - width/2;
560 int input_pos = text_pos + text_width + 10;
562 context.draw_filled_rect(
563 Vector(input_pos - 5, y_pos - 10),
564 Vector(input_width + 10, 20),
565 Color(255,255,255,255), LAYER_GUI-5);
566 context.draw_filled_rect(
567 Vector(input_pos - 4, y_pos - 9),
568 Vector(input_width + 8, 18),
569 Color(0,0,0,128), LAYER_GUI-4);
571 if(pitem.kind == MN_CONTROLFIELD_KB)
572 get_controlfield_key_into_input(&pitem);
573 else if (pitem.kind == MN_CONTROLFIELD_JS)
574 get_controlfield_js_into_input(&pitem);
576 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
578 if(active_item == index)
579 context.draw_text(field_font,
580 pitem.get_input_with_symbol(true),
581 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
582 LEFT_ALLIGN, LAYER_GUI);
584 context.draw_text(field_font,
585 pitem.get_input_with_symbol(false),
586 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
587 LEFT_ALLIGN, LAYER_GUI);
590 context.draw_text(field_font, pitem.input,
591 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
592 LEFT_ALLIGN, LAYER_GUI);
594 context.draw_text(text_font, pitem.text,
595 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
596 LEFT_ALLIGN, LAYER_GUI);
599 case MN_STRINGSELECT:
601 int list_pos_2 = list_width + 16;
602 int list_pos = list_width/2;
603 int text_pos = (text_width + 16)/2;
606 context.draw_surface(arrow_left,
607 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
609 context.draw_surface(arrow_right,
610 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
613 /* Draw input background */
614 context.draw_filled_rect(
615 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
616 Vector(list_pos_2 + 2, 20),
617 Color(255,255,255,255), LAYER_GUI - 4);
618 context.draw_filled_rect(
619 Vector(x_pos - list_pos + text_pos, y_pos - 9),
620 Vector(list_pos_2, 18),
621 Color(0,0,0,128), LAYER_GUI - 5);
623 context.draw_text(text_font, pitem.list[pitem.selected],
624 Vector(screen->w/2 + text_pos, y_pos - int(text_font->get_height()/2)),
625 CENTER_ALLIGN, LAYER_GUI);
626 context.draw_text(text_font, pitem.text,
627 Vector(screen->w/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
628 CENTER_ALLIGN, LAYER_GUI);
633 context.draw_text(text_font, pitem.text,
634 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
635 CENTER_ALLIGN, LAYER_GUI);
636 context.draw_surface(back,
637 Vector(x_pos + text_width/2 + 16, y_pos - 8),
644 context.draw_text(text_font, pitem.text,
645 Vector(screen->w/2, y_pos - (text_font->get_height()/2)),
646 CENTER_ALLIGN, LAYER_GUI);
649 context.draw_surface(checkbox_checked,
650 Vector(x_pos + (text_width+16)/2, y_pos - 8),
653 context.draw_surface(checkbox,
654 Vector(x_pos + (text_width+16)/2, y_pos - 8),
659 context.draw_text(text_font, pitem.text,
660 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
661 CENTER_ALLIGN, LAYER_GUI);
665 context.draw_text(text_font, pitem.text,
666 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
667 CENTER_ALLIGN, LAYER_GUI);
672 int Menu::get_width() const
674 /* The width of the menu has to be more than the width of the text
675 with the most characters */
677 for(unsigned int i = 0; i < item.size(); ++i)
679 int w = item[i].text.size() + item[i].input.size() + 1;
683 if( item[i].kind == MN_TOGGLE)
688 return (menu_width * 16 + 24);
691 int Menu::get_height() const
693 return item.size() * 24;
696 /* Draw the current menu. */
698 Menu::draw(DrawingContext& context)
701 int menu_height = get_height();
702 int menu_width = get_width();
704 /* Draw a transparent background */
705 context.draw_filled_rect(
706 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
707 Vector(menu_width,menu_height + 20),
708 Color(150,180,200,125), LAYER_GUI-10);
710 for(unsigned int i = 0; i < item.size(); ++i)
712 draw_item(context, i, menu_width, menu_height);
717 Menu::get_item_by_id(int id)
719 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
726 static MenuItem dummyitem;
730 int Menu::get_active_item_id()
732 return item[active_item].id;
736 Menu::isToggled(int id)
738 return get_item_by_id(id).toggled;
741 /* Check for menu event */
743 Menu::event(SDL_Event& event)
749 SDLKey key = event.key.keysym.sym;
752 keymod = SDL_GetModState();
754 /* If the current unicode character is an ASCII character,
756 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
758 ch[0] = event.key.keysym.unicode & 0x7F;
763 /* An International Character. */
766 if(item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_KB)
768 if(key == SDLK_ESCAPE)
773 *item[active_item].int_p = key;
774 menuaction = MENU_ACTION_DOWN;
781 case SDLK_UP: /* Menu Up */
782 menuaction = MENU_ACTION_UP;
784 case SDLK_DOWN: /* Menu Down */
785 menuaction = MENU_ACTION_DOWN;
787 case SDLK_LEFT: /* Menu Up */
788 menuaction = MENU_ACTION_LEFT;
790 case SDLK_RIGHT: /* Menu Down */
791 menuaction = MENU_ACTION_RIGHT;
794 if(item.size() > 0 && item[active_item].kind == MN_TEXTFIELD)
796 menuaction = MENU_ACTION_INPUT;
800 case SDLK_RETURN: /* Menu Hit */
801 menuaction = MENU_ACTION_HIT;
805 menuaction = MENU_ACTION_REMOVE;
812 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
814 menuaction = MENU_ACTION_INPUT;
819 mn_input_char = '\0';
826 case SDL_JOYAXISMOTION:
827 if(event.jaxis.axis == joystick_keymap.y_axis)
829 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
831 menuaction = MENU_ACTION_DOWN;
832 joystick_timer.start(JOYSTICK_MENU_DELAY);
834 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
836 menuaction = MENU_ACTION_UP;
837 joystick_timer.start(JOYSTICK_MENU_DELAY);
840 joystick_timer.stop();
843 case SDL_JOYHATMOTION:
844 if(event.jhat.value & SDL_HAT_UP) {
845 menuaction = MENU_ACTION_UP;
846 } else if(event.jhat.value & SDL_HAT_DOWN) {
847 menuaction = MENU_ACTION_DOWN;
850 case SDL_JOYBUTTONDOWN:
851 if (item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_JS)
853 // FIXME: This next line does nothing useable, right?
854 // *item[active_item].int_p = key;
855 menuaction = MENU_ACTION_DOWN;
857 menuaction = MENU_ACTION_HIT;
860 case SDL_MOUSEBUTTONDOWN:
862 int x = event.motion.x;
863 int y = event.motion.y;
865 if(x > pos_x - get_width()/2 &&
866 x < pos_x + get_width()/2 &&
867 y > pos_y - get_height()/2 &&
868 y < pos_y + get_height()/2)
870 menuaction = MENU_ACTION_HIT;
875 case SDL_MOUSEMOTION:
877 int x = event.motion.x;
878 int y = event.motion.y;
880 if(x > pos_x - get_width()/2 &&
881 x < pos_x + get_width()/2 &&
882 y > pos_y - get_height()/2 &&
883 y < pos_y + get_height()/2)
885 int new_active_item = (y - (pos_y - get_height()/2)) / 24;
887 /* only change the mouse focus to a selectable item */
888 if ((item[new_active_item].kind != MN_HL)
889 && (item[new_active_item].kind != MN_LABEL)
890 && (item[new_active_item].kind != MN_DEACTIVE))
891 active_item = new_active_item;
893 if(MouseCursor::current())
894 MouseCursor::current()->set_state(MC_LINK);
898 if(MouseCursor::current())
899 MouseCursor::current()->set_state(MC_NORMAL);