4 // Copyright (C) 2006 Matthias Braun <matze@braunis.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.
22 #include <sys/types.h>
35 #include "mainloop.hpp"
36 #include "video/drawing_context.hpp"
37 #include "gettext.hpp"
38 #include "math/vector.hpp"
40 #include "resources.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
44 static const float MENU_REPEAT_INITIAL = 0.4f;
45 static const float MENU_REPEAT_RATE = 0.1f;
46 static const float FLICK_CURSOR_TIME = 0.5f;
48 extern SDL_Surface* screen;
50 std::vector<Menu*> Menu::last_menus;
51 std::list<Menu*> Menu::all_menus;
52 Menu* Menu::current_ = 0;
53 Menu* Menu::previous = 0;
55 /* just displays a Yes/No text that can be used to confirm stuff */
56 bool confirm_dialog(Surface *background, std::string text)
58 //Surface* cap_screen = Surface::CaptureScreen();
59 Menu* dialog = new Menu;
60 dialog->add_inactive(-1, text);
62 dialog->add_entry(true, _("Yes"));
63 dialog->add_entry(false, _("No"));
66 Menu::set_current(dialog);
68 DrawingContext context;
70 // TODO make this a screen and not another mainloop...
74 while (SDL_PollEvent(&event)) {
75 if(event.type == SDL_QUIT)
77 main_controller->process_event(event);
81 if(background == NULL)
82 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
85 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
87 dialog->draw(context);
90 switch (dialog->check())
100 Menu::set_current(0);
108 mouse_cursor->draw(context);
109 context.do_drawing();
117 Menu::push_current(Menu* pmenu)
122 last_menus.push_back(current_);
125 current_->effect_start_time = real_time;
126 current_->effect_progress = 0.0f;
134 if (last_menus.size() >= 1) {
135 current_ = last_menus.back();
136 current_->effect_start_time = real_time;
137 current_->effect_progress = 0.0f;
138 last_menus.pop_back();
145 Menu::set_current(Menu* menu)
147 if (current_ && current_->close == true)
153 menu->effect_start_time = real_time;
154 menu->effect_progress = 0.0f;
158 last_menus.clear(); //NULL new menu pointer => close all menus
159 current_->effect_start_time = real_time;
160 current_->effect_progress = 0.0f;
161 current_->close = true;
164 // just to be sure...
165 main_controller->reset();
172 current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
174 for(std::list<Menu*>::iterator i = all_menus.begin(); i != all_menus.end(); ++i)
176 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
177 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
181 MenuItem::MenuItem(MenuItemKind _kind, int _id)
182 : kind(_kind) , id(_id)
190 MenuItem::change_text(const std::string& text_)
196 MenuItem::change_input(const std::string& text_)
202 MenuItem::set_help(const std::string& help_text)
204 std::string overflow;
205 help = normal_font->wrap_to_width(help_text, 600, &overflow);
206 while (!overflow.empty())
209 help += normal_font->wrap_to_width(overflow, 600, &overflow);
213 std::string MenuItem::get_input_with_symbol(bool active_item)
216 input_flickering = true;
218 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
223 snprintf(str, sizeof(str), "%s ",input.c_str());
225 snprintf(str, sizeof(str), "%s_",input.c_str());
227 std::string string = str;
234 all_menus.remove(this);
236 for(std::vector<MenuItem*>::iterator i = items.begin();
237 i != items.end(); ++i)
243 if (previous == this)
250 all_menus.push_back(this);
253 menuaction = MENU_ACTION_NONE;
254 delete_character = 0;
255 mn_input_char = '\0';
257 pos_x = SCREEN_WIDTH/2;
258 pos_y = SCREEN_HEIGHT/2;
262 effect_progress = 0.0f;
263 effect_start_time = 0.0f;
265 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
266 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
267 back.reset(new Surface("images/engine/menu/arrow-back.png"));
268 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
269 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
273 Menu::set_pos(float x, float y, float rw, float rh)
275 pos_x = x + get_width() * rw;
276 pos_y = y + get_height() * rh;
279 /* Add an item to a menu */
281 Menu::additem(MenuItem* item)
283 items.push_back(item);
285 /* If a new menu is being built, the active item shouldn't be set to
286 * something that isn't selectable. Set the active_item to the first
287 * selectable item added.
289 if (active_item == -1
290 && item->kind != MN_HL
291 && item->kind != MN_LABEL
292 && item->kind != MN_INACTIVE) {
293 active_item = items.size() - 1;
300 MenuItem* item = new MenuItem(MN_HL);
306 Menu::add_label(const std::string& text)
308 MenuItem* item = new MenuItem(MN_LABEL);
315 Menu::add_controlfield(int id, const std::string& text,
316 const std::string& mapping)
318 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
319 item->change_text(text);
320 item->change_input(mapping);
326 Menu::add_entry(int id, const std::string& text)
328 MenuItem* item = new MenuItem(MN_ACTION, id);
335 Menu::add_inactive(int id, const std::string& text)
337 MenuItem* item = new MenuItem(MN_INACTIVE, id);
344 Menu::add_toggle(int id, const std::string& text, bool toogled)
346 MenuItem* item = new MenuItem(MN_TOGGLE, id);
348 item->toggled = toogled;
354 Menu::add_string_select(int id, const std::string& text)
356 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
363 Menu::add_back(const std::string& text)
365 MenuItem* item = new MenuItem(MN_BACK);
372 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
374 MenuItem* item = new MenuItem(MN_GOTO, id);
376 item->target_menu = submenu;
384 for(std::vector<MenuItem*>::iterator i = items.begin();
385 i != items.end(); ++i) {
392 /* Process actions done on the menu */
396 int menu_height = (int) get_height();
397 if (menu_height > SCREEN_HEIGHT)
399 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
400 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
403 effect_progress = (real_time - effect_start_time) * 6.0f;
405 if(effect_progress >= 1.0f) {
406 effect_progress = 1.0f;
413 else if (effect_progress <= 0.0f) {
414 effect_progress = 0.0f;
417 /** check main input controller... */
418 if(main_controller->pressed(Controller::UP)) {
419 menuaction = MENU_ACTION_UP;
420 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
422 if(main_controller->hold(Controller::UP) &&
423 menu_repeat_time != 0 && real_time > menu_repeat_time) {
424 menuaction = MENU_ACTION_UP;
425 menu_repeat_time = real_time + MENU_REPEAT_RATE;
428 if(main_controller->pressed(Controller::DOWN)) {
429 menuaction = MENU_ACTION_DOWN;
430 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
432 if(main_controller->hold(Controller::DOWN) &&
433 menu_repeat_time != 0 && real_time > menu_repeat_time) {
434 menuaction = MENU_ACTION_DOWN;
435 menu_repeat_time = real_time + MENU_REPEAT_RATE;
438 if(main_controller->pressed(Controller::LEFT)) {
439 menuaction = MENU_ACTION_LEFT;
440 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
442 if(main_controller->hold(Controller::LEFT) &&
443 menu_repeat_time != 0 && real_time > menu_repeat_time) {
444 menuaction = MENU_ACTION_LEFT;
445 menu_repeat_time = real_time + MENU_REPEAT_RATE;
448 if(main_controller->pressed(Controller::RIGHT)) {
449 menuaction = MENU_ACTION_RIGHT;
450 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
452 if(main_controller->hold(Controller::RIGHT) &&
453 menu_repeat_time != 0 && real_time > menu_repeat_time) {
454 menuaction = MENU_ACTION_RIGHT;
455 menu_repeat_time = real_time + MENU_REPEAT_RATE;
458 if(main_controller->pressed(Controller::ACTION)
459 || main_controller->pressed(Controller::MENU_SELECT)) {
460 menuaction = MENU_ACTION_HIT;
462 if(main_controller->pressed(Controller::PAUSE_MENU)) {
463 menuaction = MENU_ACTION_BACK;
467 if(items.size() == 0)
470 int last_active_item = active_item;
477 active_item = int(items.size())-1;
478 } while ((items[active_item]->kind == MN_HL
479 || items[active_item]->kind == MN_LABEL
480 || items[active_item]->kind == MN_INACTIVE)
481 && (active_item != last_active_item));
485 case MENU_ACTION_DOWN:
487 if(active_item < int(items.size())-1 )
491 } while ((items[active_item]->kind == MN_HL
492 || items[active_item]->kind == MN_LABEL
493 || items[active_item]->kind == MN_INACTIVE)
494 && (active_item != last_active_item));
498 case MENU_ACTION_LEFT:
499 if(items[active_item]->kind == MN_STRINGSELECT) {
500 if(items[active_item]->selected > 0)
501 items[active_item]->selected--;
503 items[active_item]->selected = items[active_item]->list.size()-1;
505 menu_action(items[active_item]);
509 case MENU_ACTION_RIGHT:
510 if(items[active_item]->kind == MN_STRINGSELECT) {
511 if(items[active_item]->selected+1 < items[active_item]->list.size())
512 items[active_item]->selected++;
514 items[active_item]->selected = 0;
516 menu_action(items[active_item]);
520 case MENU_ACTION_HIT: {
521 hit_item = active_item;
522 switch (items[active_item]->kind) {
524 assert(items[active_item]->target_menu != 0);
525 Menu::push_current(items[active_item]->target_menu);
529 items[active_item]->toggled = !items[active_item]->toggled;
530 menu_action(items[active_item]);
533 case MN_CONTROLFIELD:
534 menu_action(items[active_item]);
538 menu_action(items[active_item]);
541 case MN_STRINGSELECT:
542 if(items[active_item]->selected+1 < items[active_item]->list.size())
543 items[active_item]->selected++;
545 items[active_item]->selected = 0;
547 menu_action(items[active_item]);
552 menuaction = MENU_ACTION_DOWN;
565 case MENU_ACTION_REMOVE:
566 if(items[active_item]->kind == MN_TEXTFIELD
567 || items[active_item]->kind == MN_NUMFIELD)
569 if(!items[active_item]->input.empty())
571 int i = items[active_item]->input.size();
573 while(delete_character > 0) /* remove characters */
575 items[active_item]->input.resize(i-1);
582 case MENU_ACTION_INPUT:
583 if(items[active_item]->kind == MN_TEXTFIELD
584 || (items[active_item]->kind == MN_NUMFIELD
585 && mn_input_char >= '0' && mn_input_char <= '9'))
587 items[active_item]->input.push_back(mn_input_char);
591 case MENU_ACTION_BACK:
595 case MENU_ACTION_NONE:
598 menuaction = MENU_ACTION_NONE;
600 assert(active_item < int(items.size()));
606 if (hit_item != -1) {
607 int id = items[hit_item]->id;
608 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)
616 Menu::menu_action(MenuItem* )
620 Menu::draw_item(DrawingContext& context, int index)
622 float menu_height = get_height();
623 float menu_width = get_width();
625 MenuItem& pitem = *(items[index]);
627 Color text_color = default_color;
629 float y_pos = pos_y + 24*index - menu_height/2 + 12;
631 int text_width = int(normal_font->get_text_width(pitem.text));
632 int input_width = int(normal_font->get_text_width(pitem.input) + 10);
635 float left = pos_x - menu_width/2 + 16;
636 float right = pos_x + menu_width/2 - 16;
638 if(pitem.list.size() > 0) {
639 list_width = (int) normal_font->get_text_width(pitem.list[pitem.selected]);
643 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
645 if(index == active_item)
648 text_color = active_color;
651 if(active_item == index)
653 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
654 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
655 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
656 Color(1.0f, 1.0f, 1.0f, blink),
659 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
660 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
661 Color(1.0f, 1.0f, 1.0f, 0.5f),
670 context.draw_text(normal_font, pitem.text,
671 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
672 ALIGN_CENTER, LAYER_GUI, inactive_color);
679 float x = pos_x - menu_width/2;
680 float y = y_pos - 12;
681 /* Draw a horizontal line with a little 3d effect */
682 context.draw_filled_rect(Vector(x, y + 6),
683 Vector(menu_width, 4),
684 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
685 context.draw_filled_rect(Vector(x, y + 6),
686 Vector(menu_width, 2),
687 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
692 context.draw_text(big_font, pitem.text,
693 Vector(pos_x, y_pos - int(big_font->get_height()/2)),
694 ALIGN_CENTER, LAYER_GUI, label_color);
699 case MN_CONTROLFIELD:
701 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
703 if(active_item == index)
704 context.draw_text(normal_font,
705 pitem.get_input_with_symbol(true),
706 Vector(right, y_pos - int(normal_font->get_height()/2)),
707 ALIGN_RIGHT, LAYER_GUI, field_color);
709 context.draw_text(normal_font,
710 pitem.get_input_with_symbol(false),
711 Vector(right, y_pos - int(normal_font->get_height()/2)),
712 ALIGN_RIGHT, LAYER_GUI, field_color);
715 context.draw_text(normal_font, pitem.input,
716 Vector(right, y_pos - int(normal_font->get_height()/2)),
717 ALIGN_RIGHT, LAYER_GUI, field_color);
719 context.draw_text(normal_font, pitem.text,
720 Vector(left, y_pos - int(normal_font->get_height()/2)),
721 ALIGN_LEFT, LAYER_GUI, text_color);
724 case MN_STRINGSELECT:
726 float roff = arrow_left->get_width();
728 context.draw_text(normal_font, pitem.text,
729 Vector(left, y_pos - int(normal_font->get_height()/2)),
730 ALIGN_LEFT, LAYER_GUI, text_color);
733 context.draw_surface(arrow_left.get(),
734 Vector(right - list_width - roff - roff, y_pos - 8),
736 context.draw_surface(arrow_right.get(),
737 Vector(right - roff, y_pos - 8),
739 context.draw_text(normal_font, pitem.list[pitem.selected],
740 Vector(right - roff, y_pos - int(normal_font->get_height()/2)),
741 ALIGN_RIGHT, LAYER_GUI, text_color);
746 context.draw_text(normal_font, pitem.text,
747 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
748 ALIGN_CENTER, LAYER_GUI, text_color);
749 context.draw_surface(back.get(),
750 Vector(x_pos + text_width/2 + 16, y_pos - 8),
757 context.draw_text(normal_font, pitem.text,
758 Vector(pos_x - menu_width/2 + 16, y_pos - (normal_font->get_height()/2)),
759 ALIGN_LEFT, LAYER_GUI, text_color);
762 context.draw_surface(checkbox_checked.get(),
763 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
766 context.draw_surface(checkbox.get(),
767 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
772 context.draw_text(normal_font, pitem.text,
773 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
774 ALIGN_CENTER, LAYER_GUI, text_color);
778 context.draw_text(normal_font, pitem.text,
779 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
780 ALIGN_CENTER, LAYER_GUI, text_color);
786 Menu::get_width() const
788 /* The width of the menu has to be more than the width of the text
789 with the most characters */
790 float menu_width = 0;
791 for(unsigned int i = 0; i < items.size(); ++i)
793 Font* font = normal_font;
794 if(items[i]->kind == MN_LABEL)
797 float w = font->get_text_width(items[i]->text) +
798 big_font->get_text_width(items[i]->input) + 16;
799 if(items[i]->kind == MN_TOGGLE)
806 return menu_width + 24;
810 Menu::get_height() const
812 return items.size() * 24;
815 /* Draw the current menu. */
817 Menu::draw(DrawingContext& context)
819 if(MouseCursor::current()) {
820 MouseCursor::current()->draw(context);
823 float menu_width = get_width();
824 float menu_height = get_height();
826 if (effect_progress != 1.0f)
830 menu_width = (current_->get_width() * (1.0f - effect_progress));
831 menu_height = (current_->get_height() * (1.0f - effect_progress));
833 else if (Menu::previous)
835 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
836 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
837 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
841 menu_width *= effect_progress;
842 menu_height *= effect_progress;
846 /* Draw a transparent background */
847 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
848 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
849 Color(0.2f, 0.3f, 0.4f, 0.8f),
853 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
854 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
855 Color(0.6f, 0.7f, 0.8f, 0.5f),
859 if (!items[active_item]->help.empty())
861 int text_width = (int) normal_font->get_text_width(items[active_item]->help);
862 int text_height = (int) normal_font->get_text_height(items[active_item]->help);
864 Rect text_rect(pos_x - text_width/2 - 8,
865 SCREEN_HEIGHT - 48 - text_height/2 - 4,
866 pos_x + text_width/2 + 8,
867 SCREEN_HEIGHT - 48 + text_height/2 + 4);
869 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
870 text_rect.p2 + Vector(4,4)),
871 Color(0.2f, 0.3f, 0.4f, 0.8f),
875 context.draw_filled_rect(text_rect,
876 Color(0.6f, 0.7f, 0.8f, 0.5f),
880 context.draw_text(normal_font, items[active_item]->help,
881 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
882 ALIGN_CENTER, LAYER_GUI);
885 if (effect_progress == 1.0f)
886 for(unsigned int i = 0; i < items.size(); ++i)
888 draw_item(context, i);
893 Menu::get_item_by_id(int id)
895 for(std::vector<MenuItem*>::iterator i = items.begin();
896 i != items.end(); ++i) {
897 MenuItem& item = **i;
903 throw std::runtime_error("MenuItem not found");
907 Menu::get_item_by_id(int id) const
909 for(std::vector<MenuItem*>::const_iterator i = items.begin();
910 i != items.end(); ++i) {
911 const MenuItem& item = **i;
917 throw std::runtime_error("MenuItem not found");
920 int Menu::get_active_item_id()
922 return items[active_item]->id;
926 Menu::is_toggled(int id) const
928 return get_item_by_id(id).toggled;
932 Menu::set_toggled(int id, bool toggled)
934 get_item_by_id(id).toggled = toggled;
938 Menu::get_parent() const
940 if (last_menus.empty())
943 return last_menus.back();
946 /* Check for menu event */
948 Menu::event(const SDL_Event& event)
950 if(effect_progress != 1.0f)
954 case SDL_MOUSEBUTTONDOWN:
956 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
957 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
959 if(x > pos_x - get_width()/2 &&
960 x < pos_x + get_width()/2 &&
961 y > pos_y - get_height()/2 &&
962 y < pos_y + get_height()/2)
964 menuaction = MENU_ACTION_HIT;
969 case SDL_MOUSEMOTION:
971 float x = event.motion.x * SCREEN_WIDTH/screen->w;
972 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
974 if(x > pos_x - get_width()/2 &&
975 x < pos_x + get_width()/2 &&
976 y > pos_y - get_height()/2 &&
977 y < pos_y + get_height()/2)
980 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
982 /* only change the mouse focus to a selectable item */
983 if ((items[new_active_item]->kind != MN_HL)
984 && (items[new_active_item]->kind != MN_LABEL)
985 && (items[new_active_item]->kind != MN_INACTIVE))
986 active_item = new_active_item;
988 if(MouseCursor::current())
989 MouseCursor::current()->set_state(MC_LINK);
993 if(MouseCursor::current())
994 MouseCursor::current()->set_state(MC_NORMAL);
1005 Menu::set_active_item(int id)
1007 for(size_t i = 0; i < items.size(); ++i) {
1008 MenuItem* item = items[i];
1009 if(item->id == id) {