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()));
607 return items[hit_item]->id;
613 Menu::menu_action(MenuItem* )
617 Menu::draw_item(DrawingContext& context, int index)
619 float menu_height = get_height();
620 float menu_width = get_width();
622 MenuItem& pitem = *(items[index]);
624 Color text_color = default_color;
626 float y_pos = pos_y + 24*index - menu_height/2 + 12;
628 int text_width = int(normal_font->get_text_width(pitem.text));
629 int input_width = int(normal_font->get_text_width(pitem.input) + 10);
632 float left = pos_x - menu_width/2 + 16;
633 float right = pos_x + menu_width/2 - 16;
635 if(pitem.list.size() > 0) {
636 list_width = (int) normal_font->get_text_width(pitem.list[pitem.selected]);
640 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
642 if(index == active_item)
645 text_color = active_color;
648 if(active_item == index)
650 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
651 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
652 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
653 Color(1.0f, 1.0f, 1.0f, blink),
656 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
657 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
658 Color(1.0f, 1.0f, 1.0f, 0.5f),
667 context.draw_text(normal_font, pitem.text,
668 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
669 ALIGN_CENTER, LAYER_GUI, inactive_color);
676 float x = pos_x - menu_width/2;
677 float y = y_pos - 12;
678 /* Draw a horizontal line with a little 3d effect */
679 context.draw_filled_rect(Vector(x, y + 6),
680 Vector(menu_width, 4),
681 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
682 context.draw_filled_rect(Vector(x, y + 6),
683 Vector(menu_width, 2),
684 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
689 context.draw_text(big_font, pitem.text,
690 Vector(pos_x, y_pos - int(big_font->get_height()/2)),
691 ALIGN_CENTER, LAYER_GUI, label_color);
696 case MN_CONTROLFIELD:
698 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
700 if(active_item == index)
701 context.draw_text(normal_font,
702 pitem.get_input_with_symbol(true),
703 Vector(right, y_pos - int(normal_font->get_height()/2)),
704 ALIGN_RIGHT, LAYER_GUI, field_color);
706 context.draw_text(normal_font,
707 pitem.get_input_with_symbol(false),
708 Vector(right, y_pos - int(normal_font->get_height()/2)),
709 ALIGN_RIGHT, LAYER_GUI, field_color);
712 context.draw_text(normal_font, pitem.input,
713 Vector(right, y_pos - int(normal_font->get_height()/2)),
714 ALIGN_RIGHT, LAYER_GUI, field_color);
716 context.draw_text(normal_font, pitem.text,
717 Vector(left, y_pos - int(normal_font->get_height()/2)),
718 ALIGN_LEFT, LAYER_GUI, text_color);
721 case MN_STRINGSELECT:
723 float roff = arrow_left->get_width();
725 context.draw_text(normal_font, pitem.text,
726 Vector(left, y_pos - int(normal_font->get_height()/2)),
727 ALIGN_LEFT, LAYER_GUI, text_color);
730 context.draw_surface(arrow_left.get(),
731 Vector(right - list_width - roff - roff, y_pos - 8),
733 context.draw_surface(arrow_right.get(),
734 Vector(right - roff, y_pos - 8),
736 context.draw_text(normal_font, pitem.list[pitem.selected],
737 Vector(right - roff, y_pos - int(normal_font->get_height()/2)),
738 ALIGN_RIGHT, LAYER_GUI, text_color);
743 context.draw_text(normal_font, pitem.text,
744 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
745 ALIGN_CENTER, LAYER_GUI, text_color);
746 context.draw_surface(back.get(),
747 Vector(x_pos + text_width/2 + 16, y_pos - 8),
754 context.draw_text(normal_font, pitem.text,
755 Vector(pos_x - menu_width/2 + 16, y_pos - (normal_font->get_height()/2)),
756 ALIGN_LEFT, LAYER_GUI, text_color);
759 context.draw_surface(checkbox_checked.get(),
760 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
763 context.draw_surface(checkbox.get(),
764 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
769 context.draw_text(normal_font, pitem.text,
770 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
771 ALIGN_CENTER, LAYER_GUI, text_color);
775 context.draw_text(normal_font, pitem.text,
776 Vector(pos_x, y_pos - int(normal_font->get_height()/2)),
777 ALIGN_CENTER, LAYER_GUI, text_color);
783 Menu::get_width() const
785 /* The width of the menu has to be more than the width of the text
786 with the most characters */
787 float menu_width = 0;
788 for(unsigned int i = 0; i < items.size(); ++i)
790 Font* font = normal_font;
791 if(items[i]->kind == MN_LABEL)
794 float w = font->get_text_width(items[i]->text) +
795 big_font->get_text_width(items[i]->input) + 16;
796 if(items[i]->kind == MN_TOGGLE)
803 return menu_width + 24;
807 Menu::get_height() const
809 return items.size() * 24;
812 /* Draw the current menu. */
814 Menu::draw(DrawingContext& context)
816 if(MouseCursor::current()) {
817 MouseCursor::current()->draw(context);
820 float menu_width = get_width();
821 float menu_height = get_height();
823 if (effect_progress != 1.0f)
827 menu_width = (current_->get_width() * (1.0f - effect_progress));
828 menu_height = (current_->get_height() * (1.0f - effect_progress));
830 else if (Menu::previous)
832 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
833 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
834 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
838 menu_width *= effect_progress;
839 menu_height *= effect_progress;
843 /* Draw a transparent background */
844 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
845 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
846 Color(0.2f, 0.3f, 0.4f, 0.8f),
850 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
851 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
852 Color(0.6f, 0.7f, 0.8f, 0.5f),
856 if (!items[active_item]->help.empty())
858 int text_width = (int) normal_font->get_text_width(items[active_item]->help);
859 int text_height = (int) normal_font->get_text_height(items[active_item]->help);
861 Rect text_rect(pos_x - text_width/2 - 8,
862 SCREEN_HEIGHT - 48 - text_height/2 - 4,
863 pos_x + text_width/2 + 8,
864 SCREEN_HEIGHT - 48 + text_height/2 + 4);
866 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
867 text_rect.p2 + Vector(4,4)),
868 Color(0.2f, 0.3f, 0.4f, 0.8f),
872 context.draw_filled_rect(text_rect,
873 Color(0.6f, 0.7f, 0.8f, 0.5f),
877 context.draw_text(normal_font, items[active_item]->help,
878 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
879 ALIGN_CENTER, LAYER_GUI);
882 if (effect_progress == 1.0f)
883 for(unsigned int i = 0; i < items.size(); ++i)
885 draw_item(context, i);
890 Menu::get_item_by_id(int id)
892 for(std::vector<MenuItem*>::iterator i = items.begin();
893 i != items.end(); ++i) {
894 MenuItem& item = **i;
900 throw std::runtime_error("MenuItem not found");
904 Menu::get_item_by_id(int id) const
906 for(std::vector<MenuItem*>::const_iterator i = items.begin();
907 i != items.end(); ++i) {
908 const MenuItem& item = **i;
914 throw std::runtime_error("MenuItem not found");
917 int Menu::get_active_item_id()
919 return items[active_item]->id;
923 Menu::is_toggled(int id) const
925 return get_item_by_id(id).toggled;
929 Menu::set_toggled(int id, bool toggled)
931 get_item_by_id(id).toggled = toggled;
935 Menu::get_parent() const
937 if (last_menus.empty())
940 return last_menus.back();
943 /* Check for menu event */
945 Menu::event(const SDL_Event& event)
947 if(effect_progress != 1.0f)
951 case SDL_MOUSEBUTTONDOWN:
953 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
954 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
956 if(x > pos_x - get_width()/2 &&
957 x < pos_x + get_width()/2 &&
958 y > pos_y - get_height()/2 &&
959 y < pos_y + get_height()/2)
961 menuaction = MENU_ACTION_HIT;
966 case SDL_MOUSEMOTION:
968 float x = event.motion.x * SCREEN_WIDTH/screen->w;
969 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
971 if(x > pos_x - get_width()/2 &&
972 x < pos_x + get_width()/2 &&
973 y > pos_y - get_height()/2 &&
974 y < pos_y + get_height()/2)
977 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
979 /* only change the mouse focus to a selectable item */
980 if ((items[new_active_item]->kind != MN_HL)
981 && (items[new_active_item]->kind != MN_LABEL)
982 && (items[new_active_item]->kind != MN_INACTIVE))
983 active_item = new_active_item;
985 if(MouseCursor::current())
986 MouseCursor::current()->set_state(MC_LINK);
990 if(MouseCursor::current())
991 MouseCursor::current()->set_state(MC_NORMAL);
1002 Menu::set_active_item(int id)
1004 for(size_t i = 0; i < items.size(); ++i) {
1005 MenuItem* item = items[i];
1006 if(item->id == id) {