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 "../audio/sound.h"
38 #include "../special/timer.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;
55 /* just displays a Yes/No text that can be used to confirm stuff */
56 bool SuperTux::confirm_dialog(Surface *background, std::string text)
58 //Surface* cap_screen = Surface::CaptureScreen();
60 Menu* dialog = new Menu;
61 dialog->additem(MN_DEACTIVE, text,0,0);
62 dialog->additem(MN_HL,"",0,0);
63 dialog->additem(MN_ACTION,_("Yes"),0,0,true);
64 dialog->additem(MN_ACTION,_("No"),0,0,false);
65 dialog->additem(MN_HL,"",0,0);
67 Menu::set_current(dialog);
69 DrawingContext context;
75 while (SDL_PollEvent(&event))
80 if(background == NULL)
81 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
83 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
85 dialog->draw(context);
88 switch (dialog->check())
106 mouse_cursor->draw(context);
107 context.do_drawing();
115 Menu::push_current(Menu* pmenu)
118 last_menus.push_back(current_);
121 current_->effect.start(500);
127 if (!last_menus.empty())
129 current_ = last_menus.back();
130 current_->effect.start(500);
132 last_menus.pop_back();
141 Menu::set_current(Menu* menu)
146 menu->effect.start(500);
151 /* Return a pointer to a new menu item */
153 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
155 MenuItem *pnew_item = new MenuItem;
157 pnew_item->kind = kind_;
158 pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
159 strcpy(pnew_item->text, text_);
161 if(kind_ == MN_TOGGLE)
162 pnew_item->toggled = init_toggle_;
164 pnew_item->toggled = false;
166 pnew_item->target_menu = target_menu_;
167 pnew_item->input = (char*) malloc(sizeof(char));
168 pnew_item->input[0] = '\0';
170 if(kind_ == MN_STRINGSELECT)
172 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
173 string_list_init(pnew_item->list);
176 pnew_item->list = NULL;
179 pnew_item->int_p = int_p_;
181 pnew_item->input_flickering = false;
182 pnew_item->input_flickering_timer.init(true);
183 pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
189 MenuItem::change_text(const char *text_)
194 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
200 MenuItem::change_input(const char *text_)
205 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
206 strcpy(input, text_);
210 std::string MenuItem::get_input_with_symbol(bool active_item)
213 input_flickering = true;
216 if(input_flickering_timer.get_left() < 0)
219 input_flickering = false;
221 input_flickering = true;
222 input_flickering_timer.start(FLICK_CURSOR_TIME);
228 sprintf(str,"%s ",input);
230 sprintf(str,"%s_",input);
232 std::string string = str;
237 /* Set ControlField for keyboard key */
238 void Menu::get_controlfield_key_into_input(MenuItem *item)
243 item->change_input(_("Up cursor"));
246 item->change_input(_("Down cursor"));
249 item->change_input(_("Left cursor"));
252 item->change_input(_("Right cursor"));
255 item->change_input(_("Return"));
258 item->change_input(_("Space"));
261 item->change_input(_("Right Shift"));
264 item->change_input(_("Left Shift"));
267 item->change_input(_("Right Control"));
270 item->change_input(_("Left Control"));
273 item->change_input(_("Right Alt"));
276 item->change_input(_("Left Alt"));
281 snprintf(tmp, 64, "%d", *item->int_p);
282 item->change_input(tmp);
288 /* Set ControlField for joystick button */
289 void Menu::get_controlfield_js_into_input(MenuItem *item)
291 std::ostringstream oss;
292 oss << "Button " << *item->int_p;
293 item->change_input(oss.str().c_str());
296 /* Free a menu and all its items */
301 for(unsigned int i = 0; i < item.size(); ++i)
305 string_list_free(item[i].list);
314 menuaction = MENU_ACTION_NONE;
315 delete_character = 0;
316 mn_input_char = '\0';
324 joystick_timer.init(true);
327 void Menu::set_pos(int x, int y, float rw, float rh)
329 pos_x = x + (int)((float)get_width() * rw);
330 pos_y = y + (int)((float)get_height() * rh);
334 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
336 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
339 /* Add an item to a menu */
341 Menu::additem(MenuItem* pmenu_item)
343 item.push_back(*pmenu_item);
353 /* Process actions done on the menu */
366 active_item = int(item.size())-1;
369 case MENU_ACTION_DOWN:
370 if(active_item < int(item.size())-1)
376 case MENU_ACTION_LEFT:
377 if(item[active_item].kind == MN_STRINGSELECT
378 && item[active_item].list->num_items != 0)
380 if(item[active_item].list->active_item > 0)
381 --item[active_item].list->active_item;
383 item[active_item].list->active_item = item[active_item].list->num_items-1;
387 case MENU_ACTION_RIGHT:
388 if(item[active_item].kind == MN_STRINGSELECT
389 && item[active_item].list->num_items != 0)
391 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
392 ++item[active_item].list->active_item;
394 item[active_item].list->active_item = 0;
398 case MENU_ACTION_HIT:
400 hit_item = active_item;
401 switch (item[active_item].kind)
404 if (item[active_item].target_menu != NULL)
405 Menu::push_current(item[active_item].target_menu);
411 item[active_item].toggled = !item[active_item].toggled;
415 Menu::set_current(0);
416 item[active_item].toggled = true;
420 menuaction = MENU_ACTION_DOWN;
433 case MENU_ACTION_REMOVE:
434 if(item[active_item].kind == MN_TEXTFIELD
435 || item[active_item].kind == MN_NUMFIELD)
437 if(item[active_item].input != NULL)
439 int i = strlen(item[active_item].input);
441 while(delete_character > 0) /* remove charactes */
443 item[active_item].input[i-1] = '\0';
450 case MENU_ACTION_INPUT:
451 if(item[active_item].kind == MN_TEXTFIELD
452 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
454 if(item[active_item].input != NULL)
456 int i = strlen(item[active_item].input);
457 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
458 item[active_item].input[i] = mn_input_char;
459 item[active_item].input[i+1] = '\0';
463 item[active_item].input = (char*) malloc(2*sizeof(char));
464 item[active_item].input[0] = mn_input_char;
465 item[active_item].input[1] = '\0';
469 case MENU_ACTION_NONE:
474 MenuItem& new_item = item[active_item];
475 if(new_item.kind == MN_DEACTIVE
476 || new_item.kind == MN_LABEL
477 || new_item.kind == MN_HL)
479 // Skip the horzontal line item
480 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
481 menuaction = MENU_ACTION_DOWN;
487 menuaction = MENU_ACTION_NONE;
489 if (active_item >= int(item.size()))
490 active_item = int(item.size()) - 1;
497 return item[hit_item].id;
503 Menu::draw_item(DrawingContext& context,
504 int index, // Position of the current item in the menu
505 int menu_width, int menu_height)
507 MenuItem& pitem = item[index];
509 int effect_offset = 0;
514 effect_time = effect.get_left() / 4;
516 effect_offset = (index % 2) ? effect_time : -effect_time;
519 Font* text_font = white_text;
521 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
523 int text_width = int(text_font->get_text_width(pitem.text));
524 int input_width = int(text_font->get_text_width(pitem.input) + 10);
526 int(text_font->get_text_width(string_list_active(pitem.list)));
529 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
531 if(index == active_item)
534 text_font = blue_text;
541 context.draw_text_center(gray_text, pitem.text,
542 Vector(0, y_pos - int(blue_text->get_height()/2)),
550 int x = pos_x - menu_width/2;
551 int y = y_pos - 12 - effect_offset;
552 /* Draw a horizontal line with a little 3d effect */
553 context.draw_filled_rect(Vector(x, y + 6),
554 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
555 context.draw_filled_rect(Vector(x, y + 6),
556 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
561 context.draw_text_center(white_big_text,
562 pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
568 case MN_CONTROLFIELD_KB:
569 case MN_CONTROLFIELD_JS:
571 int width = text_width + input_width + 5;
572 int text_pos = screen->w/2 - width/2;
573 int input_pos = text_pos + text_width + 10;
575 context.draw_filled_rect(
576 Vector(input_pos - 5, y_pos - 10),
577 Vector(input_width + 10, 20),
578 Color(255,255,255,255), LAYER_GUI-5);
579 context.draw_filled_rect(
580 Vector(input_pos - 4, y_pos - 9),
581 Vector(input_width + 8, 18),
582 Color(0,0,0,128), LAYER_GUI-4);
584 if(pitem.kind == MN_CONTROLFIELD_KB)
585 get_controlfield_key_into_input(&pitem);
586 else if (pitem.kind == MN_CONTROLFIELD_JS)
587 get_controlfield_js_into_input(&pitem);
589 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
591 if(active_item == index)
592 context.draw_text(gold_text,
593 pitem.get_input_with_symbol(true),
594 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
597 context.draw_text(gold_text,
598 pitem.get_input_with_symbol(false),
599 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
603 context.draw_text(gold_text, pitem.input,
604 Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
607 context.draw_text(text_font, pitem.text,
608 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
612 case MN_STRINGSELECT:
614 int list_pos_2 = list_width + 16;
615 int list_pos = list_width/2;
616 int text_pos = (text_width + 16)/2;
619 context.draw_surface(arrow_left,
620 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
622 context.draw_surface(arrow_right,
623 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
626 /* Draw input background */
627 context.draw_filled_rect(
628 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
629 Vector(list_pos_2 + 2, 20),
630 Color(255,255,255,255), LAYER_GUI - 4);
631 context.draw_filled_rect(
632 Vector(x_pos - list_pos + text_pos, y_pos - 9),
633 Vector(list_pos_2, 18),
634 Color(0,0,0,128), LAYER_GUI - 5);
636 context.draw_text_center(text_font, string_list_active(pitem.list),
637 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
639 context.draw_text_center(text_font, pitem.text,
640 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
646 context.draw_text_center(text_font, pitem.text,
647 Vector(0, y_pos - int(text_font->get_height()/2)),
649 context.draw_surface(back,
650 Vector(x_pos + text_width/2 + 16, y_pos - 8),
657 context.draw_text_center(text_font, pitem.text,
658 Vector(0, y_pos - (text_font->get_height()/2)),
662 context.draw_surface(checkbox_checked,
663 Vector(x_pos + (text_width+16)/2, y_pos - 8),
666 context.draw_surface(checkbox,
667 Vector(x_pos + (text_width+16)/2, y_pos - 8),
672 context.draw_text_center(text_font, pitem.text,
673 Vector(0, y_pos - int(text_font->get_height()/2)),
678 context.draw_text_center(text_font, pitem.text,
679 Vector(0, y_pos - int(text_font->get_height()/2)),
685 int Menu::get_width() const
687 /* The width of the menu has to be more than the width of the text
688 with the most characters */
690 for(unsigned int i = 0; i < item.size(); ++i)
692 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
696 if( item[i].kind == MN_TOGGLE)
701 return (menu_width * 16 + 24);
704 int Menu::get_height() const
706 return item.size() * 24;
709 /* Draw the current menu. */
711 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 mouse_cursor->set_state(MC_LINK);
895 mouse_cursor->set_state(MC_NORMAL);