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();
165 MenuItem::MenuItem(MenuItemKind _kind, int _id)
166 : kind(_kind) , id(_id)
174 MenuItem::change_text(const std::string& text_)
180 MenuItem::change_input(const std::string& text_)
186 MenuItem::set_help(const std::string& help_text)
188 std::string overflow;
189 help = Menu::default_font->wrap_to_width(help_text, 600, &overflow);
190 while (!overflow.empty())
193 help += Menu::default_font->wrap_to_width(overflow, 600, &overflow);
197 std::string MenuItem::get_input_with_symbol(bool active_item)
200 input_flickering = true;
202 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
207 snprintf(str, sizeof(str), "%s ",input.c_str());
209 snprintf(str, sizeof(str), "%s_",input.c_str());
211 std::string string = str;
218 for(std::vector<MenuItem*>::iterator i = items.begin();
219 i != items.end(); ++i)
225 if (previous == this)
232 menuaction = MENU_ACTION_NONE;
233 delete_character = 0;
234 mn_input_char = '\0';
236 pos_x = SCREEN_WIDTH/2;
237 pos_y = SCREEN_HEIGHT/2;
241 effect_progress = 0.0f;
242 effect_start_time = 0.0f;
244 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
245 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
246 back.reset(new Surface("images/engine/menu/arrow-back.png"));
247 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
248 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
252 Menu::set_pos(float x, float y, float rw, float rh)
254 pos_x = x + get_width() * rw;
255 pos_y = y + get_height() * rh;
258 /* Add an item to a menu */
260 Menu::additem(MenuItem* item)
262 items.push_back(item);
264 /* If a new menu is being built, the active item shouldn't be set to
265 * something that isnt selectable. Set the active_item to the first
266 * selectable item added
268 if (active_item == -1
269 && item->kind != MN_HL
270 && item->kind != MN_LABEL
271 && item->kind != MN_DEACTIVE) {
272 active_item = items.size() - 1;
279 MenuItem* item = new MenuItem(MN_HL);
285 Menu::add_label(const std::string& text)
287 MenuItem* item = new MenuItem(MN_LABEL);
294 Menu::add_controlfield(int id, const std::string& text,
295 const std::string& mapping)
297 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
298 item->change_text(text);
299 item->change_input(mapping);
305 Menu::add_entry(int id, const std::string& text)
307 MenuItem* item = new MenuItem(MN_ACTION, id);
314 Menu::add_deactive(int id, const std::string& text)
316 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
323 Menu::add_toggle(int id, const std::string& text, bool toogled)
325 MenuItem* item = new MenuItem(MN_TOGGLE, id);
327 item->toggled = toogled;
333 Menu::add_string_select(int id, const std::string& text)
335 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
342 Menu::add_back(const std::string& text)
344 MenuItem* item = new MenuItem(MN_BACK);
351 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
353 MenuItem* item = new MenuItem(MN_GOTO, id);
355 item->target_menu = submenu;
363 for(std::vector<MenuItem*>::iterator i = items.begin();
364 i != items.end(); ++i) {
371 /* Process actions done on the menu */
375 int menu_height = get_height();
376 if (menu_height > SCREEN_HEIGHT)
378 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
379 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
382 effect_progress = (real_time - effect_start_time) * 6.0f;
384 if(effect_progress >= 1.0f) {
385 effect_progress = 1.0f;
386 } else if (effect_progress <= 0.0f) {
387 effect_progress = 0.0f;
390 /** check main input controller... */
391 if(main_controller->pressed(Controller::UP)) {
392 menuaction = MENU_ACTION_UP;
393 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
395 if(main_controller->hold(Controller::UP) &&
396 menu_repeat_time != 0 && real_time > menu_repeat_time) {
397 menuaction = MENU_ACTION_UP;
398 menu_repeat_time = real_time + MENU_REPEAT_RATE;
401 if(main_controller->pressed(Controller::DOWN)) {
402 menuaction = MENU_ACTION_DOWN;
403 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
405 if(main_controller->hold(Controller::DOWN) &&
406 menu_repeat_time != 0 && real_time > menu_repeat_time) {
407 menuaction = MENU_ACTION_DOWN;
408 menu_repeat_time = real_time + MENU_REPEAT_RATE;
411 if(main_controller->pressed(Controller::LEFT)) {
412 menuaction = MENU_ACTION_LEFT;
413 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
415 if(main_controller->hold(Controller::LEFT) &&
416 menu_repeat_time != 0 && real_time > menu_repeat_time) {
417 menuaction = MENU_ACTION_LEFT;
418 menu_repeat_time = real_time + MENU_REPEAT_RATE;
421 if(main_controller->pressed(Controller::RIGHT)) {
422 menuaction = MENU_ACTION_RIGHT;
423 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
425 if(main_controller->hold(Controller::RIGHT) &&
426 menu_repeat_time != 0 && real_time > menu_repeat_time) {
427 menuaction = MENU_ACTION_RIGHT;
428 menu_repeat_time = real_time + MENU_REPEAT_RATE;
431 if(main_controller->pressed(Controller::ACTION)
432 || main_controller->pressed(Controller::MENU_SELECT)) {
433 menuaction = MENU_ACTION_HIT;
435 if(main_controller->pressed(Controller::PAUSE_MENU)) {
436 menuaction = MENU_ACTION_BACK;
440 if(items.size() == 0)
443 int last_active_item = active_item;
450 active_item = int(items.size())-1;
451 } while ((items[active_item]->kind == MN_HL
452 || items[active_item]->kind == MN_LABEL
453 || items[active_item]->kind == MN_DEACTIVE)
454 && (active_item != last_active_item));
458 case MENU_ACTION_DOWN:
460 if(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_LEFT:
472 if(items[active_item]->kind == MN_STRINGSELECT) {
473 if(items[active_item]->selected > 0)
474 items[active_item]->selected--;
476 items[active_item]->selected = items[active_item]->list.size()-1;
480 case MENU_ACTION_RIGHT:
481 if(items[active_item]->kind == MN_STRINGSELECT) {
482 if(items[active_item]->selected+1 < items[active_item]->list.size())
483 items[active_item]->selected++;
485 items[active_item]->selected = 0;
489 case MENU_ACTION_HIT: {
490 hit_item = active_item;
491 switch (items[active_item]->kind) {
493 assert(items[active_item]->target_menu != 0);
494 Menu::push_current(items[active_item]->target_menu);
498 items[active_item]->toggled = !items[active_item]->toggled;
499 menu_action(items[active_item]);
502 case MN_CONTROLFIELD:
503 menu_action(items[active_item]);
507 menu_action(items[active_item]);
512 menuaction = MENU_ACTION_DOWN;
525 case MENU_ACTION_REMOVE:
526 if(items[active_item]->kind == MN_TEXTFIELD
527 || items[active_item]->kind == MN_NUMFIELD)
529 if(!items[active_item]->input.empty())
531 int i = items[active_item]->input.size();
533 while(delete_character > 0) /* remove charactes */
535 items[active_item]->input.resize(i-1);
542 case MENU_ACTION_INPUT:
543 if(items[active_item]->kind == MN_TEXTFIELD
544 || (items[active_item]->kind == MN_NUMFIELD
545 && mn_input_char >= '0' && mn_input_char <= '9'))
547 items[active_item]->input.push_back(mn_input_char);
551 case MENU_ACTION_BACK:
555 case MENU_ACTION_NONE:
558 menuaction = MENU_ACTION_NONE;
560 assert(active_item < int(items.size()));
567 return items[hit_item]->id;
573 Menu::menu_action(MenuItem* )
577 Menu::draw_item(DrawingContext& context, int index)
579 float menu_height = get_height();
580 float menu_width = get_width();
582 MenuItem& pitem = *(items[index]);
584 Font* text_font = default_font;
586 float y_pos = pos_y + 24*index - menu_height/2 + 12;
588 int text_width = int(text_font->get_text_width(pitem.text));
589 int input_width = int(text_font->get_text_width(pitem.input) + 10);
592 float left = pos_x - menu_width/2 + 16;
593 float right = pos_x + menu_width/2 - 16;
595 if(pitem.list.size() > 0) {
596 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
600 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
602 if(index == active_item)
605 text_font = active_font;
608 if(active_item == index)
610 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
611 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
612 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
613 Color(1.0f, 1.0f, 1.0f, blink),
616 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
617 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
618 Color(1.0f, 1.0f, 1.0f, 0.5f),
627 context.draw_text(deactive_font, pitem.text,
628 Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
629 ALIGN_CENTER, LAYER_GUI);
636 float x = pos_x - menu_width/2;
637 float y = y_pos - 12;
638 /* Draw a horizontal line with a little 3d effect */
639 context.draw_filled_rect(Vector(x, y + 6),
640 Vector(menu_width, 4),
641 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
642 context.draw_filled_rect(Vector(x, y + 6),
643 Vector(menu_width, 2),
644 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
649 context.draw_text(label_font, pitem.text,
650 Vector(pos_x, y_pos - int(label_font->get_height()/2)),
651 ALIGN_CENTER, LAYER_GUI);
656 case MN_CONTROLFIELD:
658 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
660 if(active_item == index)
661 context.draw_text(field_font,
662 pitem.get_input_with_symbol(true),
663 Vector(right, y_pos - int(field_font->get_height()/2)),
664 ALIGN_RIGHT, LAYER_GUI);
666 context.draw_text(field_font,
667 pitem.get_input_with_symbol(false),
668 Vector(right, y_pos - int(field_font->get_height()/2)),
669 ALIGN_RIGHT, LAYER_GUI);
672 context.draw_text(field_font, pitem.input,
673 Vector(right, y_pos - int(field_font->get_height()/2)),
674 ALIGN_RIGHT, LAYER_GUI);
676 context.draw_text(text_font, pitem.text,
677 Vector(left, y_pos - int(text_font->get_height()/2)),
678 ALIGN_LEFT, LAYER_GUI);
681 case MN_STRINGSELECT:
683 float roff = arrow_left->get_width();
685 context.draw_text(text_font, pitem.text,
686 Vector(left, y_pos - int(text_font->get_height()/2)),
687 ALIGN_LEFT, LAYER_GUI);
690 context.draw_surface(arrow_left.get(),
691 Vector(right - list_width - roff - roff, y_pos - 8),
693 context.draw_surface(arrow_right.get(),
694 Vector(right - roff, y_pos - 8),
696 context.draw_text(field_font, pitem.list[pitem.selected],
697 Vector(right - roff, y_pos - int(text_font->get_height()/2)),
698 ALIGN_RIGHT, LAYER_GUI);
703 context.draw_text(text_font, pitem.text,
704 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
705 ALIGN_CENTER, LAYER_GUI);
706 context.draw_surface(back.get(),
707 Vector(x_pos + text_width/2 + 16, y_pos - 8),
714 context.draw_text(text_font, pitem.text,
715 Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
716 ALIGN_LEFT, LAYER_GUI);
719 context.draw_surface(checkbox_checked.get(),
720 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
723 context.draw_surface(checkbox.get(),
724 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
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);
735 context.draw_text(text_font, pitem.text,
736 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
737 ALIGN_CENTER, LAYER_GUI);
743 Menu::get_width() const
745 /* The width of the menu has to be more than the width of the text
746 with the most characters */
747 float menu_width = 0;
748 for(unsigned int i = 0; i < items.size(); ++i)
750 Font* font = default_font;
751 if(items[i]->kind == MN_LABEL)
754 float w = font->get_text_width(items[i]->text) +
755 label_font->get_text_width(items[i]->input) + 16;
756 if(items[i]->kind == MN_TOGGLE)
763 return menu_width + 24;
767 Menu::get_height() const
769 return items.size() * 24;
772 /* Draw the current menu. */
774 Menu::draw(DrawingContext& context)
776 if(MouseCursor::current()) {
777 MouseCursor::current()->draw(context);
780 float menu_width = get_width();
781 float menu_height = get_height();
783 if (effect_progress != 1.0f)
787 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
788 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
789 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
793 menu_width *= effect_progress;
794 menu_height *= effect_progress;
798 /* Draw a transparent background */
799 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
800 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
801 Color(0.2f, 0.3f, 0.4f, 0.8f),
805 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
806 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
807 Color(0.6f, 0.7f, 0.8f, 0.5f),
811 if (!items[active_item]->help.empty())
813 int text_width = default_font->get_text_width(items[active_item]->help);
814 int text_height = default_font->get_text_height(items[active_item]->help);
816 Rect text_rect(pos_x - text_width/2 - 8,
817 SCREEN_HEIGHT - 48 - text_height/2 - 4,
818 pos_x + text_width/2 + 8,
819 SCREEN_HEIGHT - 48 + text_height/2 + 4);
821 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
822 text_rect.p2 + Vector(4,4)),
823 Color(0.2f, 0.3f, 0.4f, 0.8f),
827 context.draw_filled_rect(text_rect,
828 Color(0.6f, 0.7f, 0.8f, 0.5f),
832 context.draw_text(default_font, items[active_item]->help,
833 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
834 ALIGN_CENTER, LAYER_GUI);
837 if (effect_progress == 1.0f)
838 for(unsigned int i = 0; i < items.size(); ++i)
840 draw_item(context, i);
845 Menu::get_item_by_id(int id)
847 for(std::vector<MenuItem*>::iterator i = items.begin();
848 i != items.end(); ++i) {
849 MenuItem& item = **i;
855 throw std::runtime_error("MenuItem not found");
859 Menu::get_item_by_id(int id) const
861 for(std::vector<MenuItem*>::const_iterator i = items.begin();
862 i != items.end(); ++i) {
863 const MenuItem& item = **i;
869 throw std::runtime_error("MenuItem not found");
872 int Menu::get_active_item_id()
874 return items[active_item]->id;
878 Menu::is_toggled(int id) const
880 return get_item_by_id(id).toggled;
884 Menu::get_parent() const
886 if (last_menus.empty())
889 return last_menus.back();
892 /* Check for menu event */
894 Menu::event(const SDL_Event& event)
896 if(effect_progress != 1.0f)
900 case SDL_MOUSEBUTTONDOWN:
902 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
903 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
905 if(x > pos_x - get_width()/2 &&
906 x < pos_x + get_width()/2 &&
907 y > pos_y - get_height()/2 &&
908 y < pos_y + get_height()/2)
910 menuaction = MENU_ACTION_HIT;
915 case SDL_MOUSEMOTION:
917 float x = event.motion.x * SCREEN_WIDTH/screen->w;
918 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
920 if(x > pos_x - get_width()/2 &&
921 x < pos_x + get_width()/2 &&
922 y > pos_y - get_height()/2 &&
923 y < pos_y + get_height()/2)
926 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
928 /* only change the mouse focus to a selectable item */
929 if ((items[new_active_item]->kind != MN_HL)
930 && (items[new_active_item]->kind != MN_LABEL)
931 && (items[new_active_item]->kind != MN_DEACTIVE))
932 active_item = new_active_item;
934 if(MouseCursor::current())
935 MouseCursor::current()->set_state(MC_LINK);
939 if(MouseCursor::current())
940 MouseCursor::current()->set_state(MC_NORMAL);
951 Menu::set_active_item(int id)
953 for(size_t i = 0; i < items.size(); ++i) {
954 MenuItem* item = items[i];