2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "gui/menu.hpp"
21 #include "control/joystickkeyboardcontroller.hpp"
22 #include "gui/menu_item.hpp"
23 #include "supertux/main.hpp"
24 #include "supertux/mainloop.hpp"
25 #include "supertux/resources.hpp"
26 #include "supertux/timer.hpp"
27 #include "util/gettext.hpp"
28 #include "video/drawing_context.hpp"
30 static const float MENU_REPEAT_INITIAL = 0.4f;
31 static const float MENU_REPEAT_RATE = 0.1f;
33 extern SDL_Surface* g_screen;
35 std::vector<Menu*> Menu::last_menus;
36 std::list<Menu*> Menu::all_menus;
37 Menu* Menu::current_ = 0;
38 Menu* Menu::previous = 0;
40 /* just displays a Yes/No text that can be used to confirm stuff */
41 bool confirm_dialog(Surface *background, std::string text)
43 //Surface* cap_screen = Surface::CaptureScreen();
44 Menu* dialog = new Menu;
45 dialog->add_inactive(-1, text);
47 dialog->add_entry(true, _("Yes"));
48 dialog->add_entry(false, _("No"));
51 Menu::set_current(dialog);
53 DrawingContext context;
55 // TODO make this a screen and not another mainloop...
59 while (SDL_PollEvent(&event)) {
60 if(event.type == SDL_QUIT)
62 g_main_controller->process_event(event);
66 if(background == NULL)
67 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
70 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
72 dialog->draw(context);
75 switch (dialog->check())
93 mouse_cursor->draw(context);
102 Menu::push_current(Menu* pmenu)
107 last_menus.push_back(current_);
110 current_->effect_start_time = real_time;
111 current_->effect_progress = 0.0f;
119 if (last_menus.size() >= 1) {
120 current_ = last_menus.back();
121 current_->effect_start_time = real_time;
122 current_->effect_progress = 0.0f;
123 last_menus.pop_back();
130 Menu::set_current(Menu* menu)
132 if (current_ && current_->close == true)
138 menu->effect_start_time = real_time;
139 menu->effect_progress = 0.0f;
143 last_menus.clear(); //NULL new menu pointer => close all menus
144 current_->effect_start_time = real_time;
145 current_->effect_progress = 0.0f;
146 current_->close = true;
149 // just to be sure...
150 g_main_controller->reset();
157 current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
159 for(std::list<Menu*>::iterator i = all_menus.begin(); i != all_menus.end(); ++i)
161 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
162 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
168 all_menus.remove(this);
170 for(std::vector<MenuItem*>::iterator i = items.begin();
171 i != items.end(); ++i)
177 if (previous == this)
201 all_menus.push_back(this);
204 menuaction = MENU_ACTION_NONE;
205 delete_character = 0;
206 mn_input_char = '\0';
208 pos_x = SCREEN_WIDTH/2;
209 pos_y = SCREEN_HEIGHT/2;
213 effect_progress = 0.0f;
214 effect_start_time = 0.0f;
216 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
217 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
218 back.reset(new Surface("images/engine/menu/arrow-back.png"));
219 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
220 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
224 Menu::set_pos(float x, float y, float rw, float rh)
226 pos_x = x + get_width() * rw;
227 pos_y = y + get_height() * rh;
230 /* Add an item to a menu */
232 Menu::additem(MenuItem* item)
234 items.push_back(item);
236 /* If a new menu is being built, the active item shouldn't be set to
237 * something that isn't selectable. Set the active_item to the first
238 * selectable item added.
240 if (active_item == -1
241 && item->kind != MN_HL
242 && item->kind != MN_LABEL
243 && item->kind != MN_INACTIVE) {
244 active_item = items.size() - 1;
251 MenuItem* item = new MenuItem(MN_HL);
257 Menu::add_label(const std::string& text)
259 MenuItem* item = new MenuItem(MN_LABEL);
266 Menu::add_controlfield(int id, const std::string& text,
267 const std::string& mapping)
269 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
270 item->change_text(text);
271 item->change_input(mapping);
277 Menu::add_entry(int id, const std::string& text)
279 MenuItem* item = new MenuItem(MN_ACTION, id);
286 Menu::add_inactive(int id, const std::string& text)
288 MenuItem* item = new MenuItem(MN_INACTIVE, id);
295 Menu::add_toggle(int id, const std::string& text, bool toogled)
297 MenuItem* item = new MenuItem(MN_TOGGLE, id);
299 item->toggled = toogled;
305 Menu::add_string_select(int id, const std::string& text)
307 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
314 Menu::add_back(const std::string& text)
316 MenuItem* item = new MenuItem(MN_BACK);
323 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
325 MenuItem* item = new MenuItem(MN_GOTO, id);
327 item->target_menu = submenu;
335 for(std::vector<MenuItem*>::iterator i = items.begin();
336 i != items.end(); ++i) {
343 /* Process actions done on the menu */
347 int menu_height = (int) get_height();
348 if (menu_height > SCREEN_HEIGHT)
350 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
351 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
354 effect_progress = (real_time - effect_start_time) * 6.0f;
356 if(effect_progress >= 1.0f) {
357 effect_progress = 1.0f;
364 else if (effect_progress <= 0.0f) {
365 effect_progress = 0.0f;
368 /** check main input controller... */
369 if(g_main_controller->pressed(Controller::UP)) {
370 menuaction = MENU_ACTION_UP;
371 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
373 if(g_main_controller->hold(Controller::UP) &&
374 menu_repeat_time != 0 && real_time > menu_repeat_time) {
375 menuaction = MENU_ACTION_UP;
376 menu_repeat_time = real_time + MENU_REPEAT_RATE;
379 if(g_main_controller->pressed(Controller::DOWN)) {
380 menuaction = MENU_ACTION_DOWN;
381 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
383 if(g_main_controller->hold(Controller::DOWN) &&
384 menu_repeat_time != 0 && real_time > menu_repeat_time) {
385 menuaction = MENU_ACTION_DOWN;
386 menu_repeat_time = real_time + MENU_REPEAT_RATE;
389 if(g_main_controller->pressed(Controller::LEFT)) {
390 menuaction = MENU_ACTION_LEFT;
391 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
393 if(g_main_controller->hold(Controller::LEFT) &&
394 menu_repeat_time != 0 && real_time > menu_repeat_time) {
395 menuaction = MENU_ACTION_LEFT;
396 menu_repeat_time = real_time + MENU_REPEAT_RATE;
399 if(g_main_controller->pressed(Controller::RIGHT)) {
400 menuaction = MENU_ACTION_RIGHT;
401 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
403 if(g_main_controller->hold(Controller::RIGHT) &&
404 menu_repeat_time != 0 && real_time > menu_repeat_time) {
405 menuaction = MENU_ACTION_RIGHT;
406 menu_repeat_time = real_time + MENU_REPEAT_RATE;
409 if(g_main_controller->pressed(Controller::ACTION)
410 || g_main_controller->pressed(Controller::MENU_SELECT)) {
411 menuaction = MENU_ACTION_HIT;
413 if(g_main_controller->pressed(Controller::PAUSE_MENU)) {
414 menuaction = MENU_ACTION_BACK;
418 if(items.size() == 0)
421 int last_active_item = active_item;
428 active_item = int(items.size())-1;
429 } while ((items[active_item]->kind == MN_HL
430 || items[active_item]->kind == MN_LABEL
431 || items[active_item]->kind == MN_INACTIVE)
432 && (active_item != last_active_item));
436 case MENU_ACTION_DOWN:
438 if(active_item < int(items.size())-1 )
442 } while ((items[active_item]->kind == MN_HL
443 || items[active_item]->kind == MN_LABEL
444 || items[active_item]->kind == MN_INACTIVE)
445 && (active_item != last_active_item));
449 case MENU_ACTION_LEFT:
450 if(items[active_item]->kind == MN_STRINGSELECT) {
451 if(items[active_item]->selected > 0)
452 items[active_item]->selected--;
454 items[active_item]->selected = items[active_item]->list.size()-1;
456 menu_action(items[active_item]);
460 case MENU_ACTION_RIGHT:
461 if(items[active_item]->kind == MN_STRINGSELECT) {
462 if(items[active_item]->selected+1 < items[active_item]->list.size())
463 items[active_item]->selected++;
465 items[active_item]->selected = 0;
467 menu_action(items[active_item]);
471 case MENU_ACTION_HIT: {
472 hit_item = active_item;
473 switch (items[active_item]->kind) {
475 assert(items[active_item]->target_menu != 0);
476 Menu::push_current(items[active_item]->target_menu);
480 items[active_item]->toggled = !items[active_item]->toggled;
481 menu_action(items[active_item]);
484 case MN_CONTROLFIELD:
485 menu_action(items[active_item]);
489 menu_action(items[active_item]);
492 case MN_STRINGSELECT:
493 if(items[active_item]->selected+1 < items[active_item]->list.size())
494 items[active_item]->selected++;
496 items[active_item]->selected = 0;
498 menu_action(items[active_item]);
503 menuaction = MENU_ACTION_DOWN;
516 case MENU_ACTION_REMOVE:
517 if(items[active_item]->kind == MN_TEXTFIELD
518 || items[active_item]->kind == MN_NUMFIELD)
520 if(!items[active_item]->input.empty())
522 int i = items[active_item]->input.size();
524 while(delete_character > 0) /* remove characters */
526 items[active_item]->input.resize(i-1);
533 case MENU_ACTION_INPUT:
534 if(items[active_item]->kind == MN_TEXTFIELD
535 || (items[active_item]->kind == MN_NUMFIELD
536 && mn_input_char >= '0' && mn_input_char <= '9'))
538 items[active_item]->input.push_back(mn_input_char);
542 case MENU_ACTION_BACK:
546 case MENU_ACTION_NONE:
549 menuaction = MENU_ACTION_NONE;
551 assert(active_item < int(items.size()));
557 if (hit_item != -1) {
558 int id = items[hit_item]->id;
559 hit_item = -1; //Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib)
567 Menu::menu_action(MenuItem* )
571 Menu::draw_item(DrawingContext& context, int index)
573 float menu_height = get_height();
574 float menu_width = get_width();
576 MenuItem& pitem = *(items[index]);
578 Color text_color = default_color;
580 float y_pos = pos_y + 24*index - menu_height/2 + 12;
582 int text_width = int(normal_font->get_text_width(pitem.text));
583 int input_width = int(normal_font->get_text_width(pitem.input) + 10);
586 float left = pos_x - menu_width/2 + 16;
587 float right = pos_x + menu_width/2 - 16;
589 if(pitem.list.size() > 0) {
590 list_width = (int) normal_font->get_text_width(pitem.list[pitem.selected]);
594 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
596 if(index == active_item)
599 text_color = active_color;
602 if(active_item == index)
604 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
605 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
606 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
607 Color(1.0f, 1.0f, 1.0f, blink),
610 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
611 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
612 Color(1.0f, 1.0f, 1.0f, 0.5f),
621 context.draw_text(normal_font, pitem.text,
622 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
623 ALIGN_CENTER, LAYER_GUI, inactive_color);
630 float x = pos_x - menu_width/2;
631 float y = y_pos - 12;
632 /* Draw a horizontal line with a little 3d effect */
633 context.draw_filled_rect(Vector(x, y + 6),
634 Vector(menu_width, 4),
635 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
636 context.draw_filled_rect(Vector(x, y + 6),
637 Vector(menu_width, 2),
638 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
643 context.draw_text(big_font, pitem.text,
644 Vector(pos_x, y_pos - int(big_font->get_height()/2)),
645 ALIGN_CENTER, LAYER_GUI, label_color);
650 case MN_CONTROLFIELD:
652 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
654 if(active_item == index)
655 context.draw_text(normal_font,
656 pitem.get_input_with_symbol(true),
657 Vector(right, y_pos - int(normal_font->get_height()/2)),
658 ALIGN_RIGHT, LAYER_GUI, field_color);
660 context.draw_text(normal_font,
661 pitem.get_input_with_symbol(false),
662 Vector(right, y_pos - int(normal_font->get_height()/2)),
663 ALIGN_RIGHT, LAYER_GUI, field_color);
666 context.draw_text(normal_font, pitem.input,
667 Vector(right, y_pos - int(normal_font->get_height()/2)),
668 ALIGN_RIGHT, LAYER_GUI, field_color);
670 context.draw_text(normal_font, pitem.text,
671 Vector(left, y_pos - int(normal_font->get_height()/2)),
672 ALIGN_LEFT, LAYER_GUI, text_color);
675 case MN_STRINGSELECT:
677 float roff = arrow_left->get_width();
679 context.draw_text(normal_font, pitem.text,
680 Vector(left, y_pos - int(normal_font->get_height()/2)),
681 ALIGN_LEFT, LAYER_GUI, text_color);
684 context.draw_surface(arrow_left.get(),
685 Vector(right - list_width - roff - roff, y_pos - 8),
687 context.draw_surface(arrow_right.get(),
688 Vector(right - roff, y_pos - 8),
690 context.draw_text(normal_font, pitem.list[pitem.selected],
691 Vector(right - roff, y_pos - int(normal_font->get_height()/2)),
692 ALIGN_RIGHT, LAYER_GUI, text_color);
697 context.draw_text(normal_font, pitem.text,
698 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
699 ALIGN_CENTER, LAYER_GUI, text_color);
700 context.draw_surface(back.get(),
701 Vector(x_pos + text_width/2 + 16, y_pos - 8),
708 context.draw_text(normal_font, pitem.text,
709 Vector(pos_x - menu_width/2 + 16, y_pos - (normal_font->get_height()/2)),
710 ALIGN_LEFT, LAYER_GUI, text_color);
713 context.draw_surface(checkbox_checked.get(),
714 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
717 context.draw_surface(checkbox.get(),
718 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
723 context.draw_text(normal_font, pitem.text,
724 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
725 ALIGN_CENTER, LAYER_GUI, text_color);
729 context.draw_text(normal_font, pitem.text,
730 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
731 ALIGN_CENTER, LAYER_GUI, text_color);
737 Menu::get_width() const
739 /* The width of the menu has to be more than the width of the text
740 with the most characters */
741 float menu_width = 0;
742 for(unsigned int i = 0; i < items.size(); ++i)
744 Font* font = normal_font;
745 if(items[i]->kind == MN_LABEL)
748 float w = font->get_text_width(items[i]->text) +
749 big_font->get_text_width(items[i]->input) + 16;
750 if(items[i]->kind == MN_TOGGLE)
757 return menu_width + 24;
761 Menu::get_height() const
763 return items.size() * 24;
766 /* Draw the current menu. */
768 Menu::draw(DrawingContext& context)
770 if(MouseCursor::current()) {
771 MouseCursor::current()->draw(context);
774 float menu_width = get_width();
775 float menu_height = get_height();
777 if (effect_progress != 1.0f)
781 menu_width = (current_->get_width() * (1.0f - effect_progress));
782 menu_height = (current_->get_height() * (1.0f - effect_progress));
784 else if (Menu::previous)
786 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
787 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
788 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
792 menu_width *= effect_progress;
793 menu_height *= effect_progress;
797 /* Draw a transparent background */
798 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
799 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
800 Color(0.2f, 0.3f, 0.4f, 0.8f),
804 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
805 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
806 Color(0.6f, 0.7f, 0.8f, 0.5f),
810 if (!items[active_item]->help.empty())
812 int text_width = (int) normal_font->get_text_width(items[active_item]->help);
813 int text_height = (int) normal_font->get_text_height(items[active_item]->help);
815 Rect text_rect(pos_x - text_width/2 - 8,
816 SCREEN_HEIGHT - 48 - text_height/2 - 4,
817 pos_x + text_width/2 + 8,
818 SCREEN_HEIGHT - 48 + text_height/2 + 4);
820 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
821 text_rect.p2 + Vector(4,4)),
822 Color(0.2f, 0.3f, 0.4f, 0.8f),
826 context.draw_filled_rect(text_rect,
827 Color(0.6f, 0.7f, 0.8f, 0.5f),
831 context.draw_text(normal_font, items[active_item]->help,
832 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
833 ALIGN_CENTER, LAYER_GUI);
836 if (effect_progress == 1.0f)
837 for(unsigned int i = 0; i < items.size(); ++i)
839 draw_item(context, i);
844 Menu::get_item_by_id(int id)
846 for(std::vector<MenuItem*>::iterator i = items.begin();
847 i != items.end(); ++i) {
848 MenuItem& item = **i;
854 throw std::runtime_error("MenuItem not found");
858 Menu::get_item_by_id(int id) const
860 for(std::vector<MenuItem*>::const_iterator i = items.begin();
861 i != items.end(); ++i) {
862 const MenuItem& item = **i;
868 throw std::runtime_error("MenuItem not found");
871 int Menu::get_active_item_id()
873 return items[active_item]->id;
877 Menu::is_toggled(int id) const
879 return get_item_by_id(id).toggled;
883 Menu::set_toggled(int id, bool toggled)
885 get_item_by_id(id).toggled = toggled;
889 Menu::get_parent() const
891 if (last_menus.empty())
894 return last_menus.back();
897 /* Check for menu event */
899 Menu::event(const SDL_Event& event)
901 if(effect_progress != 1.0f)
905 case SDL_MOUSEBUTTONDOWN:
907 int x = int(event.motion.x * float(SCREEN_WIDTH)/g_screen->w);
908 int y = int(event.motion.y * float(SCREEN_HEIGHT)/g_screen->h);
910 if(x > pos_x - get_width()/2 &&
911 x < pos_x + get_width()/2 &&
912 y > pos_y - get_height()/2 &&
913 y < pos_y + get_height()/2)
915 menuaction = MENU_ACTION_HIT;
920 case SDL_MOUSEMOTION:
922 float x = event.motion.x * SCREEN_WIDTH/g_screen->w;
923 float y = event.motion.y * SCREEN_HEIGHT/g_screen->h;
925 if(x > pos_x - get_width()/2 &&
926 x < pos_x + get_width()/2 &&
927 y > pos_y - get_height()/2 &&
928 y < pos_y + get_height()/2)
931 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
933 /* only change the mouse focus to a selectable item */
934 if ((items[new_active_item]->kind != MN_HL)
935 && (items[new_active_item]->kind != MN_LABEL)
936 && (items[new_active_item]->kind != MN_INACTIVE))
937 active_item = new_active_item;
939 if(MouseCursor::current())
940 MouseCursor::current()->set_state(MC_LINK);
944 if(MouseCursor::current())
945 MouseCursor::current()->set_state(MC_NORMAL);
956 Menu::set_active_item(int id)
958 for(size_t i = 0; i < items.size(); ++i) {
959 MenuItem* item = items[i];