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 Menu* Menu::current_ = 0;
52 Menu* Menu::previous = 0;
53 Font* Menu::default_font;
54 Font* Menu::active_font;
55 Font* Menu::deactive_font;
56 Font* Menu::label_font;
57 Font* Menu::field_font;
59 /* just displays a Yes/No text that can be used to confirm stuff */
60 bool confirm_dialog(Surface *background, std::string text)
62 //Surface* cap_screen = Surface::CaptureScreen();
63 Menu* dialog = new Menu;
64 dialog->add_deactive(-1, text);
66 dialog->add_entry(true, _("Yes"));
67 dialog->add_entry(false, _("No"));
70 Menu::set_current(dialog);
72 DrawingContext context;
74 // TODO make this a screen and not another mainloop...
78 while (SDL_PollEvent(&event)) {
79 if(event.type == SDL_QUIT)
81 main_controller->process_event(event);
85 if(background == NULL)
86 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
89 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
91 dialog->draw(context);
94 switch (dialog->check())
104 Menu::set_current(0);
112 mouse_cursor->draw(context);
113 context.do_drawing();
121 Menu::push_current(Menu* pmenu)
126 last_menus.push_back(current_);
129 current_->effect_start_time = real_time;
130 current_->effect_progress = 0.0f;
138 if (last_menus.size() >= 1) {
139 current_ = last_menus.back();
140 current_->effect_start_time = real_time;
141 current_->effect_progress = 0.0f;
142 last_menus.pop_back();
149 Menu::set_current(Menu* menu)
157 menu->effect_start_time = real_time;
158 menu->effect_progress = 0.0f;
161 // just to be sure...
162 main_controller->reset();
169 current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
171 for(std::vector<Menu*>::iterator i = last_menus.begin(); i != last_menus.end(); ++i)
173 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
174 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
178 MenuItem::MenuItem(MenuItemKind _kind, int _id)
179 : kind(_kind) , id(_id)
187 MenuItem::change_text(const std::string& text_)
193 MenuItem::change_input(const std::string& text_)
199 MenuItem::set_help(const std::string& help_text)
201 std::string overflow;
202 help = Menu::default_font->wrap_to_width(help_text, 600, &overflow);
203 while (!overflow.empty())
206 help += Menu::default_font->wrap_to_width(overflow, 600, &overflow);
210 std::string MenuItem::get_input_with_symbol(bool active_item)
213 input_flickering = true;
215 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
220 snprintf(str, sizeof(str), "%s ",input.c_str());
222 snprintf(str, sizeof(str), "%s_",input.c_str());
224 std::string string = str;
231 for(std::vector<MenuItem*>::iterator i = items.begin();
232 i != items.end(); ++i)
238 if (previous == this)
245 menuaction = MENU_ACTION_NONE;
246 delete_character = 0;
247 mn_input_char = '\0';
249 pos_x = SCREEN_WIDTH/2;
250 pos_y = SCREEN_HEIGHT/2;
254 effect_progress = 0.0f;
255 effect_start_time = 0.0f;
257 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
258 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
259 back.reset(new Surface("images/engine/menu/arrow-back.png"));
260 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
261 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
265 Menu::set_pos(float x, float y, float rw, float rh)
267 pos_x = x + get_width() * rw;
268 pos_y = y + get_height() * rh;
271 /* Add an item to a menu */
273 Menu::additem(MenuItem* item)
275 items.push_back(item);
277 /* If a new menu is being built, the active item shouldn't be set to
278 * something that isnt selectable. Set the active_item to the first
279 * selectable item added
281 if (active_item == -1
282 && item->kind != MN_HL
283 && item->kind != MN_LABEL
284 && item->kind != MN_DEACTIVE) {
285 active_item = items.size() - 1;
292 MenuItem* item = new MenuItem(MN_HL);
298 Menu::add_label(const std::string& text)
300 MenuItem* item = new MenuItem(MN_LABEL);
307 Menu::add_controlfield(int id, const std::string& text,
308 const std::string& mapping)
310 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
311 item->change_text(text);
312 item->change_input(mapping);
318 Menu::add_entry(int id, const std::string& text)
320 MenuItem* item = new MenuItem(MN_ACTION, id);
327 Menu::add_deactive(int id, const std::string& text)
329 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
336 Menu::add_toggle(int id, const std::string& text, bool toogled)
338 MenuItem* item = new MenuItem(MN_TOGGLE, id);
340 item->toggled = toogled;
346 Menu::add_string_select(int id, const std::string& text)
348 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
355 Menu::add_back(const std::string& text)
357 MenuItem* item = new MenuItem(MN_BACK);
364 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
366 MenuItem* item = new MenuItem(MN_GOTO, id);
368 item->target_menu = submenu;
376 for(std::vector<MenuItem*>::iterator i = items.begin();
377 i != items.end(); ++i) {
384 /* Process actions done on the menu */
388 int menu_height = (int) get_height();
389 if (menu_height > SCREEN_HEIGHT)
391 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
392 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
395 effect_progress = (real_time - effect_start_time) * 6.0f;
397 if(effect_progress >= 1.0f) {
398 effect_progress = 1.0f;
399 } else if (effect_progress <= 0.0f) {
400 effect_progress = 0.0f;
403 /** check main input controller... */
404 if(main_controller->pressed(Controller::UP)) {
405 menuaction = MENU_ACTION_UP;
406 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
408 if(main_controller->hold(Controller::UP) &&
409 menu_repeat_time != 0 && real_time > menu_repeat_time) {
410 menuaction = MENU_ACTION_UP;
411 menu_repeat_time = real_time + MENU_REPEAT_RATE;
414 if(main_controller->pressed(Controller::DOWN)) {
415 menuaction = MENU_ACTION_DOWN;
416 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
418 if(main_controller->hold(Controller::DOWN) &&
419 menu_repeat_time != 0 && real_time > menu_repeat_time) {
420 menuaction = MENU_ACTION_DOWN;
421 menu_repeat_time = real_time + MENU_REPEAT_RATE;
424 if(main_controller->pressed(Controller::LEFT)) {
425 menuaction = MENU_ACTION_LEFT;
426 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
428 if(main_controller->hold(Controller::LEFT) &&
429 menu_repeat_time != 0 && real_time > menu_repeat_time) {
430 menuaction = MENU_ACTION_LEFT;
431 menu_repeat_time = real_time + MENU_REPEAT_RATE;
434 if(main_controller->pressed(Controller::RIGHT)) {
435 menuaction = MENU_ACTION_RIGHT;
436 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
438 if(main_controller->hold(Controller::RIGHT) &&
439 menu_repeat_time != 0 && real_time > menu_repeat_time) {
440 menuaction = MENU_ACTION_RIGHT;
441 menu_repeat_time = real_time + MENU_REPEAT_RATE;
444 if(main_controller->pressed(Controller::ACTION)
445 || main_controller->pressed(Controller::MENU_SELECT)) {
446 menuaction = MENU_ACTION_HIT;
448 if(main_controller->pressed(Controller::PAUSE_MENU)) {
449 menuaction = MENU_ACTION_BACK;
453 if(items.size() == 0)
456 int last_active_item = active_item;
463 active_item = int(items.size())-1;
464 } while ((items[active_item]->kind == MN_HL
465 || items[active_item]->kind == MN_LABEL
466 || items[active_item]->kind == MN_DEACTIVE)
467 && (active_item != last_active_item));
471 case MENU_ACTION_DOWN:
473 if(active_item < int(items.size())-1 )
477 } while ((items[active_item]->kind == MN_HL
478 || items[active_item]->kind == MN_LABEL
479 || items[active_item]->kind == MN_DEACTIVE)
480 && (active_item != last_active_item));
484 case MENU_ACTION_LEFT:
485 if(items[active_item]->kind == MN_STRINGSELECT) {
486 if(items[active_item]->selected > 0)
487 items[active_item]->selected--;
489 items[active_item]->selected = items[active_item]->list.size()-1;
491 menu_action(items[active_item]);
495 case MENU_ACTION_RIGHT:
496 if(items[active_item]->kind == MN_STRINGSELECT) {
497 if(items[active_item]->selected+1 < items[active_item]->list.size())
498 items[active_item]->selected++;
500 items[active_item]->selected = 0;
502 menu_action(items[active_item]);
506 case MENU_ACTION_HIT: {
507 hit_item = active_item;
508 switch (items[active_item]->kind) {
510 assert(items[active_item]->target_menu != 0);
511 Menu::push_current(items[active_item]->target_menu);
515 items[active_item]->toggled = !items[active_item]->toggled;
516 menu_action(items[active_item]);
519 case MN_CONTROLFIELD:
520 menu_action(items[active_item]);
524 menu_action(items[active_item]);
527 case MN_STRINGSELECT:
528 if(items[active_item]->selected+1 < items[active_item]->list.size())
529 items[active_item]->selected++;
531 items[active_item]->selected = 0;
533 menu_action(items[active_item]);
538 menuaction = MENU_ACTION_DOWN;
551 case MENU_ACTION_REMOVE:
552 if(items[active_item]->kind == MN_TEXTFIELD
553 || items[active_item]->kind == MN_NUMFIELD)
555 if(!items[active_item]->input.empty())
557 int i = items[active_item]->input.size();
559 while(delete_character > 0) /* remove charactes */
561 items[active_item]->input.resize(i-1);
568 case MENU_ACTION_INPUT:
569 if(items[active_item]->kind == MN_TEXTFIELD
570 || (items[active_item]->kind == MN_NUMFIELD
571 && mn_input_char >= '0' && mn_input_char <= '9'))
573 items[active_item]->input.push_back(mn_input_char);
577 case MENU_ACTION_BACK:
581 case MENU_ACTION_NONE:
584 menuaction = MENU_ACTION_NONE;
586 assert(active_item < int(items.size()));
593 return items[hit_item]->id;
599 Menu::menu_action(MenuItem* )
603 Menu::draw_item(DrawingContext& context, int index)
605 float menu_height = get_height();
606 float menu_width = get_width();
608 MenuItem& pitem = *(items[index]);
610 Font* text_font = default_font;
612 float y_pos = pos_y + 24*index - menu_height/2 + 12;
614 int text_width = int(text_font->get_text_width(pitem.text));
615 int input_width = int(text_font->get_text_width(pitem.input) + 10);
618 float left = pos_x - menu_width/2 + 16;
619 float right = pos_x + menu_width/2 - 16;
621 if(pitem.list.size() > 0) {
622 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
626 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
628 if(index == active_item)
631 text_font = active_font;
634 if(active_item == index)
636 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
637 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
638 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
639 Color(1.0f, 1.0f, 1.0f, blink),
642 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
643 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
644 Color(1.0f, 1.0f, 1.0f, 0.5f),
653 context.draw_text(deactive_font, pitem.text,
654 Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
655 ALIGN_CENTER, LAYER_GUI);
662 float x = pos_x - menu_width/2;
663 float y = y_pos - 12;
664 /* Draw a horizontal line with a little 3d effect */
665 context.draw_filled_rect(Vector(x, y + 6),
666 Vector(menu_width, 4),
667 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
668 context.draw_filled_rect(Vector(x, y + 6),
669 Vector(menu_width, 2),
670 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
675 context.draw_text(label_font, pitem.text,
676 Vector(pos_x, y_pos - int(label_font->get_height()/2)),
677 ALIGN_CENTER, LAYER_GUI);
682 case MN_CONTROLFIELD:
684 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
686 if(active_item == index)
687 context.draw_text(field_font,
688 pitem.get_input_with_symbol(true),
689 Vector(right, y_pos - int(field_font->get_height()/2)),
690 ALIGN_RIGHT, LAYER_GUI);
692 context.draw_text(field_font,
693 pitem.get_input_with_symbol(false),
694 Vector(right, y_pos - int(field_font->get_height()/2)),
695 ALIGN_RIGHT, LAYER_GUI);
698 context.draw_text(field_font, pitem.input,
699 Vector(right, y_pos - int(field_font->get_height()/2)),
700 ALIGN_RIGHT, LAYER_GUI);
702 context.draw_text(text_font, pitem.text,
703 Vector(left, y_pos - int(text_font->get_height()/2)),
704 ALIGN_LEFT, LAYER_GUI);
707 case MN_STRINGSELECT:
709 float roff = arrow_left->get_width();
711 context.draw_text(text_font, pitem.text,
712 Vector(left, y_pos - int(text_font->get_height()/2)),
713 ALIGN_LEFT, LAYER_GUI);
716 context.draw_surface(arrow_left.get(),
717 Vector(right - list_width - roff - roff, y_pos - 8),
719 context.draw_surface(arrow_right.get(),
720 Vector(right - roff, y_pos - 8),
722 context.draw_text(field_font, pitem.list[pitem.selected],
723 Vector(right - roff, y_pos - int(text_font->get_height()/2)),
724 ALIGN_RIGHT, LAYER_GUI);
729 context.draw_text(text_font, pitem.text,
730 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
731 ALIGN_CENTER, LAYER_GUI);
732 context.draw_surface(back.get(),
733 Vector(x_pos + text_width/2 + 16, y_pos - 8),
740 context.draw_text(text_font, pitem.text,
741 Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
742 ALIGN_LEFT, LAYER_GUI);
745 context.draw_surface(checkbox_checked.get(),
746 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
749 context.draw_surface(checkbox.get(),
750 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
755 context.draw_text(text_font, pitem.text,
756 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
757 ALIGN_CENTER, LAYER_GUI);
761 context.draw_text(text_font, pitem.text,
762 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
763 ALIGN_CENTER, LAYER_GUI);
769 Menu::get_width() const
771 /* The width of the menu has to be more than the width of the text
772 with the most characters */
773 float menu_width = 0;
774 for(unsigned int i = 0; i < items.size(); ++i)
776 Font* font = default_font;
777 if(items[i]->kind == MN_LABEL)
780 float w = font->get_text_width(items[i]->text) +
781 label_font->get_text_width(items[i]->input) + 16;
782 if(items[i]->kind == MN_TOGGLE)
789 return menu_width + 24;
793 Menu::get_height() const
795 return items.size() * 24;
798 /* Draw the current menu. */
800 Menu::draw(DrawingContext& context)
802 if(MouseCursor::current()) {
803 MouseCursor::current()->draw(context);
806 float menu_width = get_width();
807 float menu_height = get_height();
809 if (effect_progress != 1.0f)
813 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
814 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
815 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
819 menu_width *= effect_progress;
820 menu_height *= effect_progress;
824 /* Draw a transparent background */
825 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
826 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
827 Color(0.2f, 0.3f, 0.4f, 0.8f),
831 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
832 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
833 Color(0.6f, 0.7f, 0.8f, 0.5f),
837 if (!items[active_item]->help.empty())
839 int text_width = (int) default_font->get_text_width(items[active_item]->help);
840 int text_height = (int) default_font->get_text_height(items[active_item]->help);
842 Rect text_rect(pos_x - text_width/2 - 8,
843 SCREEN_HEIGHT - 48 - text_height/2 - 4,
844 pos_x + text_width/2 + 8,
845 SCREEN_HEIGHT - 48 + text_height/2 + 4);
847 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
848 text_rect.p2 + Vector(4,4)),
849 Color(0.2f, 0.3f, 0.4f, 0.8f),
853 context.draw_filled_rect(text_rect,
854 Color(0.6f, 0.7f, 0.8f, 0.5f),
858 context.draw_text(default_font, items[active_item]->help,
859 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
860 ALIGN_CENTER, LAYER_GUI);
863 if (effect_progress == 1.0f)
864 for(unsigned int i = 0; i < items.size(); ++i)
866 draw_item(context, i);
871 Menu::get_item_by_id(int id)
873 for(std::vector<MenuItem*>::iterator i = items.begin();
874 i != items.end(); ++i) {
875 MenuItem& item = **i;
881 throw std::runtime_error("MenuItem not found");
885 Menu::get_item_by_id(int id) const
887 for(std::vector<MenuItem*>::const_iterator i = items.begin();
888 i != items.end(); ++i) {
889 const MenuItem& item = **i;
895 throw std::runtime_error("MenuItem not found");
898 int Menu::get_active_item_id()
900 return items[active_item]->id;
904 Menu::is_toggled(int id) const
906 return get_item_by_id(id).toggled;
910 Menu::set_toggled(int id, bool toggled)
912 get_item_by_id(id).toggled = toggled;
916 Menu::get_parent() const
918 if (last_menus.empty())
921 return last_menus.back();
924 /* Check for menu event */
926 Menu::event(const SDL_Event& event)
928 if(effect_progress != 1.0f)
932 case SDL_MOUSEBUTTONDOWN:
934 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
935 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
937 if(x > pos_x - get_width()/2 &&
938 x < pos_x + get_width()/2 &&
939 y > pos_y - get_height()/2 &&
940 y < pos_y + get_height()/2)
942 menuaction = MENU_ACTION_HIT;
947 case SDL_MOUSEMOTION:
949 float x = event.motion.x * SCREEN_WIDTH/screen->w;
950 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
952 if(x > pos_x - get_width()/2 &&
953 x < pos_x + get_width()/2 &&
954 y > pos_y - get_height()/2 &&
955 y < pos_y + get_height()/2)
958 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
960 /* only change the mouse focus to a selectable item */
961 if ((items[new_active_item]->kind != MN_HL)
962 && (items[new_active_item]->kind != MN_LABEL)
963 && (items[new_active_item]->kind != MN_DEACTIVE))
964 active_item = new_active_item;
966 if(MouseCursor::current())
967 MouseCursor::current()->set_state(MC_LINK);
971 if(MouseCursor::current())
972 MouseCursor::current()->set_state(MC_NORMAL);
983 Menu::set_active_item(int id)
985 for(size_t i = 0; i < items.size(); ++i) {
986 MenuItem* item = items[i];