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;
53 Font* Menu::default_font;
54 Font* Menu::active_font;
55 Font* Menu::deactive_font;
56 Font* Menu::label_font;
57 Font* Menu::field_font;
59 /* just displays a Yes/No text that can be used to confirm stuff */
60 bool SuperTux::confirm_dialog(Surface *background, std::string text)
62 //Surface* cap_screen = Surface::CaptureScreen();
64 Menu* dialog = new Menu;
65 dialog->additem(MN_DEACTIVE, text,0,0);
66 dialog->additem(MN_HL,"",0,0);
67 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
68 dialog->additem(MN_ACTION,_("No"),0,0,false);
69 dialog->additem(MN_HL,"",0,0);
71 Menu::set_current(dialog);
73 DrawingContext context;
79 while (SDL_PollEvent(&event))
84 if(background == NULL)
85 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
87 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
89 dialog->draw(context);
92 switch (dialog->check())
102 Menu::set_current(0);
110 mouse_cursor->draw(context);
111 context.do_drawing();
119 Menu::push_current(Menu* pmenu)
122 last_menus.push_back(current_);
125 current_->effect.start(500);
131 if (!last_menus.empty())
133 current_ = last_menus.back();
134 current_->effect.start(500);
136 last_menus.pop_back();
145 Menu::set_current(Menu* menu)
150 menu->effect.start(500);
155 MenuItem::MenuItem(MenuItemKind _kind, int _id) : kind(_kind) , id(_id)
160 MenuItem::MenuItem(MenuItemKind _kind, int _id, const std::string& _text) : kind(_kind) , id(_id) , text(_text)
165 /* Return a pointer to a new menu item */
167 MenuItem::create(MenuItemKind kind_, const std::string& text_, int init_toggle_, Menu* target_menu_, int id_, int* int_p_)
169 MenuItem *pnew_item = new MenuItem(kind_,id_);
171 pnew_item->text = text_;
173 if(kind_ == MN_TOGGLE)
174 pnew_item->toggled = init_toggle_;
176 pnew_item->toggled = false;
178 pnew_item->target_menu = target_menu_;
180 pnew_item->int_p = int_p_;
182 pnew_item->list.second = 0;
184 pnew_item->input_flickering = false;
185 pnew_item->input_flickering_timer.init(true);
186 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
192 MenuItem::change_text(const std::string& text_)
198 MenuItem::change_input(const std::string& text_)
203 std::string MenuItem::get_input_with_symbol(bool active_item)
206 input_flickering = true;
209 if(input_flickering_timer.get_left() < 0)
212 input_flickering = false;
214 input_flickering = true;
215 input_flickering_timer.start(FLICK_CURSOR_TIME);
221 sprintf(str,"%s ",input.c_str());
223 sprintf(str,"%s_",input.c_str());
225 std::string string = str;
230 /* Set ControlField for keyboard key */
231 void Menu::get_controlfield_key_into_input(MenuItem *item)
236 item->change_input(_("Up cursor"));
239 item->change_input(_("Down cursor"));
242 item->change_input(_("Left cursor"));
245 item->change_input(_("Right cursor"));
248 item->change_input(_("Return"));
251 item->change_input(_("Space"));
254 item->change_input(_("Right Shift"));
257 item->change_input(_("Left Shift"));
260 item->change_input(_("Right Control"));
263 item->change_input(_("Left Control"));
266 item->change_input(_("Right Alt"));
269 item->change_input(_("Left Alt"));
274 snprintf(tmp, 64, "%d", *item->int_p);
275 item->change_input(tmp);
281 /* Set ControlField for joystick button */
282 void Menu::get_controlfield_js_into_input(MenuItem *item)
284 std::ostringstream oss;
285 oss << "Button " << *item->int_p;
286 item->change_input(oss.str().c_str());
289 /* Free a menu and all its items */
294 for(unsigned int i = 0; i < item.size(); ++i)
296 item[i].list.first.clear();
305 menuaction = MENU_ACTION_NONE;
306 delete_character = 0;
307 mn_input_char = '\0';
315 joystick_timer.init(true);
318 void Menu::set_pos(int x, int y, float rw, float rh)
320 pos_x = x + (int)((float)get_width() * rw);
321 pos_y = y + (int)((float)get_height() * rh);
325 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
327 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
330 /* Add an item to a menu */
332 Menu::additem(MenuItem* pmenu_item)
334 item.push_back(*pmenu_item);
338 /* Add an item to a menu */
340 Menu::additem(const MenuItem& pmenu_item)
342 item.push_back(pmenu_item);
351 /* Process actions done on the menu */
364 active_item = int(item.size())-1;
367 case MENU_ACTION_DOWN:
368 if(active_item < int(item.size())-1)
374 case MENU_ACTION_LEFT:
375 if(item[active_item].kind == MN_STRINGSELECT
376 && item[active_item].list.first.size() != 0)
378 if(item[active_item].list.second != item[active_item].list.first.begin())
379 --item[active_item].list.second;
381 item[active_item].list.second = item[active_item].list.first.end();
385 case MENU_ACTION_RIGHT:
386 if(item[active_item].kind == MN_STRINGSELECT
387 && item[active_item].list.first.size() != 0)
389 if(item[active_item].list.second != item[active_item].list.first.end())
390 ++item[active_item].list.second;
392 item[active_item].list.second = item[active_item].list.first.begin();
396 case MENU_ACTION_HIT:
398 hit_item = active_item;
399 switch (item[active_item].kind)
402 if (item[active_item].target_menu != NULL)
403 Menu::push_current(item[active_item].target_menu);
409 item[active_item].toggled = !item[active_item].toggled;
413 Menu::set_current(0);
414 item[active_item].toggled = true;
418 menuaction = MENU_ACTION_DOWN;
431 case MENU_ACTION_REMOVE:
432 if(item[active_item].kind == MN_TEXTFIELD
433 || item[active_item].kind == MN_NUMFIELD)
435 if(!item[active_item].input.empty())
437 int i = item[active_item].input.size();
439 while(delete_character > 0) /* remove charactes */
441 item[active_item].input.resize(i-1);
448 case MENU_ACTION_INPUT:
449 if(item[active_item].kind == MN_TEXTFIELD
450 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
452 item[active_item].input.push_back(mn_input_char);
455 case MENU_ACTION_NONE:
460 if(active_item > 0 && active_item < (int)item.size())
462 // FIXME: wtf?! having a hack to avoid horizontal lines...
463 // Elegant solution would be to check for horizontal lines, right
464 // when it was asked to move menu up and down
465 if(item[active_item].kind == MN_DEACTIVE ||
466 item[active_item].kind == MN_LABEL ||
467 item[active_item].kind == MN_HL)
469 // Skip the horzontal line item
470 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
471 menuaction = MENU_ACTION_DOWN;
478 menuaction = MENU_ACTION_NONE;
480 if (active_item >= int(item.size()))
481 active_item = int(item.size()) - 1;
488 return item[hit_item].id;
494 Menu::draw_item(DrawingContext& context,
495 int index, // Position of the current item in the menu
496 int menu_width, int menu_height)
498 MenuItem& pitem = item[index];
500 int effect_offset = 0;
505 effect_time = effect.get_left() / 4;
507 effect_offset = (index % 2) ? effect_time : -effect_time;
510 Font* text_font = default_font;
512 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
514 int text_width = int(text_font->get_text_width(pitem.text));
515 int input_width = int(text_font->get_text_width(pitem.input) + 10);
517 std::set<std::string>::iterator tmp = 0;
518 if(pitem.list.second != tmp)
519 list_width = int(text_font->get_text_width((*pitem.list.second)));
522 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
524 if(index == active_item)
527 text_font = active_font;
534 context.draw_text(deactive_font, pitem.text,
535 Vector(screen->w/2, y_pos - int(deactive_font->get_height()/2)),
536 CENTER_ALLIGN, LAYER_GUI);
543 int x = pos_x - menu_width/2;
544 int y = y_pos - 12 - effect_offset;
545 /* Draw a horizontal line with a little 3d effect */
546 context.draw_filled_rect(Vector(x, y + 6),
547 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
548 context.draw_filled_rect(Vector(x, y + 6),
549 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
554 context.draw_text(label_font, pitem.text,
555 Vector(screen->w/2, y_pos - int(label_font->get_height()/2)),
556 CENTER_ALLIGN, LAYER_GUI);
561 case MN_CONTROLFIELD_KB:
562 case MN_CONTROLFIELD_JS:
564 int width = text_width + input_width + 5;
565 int text_pos = screen->w/2 - width/2;
566 int input_pos = text_pos + text_width + 10;
568 context.draw_filled_rect(
569 Vector(input_pos - 5, y_pos - 10),
570 Vector(input_width + 10, 20),
571 Color(255,255,255,255), LAYER_GUI-5);
572 context.draw_filled_rect(
573 Vector(input_pos - 4, y_pos - 9),
574 Vector(input_width + 8, 18),
575 Color(0,0,0,128), LAYER_GUI-4);
577 if(pitem.kind == MN_CONTROLFIELD_KB)
578 get_controlfield_key_into_input(&pitem);
579 else if (pitem.kind == MN_CONTROLFIELD_JS)
580 get_controlfield_js_into_input(&pitem);
582 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
584 if(active_item == index)
585 context.draw_text(field_font,
586 pitem.get_input_with_symbol(true),
587 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
588 LEFT_ALLIGN, LAYER_GUI);
590 context.draw_text(field_font,
591 pitem.get_input_with_symbol(false),
592 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
593 LEFT_ALLIGN, LAYER_GUI);
596 context.draw_text(field_font, pitem.input,
597 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
598 LEFT_ALLIGN, LAYER_GUI);
600 context.draw_text(text_font, pitem.text,
601 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
602 LEFT_ALLIGN, LAYER_GUI);
605 case MN_STRINGSELECT:
607 int list_pos_2 = list_width + 16;
608 int list_pos = list_width/2;
609 int text_pos = (text_width + 16)/2;
612 context.draw_surface(arrow_left,
613 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
615 context.draw_surface(arrow_right,
616 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
619 /* Draw input background */
620 context.draw_filled_rect(
621 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
622 Vector(list_pos_2 + 2, 20),
623 Color(255,255,255,255), LAYER_GUI - 4);
624 context.draw_filled_rect(
625 Vector(x_pos - list_pos + text_pos, y_pos - 9),
626 Vector(list_pos_2, 18),
627 Color(0,0,0,128), LAYER_GUI - 5);
629 context.draw_text(text_font, (*pitem.list.second),
630 Vector(screen->w/2 + text_pos, y_pos - int(text_font->get_height()/2)),
631 CENTER_ALLIGN, LAYER_GUI);
632 context.draw_text(text_font, pitem.text,
633 Vector(screen->w/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
634 CENTER_ALLIGN, LAYER_GUI);
639 context.draw_text(text_font, pitem.text,
640 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
641 CENTER_ALLIGN, LAYER_GUI);
642 context.draw_surface(back,
643 Vector(x_pos + text_width/2 + 16, y_pos - 8),
650 context.draw_text(text_font, pitem.text,
651 Vector(screen->w/2, y_pos - (text_font->get_height()/2)),
652 CENTER_ALLIGN, LAYER_GUI);
655 context.draw_surface(checkbox_checked,
656 Vector(x_pos + (text_width+16)/2, y_pos - 8),
659 context.draw_surface(checkbox,
660 Vector(x_pos + (text_width+16)/2, y_pos - 8),
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);
671 context.draw_text(text_font, pitem.text,
672 Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
673 CENTER_ALLIGN, LAYER_GUI);
678 int Menu::get_width() const
680 /* The width of the menu has to be more than the width of the text
681 with the most characters */
683 for(unsigned int i = 0; i < item.size(); ++i)
685 int w = item[i].text.size() + item[i].input.size() + 1; //+ ((item[i].list.second != item[i].list.first.end()) ? (strlen((*(item[i].list.second)).c_str())) : 0);
689 if( item[i].kind == MN_TOGGLE)
694 return (menu_width * 16 + 24);
697 int Menu::get_height() const
699 return item.size() * 24;
702 /* Draw the current menu. */
704 Menu::draw(DrawingContext& context)
707 int menu_height = get_height();
708 int menu_width = get_width();
710 /* Draw a transparent background */
711 context.draw_filled_rect(
712 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
713 Vector(menu_width,menu_height + 20),
714 Color(150,180,200,125), LAYER_GUI-10);
716 for(unsigned int i = 0; i < item.size(); ++i)
718 draw_item(context, i, menu_width, menu_height);
723 Menu::get_item_by_id(int id)
725 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
732 static MenuItem dummyitem;
736 int Menu::get_active_item_id()
738 return item[active_item].id;
742 Menu::isToggled(int id)
744 return get_item_by_id(id).toggled;
747 /* Check for menu event */
749 Menu::event(SDL_Event& event)
755 SDLKey key = key = event.key.keysym.sym;
758 keymod = SDL_GetModState();
760 /* If the current unicode character is an ASCII character,
762 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
764 ch[0] = event.key.keysym.unicode & 0x7F;
769 /* An International Character. */
772 if(item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_KB)
774 if(key == SDLK_ESCAPE)
779 *item[active_item].int_p = key;
780 menuaction = MENU_ACTION_DOWN;
787 case SDLK_UP: /* Menu Up */
788 menuaction = MENU_ACTION_UP;
790 case SDLK_DOWN: /* Menu Down */
791 menuaction = MENU_ACTION_DOWN;
793 case SDLK_LEFT: /* Menu Up */
794 menuaction = MENU_ACTION_LEFT;
796 case SDLK_RIGHT: /* Menu Down */
797 menuaction = MENU_ACTION_RIGHT;
800 if(item.size() > 0 && item[active_item].kind == MN_TEXTFIELD)
802 menuaction = MENU_ACTION_INPUT;
806 case SDLK_RETURN: /* Menu Hit */
807 menuaction = MENU_ACTION_HIT;
811 menuaction = MENU_ACTION_REMOVE;
818 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
820 menuaction = MENU_ACTION_INPUT;
825 mn_input_char = '\0';
832 case SDL_JOYAXISMOTION:
833 if(event.jaxis.axis == joystick_keymap.y_axis)
835 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
837 menuaction = MENU_ACTION_DOWN;
838 joystick_timer.start(JOYSTICK_MENU_DELAY);
840 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
842 menuaction = MENU_ACTION_UP;
843 joystick_timer.start(JOYSTICK_MENU_DELAY);
846 joystick_timer.stop();
849 case SDL_JOYHATMOTION:
850 if(event.jhat.value & SDL_HAT_UP) {
851 menuaction = MENU_ACTION_UP;
852 } else if(event.jhat.value & SDL_HAT_DOWN) {
853 menuaction = MENU_ACTION_DOWN;
856 case SDL_JOYBUTTONDOWN:
857 if (item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_JS)
859 // FIXME: This next line does nothing useable, right?
860 // *item[active_item].int_p = key;
861 menuaction = MENU_ACTION_DOWN;
863 menuaction = MENU_ACTION_HIT;
866 case SDL_MOUSEBUTTONDOWN:
868 int x = event.motion.x;
869 int y = event.motion.y;
871 if(x > pos_x - get_width()/2 &&
872 x < pos_x + get_width()/2 &&
873 y > pos_y - get_height()/2 &&
874 y < pos_y + get_height()/2)
876 menuaction = MENU_ACTION_HIT;
881 case SDL_MOUSEMOTION:
883 int x = event.motion.x;
884 int y = event.motion.y;
886 if(x > pos_x - get_width()/2 &&
887 x < pos_x + get_width()/2 &&
888 y > pos_y - get_height()/2 &&
889 y < pos_y + get_height()/2)
891 active_item = (y - (pos_y - get_height()/2)) / 24;
892 if(MouseCursor::current())
893 MouseCursor::current()->set_state(MC_LINK);
897 if(MouseCursor::current())
898 MouseCursor::current()->set_state(MC_NORMAL);