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"
34 #include "video/screen.h"
35 #include "video/drawing_context.h"
36 #include "app/setup.h"
37 #include "app/gettext.h"
38 #include "math/vector.h"
40 #include "control/joystickkeyboardcontroller.h"
42 static const int MENU_REPEAT_INITIAL = 400;
43 static const int MENU_REPEAT_RATE = 200;
44 static const int FLICK_CURSOR_TIME=500;
46 extern SDL_Surface* screen;
48 using namespace SuperTux;
51 Surface* checkbox_checked;
56 std::vector<Menu*> Menu::last_menus;
57 Menu* Menu::current_ = 0;
58 Font* Menu::default_font;
59 Font* Menu::active_font;
60 Font* Menu::deactive_font;
61 Font* Menu::label_font;
62 Font* Menu::field_font;
64 /* just displays a Yes/No text that can be used to confirm stuff */
65 bool confirm_dialog(Surface *background, std::string text)
67 //Surface* cap_screen = Surface::CaptureScreen();
68 Menu* dialog = new Menu;
69 dialog->add_deactive(-1, text);
71 dialog->add_entry(true, _("Yes"));
72 dialog->add_entry(false, _("No"));
75 Menu::set_current(dialog);
77 DrawingContext context;
83 if(event.type == SDL_QUIT)
84 throw std::runtime_error("received window close event");
86 while (SDL_PollEvent(&event)) {
90 if(background == NULL)
91 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
93 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
95 dialog->draw(context);
98 switch (dialog->check())
102 Menu::set_current(0);
108 Menu::set_current(0);
116 mouse_cursor->draw(context);
117 context.do_drawing();
125 Menu::push_current(Menu* pmenu)
128 last_menus.push_back(current_);
131 current_->effect.start(500);
137 if (last_menus.size() >= 1) {
138 current_ = last_menus.back();
139 current_->effect.start(500);
141 last_menus.pop_back();
146 Menu::set_current(Menu* menu)
151 menu->effect.start(500);
154 // just to be sure...
155 main_controller->reset();
158 MenuItem::MenuItem(MenuItemKind _kind, int _id)
159 : kind(_kind) , id(_id)
164 input_flickering = false;
165 input_flickering_timer.init(true);
166 input_flickering_timer.start(FLICK_CURSOR_TIME);
170 MenuItem::change_text(const std::string& text_)
176 MenuItem::change_input(const std::string& text_)
181 std::string MenuItem::get_input_with_symbol(bool active_item)
184 input_flickering = true;
187 if(input_flickering_timer.get_left() < 0)
190 input_flickering = false;
192 input_flickering = true;
193 input_flickering_timer.start(FLICK_CURSOR_TIME);
199 sprintf(str,"%s ",input.c_str());
201 sprintf(str,"%s_",input.c_str());
203 std::string string = str;
210 for(std::vector<MenuItem*>::iterator i = items.begin();
211 i != items.end(); ++i)
218 menuaction = MENU_ACTION_NONE;
219 delete_character = 0;
220 mn_input_char = '\0';
222 pos_x = SCREEN_WIDTH/2;
223 pos_y = SCREEN_HEIGHT/2;
228 repeat_timer.init(true);
231 void Menu::set_pos(int x, int y, float rw, float rh)
233 pos_x = x + (int)((float)get_width() * rw);
234 pos_y = y + (int)((float)get_height() * rh);
237 /* Add an item to a menu */
239 Menu::additem(MenuItem* item)
241 items.push_back(item);
243 /* If a new menu is being built, the active item shouldn't be set to
244 * something that isnt selectable. Set the active_item to the first
245 * selectable item added
247 if (active_item == -1
248 && item->kind != MN_HL
249 && item->kind != MN_LABEL
250 && item->kind != MN_DEACTIVE) {
251 active_item = items.size() - 1;
258 additem(new MenuItem(MN_HL));
262 Menu::add_label(const std::string& text)
264 MenuItem* item = new MenuItem(MN_LABEL);
270 Menu::add_controlfield(int id, const std::string& text,
271 const std::string& mapping)
273 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
274 item->change_text(text);
275 item->change_input(mapping);
280 Menu::add_entry(int id, const std::string& text)
282 MenuItem* item = new MenuItem(MN_ACTION, id);
288 Menu::add_deactive(int id, const std::string& text)
290 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
296 Menu::add_toggle(int id, const std::string& text, bool toogled)
298 MenuItem* item = new MenuItem(MN_TOGGLE, id);
300 item->toggled = toogled;
305 Menu::add_back(const std::string& text)
307 MenuItem* item = new MenuItem(MN_BACK);
313 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
315 MenuItem* item = new MenuItem(MN_GOTO, id);
317 item->target_menu = submenu;
324 for(std::vector<MenuItem*>::iterator i = items.begin();
325 i != items.end(); ++i) {
332 /* Process actions done on the menu */
336 /** check main input controller... */
337 if(main_controller->pressed(Controller::UP)) {
338 menuaction = MENU_ACTION_UP;
339 repeat_timer.start(MENU_REPEAT_INITIAL);
341 if(main_controller->hold(Controller::UP) && !repeat_timer.check()) {
342 menuaction = MENU_ACTION_UP;
343 repeat_timer.start(MENU_REPEAT_RATE);
345 if(main_controller->pressed(Controller::DOWN)) {
346 menuaction = MENU_ACTION_DOWN;
347 repeat_timer.start(MENU_REPEAT_INITIAL);
349 if(main_controller->hold(Controller::DOWN) && !repeat_timer.check()) {
350 menuaction = MENU_ACTION_DOWN;
351 repeat_timer.start(MENU_REPEAT_RATE);
353 if(main_controller->pressed(Controller::JUMP)
354 || main_controller->pressed(Controller::ACTION)
355 || main_controller->pressed(Controller::MENU_SELECT)) {
356 menuaction = MENU_ACTION_HIT;
358 if(main_controller->pressed(Controller::PAUSE_MENU)) {
359 menuaction = MENU_ACTION_BACK;
363 if(items.size() == 0)
366 int last_active_item = active_item;
373 active_item = int(items.size())-1;
374 } while ((items[active_item]->kind == MN_HL
375 || items[active_item]->kind == MN_LABEL
376 || items[active_item]->kind == MN_DEACTIVE)
377 && (active_item != last_active_item));
381 case MENU_ACTION_DOWN:
383 if(active_item < int(items.size())-1 )
387 } while ((items[active_item]->kind == MN_HL
388 || items[active_item]->kind == MN_LABEL
389 || items[active_item]->kind == MN_DEACTIVE)
390 && (active_item != last_active_item));
394 case MENU_ACTION_LEFT:
395 if(items[active_item]->kind == MN_STRINGSELECT) {
396 if(items[active_item]->selected > 0)
397 items[active_item]->selected--;
399 items[active_item]->selected = items[active_item]->list.size()-1;
403 case MENU_ACTION_RIGHT:
404 if(items[active_item]->kind == MN_STRINGSELECT) {
405 if(items[active_item]->selected+1 < items[active_item]->list.size())
406 items[active_item]->selected++;
408 items[active_item]->selected = 0;
412 case MENU_ACTION_HIT: {
413 hit_item = active_item;
414 switch (items[active_item]->kind) {
416 assert(items[active_item]->target_menu != 0);
417 Menu::push_current(items[active_item]->target_menu);
421 items[active_item]->toggled = !items[active_item]->toggled;
422 menu_action(items[active_item]);
425 case MN_CONTROLFIELD:
426 menu_action(items[active_item]);
430 menu_action(items[active_item]);
435 menuaction = MENU_ACTION_DOWN;
448 case MENU_ACTION_REMOVE:
449 if(items[active_item]->kind == MN_TEXTFIELD
450 || items[active_item]->kind == MN_NUMFIELD)
452 if(!items[active_item]->input.empty())
454 int i = items[active_item]->input.size();
456 while(delete_character > 0) /* remove charactes */
458 items[active_item]->input.resize(i-1);
465 case MENU_ACTION_INPUT:
466 if(items[active_item]->kind == MN_TEXTFIELD
467 || (items[active_item]->kind == MN_NUMFIELD
468 && mn_input_char >= '0' && mn_input_char <= '9'))
470 items[active_item]->input.push_back(mn_input_char);
474 case MENU_ACTION_BACK:
478 case MENU_ACTION_NONE:
481 menuaction = MENU_ACTION_NONE;
483 assert(active_item < int(items.size()));
490 return items[hit_item]->id;
496 Menu::menu_action(MenuItem* )
500 Menu::draw_item(DrawingContext& context, int index)
502 int menu_height = get_height();
503 int menu_width = get_width();
505 MenuItem& pitem = *(items[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.size() > 0) {
525 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
529 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
531 if(index == active_item)
534 text_font = active_font;
541 context.draw_text(deactive_font, pitem.text,
542 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
543 CENTER_ALLIGN, LAYER_GUI);
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(label_font, pitem.text,
562 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
563 CENTER_ALLIGN, LAYER_GUI);
568 case MN_CONTROLFIELD:
570 int width = text_width + input_width + 5;
571 int text_pos = SCREEN_WIDTH/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_TEXTFIELD || pitem.kind == MN_NUMFIELD)
585 if(active_item == index)
586 context.draw_text(field_font,
587 pitem.get_input_with_symbol(true),
588 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
589 LEFT_ALLIGN, LAYER_GUI);
591 context.draw_text(field_font,
592 pitem.get_input_with_symbol(false),
593 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
594 LEFT_ALLIGN, LAYER_GUI);
597 context.draw_text(field_font, pitem.input,
598 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
599 LEFT_ALLIGN, LAYER_GUI);
601 context.draw_text(text_font, pitem.text,
602 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
603 LEFT_ALLIGN, LAYER_GUI);
606 case MN_STRINGSELECT:
608 int list_pos_2 = list_width + 16;
609 int list_pos = list_width/2;
610 int text_pos = (text_width + 16)/2;
613 context.draw_surface(arrow_left,
614 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
616 context.draw_surface(arrow_right,
617 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
620 /* Draw input background */
621 context.draw_filled_rect(
622 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
623 Vector(list_pos_2 + 2, 20),
624 Color(255,255,255,255), LAYER_GUI - 4);
625 context.draw_filled_rect(
626 Vector(x_pos - list_pos + text_pos, y_pos - 9),
627 Vector(list_pos_2, 18),
628 Color(0,0,0,128), LAYER_GUI - 5);
630 context.draw_text(text_font, pitem.list[pitem.selected],
631 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
632 CENTER_ALLIGN, LAYER_GUI);
633 context.draw_text(text_font, pitem.text,
634 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
635 CENTER_ALLIGN, LAYER_GUI);
640 context.draw_text(text_font, pitem.text,
641 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
642 CENTER_ALLIGN, LAYER_GUI);
643 context.draw_surface(back,
644 Vector(x_pos + text_width/2 + 16, y_pos - 8),
651 context.draw_text(text_font, pitem.text,
652 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
653 CENTER_ALLIGN, LAYER_GUI);
656 context.draw_surface(checkbox_checked,
657 Vector(x_pos + (text_width+16)/2, y_pos - 8),
660 context.draw_surface(checkbox,
661 Vector(x_pos + (text_width+16)/2, y_pos - 8),
666 context.draw_text(text_font, pitem.text,
667 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
668 CENTER_ALLIGN, LAYER_GUI);
672 context.draw_text(text_font, pitem.text,
673 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
674 CENTER_ALLIGN, LAYER_GUI);
679 int Menu::get_width() const
681 /* The width of the menu has to be more than the width of the text
682 with the most characters */
684 for(unsigned int i = 0; i < items.size(); ++i)
686 int w = items[i]->text.size() + items[i]->input.size() + 1;
690 if( items[i]->kind == MN_TOGGLE)
695 return (menu_width * 16 + 24);
698 int Menu::get_height() const
700 return items.size() * 24;
703 /* Draw the current menu. */
705 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*items.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 < items.size(); ++i)
718 draw_item(context, i);
723 Menu::get_item_by_id(int id)
725 for(std::vector<MenuItem*>::iterator i = items.begin();
726 i != items.end(); ++i) {
727 MenuItem& item = **i;
733 throw std::runtime_error("MenuItem not found");
737 Menu::get_item_by_id(int id) const
739 for(std::vector<MenuItem*>::const_iterator i = items.begin();
740 i != items.end(); ++i) {
741 const MenuItem& item = **i;
747 throw std::runtime_error("MenuItem not found");
750 int Menu::get_active_item_id()
752 return items[active_item]->id;
756 Menu::is_toggled(int id) const
758 return get_item_by_id(id).toggled;
761 /* Check for menu event */
763 Menu::event(const SDL_Event& event)
769 case SDL_MOUSEBUTTONDOWN:
771 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
772 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
774 if(x > pos_x - get_width()/2 &&
775 x < pos_x + get_width()/2 &&
776 y > pos_y - get_height()/2 &&
777 y < pos_y + get_height()/2)
779 menuaction = MENU_ACTION_HIT;
784 case SDL_MOUSEMOTION:
786 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
787 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
789 if(x > pos_x - get_width()/2 &&
790 x < pos_x + get_width()/2 &&
791 y > pos_y - get_height()/2 &&
792 y < pos_y + get_height()/2)
794 int new_active_item = (y - (pos_y - get_height()/2)) / 24;
796 /* only change the mouse focus to a selectable item */
797 if ((items[new_active_item]->kind != MN_HL)
798 && (items[new_active_item]->kind != MN_LABEL)
799 && (items[new_active_item]->kind != MN_DEACTIVE))
800 active_item = new_active_item;
802 if(MouseCursor::current())
803 MouseCursor::current()->set_state(MC_LINK);
807 if(MouseCursor::current())
808 MouseCursor::current()->set_state(MC_NORMAL);
819 Menu::set_active_item(int id)
821 for(size_t i = 0; i < items.size(); ++i) {
822 MenuItem* item = items[i];