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.2f;
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_)
185 std::string MenuItem::get_input_with_symbol(bool active_item)
188 input_flickering = true;
190 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
195 snprintf(str, sizeof(str), "%s ",input.c_str());
197 snprintf(str, sizeof(str), "%s_",input.c_str());
199 std::string string = str;
206 for(std::vector<MenuItem*>::iterator i = items.begin();
207 i != items.end(); ++i)
213 if (previous == this)
220 menuaction = MENU_ACTION_NONE;
221 delete_character = 0;
222 mn_input_char = '\0';
224 pos_x = SCREEN_WIDTH/2;
225 pos_y = SCREEN_HEIGHT/2;
229 effect_progress = 0.0f;
230 effect_start_time = 0.0f;
232 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
233 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
234 back.reset(new Surface("images/engine/menu/arrow-back.png"));
235 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
236 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
240 Menu::set_pos(float x, float y, float rw, float rh)
242 pos_x = x + get_width() * rw;
243 pos_y = y + get_height() * rh;
246 /* Add an item to a menu */
248 Menu::additem(MenuItem* item)
250 items.push_back(item);
252 /* If a new menu is being built, the active item shouldn't be set to
253 * something that isnt selectable. Set the active_item to the first
254 * selectable item added
256 if (active_item == -1
257 && item->kind != MN_HL
258 && item->kind != MN_LABEL
259 && item->kind != MN_DEACTIVE) {
260 active_item = items.size() - 1;
267 additem(new MenuItem(MN_HL));
271 Menu::add_label(const std::string& text)
273 MenuItem* item = new MenuItem(MN_LABEL);
279 Menu::add_controlfield(int id, const std::string& text,
280 const std::string& mapping)
282 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
283 item->change_text(text);
284 item->change_input(mapping);
289 Menu::add_entry(int id, const std::string& text)
291 MenuItem* item = new MenuItem(MN_ACTION, id);
297 Menu::add_deactive(int id, const std::string& text)
299 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
305 Menu::add_toggle(int id, const std::string& text, bool toogled)
307 MenuItem* item = new MenuItem(MN_TOGGLE, id);
309 item->toggled = toogled;
314 Menu::add_back(const std::string& text)
316 MenuItem* item = new MenuItem(MN_BACK);
322 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
324 MenuItem* item = new MenuItem(MN_GOTO, id);
326 item->target_menu = submenu;
333 for(std::vector<MenuItem*>::iterator i = items.begin();
334 i != items.end(); ++i) {
341 /* Process actions done on the menu */
345 int menu_height = get_height();
346 if (menu_height > SCREEN_HEIGHT)
348 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
349 pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
352 effect_progress = (real_time - effect_start_time) * 6.0f;
354 if(effect_progress >= 1.0f) {
355 effect_progress = 1.0f;
356 } else if (effect_progress <= 0.0f) {
357 effect_progress = 0.0f;
360 /** check main input controller... */
361 if(main_controller->pressed(Controller::UP)) {
362 menuaction = MENU_ACTION_UP;
363 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
365 if(main_controller->hold(Controller::UP) &&
366 menu_repeat_time != 0 && real_time > menu_repeat_time) {
367 menuaction = MENU_ACTION_UP;
368 menu_repeat_time = real_time + MENU_REPEAT_RATE;
370 if(main_controller->pressed(Controller::DOWN)) {
371 menuaction = MENU_ACTION_DOWN;
372 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
374 if(main_controller->hold(Controller::DOWN) &&
375 menu_repeat_time != 0 && real_time > menu_repeat_time) {
376 menuaction = MENU_ACTION_DOWN;
377 menu_repeat_time = real_time + MENU_REPEAT_RATE;
379 if(main_controller->pressed(Controller::ACTION)
380 || main_controller->pressed(Controller::MENU_SELECT)) {
381 menuaction = MENU_ACTION_HIT;
383 if(main_controller->pressed(Controller::PAUSE_MENU)) {
384 menuaction = MENU_ACTION_BACK;
388 if(items.size() == 0)
391 int last_active_item = active_item;
398 active_item = int(items.size())-1;
399 } while ((items[active_item]->kind == MN_HL
400 || items[active_item]->kind == MN_LABEL
401 || items[active_item]->kind == MN_DEACTIVE)
402 && (active_item != last_active_item));
406 case MENU_ACTION_DOWN:
408 if(active_item < int(items.size())-1 )
412 } while ((items[active_item]->kind == MN_HL
413 || items[active_item]->kind == MN_LABEL
414 || items[active_item]->kind == MN_DEACTIVE)
415 && (active_item != last_active_item));
419 case MENU_ACTION_LEFT:
420 if(items[active_item]->kind == MN_STRINGSELECT) {
421 if(items[active_item]->selected > 0)
422 items[active_item]->selected--;
424 items[active_item]->selected = items[active_item]->list.size()-1;
428 case MENU_ACTION_RIGHT:
429 if(items[active_item]->kind == MN_STRINGSELECT) {
430 if(items[active_item]->selected+1 < items[active_item]->list.size())
431 items[active_item]->selected++;
433 items[active_item]->selected = 0;
437 case MENU_ACTION_HIT: {
438 hit_item = active_item;
439 switch (items[active_item]->kind) {
441 assert(items[active_item]->target_menu != 0);
442 Menu::push_current(items[active_item]->target_menu);
446 items[active_item]->toggled = !items[active_item]->toggled;
447 menu_action(items[active_item]);
450 case MN_CONTROLFIELD:
451 menu_action(items[active_item]);
455 menu_action(items[active_item]);
460 menuaction = MENU_ACTION_DOWN;
473 case MENU_ACTION_REMOVE:
474 if(items[active_item]->kind == MN_TEXTFIELD
475 || items[active_item]->kind == MN_NUMFIELD)
477 if(!items[active_item]->input.empty())
479 int i = items[active_item]->input.size();
481 while(delete_character > 0) /* remove charactes */
483 items[active_item]->input.resize(i-1);
490 case MENU_ACTION_INPUT:
491 if(items[active_item]->kind == MN_TEXTFIELD
492 || (items[active_item]->kind == MN_NUMFIELD
493 && mn_input_char >= '0' && mn_input_char <= '9'))
495 items[active_item]->input.push_back(mn_input_char);
499 case MENU_ACTION_BACK:
503 case MENU_ACTION_NONE:
506 menuaction = MENU_ACTION_NONE;
508 assert(active_item < int(items.size()));
515 return items[hit_item]->id;
521 Menu::menu_action(MenuItem* )
525 Menu::draw_item(DrawingContext& context, int index)
527 float menu_height = get_height();
528 float menu_width = get_width();
530 MenuItem& pitem = *(items[index]);
532 Font* text_font = default_font;
534 float y_pos = pos_y + 24*index - menu_height/2 + 12;
536 int text_width = int(text_font->get_text_width(pitem.text));
537 int input_width = int(text_font->get_text_width(pitem.input) + 10);
539 if(pitem.list.size() > 0) {
540 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
544 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
546 if(index == active_item)
549 text_font = active_font;
552 if(active_item == index)
554 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
555 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
556 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
557 Color(1.0f, 1.0f, 1.0f, blink),
560 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
561 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
562 Color(1.0f, 1.0f, 1.0f, 0.5f),
571 context.draw_text(deactive_font, pitem.text,
572 Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
573 ALIGN_CENTER, LAYER_GUI);
580 float x = pos_x - menu_width/2;
581 float y = y_pos - 12;
582 /* Draw a horizontal line with a little 3d effect */
583 context.draw_filled_rect(Vector(x, y + 6),
584 Vector(menu_width, 4),
585 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
586 context.draw_filled_rect(Vector(x, y + 6),
587 Vector(menu_width, 2),
588 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
593 context.draw_text(label_font, pitem.text,
594 Vector(pos_x, y_pos - int(label_font->get_height()/2)),
595 ALIGN_CENTER, LAYER_GUI);
600 case MN_CONTROLFIELD:
602 float left = pos_x - menu_width/2 + 16;
603 float right = pos_x + menu_width/2 - 16;
605 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
607 if(active_item == index)
608 context.draw_text(field_font,
609 pitem.get_input_with_symbol(true),
610 Vector(right, y_pos - int(field_font->get_height()/2)),
611 ALIGN_RIGHT, LAYER_GUI);
613 context.draw_text(field_font,
614 pitem.get_input_with_symbol(false),
615 Vector(right, y_pos - int(field_font->get_height()/2)),
616 ALIGN_RIGHT, LAYER_GUI);
619 context.draw_text(field_font, pitem.input,
620 Vector(right, y_pos - int(field_font->get_height()/2)),
621 ALIGN_RIGHT, LAYER_GUI);
623 context.draw_text(text_font, pitem.text,
624 Vector(left, y_pos - int(text_font->get_height()/2)),
625 ALIGN_LEFT, LAYER_GUI);
628 case MN_STRINGSELECT:
630 int list_pos_2 = list_width + 16;
631 int list_pos = list_width/2;
632 int text_pos = (text_width + 16)/2;
635 context.draw_surface(arrow_left.get(),
636 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
638 context.draw_surface(arrow_right.get(),
639 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
642 /* Draw input background */
643 context.draw_filled_rect(
644 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
645 Vector(list_pos_2 + 2, 20),
646 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
647 context.draw_filled_rect(
648 Vector(x_pos - list_pos + text_pos, y_pos - 9),
649 Vector(list_pos_2, 18),
650 Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
652 context.draw_text(text_font, pitem.list[pitem.selected],
653 Vector(pos_x + text_pos, y_pos - int(text_font->get_height()/2)),
654 ALIGN_CENTER, LAYER_GUI);
655 context.draw_text(text_font, pitem.text,
656 Vector(pos_x + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
657 ALIGN_CENTER, LAYER_GUI);
662 context.draw_text(text_font, pitem.text,
663 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
664 ALIGN_CENTER, LAYER_GUI);
665 context.draw_surface(back.get(),
666 Vector(x_pos + text_width/2 + 16, y_pos - 8),
673 context.draw_text(text_font, pitem.text,
674 Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
675 ALIGN_LEFT, LAYER_GUI);
678 context.draw_surface(checkbox_checked.get(),
679 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
682 context.draw_surface(checkbox.get(),
683 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
688 context.draw_text(text_font, pitem.text,
689 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
690 ALIGN_CENTER, LAYER_GUI);
694 context.draw_text(text_font, pitem.text,
695 Vector(pos_x, y_pos - int(text_font->get_height()/2)),
696 ALIGN_CENTER, LAYER_GUI);
702 Menu::get_width() const
704 /* The width of the menu has to be more than the width of the text
705 with the most characters */
706 float menu_width = 0;
707 for(unsigned int i = 0; i < items.size(); ++i)
709 Font* font = default_font;
710 if(items[i]->kind == MN_LABEL)
713 float w = font->get_text_width(items[i]->text) +
714 label_font->get_text_width(items[i]->input) + 16;
715 if(items[i]->kind == MN_TOGGLE)
722 return menu_width + 24;
726 Menu::get_height() const
728 return items.size() * 24;
731 /* Draw the current menu. */
733 Menu::draw(DrawingContext& context)
735 if(MouseCursor::current()) {
736 MouseCursor::current()->draw(context);
739 float menu_width = get_width();
740 float menu_height = get_height();
742 if (effect_progress != 1.0f)
746 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
747 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
748 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
752 menu_width *= effect_progress;
753 menu_height *= effect_progress;
757 /* Draw a transparent background */
758 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
759 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
760 Color(0.2f, 0.3f, 0.4f, 0.8f),
764 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
765 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
766 Color(0.6f, 0.7f, 0.8f, 0.5f),
770 if (effect_progress == 1.0f)
771 for(unsigned int i = 0; i < items.size(); ++i)
773 draw_item(context, i);
778 Menu::get_item_by_id(int id)
780 for(std::vector<MenuItem*>::iterator i = items.begin();
781 i != items.end(); ++i) {
782 MenuItem& item = **i;
788 throw std::runtime_error("MenuItem not found");
792 Menu::get_item_by_id(int id) const
794 for(std::vector<MenuItem*>::const_iterator i = items.begin();
795 i != items.end(); ++i) {
796 const MenuItem& item = **i;
802 throw std::runtime_error("MenuItem not found");
805 int Menu::get_active_item_id()
807 return items[active_item]->id;
811 Menu::is_toggled(int id) const
813 return get_item_by_id(id).toggled;
817 Menu::get_parent() const
819 if (last_menus.empty())
822 return last_menus.back();
825 /* Check for menu event */
827 Menu::event(const SDL_Event& event)
829 if(effect_progress != 1.0f)
833 case SDL_MOUSEBUTTONDOWN:
835 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
836 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
838 if(x > pos_x - get_width()/2 &&
839 x < pos_x + get_width()/2 &&
840 y > pos_y - get_height()/2 &&
841 y < pos_y + get_height()/2)
843 menuaction = MENU_ACTION_HIT;
848 case SDL_MOUSEMOTION:
850 float x = event.motion.x * SCREEN_WIDTH/screen->w;
851 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
853 if(x > pos_x - get_width()/2 &&
854 x < pos_x + get_width()/2 &&
855 y > pos_y - get_height()/2 &&
856 y < pos_y + get_height()/2)
859 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
861 /* only change the mouse focus to a selectable item */
862 if ((items[new_active_item]->kind != MN_HL)
863 && (items[new_active_item]->kind != MN_LABEL)
864 && (items[new_active_item]->kind != MN_DEACTIVE))
865 active_item = new_active_item;
867 if(MouseCursor::current())
868 MouseCursor::current()->set_state(MC_LINK);
872 if(MouseCursor::current())
873 MouseCursor::current()->set_state(MC_NORMAL);
884 Menu::set_active_item(int id)
886 for(size_t i = 0; i < items.size(); ++i) {
887 MenuItem* item = items[i];