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 /* Return a pointer to a new menu item */
157 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
159 MenuItem *pnew_item = new MenuItem;
161 pnew_item->kind = kind_;
162 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
163 strcpy(pnew_item->text, text_);
165 if(kind_ == MN_TOGGLE)
166 pnew_item->toggled = init_toggle_;
168 pnew_item->toggled = false;
170 pnew_item->target_menu = target_menu_;
171 pnew_item->input = (char*) malloc(sizeof(char));
172 pnew_item->input[0] = '\0';
175 pnew_item->int_p = int_p_;
177 pnew_item->list.second = 0;
179 pnew_item->input_flickering = false;
180 pnew_item->input_flickering_timer.init(true);
181 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
187 MenuItem::change_text(const char *text_)
192 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
198 MenuItem::change_input(const char *text_)
203 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
204 strcpy(input, text_);
208 std::string MenuItem::get_input_with_symbol(bool active_item)
211 input_flickering = true;
214 if(input_flickering_timer.get_left() < 0)
217 input_flickering = false;
219 input_flickering = true;
220 input_flickering_timer.start(FLICK_CURSOR_TIME);
226 sprintf(str,"%s ",input);
228 sprintf(str,"%s_",input);
230 std::string string = str;
235 /* Set ControlField for keyboard key */
236 void Menu::get_controlfield_key_into_input(MenuItem *item)
241 item->change_input(_("Up cursor"));
244 item->change_input(_("Down cursor"));
247 item->change_input(_("Left cursor"));
250 item->change_input(_("Right cursor"));
253 item->change_input(_("Return"));
256 item->change_input(_("Space"));
259 item->change_input(_("Right Shift"));
262 item->change_input(_("Left Shift"));
265 item->change_input(_("Right Control"));
268 item->change_input(_("Left Control"));
271 item->change_input(_("Right Alt"));
274 item->change_input(_("Left Alt"));
279 snprintf(tmp, 64, "%d", *item->int_p);
280 item->change_input(tmp);
286 /* Set ControlField for joystick button */
287 void Menu::get_controlfield_js_into_input(MenuItem *item)
289 std::ostringstream oss;
290 oss << "Button " << *item->int_p;
291 item->change_input(oss.str().c_str());
294 /* Free a menu and all its items */
299 for(unsigned int i = 0; i < item.size(); ++i)
303 item[i].list.first.clear();
312 menuaction = MENU_ACTION_NONE;
313 delete_character = 0;
314 mn_input_char = '\0';
322 joystick_timer.init(true);
325 void Menu::set_pos(int x, int y, float rw, float rh)
327 pos_x = x + (int)((float)get_width() * rw);
328 pos_y = y + (int)((float)get_height() * rh);
332 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
334 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
337 /* Add an item to a menu */
339 Menu::additem(MenuItem* pmenu_item)
341 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 != NULL)
437 int i = strlen(item[active_item].input);
439 while(delete_character > 0) /* remove charactes */
441 item[active_item].input[i-1] = '\0';
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 if(item[active_item].input != NULL)
454 int i = strlen(item[active_item].input);
455 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
456 item[active_item].input[i] = mn_input_char;
457 item[active_item].input[i+1] = '\0';
461 item[active_item].input = (char*) malloc(2*sizeof(char));
462 item[active_item].input[0] = mn_input_char;
463 item[active_item].input[1] = '\0';
467 case MENU_ACTION_NONE:
472 MenuItem& new_item = item[active_item];
473 if(new_item.kind == MN_DEACTIVE
474 || new_item.kind == MN_LABEL
475 || new_item.kind == MN_HL)
477 // Skip the horzontal line item
478 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
479 menuaction = MENU_ACTION_DOWN;
485 menuaction = MENU_ACTION_NONE;
487 if (active_item >= int(item.size()))
488 active_item = int(item.size()) - 1;
495 return item[hit_item].id;
501 Menu::draw_item(DrawingContext& context,
502 int index, // Position of the current item in the menu
503 int menu_width, int menu_height)
505 MenuItem& pitem = item[index];
507 int effect_offset = 0;
512 effect_time = effect.get_left() / 4;
514 effect_offset = (index % 2) ? effect_time : -effect_time;
517 Font* text_font = default_font;
519 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
521 int text_width = int(text_font->get_text_width(pitem.text));
522 int input_width = int(text_font->get_text_width(pitem.input) + 10);
524 if(pitem.list.second != 0)
525 list_width = int(text_font->get_text_width((*pitem.list.second)));
528 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
530 if(index == active_item)
533 text_font = active_font;
540 context.draw_text_center(deactive_font, pitem.text,
541 Vector(0, y_pos - int(deactive_font->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(label_font,
561 pitem.text, Vector(0, y_pos - int(label_font->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(field_font,
592 pitem.get_input_with_symbol(true),
593 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
596 context.draw_text(field_font,
597 pitem.get_input_with_symbol(false),
598 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
602 context.draw_text(field_font, pitem.input,
603 Vector(input_pos, y_pos - int(field_font->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, (*pitem.list.second),
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); //+ ((item[i].list.second != item[i].list.first.end()) ? (strlen((*(item[i].list.second)).c_str())) : 0);
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)
713 int menu_height = get_height();
714 int menu_width = get_width();
716 /* Draw a transparent background */
717 context.draw_filled_rect(
718 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
719 Vector(menu_width,menu_height + 20),
720 Color(150,180,200,125), LAYER_GUI-10);
722 for(unsigned int i = 0; i < item.size(); ++i)
724 draw_item(context, i, menu_width, menu_height);
729 Menu::get_item_by_id(int id)
731 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
738 static MenuItem dummyitem;
742 int Menu::get_active_item_id()
744 return item[active_item].id;
748 Menu::isToggled(int id)
750 return get_item_by_id(id).toggled;
753 /* Check for menu event */
755 Menu::event(SDL_Event& event)
761 SDLKey key = key = event.key.keysym.sym;
764 keymod = SDL_GetModState();
766 /* If the current unicode character is an ASCII character,
768 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
770 ch[0] = event.key.keysym.unicode & 0x7F;
775 /* An International Character. */
778 if(item[active_item].kind == MN_CONTROLFIELD_KB)
780 if(key == SDLK_ESCAPE)
785 *item[active_item].int_p = key;
786 menuaction = MENU_ACTION_DOWN;
793 case SDLK_UP: /* Menu Up */
794 menuaction = MENU_ACTION_UP;
796 case SDLK_DOWN: /* Menu Down */
797 menuaction = MENU_ACTION_DOWN;
799 case SDLK_LEFT: /* Menu Up */
800 menuaction = MENU_ACTION_LEFT;
802 case SDLK_RIGHT: /* Menu Down */
803 menuaction = MENU_ACTION_RIGHT;
806 if(item[active_item].kind == MN_TEXTFIELD)
808 menuaction = MENU_ACTION_INPUT;
812 case SDLK_RETURN: /* Menu Hit */
813 menuaction = MENU_ACTION_HIT;
817 menuaction = MENU_ACTION_REMOVE;
824 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
826 menuaction = MENU_ACTION_INPUT;
831 mn_input_char = '\0';
838 case SDL_JOYAXISMOTION:
839 if(event.jaxis.axis == joystick_keymap.y_axis)
841 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
843 menuaction = MENU_ACTION_DOWN;
844 joystick_timer.start(JOYSTICK_MENU_DELAY);
846 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
848 menuaction = MENU_ACTION_UP;
849 joystick_timer.start(JOYSTICK_MENU_DELAY);
852 joystick_timer.stop();
855 case SDL_JOYBUTTONDOWN:
856 if (item[active_item].kind == MN_CONTROLFIELD_JS)
858 // FIXME: This next line does nothing useable, right?
859 // *item[active_item].int_p = key;
860 menuaction = MENU_ACTION_DOWN;
862 menuaction = MENU_ACTION_HIT;
865 case SDL_MOUSEBUTTONDOWN:
867 int x = event.motion.x;
868 int y = event.motion.y;
870 if(x > pos_x - get_width()/2 &&
871 x < pos_x + get_width()/2 &&
872 y > pos_y - get_height()/2 &&
873 y < pos_y + get_height()/2)
875 menuaction = MENU_ACTION_HIT;
880 case SDL_MOUSEMOTION:
882 int x = event.motion.x;
883 int y = event.motion.y;
885 if(x > pos_x - get_width()/2 &&
886 x < pos_x + get_width()/2 &&
887 y > pos_y - get_height()/2 &&
888 y < pos_y + get_height()/2)
890 active_item = (y - (pos_y - get_height()/2)) / 24;
891 if(MouseCursor::current())
892 MouseCursor::current()->set_state(MC_LINK);
896 if(MouseCursor::current())
897 MouseCursor::current()->set_state(MC_NORMAL);