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 "gui/mousecursor.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/mainloop.hpp"
26 #include "supertux/resources.hpp"
27 #include "supertux/timer.hpp"
28 #include "util/gettext.hpp"
29 #include "video/drawing_context.hpp"
30 #include "video/font.hpp"
32 static const float MENU_REPEAT_INITIAL = 0.4f;
33 static const float MENU_REPEAT_RATE = 0.1f;
35 extern SDL_Surface* g_screen;
37 std::vector<Menu*> Menu::last_menus;
38 std::list<Menu*> Menu::all_menus;
39 Menu* Menu::current_ = 0;
40 Menu* Menu::previous = 0;
42 /* just displays a Yes/No text that can be used to confirm stuff */
43 bool confirm_dialog(Surface *background, std::string text)
45 //Surface* cap_screen = Surface::CaptureScreen();
46 Menu* dialog = new Menu;
47 dialog->add_inactive(-1, text);
49 dialog->add_entry(true, _("Yes"));
50 dialog->add_entry(false, _("No"));
53 Menu::set_current(dialog);
55 DrawingContext context;
57 // TODO make this a screen and not another mainloop...
61 while (SDL_PollEvent(&event)) {
62 if(event.type == SDL_QUIT)
64 g_main_controller->process_event(event);
68 if(background == NULL)
69 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
72 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
74 dialog->draw(context);
77 switch (dialog->check())
95 mouse_cursor->draw(context);
104 Menu::push_current(Menu* pmenu)
109 last_menus.push_back(current_);
112 current_->effect_start_time = real_time;
113 current_->effect_progress = 0.0f;
121 if (last_menus.size() >= 1) {
122 current_ = last_menus.back();
123 current_->effect_start_time = real_time;
124 current_->effect_progress = 0.0f;
125 last_menus.pop_back();
132 Menu::set_current(Menu* menu)
134 if (current_ && current_->close == true)
140 menu->effect_start_time = real_time;
141 menu->effect_progress = 0.0f;
145 last_menus.clear(); //NULL new menu pointer => close all menus
146 current_->effect_start_time = real_time;
147 current_->effect_progress = 0.0f;
148 current_->close = true;
151 // just to be sure...
152 g_main_controller->reset();
159 current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
161 for(std::list<Menu*>::iterator i = all_menus.begin(); i != all_menus.end(); ++i)
163 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
164 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
170 all_menus.remove(this);
172 for(std::vector<MenuItem*>::iterator i = items.begin();
173 i != items.end(); ++i)
179 if (previous == this)
203 all_menus.push_back(this);
206 menuaction = MENU_ACTION_NONE;
207 delete_character = 0;
208 mn_input_char = '\0';
210 pos_x = SCREEN_WIDTH/2;
211 pos_y = SCREEN_HEIGHT/2;
215 effect_progress = 0.0f;
216 effect_start_time = 0.0f;
218 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
219 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
220 back.reset(new Surface("images/engine/menu/arrow-back.png"));
221 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
222 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
226 Menu::set_pos(float x, float y, float rw, float rh)
228 pos_x = x + get_width() * rw;
229 pos_y = y + get_height() * rh;
232 /* Add an item to a menu */
234 Menu::additem(MenuItem* item)
236 items.push_back(item);
238 /* If a new menu is being built, the active item shouldn't be set to
239 * something that isn't selectable. Set the active_item to the first
240 * selectable item added.
242 if (active_item == -1
243 && item->kind != MN_HL
244 && item->kind != MN_LABEL
245 && item->kind != MN_INACTIVE) {
246 active_item = items.size() - 1;
253 MenuItem* item = new MenuItem(MN_HL);
259 Menu::add_label(const std::string& text)
261 MenuItem* item = new MenuItem(MN_LABEL);
268 Menu::add_controlfield(int id, const std::string& text,
269 const std::string& mapping)
271 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
272 item->change_text(text);
273 item->change_input(mapping);
279 Menu::add_entry(int id, const std::string& text)
281 MenuItem* item = new MenuItem(MN_ACTION, id);
288 Menu::add_inactive(int id, const std::string& text)
290 MenuItem* item = new MenuItem(MN_INACTIVE, id);
297 Menu::add_toggle(int id, const std::string& text, bool toogled)
299 MenuItem* item = new MenuItem(MN_TOGGLE, id);
301 item->toggled = toogled;
307 Menu::add_string_select(int id, const std::string& text)
309 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
316 Menu::add_back(const std::string& text)
318 MenuItem* item = new MenuItem(MN_BACK);
325 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
327 MenuItem* item = new MenuItem(MN_GOTO, id);
329 item->target_menu = submenu;
337 for(std::vector<MenuItem*>::iterator i = items.begin();
338 i != items.end(); ++i) {
345 /* Process actions done on the menu */
349 int menu_height = (int) get_height();
350 if (menu_height > SCREEN_HEIGHT)
352 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
353 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
356 effect_progress = (real_time - effect_start_time) * 6.0f;
358 if(effect_progress >= 1.0f) {
359 effect_progress = 1.0f;
366 else if (effect_progress <= 0.0f) {
367 effect_progress = 0.0f;
370 /** check main input controller... */
371 if(g_main_controller->pressed(Controller::UP)) {
372 menuaction = MENU_ACTION_UP;
373 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
375 if(g_main_controller->hold(Controller::UP) &&
376 menu_repeat_time != 0 && real_time > menu_repeat_time) {
377 menuaction = MENU_ACTION_UP;
378 menu_repeat_time = real_time + MENU_REPEAT_RATE;
381 if(g_main_controller->pressed(Controller::DOWN)) {
382 menuaction = MENU_ACTION_DOWN;
383 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
385 if(g_main_controller->hold(Controller::DOWN) &&
386 menu_repeat_time != 0 && real_time > menu_repeat_time) {
387 menuaction = MENU_ACTION_DOWN;
388 menu_repeat_time = real_time + MENU_REPEAT_RATE;
391 if(g_main_controller->pressed(Controller::LEFT)) {
392 menuaction = MENU_ACTION_LEFT;
393 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
395 if(g_main_controller->hold(Controller::LEFT) &&
396 menu_repeat_time != 0 && real_time > menu_repeat_time) {
397 menuaction = MENU_ACTION_LEFT;
398 menu_repeat_time = real_time + MENU_REPEAT_RATE;
401 if(g_main_controller->pressed(Controller::RIGHT)) {
402 menuaction = MENU_ACTION_RIGHT;
403 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
405 if(g_main_controller->hold(Controller::RIGHT) &&
406 menu_repeat_time != 0 && real_time > menu_repeat_time) {
407 menuaction = MENU_ACTION_RIGHT;
408 menu_repeat_time = real_time + MENU_REPEAT_RATE;
411 if(g_main_controller->pressed(Controller::ACTION)
412 || g_main_controller->pressed(Controller::MENU_SELECT)) {
413 menuaction = MENU_ACTION_HIT;
415 if(g_main_controller->pressed(Controller::PAUSE_MENU)) {
416 menuaction = MENU_ACTION_BACK;
420 if(items.size() == 0)
423 int last_active_item = active_item;
430 active_item = int(items.size())-1;
431 } while ((items[active_item]->kind == MN_HL
432 || items[active_item]->kind == MN_LABEL
433 || items[active_item]->kind == MN_INACTIVE)
434 && (active_item != last_active_item));
438 case MENU_ACTION_DOWN:
440 if(active_item < int(items.size())-1 )
444 } while ((items[active_item]->kind == MN_HL
445 || items[active_item]->kind == MN_LABEL
446 || items[active_item]->kind == MN_INACTIVE)
447 && (active_item != last_active_item));
451 case MENU_ACTION_LEFT:
452 if(items[active_item]->kind == MN_STRINGSELECT) {
453 if(items[active_item]->selected > 0)
454 items[active_item]->selected--;
456 items[active_item]->selected = items[active_item]->list.size()-1;
458 menu_action(items[active_item]);
462 case MENU_ACTION_RIGHT:
463 if(items[active_item]->kind == MN_STRINGSELECT) {
464 if(items[active_item]->selected+1 < items[active_item]->list.size())
465 items[active_item]->selected++;
467 items[active_item]->selected = 0;
469 menu_action(items[active_item]);
473 case MENU_ACTION_HIT: {
474 hit_item = active_item;
475 switch (items[active_item]->kind) {
477 assert(items[active_item]->target_menu != 0);
478 Menu::push_current(items[active_item]->target_menu);
482 items[active_item]->toggled = !items[active_item]->toggled;
483 menu_action(items[active_item]);
486 case MN_CONTROLFIELD:
487 menu_action(items[active_item]);
491 menu_action(items[active_item]);
494 case MN_STRINGSELECT:
495 if(items[active_item]->selected+1 < items[active_item]->list.size())
496 items[active_item]->selected++;
498 items[active_item]->selected = 0;
500 menu_action(items[active_item]);
505 menuaction = MENU_ACTION_DOWN;
518 case MENU_ACTION_REMOVE:
519 if(items[active_item]->kind == MN_TEXTFIELD
520 || items[active_item]->kind == MN_NUMFIELD)
522 if(!items[active_item]->input.empty())
524 int i = items[active_item]->input.size();
526 while(delete_character > 0) /* remove characters */
528 items[active_item]->input.resize(i-1);
535 case MENU_ACTION_INPUT:
536 if(items[active_item]->kind == MN_TEXTFIELD
537 || (items[active_item]->kind == MN_NUMFIELD
538 && mn_input_char >= '0' && mn_input_char <= '9'))
540 items[active_item]->input.push_back(mn_input_char);
544 case MENU_ACTION_BACK:
548 case MENU_ACTION_NONE:
551 menuaction = MENU_ACTION_NONE;
553 assert(active_item < int(items.size()));
559 if (hit_item != -1) {
560 int id = items[hit_item]->id;
561 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)
569 Menu::menu_action(MenuItem* )
573 Menu::draw_item(DrawingContext& context, int index)
575 float menu_height = get_height();
576 float menu_width = get_width();
578 MenuItem& pitem = *(items[index]);
580 Color text_color = default_color;
582 float y_pos = pos_y + 24*index - menu_height/2 + 12;
584 int text_width = int(normal_font->get_text_width(pitem.text));
585 int input_width = int(normal_font->get_text_width(pitem.input) + 10);
588 float left = pos_x - menu_width/2 + 16;
589 float right = pos_x + menu_width/2 - 16;
591 if(pitem.list.size() > 0) {
592 list_width = (int) normal_font->get_text_width(pitem.list[pitem.selected]);
596 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
598 if(index == active_item)
601 text_color = active_color;
604 if(active_item == index)
606 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
607 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
608 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
609 Color(1.0f, 1.0f, 1.0f, blink),
612 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
613 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
614 Color(1.0f, 1.0f, 1.0f, 0.5f),
623 context.draw_text(normal_font, pitem.text,
624 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
625 ALIGN_CENTER, LAYER_GUI, inactive_color);
632 float x = pos_x - menu_width/2;
633 float y = y_pos - 12;
634 /* Draw a horizontal line with a little 3d effect */
635 context.draw_filled_rect(Vector(x, y + 6),
636 Vector(menu_width, 4),
637 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
638 context.draw_filled_rect(Vector(x, y + 6),
639 Vector(menu_width, 2),
640 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
645 context.draw_text(big_font, pitem.text,
646 Vector(pos_x, y_pos - int(big_font->get_height()/2)),
647 ALIGN_CENTER, LAYER_GUI, label_color);
652 case MN_CONTROLFIELD:
654 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
656 if(active_item == index)
657 context.draw_text(normal_font,
658 pitem.get_input_with_symbol(true),
659 Vector(right, y_pos - int(normal_font->get_height()/2)),
660 ALIGN_RIGHT, LAYER_GUI, field_color);
662 context.draw_text(normal_font,
663 pitem.get_input_with_symbol(false),
664 Vector(right, y_pos - int(normal_font->get_height()/2)),
665 ALIGN_RIGHT, LAYER_GUI, field_color);
668 context.draw_text(normal_font, pitem.input,
669 Vector(right, y_pos - int(normal_font->get_height()/2)),
670 ALIGN_RIGHT, LAYER_GUI, field_color);
672 context.draw_text(normal_font, pitem.text,
673 Vector(left, y_pos - int(normal_font->get_height()/2)),
674 ALIGN_LEFT, LAYER_GUI, text_color);
677 case MN_STRINGSELECT:
679 float roff = arrow_left->get_width();
681 context.draw_text(normal_font, pitem.text,
682 Vector(left, y_pos - int(normal_font->get_height()/2)),
683 ALIGN_LEFT, LAYER_GUI, text_color);
686 context.draw_surface(arrow_left.get(),
687 Vector(right - list_width - roff - roff, y_pos - 8),
689 context.draw_surface(arrow_right.get(),
690 Vector(right - roff, y_pos - 8),
692 context.draw_text(normal_font, pitem.list[pitem.selected],
693 Vector(right - roff, y_pos - int(normal_font->get_height()/2)),
694 ALIGN_RIGHT, LAYER_GUI, text_color);
699 context.draw_text(normal_font, pitem.text,
700 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
701 ALIGN_CENTER, LAYER_GUI, text_color);
702 context.draw_surface(back.get(),
703 Vector(x_pos + text_width/2 + 16, y_pos - 8),
710 context.draw_text(normal_font, pitem.text,
711 Vector(pos_x - menu_width/2 + 16, y_pos - (normal_font->get_height()/2)),
712 ALIGN_LEFT, LAYER_GUI, text_color);
715 context.draw_surface(checkbox_checked.get(),
716 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
719 context.draw_surface(checkbox.get(),
720 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
725 context.draw_text(normal_font, pitem.text,
726 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
727 ALIGN_CENTER, LAYER_GUI, text_color);
731 context.draw_text(normal_font, pitem.text,
732 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
733 ALIGN_CENTER, LAYER_GUI, text_color);
739 Menu::get_width() const
741 /* The width of the menu has to be more than the width of the text
742 with the most characters */
743 float menu_width = 0;
744 for(unsigned int i = 0; i < items.size(); ++i)
746 Font* font = normal_font;
747 if(items[i]->kind == MN_LABEL)
750 float w = font->get_text_width(items[i]->text) +
751 big_font->get_text_width(items[i]->input) + 16;
752 if(items[i]->kind == MN_TOGGLE)
759 return menu_width + 24;
763 Menu::get_height() const
765 return items.size() * 24;
768 /* Draw the current menu. */
770 Menu::draw(DrawingContext& context)
772 if(MouseCursor::current()) {
773 MouseCursor::current()->draw(context);
776 float menu_width = get_width();
777 float menu_height = get_height();
779 if (effect_progress != 1.0f)
783 menu_width = (current_->get_width() * (1.0f - effect_progress));
784 menu_height = (current_->get_height() * (1.0f - effect_progress));
786 else if (Menu::previous)
788 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
789 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
790 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
794 menu_width *= effect_progress;
795 menu_height *= effect_progress;
799 /* Draw a transparent background */
800 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
801 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
802 Color(0.2f, 0.3f, 0.4f, 0.8f),
806 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
807 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
808 Color(0.6f, 0.7f, 0.8f, 0.5f),
812 if (!items[active_item]->help.empty())
814 int text_width = (int) normal_font->get_text_width(items[active_item]->help);
815 int text_height = (int) normal_font->get_text_height(items[active_item]->help);
817 Rect text_rect(pos_x - text_width/2 - 8,
818 SCREEN_HEIGHT - 48 - text_height/2 - 4,
819 pos_x + text_width/2 + 8,
820 SCREEN_HEIGHT - 48 + text_height/2 + 4);
822 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
823 text_rect.p2 + Vector(4,4)),
824 Color(0.2f, 0.3f, 0.4f, 0.8f),
828 context.draw_filled_rect(text_rect,
829 Color(0.6f, 0.7f, 0.8f, 0.5f),
833 context.draw_text(normal_font, items[active_item]->help,
834 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
835 ALIGN_CENTER, LAYER_GUI);
838 if (effect_progress == 1.0f)
839 for(unsigned int i = 0; i < items.size(); ++i)
841 draw_item(context, i);
846 Menu::get_item_by_id(int id)
848 for(std::vector<MenuItem*>::iterator i = items.begin();
849 i != items.end(); ++i) {
850 MenuItem& item = **i;
856 throw std::runtime_error("MenuItem not found");
860 Menu::get_item_by_id(int id) const
862 for(std::vector<MenuItem*>::const_iterator i = items.begin();
863 i != items.end(); ++i) {
864 const MenuItem& item = **i;
870 throw std::runtime_error("MenuItem not found");
873 int Menu::get_active_item_id()
875 return items[active_item]->id;
879 Menu::is_toggled(int id) const
881 return get_item_by_id(id).toggled;
885 Menu::set_toggled(int id, bool toggled)
887 get_item_by_id(id).toggled = toggled;
891 Menu::get_parent() const
893 if (last_menus.empty())
896 return last_menus.back();
899 /* Check for menu event */
901 Menu::event(const SDL_Event& event)
903 if(effect_progress != 1.0f)
907 case SDL_MOUSEBUTTONDOWN:
909 int x = int(event.motion.x * float(SCREEN_WIDTH)/g_screen->w);
910 int y = int(event.motion.y * float(SCREEN_HEIGHT)/g_screen->h);
912 if(x > pos_x - get_width()/2 &&
913 x < pos_x + get_width()/2 &&
914 y > pos_y - get_height()/2 &&
915 y < pos_y + get_height()/2)
917 menuaction = MENU_ACTION_HIT;
922 case SDL_MOUSEMOTION:
924 float x = event.motion.x * SCREEN_WIDTH/g_screen->w;
925 float y = event.motion.y * SCREEN_HEIGHT/g_screen->h;
927 if(x > pos_x - get_width()/2 &&
928 x < pos_x + get_width()/2 &&
929 y > pos_y - get_height()/2 &&
930 y < pos_y + get_height()/2)
933 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
935 /* only change the mouse focus to a selectable item */
936 if ((items[new_active_item]->kind != MN_HL)
937 && (items[new_active_item]->kind != MN_LABEL)
938 && (items[new_active_item]->kind != MN_INACTIVE))
939 active_item = new_active_item;
941 if(MouseCursor::current())
942 MouseCursor::current()->set_state(MC_LINK);
946 if(MouseCursor::current())
947 MouseCursor::current()->set_state(MC_NORMAL);
958 Menu::set_active_item(int id)
960 for(size_t i = 0; i < items.size(); ++i) {
961 MenuItem* item = items[i];