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::inactive_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_inactive(-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)
152 //last_menus.clear();
153 if (current_ && current_->close == true)
159 menu->effect_start_time = real_time;
160 menu->effect_progress = 0.0f;
164 current_->effect_start_time = real_time;
165 current_->effect_progress = 0.0f;
166 current_->close = true;
169 // just to be sure...
170 main_controller->reset();
177 current_->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
179 for(std::list<Menu*>::iterator i = all_menus.begin(); i != all_menus.end(); ++i)
181 // FIXME: This is of course not quite right, since it ignores any previous set_pos() calls
182 (*i)->set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
186 MenuItem::MenuItem(MenuItemKind _kind, int _id)
187 : kind(_kind) , id(_id)
195 MenuItem::change_text(const std::string& text_)
201 MenuItem::change_input(const std::string& text_)
207 MenuItem::set_help(const std::string& help_text)
209 std::string overflow;
210 help = Menu::default_font->wrap_to_width(help_text, 600, &overflow);
211 while (!overflow.empty())
214 help += Menu::default_font->wrap_to_width(overflow, 600, &overflow);
218 std::string MenuItem::get_input_with_symbol(bool active_item)
221 input_flickering = true;
223 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
228 snprintf(str, sizeof(str), "%s ",input.c_str());
230 snprintf(str, sizeof(str), "%s_",input.c_str());
232 std::string string = str;
239 all_menus.remove(this);
241 for(std::vector<MenuItem*>::iterator i = items.begin();
242 i != items.end(); ++i)
248 if (previous == this)
255 all_menus.push_back(this);
258 menuaction = MENU_ACTION_NONE;
259 delete_character = 0;
260 mn_input_char = '\0';
262 pos_x = SCREEN_WIDTH/2;
263 pos_y = SCREEN_HEIGHT/2;
267 effect_progress = 0.0f;
268 effect_start_time = 0.0f;
270 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
271 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
272 back.reset(new Surface("images/engine/menu/arrow-back.png"));
273 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
274 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
278 Menu::set_pos(float x, float y, float rw, float rh)
280 pos_x = x + get_width() * rw;
281 pos_y = y + get_height() * rh;
284 /* Add an item to a menu */
286 Menu::additem(MenuItem* item)
288 items.push_back(item);
290 /* If a new menu is being built, the active item shouldn't be set to
291 * something that isn't selectable. Set the active_item to the first
292 * selectable item added.
294 if (active_item == -1
295 && item->kind != MN_HL
296 && item->kind != MN_LABEL
297 && item->kind != MN_INACTIVE) {
298 active_item = items.size() - 1;
305 MenuItem* item = new MenuItem(MN_HL);
311 Menu::add_label(const std::string& text)
313 MenuItem* item = new MenuItem(MN_LABEL);
320 Menu::add_controlfield(int id, const std::string& text,
321 const std::string& mapping)
323 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
324 item->change_text(text);
325 item->change_input(mapping);
331 Menu::add_entry(int id, const std::string& text)
333 MenuItem* item = new MenuItem(MN_ACTION, id);
340 Menu::add_inactive(int id, const std::string& text)
342 MenuItem* item = new MenuItem(MN_INACTIVE, id);
349 Menu::add_toggle(int id, const std::string& text, bool toogled)
351 MenuItem* item = new MenuItem(MN_TOGGLE, id);
353 item->toggled = toogled;
359 Menu::add_string_select(int id, const std::string& text)
361 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
368 Menu::add_back(const std::string& text)
370 MenuItem* item = new MenuItem(MN_BACK);
377 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
379 MenuItem* item = new MenuItem(MN_GOTO, id);
381 item->target_menu = submenu;
389 for(std::vector<MenuItem*>::iterator i = items.begin();
390 i != items.end(); ++i) {
397 /* Process actions done on the menu */
401 int menu_height = (int) get_height();
402 if (menu_height > SCREEN_HEIGHT)
404 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
405 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
408 effect_progress = (real_time - effect_start_time) * 6.0f;
410 if(effect_progress >= 1.0f) {
411 effect_progress = 1.0f;
418 else if (effect_progress <= 0.0f) {
419 effect_progress = 0.0f;
422 /** check main input controller... */
423 if(main_controller->pressed(Controller::UP)) {
424 menuaction = MENU_ACTION_UP;
425 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
427 if(main_controller->hold(Controller::UP) &&
428 menu_repeat_time != 0 && real_time > menu_repeat_time) {
429 menuaction = MENU_ACTION_UP;
430 menu_repeat_time = real_time + MENU_REPEAT_RATE;
433 if(main_controller->pressed(Controller::DOWN)) {
434 menuaction = MENU_ACTION_DOWN;
435 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
437 if(main_controller->hold(Controller::DOWN) &&
438 menu_repeat_time != 0 && real_time > menu_repeat_time) {
439 menuaction = MENU_ACTION_DOWN;
440 menu_repeat_time = real_time + MENU_REPEAT_RATE;
443 if(main_controller->pressed(Controller::LEFT)) {
444 menuaction = MENU_ACTION_LEFT;
445 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
447 if(main_controller->hold(Controller::LEFT) &&
448 menu_repeat_time != 0 && real_time > menu_repeat_time) {
449 menuaction = MENU_ACTION_LEFT;
450 menu_repeat_time = real_time + MENU_REPEAT_RATE;
453 if(main_controller->pressed(Controller::RIGHT)) {
454 menuaction = MENU_ACTION_RIGHT;
455 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
457 if(main_controller->hold(Controller::RIGHT) &&
458 menu_repeat_time != 0 && real_time > menu_repeat_time) {
459 menuaction = MENU_ACTION_RIGHT;
460 menu_repeat_time = real_time + MENU_REPEAT_RATE;
463 if(main_controller->pressed(Controller::ACTION)
464 || main_controller->pressed(Controller::MENU_SELECT)) {
465 menuaction = MENU_ACTION_HIT;
467 if(main_controller->pressed(Controller::PAUSE_MENU)) {
468 menuaction = MENU_ACTION_BACK;
472 if(items.size() == 0)
475 int last_active_item = active_item;
482 active_item = int(items.size())-1;
483 } while ((items[active_item]->kind == MN_HL
484 || items[active_item]->kind == MN_LABEL
485 || items[active_item]->kind == MN_INACTIVE)
486 && (active_item != last_active_item));
490 case MENU_ACTION_DOWN:
492 if(active_item < int(items.size())-1 )
496 } while ((items[active_item]->kind == MN_HL
497 || items[active_item]->kind == MN_LABEL
498 || items[active_item]->kind == MN_INACTIVE)
499 && (active_item != last_active_item));
503 case MENU_ACTION_LEFT:
504 if(items[active_item]->kind == MN_STRINGSELECT) {
505 if(items[active_item]->selected > 0)
506 items[active_item]->selected--;
508 items[active_item]->selected = items[active_item]->list.size()-1;
510 menu_action(items[active_item]);
514 case MENU_ACTION_RIGHT:
515 if(items[active_item]->kind == MN_STRINGSELECT) {
516 if(items[active_item]->selected+1 < items[active_item]->list.size())
517 items[active_item]->selected++;
519 items[active_item]->selected = 0;
521 menu_action(items[active_item]);
525 case MENU_ACTION_HIT: {
526 hit_item = active_item;
527 switch (items[active_item]->kind) {
529 assert(items[active_item]->target_menu != 0);
530 Menu::push_current(items[active_item]->target_menu);
534 items[active_item]->toggled = !items[active_item]->toggled;
535 menu_action(items[active_item]);
538 case MN_CONTROLFIELD:
539 menu_action(items[active_item]);
543 menu_action(items[active_item]);
546 case MN_STRINGSELECT:
547 if(items[active_item]->selected+1 < items[active_item]->list.size())
548 items[active_item]->selected++;
550 items[active_item]->selected = 0;
552 menu_action(items[active_item]);
557 menuaction = MENU_ACTION_DOWN;
570 case MENU_ACTION_REMOVE:
571 if(items[active_item]->kind == MN_TEXTFIELD
572 || items[active_item]->kind == MN_NUMFIELD)
574 if(!items[active_item]->input.empty())
576 int i = items[active_item]->input.size();
578 while(delete_character > 0) /* remove characters */
580 items[active_item]->input.resize(i-1);
587 case MENU_ACTION_INPUT:
588 if(items[active_item]->kind == MN_TEXTFIELD
589 || (items[active_item]->kind == MN_NUMFIELD
590 && mn_input_char >= '0' && mn_input_char <= '9'))
592 items[active_item]->input.push_back(mn_input_char);
596 case MENU_ACTION_BACK:
600 case MENU_ACTION_NONE:
603 menuaction = MENU_ACTION_NONE;
605 assert(active_item < int(items.size()));
612 return items[hit_item]->id;
618 Menu::menu_action(MenuItem* )
622 Menu::draw_item(DrawingContext& context, int index)
624 float menu_height = get_height();
625 float menu_width = get_width();
627 MenuItem& pitem = *(items[index]);
629 Font* text_font = default_font;
631 float y_pos = pos_y + 24*index - menu_height/2 + 12;
633 int text_width = int(text_font->get_text_width(pitem.text));
634 int input_width = int(text_font->get_text_width(pitem.input) + 10);
637 float left = pos_x - menu_width/2 + 16;
638 float right = pos_x + menu_width/2 - 16;
640 if(pitem.list.size() > 0) {
641 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
645 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
647 if(index == active_item)
650 text_font = active_font;
653 if(active_item == index)
655 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
656 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
657 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
658 Color(1.0f, 1.0f, 1.0f, blink),
661 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
662 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
663 Color(1.0f, 1.0f, 1.0f, 0.5f),
672 context.draw_text(inactive_font, pitem.text,
673 Vector(pos_x, y_pos - int(inactive_font->get_height()/2)),
674 ALIGN_CENTER, LAYER_GUI);
681 float x = pos_x - menu_width/2;
682 float y = y_pos - 12;
683 /* Draw a horizontal line with a little 3d effect */
684 context.draw_filled_rect(Vector(x, y + 6),
685 Vector(menu_width, 4),
686 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
687 context.draw_filled_rect(Vector(x, y + 6),
688 Vector(menu_width, 2),
689 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
694 context.draw_text(label_font, pitem.text,
695 Vector(pos_x, y_pos - int(label_font->get_height()/2)),
696 ALIGN_CENTER, LAYER_GUI);
701 case MN_CONTROLFIELD:
703 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
705 if(active_item == index)
706 context.draw_text(field_font,
707 pitem.get_input_with_symbol(true),
708 Vector(right, y_pos - int(field_font->get_height()/2)),
709 ALIGN_RIGHT, LAYER_GUI);
711 context.draw_text(field_font,
712 pitem.get_input_with_symbol(false),
713 Vector(right, y_pos - int(field_font->get_height()/2)),
714 ALIGN_RIGHT, LAYER_GUI);
717 context.draw_text(field_font, pitem.input,
718 Vector(right, y_pos - int(field_font->get_height()/2)),
719 ALIGN_RIGHT, LAYER_GUI);
721 context.draw_text(text_font, pitem.text,
722 Vector(left, y_pos - int(text_font->get_height()/2)),
723 ALIGN_LEFT, LAYER_GUI);
726 case MN_STRINGSELECT:
728 float roff = arrow_left->get_width();
730 context.draw_text(text_font, pitem.text,
731 Vector(left, y_pos - int(text_font->get_height()/2)),
732 ALIGN_LEFT, LAYER_GUI);
735 context.draw_surface(arrow_left.get(),
736 Vector(right - list_width - roff - roff, y_pos - 8),
738 context.draw_surface(arrow_right.get(),
739 Vector(right - roff, y_pos - 8),
741 context.draw_text(field_font, pitem.list[pitem.selected],
742 Vector(right - roff, y_pos - int(text_font->get_height()/2)),
743 ALIGN_RIGHT, LAYER_GUI);
748 context.draw_text(text_font, pitem.text,
749 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
750 ALIGN_CENTER, LAYER_GUI);
751 context.draw_surface(back.get(),
752 Vector(x_pos + text_width/2 + 16, y_pos - 8),
759 context.draw_text(text_font, pitem.text,
760 Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
761 ALIGN_LEFT, LAYER_GUI);
764 context.draw_surface(checkbox_checked.get(),
765 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
768 context.draw_surface(checkbox.get(),
769 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
774 context.draw_text(text_font, pitem.text,
775 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
776 ALIGN_CENTER, LAYER_GUI);
780 context.draw_text(text_font, pitem.text,
781 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
782 ALIGN_CENTER, LAYER_GUI);
788 Menu::get_width() const
790 /* The width of the menu has to be more than the width of the text
791 with the most characters */
792 float menu_width = 0;
793 for(unsigned int i = 0; i < items.size(); ++i)
795 Font* font = default_font;
796 if(items[i]->kind == MN_LABEL)
799 float w = font->get_text_width(items[i]->text) +
800 label_font->get_text_width(items[i]->input) + 16;
801 if(items[i]->kind == MN_TOGGLE)
808 return menu_width + 24;
812 Menu::get_height() const
814 return items.size() * 24;
817 /* Draw the current menu. */
819 Menu::draw(DrawingContext& context)
821 if(MouseCursor::current()) {
822 MouseCursor::current()->draw(context);
825 float menu_width = get_width();
826 float menu_height = get_height();
828 if (effect_progress != 1.0f)
832 menu_width = (current_->get_width() * (1.0f - effect_progress));
833 menu_height = (current_->get_height() * (1.0f - effect_progress));
835 else if (Menu::previous)
837 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
838 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
839 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
843 menu_width *= effect_progress;
844 menu_height *= effect_progress;
848 /* Draw a transparent background */
849 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
850 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
851 Color(0.2f, 0.3f, 0.4f, 0.8f),
855 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
856 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
857 Color(0.6f, 0.7f, 0.8f, 0.5f),
861 if (!items[active_item]->help.empty())
863 int text_width = (int) default_font->get_text_width(items[active_item]->help);
864 int text_height = (int) default_font->get_text_height(items[active_item]->help);
866 Rect text_rect(pos_x - text_width/2 - 8,
867 SCREEN_HEIGHT - 48 - text_height/2 - 4,
868 pos_x + text_width/2 + 8,
869 SCREEN_HEIGHT - 48 + text_height/2 + 4);
871 context.draw_filled_rect(Rect(text_rect.p1 - Vector(4,4),
872 text_rect.p2 + Vector(4,4)),
873 Color(0.2f, 0.3f, 0.4f, 0.8f),
877 context.draw_filled_rect(text_rect,
878 Color(0.6f, 0.7f, 0.8f, 0.5f),
882 context.draw_text(default_font, items[active_item]->help,
883 Vector(pos_x, SCREEN_HEIGHT - 48 - text_height/2),
884 ALIGN_CENTER, LAYER_GUI);
887 if (effect_progress == 1.0f)
888 for(unsigned int i = 0; i < items.size(); ++i)
890 draw_item(context, i);
895 Menu::get_item_by_id(int id)
897 for(std::vector<MenuItem*>::iterator i = items.begin();
898 i != items.end(); ++i) {
899 MenuItem& item = **i;
905 throw std::runtime_error("MenuItem not found");
909 Menu::get_item_by_id(int id) const
911 for(std::vector<MenuItem*>::const_iterator i = items.begin();
912 i != items.end(); ++i) {
913 const MenuItem& item = **i;
919 throw std::runtime_error("MenuItem not found");
922 int Menu::get_active_item_id()
924 return items[active_item]->id;
928 Menu::is_toggled(int id) const
930 return get_item_by_id(id).toggled;
934 Menu::set_toggled(int id, bool toggled)
936 get_item_by_id(id).toggled = toggled;
940 Menu::get_parent() const
942 if (last_menus.empty())
945 return last_menus.back();
948 /* Check for menu event */
950 Menu::event(const SDL_Event& event)
952 if(effect_progress != 1.0f)
956 case SDL_MOUSEBUTTONDOWN:
958 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
959 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
961 if(x > pos_x - get_width()/2 &&
962 x < pos_x + get_width()/2 &&
963 y > pos_y - get_height()/2 &&
964 y < pos_y + get_height()/2)
966 menuaction = MENU_ACTION_HIT;
971 case SDL_MOUSEMOTION:
973 float x = event.motion.x * SCREEN_WIDTH/screen->w;
974 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
976 if(x > pos_x - get_width()/2 &&
977 x < pos_x + get_width()/2 &&
978 y > pos_y - get_height()/2 &&
979 y < pos_y + get_height()/2)
982 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
984 /* only change the mouse focus to a selectable item */
985 if ((items[new_active_item]->kind != MN_HL)
986 && (items[new_active_item]->kind != MN_LABEL)
987 && (items[new_active_item]->kind != MN_INACTIVE))
988 active_item = new_active_item;
990 if(MouseCursor::current())
991 MouseCursor::current()->set_state(MC_LINK);
995 if(MouseCursor::current())
996 MouseCursor::current()->set_state(MC_NORMAL);
1007 Menu::set_active_item(int id)
1009 for(size_t i = 0; i < items.size(); ++i) {
1010 MenuItem* item = items[i];
1011 if(item->id == id) {