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';
174 if(kind_ == MN_STRINGSELECT)
176 pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
177 string_list_init(pnew_item->list);
180 pnew_item->list = NULL;
183 pnew_item->int_p = int_p_;
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 char *text_)
198 text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
204 MenuItem::change_input(const char *text_)
209 input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
210 strcpy(input, text_);
214 std::string MenuItem::get_input_with_symbol(bool active_item)
217 input_flickering = true;
220 if(input_flickering_timer.get_left() < 0)
223 input_flickering = false;
225 input_flickering = true;
226 input_flickering_timer.start(FLICK_CURSOR_TIME);
232 sprintf(str,"%s ",input);
234 sprintf(str,"%s_",input);
236 std::string string = str;
241 /* Set ControlField for keyboard key */
242 void Menu::get_controlfield_key_into_input(MenuItem *item)
247 item->change_input(_("Up cursor"));
250 item->change_input(_("Down cursor"));
253 item->change_input(_("Left cursor"));
256 item->change_input(_("Right cursor"));
259 item->change_input(_("Return"));
262 item->change_input(_("Space"));
265 item->change_input(_("Right Shift"));
268 item->change_input(_("Left Shift"));
271 item->change_input(_("Right Control"));
274 item->change_input(_("Left Control"));
277 item->change_input(_("Right Alt"));
280 item->change_input(_("Left Alt"));
285 snprintf(tmp, 64, "%d", *item->int_p);
286 item->change_input(tmp);
292 /* Set ControlField for joystick button */
293 void Menu::get_controlfield_js_into_input(MenuItem *item)
295 std::ostringstream oss;
296 oss << "Button " << *item->int_p;
297 item->change_input(oss.str().c_str());
300 /* Free a menu and all its items */
305 for(unsigned int i = 0; i < item.size(); ++i)
309 string_list_free(item[i].list);
318 menuaction = MENU_ACTION_NONE;
319 delete_character = 0;
320 mn_input_char = '\0';
328 joystick_timer.init(true);
331 void Menu::set_pos(int x, int y, float rw, float rh)
333 pos_x = x + (int)((float)get_width() * rw);
334 pos_y = y + (int)((float)get_height() * rh);
338 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
340 additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
343 /* Add an item to a menu */
345 Menu::additem(MenuItem* pmenu_item)
347 item.push_back(*pmenu_item);
357 /* Process actions done on the menu */
370 active_item = int(item.size())-1;
373 case MENU_ACTION_DOWN:
374 if(active_item < int(item.size())-1)
380 case MENU_ACTION_LEFT:
381 if(item[active_item].kind == MN_STRINGSELECT
382 && item[active_item].list->num_items != 0)
384 if(item[active_item].list->active_item > 0)
385 --item[active_item].list->active_item;
387 item[active_item].list->active_item = item[active_item].list->num_items-1;
391 case MENU_ACTION_RIGHT:
392 if(item[active_item].kind == MN_STRINGSELECT
393 && item[active_item].list->num_items != 0)
395 if(item[active_item].list->active_item < item[active_item].list->num_items-1)
396 ++item[active_item].list->active_item;
398 item[active_item].list->active_item = 0;
402 case MENU_ACTION_HIT:
404 hit_item = active_item;
405 switch (item[active_item].kind)
408 if (item[active_item].target_menu != NULL)
409 Menu::push_current(item[active_item].target_menu);
415 item[active_item].toggled = !item[active_item].toggled;
419 Menu::set_current(0);
420 item[active_item].toggled = true;
424 menuaction = MENU_ACTION_DOWN;
437 case MENU_ACTION_REMOVE:
438 if(item[active_item].kind == MN_TEXTFIELD
439 || item[active_item].kind == MN_NUMFIELD)
441 if(item[active_item].input != NULL)
443 int i = strlen(item[active_item].input);
445 while(delete_character > 0) /* remove charactes */
447 item[active_item].input[i-1] = '\0';
454 case MENU_ACTION_INPUT:
455 if(item[active_item].kind == MN_TEXTFIELD
456 || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
458 if(item[active_item].input != NULL)
460 int i = strlen(item[active_item].input);
461 item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
462 item[active_item].input[i] = mn_input_char;
463 item[active_item].input[i+1] = '\0';
467 item[active_item].input = (char*) malloc(2*sizeof(char));
468 item[active_item].input[0] = mn_input_char;
469 item[active_item].input[1] = '\0';
473 case MENU_ACTION_NONE:
478 MenuItem& new_item = item[active_item];
479 if(new_item.kind == MN_DEACTIVE
480 || new_item.kind == MN_LABEL
481 || new_item.kind == MN_HL)
483 // Skip the horzontal line item
484 if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
485 menuaction = MENU_ACTION_DOWN;
491 menuaction = MENU_ACTION_NONE;
493 if (active_item >= int(item.size()))
494 active_item = int(item.size()) - 1;
501 return item[hit_item].id;
507 Menu::draw_item(DrawingContext& context,
508 int index, // Position of the current item in the menu
509 int menu_width, int menu_height)
511 MenuItem& pitem = item[index];
513 int effect_offset = 0;
518 effect_time = effect.get_left() / 4;
520 effect_offset = (index % 2) ? effect_time : -effect_time;
523 Font* text_font = default_font;
525 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
527 int text_width = int(text_font->get_text_width(pitem.text));
528 int input_width = int(text_font->get_text_width(pitem.input) + 10);
530 int(text_font->get_text_width(string_list_active(pitem.list)));
533 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
535 if(index == active_item)
538 text_font = active_font;
545 context.draw_text_center(deactive_font, pitem.text,
546 Vector(0, y_pos - int(deactive_font->get_height()/2)),
554 int x = pos_x - menu_width/2;
555 int y = y_pos - 12 - effect_offset;
556 /* Draw a horizontal line with a little 3d effect */
557 context.draw_filled_rect(Vector(x, y + 6),
558 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
559 context.draw_filled_rect(Vector(x, y + 6),
560 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
565 context.draw_text_center(label_font,
566 pitem.text, Vector(0, y_pos - int(label_font->get_height()/2)),
572 case MN_CONTROLFIELD_KB:
573 case MN_CONTROLFIELD_JS:
575 int width = text_width + input_width + 5;
576 int text_pos = screen->w/2 - width/2;
577 int input_pos = text_pos + text_width + 10;
579 context.draw_filled_rect(
580 Vector(input_pos - 5, y_pos - 10),
581 Vector(input_width + 10, 20),
582 Color(255,255,255,255), LAYER_GUI-5);
583 context.draw_filled_rect(
584 Vector(input_pos - 4, y_pos - 9),
585 Vector(input_width + 8, 18),
586 Color(0,0,0,128), LAYER_GUI-4);
588 if(pitem.kind == MN_CONTROLFIELD_KB)
589 get_controlfield_key_into_input(&pitem);
590 else if (pitem.kind == MN_CONTROLFIELD_JS)
591 get_controlfield_js_into_input(&pitem);
593 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
595 if(active_item == index)
596 context.draw_text(field_font,
597 pitem.get_input_with_symbol(true),
598 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
601 context.draw_text(field_font,
602 pitem.get_input_with_symbol(false),
603 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
607 context.draw_text(field_font, pitem.input,
608 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
611 context.draw_text(text_font, pitem.text,
612 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
616 case MN_STRINGSELECT:
618 int list_pos_2 = list_width + 16;
619 int list_pos = list_width/2;
620 int text_pos = (text_width + 16)/2;
623 context.draw_surface(arrow_left,
624 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
626 context.draw_surface(arrow_right,
627 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
630 /* Draw input background */
631 context.draw_filled_rect(
632 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
633 Vector(list_pos_2 + 2, 20),
634 Color(255,255,255,255), LAYER_GUI - 4);
635 context.draw_filled_rect(
636 Vector(x_pos - list_pos + text_pos, y_pos - 9),
637 Vector(list_pos_2, 18),
638 Color(0,0,0,128), LAYER_GUI - 5);
640 context.draw_text_center(text_font, string_list_active(pitem.list),
641 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
643 context.draw_text_center(text_font, pitem.text,
644 Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
650 context.draw_text_center(text_font, pitem.text,
651 Vector(0, y_pos - int(text_font->get_height()/2)),
653 context.draw_surface(back,
654 Vector(x_pos + text_width/2 + 16, y_pos - 8),
661 context.draw_text_center(text_font, pitem.text,
662 Vector(0, y_pos - (text_font->get_height()/2)),
666 context.draw_surface(checkbox_checked,
667 Vector(x_pos + (text_width+16)/2, y_pos - 8),
670 context.draw_surface(checkbox,
671 Vector(x_pos + (text_width+16)/2, y_pos - 8),
676 context.draw_text_center(text_font, pitem.text,
677 Vector(0, y_pos - int(text_font->get_height()/2)),
682 context.draw_text_center(text_font, pitem.text,
683 Vector(0, y_pos - int(text_font->get_height()/2)),
689 int Menu::get_width() const
691 /* The width of the menu has to be more than the width of the text
692 with the most characters */
694 for(unsigned int i = 0; i < item.size(); ++i)
696 int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
700 if( item[i].kind == MN_TOGGLE)
705 return (menu_width * 16 + 24);
708 int Menu::get_height() const
710 return item.size() * 24;
713 /* Draw the current menu. */
715 Menu::draw(DrawingContext& context)
717 int menu_height = get_height();
718 int menu_width = get_width();
720 /* Draw a transparent background */
721 context.draw_filled_rect(
722 Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
723 Vector(menu_width,menu_height + 20),
724 Color(150,180,200,125), LAYER_GUI-10);
726 for(unsigned int i = 0; i < item.size(); ++i)
728 draw_item(context, i, menu_width, menu_height);
733 Menu::get_item_by_id(int id)
735 for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
742 static MenuItem dummyitem;
746 int Menu::get_active_item_id()
748 return item[active_item].id;
752 Menu::isToggled(int id)
754 return get_item_by_id(id).toggled;
757 /* Check for menu event */
759 Menu::event(SDL_Event& event)
765 SDLKey key = key = event.key.keysym.sym;
768 keymod = SDL_GetModState();
770 /* If the current unicode character is an ASCII character,
772 if ( (event.key.keysym.unicode & 0xFF80) == 0 )
774 ch[0] = event.key.keysym.unicode & 0x7F;
779 /* An International Character. */
782 if(item[active_item].kind == MN_CONTROLFIELD_KB)
784 if(key == SDLK_ESCAPE)
789 *item[active_item].int_p = key;
790 menuaction = MENU_ACTION_DOWN;
797 case SDLK_UP: /* Menu Up */
798 menuaction = MENU_ACTION_UP;
800 case SDLK_DOWN: /* Menu Down */
801 menuaction = MENU_ACTION_DOWN;
803 case SDLK_LEFT: /* Menu Up */
804 menuaction = MENU_ACTION_LEFT;
806 case SDLK_RIGHT: /* Menu Down */
807 menuaction = MENU_ACTION_RIGHT;
810 if(item[active_item].kind == MN_TEXTFIELD)
812 menuaction = MENU_ACTION_INPUT;
816 case SDLK_RETURN: /* Menu Hit */
817 menuaction = MENU_ACTION_HIT;
821 menuaction = MENU_ACTION_REMOVE;
828 if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
830 menuaction = MENU_ACTION_INPUT;
835 mn_input_char = '\0';
842 case SDL_JOYAXISMOTION:
843 if(event.jaxis.axis == joystick_keymap.y_axis)
845 if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
847 menuaction = MENU_ACTION_DOWN;
848 joystick_timer.start(JOYSTICK_MENU_DELAY);
850 else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
852 menuaction = MENU_ACTION_UP;
853 joystick_timer.start(JOYSTICK_MENU_DELAY);
856 joystick_timer.stop();
859 case SDL_JOYBUTTONDOWN:
860 if (item[active_item].kind == MN_CONTROLFIELD_JS)
862 // FIXME: This next line does nothing useable, right?
863 // *item[active_item].int_p = key;
864 menuaction = MENU_ACTION_DOWN;
866 menuaction = MENU_ACTION_HIT;
869 case SDL_MOUSEBUTTONDOWN:
871 int x = event.motion.x;
872 int y = event.motion.y;
874 if(x > pos_x - get_width()/2 &&
875 x < pos_x + get_width()/2 &&
876 y > pos_y - get_height()/2 &&
877 y < pos_y + get_height()/2)
879 menuaction = MENU_ACTION_HIT;
884 case SDL_MOUSEMOTION:
886 int x = event.motion.x;
887 int y = event.motion.y;
889 if(x > pos_x - get_width()/2 &&
890 x < pos_x + get_width()/2 &&
891 y > pos_y - get_height()/2 &&
892 y < pos_y + get_height()/2)
894 active_item = (y - (pos_y - get_height()/2)) / 24;
895 mouse_cursor->set_state(MC_LINK);
899 mouse_cursor->set_state(MC_NORMAL);