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;
54 Font* Menu::default_font;
55 Font* Menu::active_font;
56 Font* Menu::deactive_font;
57 Font* Menu::label_font;
58 Font* Menu::field_font;
60 /* just displays a Yes/No text that can be used to confirm stuff */
61 bool confirm_dialog(Surface *background, std::string text)
63 //Surface* cap_screen = Surface::CaptureScreen();
64 Menu* dialog = new Menu;
65 dialog->add_deactive(-1, text);
67 dialog->add_entry(true, _("Yes"));
68 dialog->add_entry(false, _("No"));
71 Menu::set_current(dialog);
73 DrawingContext context;
75 // TODO make this a screen and not another mainloop...
79 while (SDL_PollEvent(&event)) {
80 if(event.type == SDL_QUIT)
82 main_controller->process_event(event);
86 if(background == NULL)
87 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
90 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
92 dialog->draw(context);
95 switch (dialog->check())
105 Menu::set_current(0);
113 mouse_cursor->draw(context);
114 context.do_drawing();
122 Menu::push_current(Menu* pmenu)
127 last_menus.push_back(current_);
130 current_->effect_start_time = real_time;
131 current_->effect_progress = 0.0f;
139 if (last_menus.size() >= 1) {
140 current_ = last_menus.back();
141 current_->effect_start_time = real_time;
142 current_->effect_progress = 0.0f;
143 last_menus.pop_back();
150 Menu::set_current(Menu* menu)
158 menu->effect_start_time = real_time;
159 menu->effect_progress = 0.0f;
162 // just to be sure...
163 main_controller->reset();
170 current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
172 for(std::list<Menu*>::iterator i = all_menus.begin(); i != all_menus.end(); ++i)
174 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
175 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
179 MenuItem::MenuItem(MenuItemKind _kind, int _id)
180 : kind(_kind) , id(_id)
188 MenuItem::change_text(const std::string& text_)
194 MenuItem::change_input(const std::string& text_)
200 MenuItem::set_help(const std::string& help_text)
202 std::string overflow;
203 help = Menu::default_font->wrap_to_width(help_text, 600, &overflow);
204 while (!overflow.empty())
207 help += Menu::default_font->wrap_to_width(overflow, 600, &overflow);
211 std::string MenuItem::get_input_with_symbol(bool active_item)
214 input_flickering = true;
216 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
221 snprintf(str, sizeof(str), "%s ",input.c_str());
223 snprintf(str, sizeof(str), "%s_",input.c_str());
225 std::string string = str;
232 all_menus.remove(this);
234 for(std::vector<MenuItem*>::iterator i = items.begin();
235 i != items.end(); ++i)
241 if (previous == this)
247 all_menus.push_back(this);
250 menuaction = MENU_ACTION_NONE;
251 delete_character = 0;
252 mn_input_char = '\0';
254 pos_x = SCREEN_WIDTH/2;
255 pos_y = SCREEN_HEIGHT/2;
259 effect_progress = 0.0f;
260 effect_start_time = 0.0f;
262 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
263 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
264 back.reset(new Surface("images/engine/menu/arrow-back.png"));
265 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
266 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
270 Menu::set_pos(float x, float y, float rw, float rh)
272 pos_x = x + get_width() * rw;
273 pos_y = y + get_height() * rh;
276 /* Add an item to a menu */
278 Menu::additem(MenuItem* item)
280 items.push_back(item);
282 /* If a new menu is being built, the active item shouldn't be set to
283 * something that isnt selectable. Set the active_item to the first
284 * selectable item added
286 if (active_item == -1
287 && item->kind != MN_HL
288 && item->kind != MN_LABEL
289 && item->kind != MN_DEACTIVE) {
290 active_item = items.size() - 1;
297 MenuItem* item = new MenuItem(MN_HL);
303 Menu::add_label(const std::string& text)
305 MenuItem* item = new MenuItem(MN_LABEL);
312 Menu::add_controlfield(int id, const std::string& text,
313 const std::string& mapping)
315 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
316 item->change_text(text);
317 item->change_input(mapping);
323 Menu::add_entry(int id, const std::string& text)
325 MenuItem* item = new MenuItem(MN_ACTION, id);
332 Menu::add_deactive(int id, const std::string& text)
334 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
341 Menu::add_toggle(int id, const std::string& text, bool toogled)
343 MenuItem* item = new MenuItem(MN_TOGGLE, id);
345 item->toggled = toogled;
351 Menu::add_string_select(int id, const std::string& text)
353 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
360 Menu::add_back(const std::string& text)
362 MenuItem* item = new MenuItem(MN_BACK);
369 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
371 MenuItem* item = new MenuItem(MN_GOTO, id);
373 item->target_menu = submenu;
381 for(std::vector<MenuItem*>::iterator i = items.begin();
382 i != items.end(); ++i) {
389 /* Process actions done on the menu */
393 int menu_height = (int) get_height();
394 if (menu_height > SCREEN_HEIGHT)
396 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
397 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
400 effect_progress = (real_time - effect_start_time) * 6.0f;
402 if(effect_progress >= 1.0f) {
403 effect_progress = 1.0f;
404 } else if (effect_progress <= 0.0f) {
405 effect_progress = 0.0f;
408 /** check main input controller... */
409 if(main_controller->pressed(Controller::UP)) {
410 menuaction = MENU_ACTION_UP;
411 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
413 if(main_controller->hold(Controller::UP) &&
414 menu_repeat_time != 0 && real_time > menu_repeat_time) {
415 menuaction = MENU_ACTION_UP;
416 menu_repeat_time = real_time + MENU_REPEAT_RATE;
419 if(main_controller->pressed(Controller::DOWN)) {
420 menuaction = MENU_ACTION_DOWN;
421 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
423 if(main_controller->hold(Controller::DOWN) &&
424 menu_repeat_time != 0 && real_time > menu_repeat_time) {
425 menuaction = MENU_ACTION_DOWN;
426 menu_repeat_time = real_time + MENU_REPEAT_RATE;
429 if(main_controller->pressed(Controller::LEFT)) {
430 menuaction = MENU_ACTION_LEFT;
431 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
433 if(main_controller->hold(Controller::LEFT) &&
434 menu_repeat_time != 0 && real_time > menu_repeat_time) {
435 menuaction = MENU_ACTION_LEFT;
436 menu_repeat_time = real_time + MENU_REPEAT_RATE;
439 if(main_controller->pressed(Controller::RIGHT)) {
440 menuaction = MENU_ACTION_RIGHT;
441 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
443 if(main_controller->hold(Controller::RIGHT) &&
444 menu_repeat_time != 0 && real_time > menu_repeat_time) {
445 menuaction = MENU_ACTION_RIGHT;
446 menu_repeat_time = real_time + MENU_REPEAT_RATE;
449 if(main_controller->pressed(Controller::ACTION)
450 || main_controller->pressed(Controller::MENU_SELECT)) {
451 menuaction = MENU_ACTION_HIT;
453 if(main_controller->pressed(Controller::PAUSE_MENU)) {
454 menuaction = MENU_ACTION_BACK;
458 if(items.size() == 0)
461 int last_active_item = active_item;
468 active_item = int(items.size())-1;
469 } while ((items[active_item]->kind == MN_HL
470 || items[active_item]->kind == MN_LABEL
471 || items[active_item]->kind == MN_DEACTIVE)
472 && (active_item != last_active_item));
476 case MENU_ACTION_DOWN:
478 if(active_item < int(items.size())-1 )
482 } while ((items[active_item]->kind == MN_HL
483 || items[active_item]->kind == MN_LABEL
484 || items[active_item]->kind == MN_DEACTIVE)
485 && (active_item != last_active_item));
489 case MENU_ACTION_LEFT:
490 if(items[active_item]->kind == MN_STRINGSELECT) {
491 if(items[active_item]->selected > 0)
492 items[active_item]->selected--;
494 items[active_item]->selected = items[active_item]->list.size()-1;
496 menu_action(items[active_item]);
500 case MENU_ACTION_RIGHT:
501 if(items[active_item]->kind == MN_STRINGSELECT) {
502 if(items[active_item]->selected+1 < items[active_item]->list.size())
503 items[active_item]->selected++;
505 items[active_item]->selected = 0;
507 menu_action(items[active_item]);
511 case MENU_ACTION_HIT: {
512 hit_item = active_item;
513 switch (items[active_item]->kind) {
515 assert(items[active_item]->target_menu != 0);
516 Menu::push_current(items[active_item]->target_menu);
520 items[active_item]->toggled = !items[active_item]->toggled;
521 menu_action(items[active_item]);
524 case MN_CONTROLFIELD:
525 menu_action(items[active_item]);
529 menu_action(items[active_item]);
532 case MN_STRINGSELECT:
533 if(items[active_item]->selected+1 < items[active_item]->list.size())
534 items[active_item]->selected++;
536 items[active_item]->selected = 0;
538 menu_action(items[active_item]);
543 menuaction = MENU_ACTION_DOWN;
556 case MENU_ACTION_REMOVE:
557 if(items[active_item]->kind == MN_TEXTFIELD
558 || items[active_item]->kind == MN_NUMFIELD)
560 if(!items[active_item]->input.empty())
562 int i = items[active_item]->input.size();
564 while(delete_character > 0) /* remove charactes */
566 items[active_item]->input.resize(i-1);
573 case MENU_ACTION_INPUT:
574 if(items[active_item]->kind == MN_TEXTFIELD
575 || (items[active_item]->kind == MN_NUMFIELD
576 && mn_input_char >= '0' && mn_input_char <= '9'))
578 items[active_item]->input.push_back(mn_input_char);
582 case MENU_ACTION_BACK:
586 case MENU_ACTION_NONE:
589 menuaction = MENU_ACTION_NONE;
591 assert(active_item < int(items.size()));
598 return items[hit_item]->id;
604 Menu::menu_action(MenuItem* )
608 Menu::draw_item(DrawingContext& context, int index)
610 float menu_height = get_height();
611 float menu_width = get_width();
613 MenuItem& pitem = *(items[index]);
615 Font* text_font = default_font;
617 float y_pos = pos_y + 24*index - menu_height/2 + 12;
619 int text_width = int(text_font->get_text_width(pitem.text));
620 int input_width = int(text_font->get_text_width(pitem.input) + 10);
623 float left = pos_x - menu_width/2 + 16;
624 float right = pos_x + menu_width/2 - 16;
626 if(pitem.list.size() > 0) {
627 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
631 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
633 if(index == active_item)
636 text_font = active_font;
639 if(active_item == index)
641 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
642 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
643 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
644 Color(1.0f, 1.0f, 1.0f, blink),
647 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
648 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
649 Color(1.0f, 1.0f, 1.0f, 0.5f),
658 context.draw_text(deactive_font, pitem.text,
659 Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
660 ALIGN_CENTER, LAYER_GUI);
667 float x = pos_x - menu_width/2;
668 float y = y_pos - 12;
669 /* Draw a horizontal line with a little 3d effect */
670 context.draw_filled_rect(Vector(x, y + 6),
671 Vector(menu_width, 4),
672 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
673 context.draw_filled_rect(Vector(x, y + 6),
674 Vector(menu_width, 2),
675 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
680 context.draw_text(label_font, pitem.text,
681 Vector(pos_x, y_pos - int(label_font->get_height()/2)),
682 ALIGN_CENTER, LAYER_GUI);
687 case MN_CONTROLFIELD:
689 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
691 if(active_item == index)
692 context.draw_text(field_font,
693 pitem.get_input_with_symbol(true),
694 Vector(right, y_pos - int(field_font->get_height()/2)),
695 ALIGN_RIGHT, LAYER_GUI);
697 context.draw_text(field_font,
698 pitem.get_input_with_symbol(false),
699 Vector(right, y_pos - int(field_font->get_height()/2)),
700 ALIGN_RIGHT, LAYER_GUI);
703 context.draw_text(field_font, pitem.input,
704 Vector(right, y_pos - int(field_font->get_height()/2)),
705 ALIGN_RIGHT, LAYER_GUI);
707 context.draw_text(text_font, pitem.text,
708 Vector(left, y_pos - int(text_font->get_height()/2)),
709 ALIGN_LEFT, LAYER_GUI);
712 case MN_STRINGSELECT:
714 float roff = arrow_left->get_width();
716 context.draw_text(text_font, pitem.text,
717 Vector(left, y_pos - int(text_font->get_height()/2)),
718 ALIGN_LEFT, LAYER_GUI);
721 context.draw_surface(arrow_left.get(),
722 Vector(right - list_width - roff - roff, y_pos - 8),
724 context.draw_surface(arrow_right.get(),
725 Vector(right - roff, y_pos - 8),
727 context.draw_text(field_font, pitem.list[pitem.selected],
728 Vector(right - roff, y_pos - int(text_font->get_height()/2)),
729 ALIGN_RIGHT, LAYER_GUI);
734 context.draw_text(text_font, pitem.text,
735 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
736 ALIGN_CENTER, LAYER_GUI);
737 context.draw_surface(back.get(),
738 Vector(x_pos + text_width/2 + 16, y_pos - 8),
745 context.draw_text(text_font, pitem.text,
746 Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
747 ALIGN_LEFT, LAYER_GUI);
750 context.draw_surface(checkbox_checked.get(),
751 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
754 context.draw_surface(checkbox.get(),
755 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
760 context.draw_text(text_font, pitem.text,
761 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
762 ALIGN_CENTER, LAYER_GUI);
766 context.draw_text(text_font, pitem.text,
767 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
768 ALIGN_CENTER, LAYER_GUI);
774 Menu::get_width() const
776 /* The width of the menu has to be more than the width of the text
777 with the most characters */
778 float menu_width = 0;
779 for(unsigned int i = 0; i < items.size(); ++i)
781 Font* font = default_font;
782 if(items[i]->kind == MN_LABEL)
785 float w = font->get_text_width(items[i]->text) +
786 label_font->get_text_width(items[i]->input) + 16;
787 if(items[i]->kind == MN_TOGGLE)
794 return menu_width + 24;
798 Menu::get_height() const
800 return items.size() * 24;
803 /* Draw the current menu. */
805 Menu::draw(DrawingContext& context)
807 if(MouseCursor::current()) {
808 MouseCursor::current()->draw(context);
811 float menu_width = get_width();
812 float menu_height = get_height();
814 if (effect_progress != 1.0f)
818 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
819 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
820 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
824 menu_width *= effect_progress;
825 menu_height *= effect_progress;
829 /* Draw a transparent background */
830 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
831 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
832 Color(0.2f, 0.3f, 0.4f, 0.8f),
836 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
837 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
838 Color(0.6f, 0.7f, 0.8f, 0.5f),
842 if (!items[active_item]->help.empty())
844 int text_width = (int) default_font->get_text_width(items[active_item]->help);
845 int text_height = (int) default_font->get_text_height(items[active_item]->help);
847 Rect text_rect(pos_x - text_width/2 - 8,
848 SCREEN_HEIGHT - 48 - text_height/2 - 4,
849 pos_x + text_width/2 + 8,
850 SCREEN_HEIGHT - 48 + text_height/2 + 4);
852 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
853 text_rect.p2 + Vector(4,4)),
854 Color(0.2f, 0.3f, 0.4f, 0.8f),
858 context.draw_filled_rect(text_rect,
859 Color(0.6f, 0.7f, 0.8f, 0.5f),
863 context.draw_text(default_font, items[active_item]->help,
864 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
865 ALIGN_CENTER, LAYER_GUI);
868 if (effect_progress == 1.0f)
869 for(unsigned int i = 0; i < items.size(); ++i)
871 draw_item(context, i);
876 Menu::get_item_by_id(int id)
878 for(std::vector<MenuItem*>::iterator i = items.begin();
879 i != items.end(); ++i) {
880 MenuItem& item = **i;
886 throw std::runtime_error("MenuItem not found");
890 Menu::get_item_by_id(int id) const
892 for(std::vector<MenuItem*>::const_iterator i = items.begin();
893 i != items.end(); ++i) {
894 const MenuItem& item = **i;
900 throw std::runtime_error("MenuItem not found");
903 int Menu::get_active_item_id()
905 return items[active_item]->id;
909 Menu::is_toggled(int id) const
911 return get_item_by_id(id).toggled;
915 Menu::set_toggled(int id, bool toggled)
917 get_item_by_id(id).toggled = toggled;
921 Menu::get_parent() const
923 if (last_menus.empty())
926 return last_menus.back();
929 /* Check for menu event */
931 Menu::event(const SDL_Event& event)
933 if(effect_progress != 1.0f)
937 case SDL_MOUSEBUTTONDOWN:
939 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
940 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
942 if(x > pos_x - get_width()/2 &&
943 x < pos_x + get_width()/2 &&
944 y > pos_y - get_height()/2 &&
945 y < pos_y + get_height()/2)
947 menuaction = MENU_ACTION_HIT;
952 case SDL_MOUSEMOTION:
954 float x = event.motion.x * SCREEN_WIDTH/screen->w;
955 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
957 if(x > pos_x - get_width()/2 &&
958 x < pos_x + get_width()/2 &&
959 y > pos_y - get_height()/2 &&
960 y < pos_y + get_height()/2)
963 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
965 /* only change the mouse focus to a selectable item */
966 if ((items[new_active_item]->kind != MN_HL)
967 && (items[new_active_item]->kind != MN_LABEL)
968 && (items[new_active_item]->kind != MN_DEACTIVE))
969 active_item = new_active_item;
971 if(MouseCursor::current())
972 MouseCursor::current()->set_state(MC_LINK);
976 if(MouseCursor::current())
977 MouseCursor::current()->set_state(MC_NORMAL);
988 Menu::set_active_item(int id)
990 for(size_t i = 0; i < items.size(); ++i) {
991 MenuItem* item = items[i];