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>
34 #include "mainloop.hpp"
35 #include "video/drawing_context.hpp"
36 #include "gettext.hpp"
37 #include "math/vector.hpp"
39 #include "resources.hpp"
41 #include "control/joystickkeyboardcontroller.hpp"
43 static const float MENU_REPEAT_INITIAL = 0.4f;
44 static const float MENU_REPEAT_RATE = 0.2f;
45 static const float FLICK_CURSOR_TIME = 0.5f;
47 extern SDL_Surface* screen;
49 std::vector<Menu*> Menu::last_menus;
50 Menu* Menu::current_ = 0;
51 Menu* Menu::previous = 0;
52 Font* Menu::default_font;
53 Font* Menu::active_font;
54 Font* Menu::deactive_font;
55 Font* Menu::label_font;
56 Font* Menu::field_font;
58 /* just displays a Yes/No text that can be used to confirm stuff */
59 bool confirm_dialog(Surface *background, std::string text)
61 //Surface* cap_screen = Surface::CaptureScreen();
62 Menu* dialog = new Menu;
63 dialog->add_deactive(-1, text);
65 dialog->add_entry(true, _("Yes"));
66 dialog->add_entry(false, _("No"));
69 Menu::set_current(dialog);
71 DrawingContext context;
73 // TODO make this a screen and not another mainloop...
77 while (SDL_PollEvent(&event)) {
78 if(event.type == SDL_QUIT)
80 main_controller->process_event(event);
84 if(background == NULL)
85 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
88 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
90 dialog->draw(context);
93 switch (dialog->check())
103 Menu::set_current(0);
111 mouse_cursor->draw(context);
112 context.do_drawing();
120 Menu::push_current(Menu* pmenu)
125 last_menus.push_back(current_);
128 current_->effect_start_time = real_time;
129 current_->effect_progress = 0.0f;
137 if (last_menus.size() >= 1) {
138 current_ = last_menus.back();
139 current_->effect_start_time = real_time;
140 current_->effect_progress = 0.0f;
141 last_menus.pop_back();
148 Menu::set_current(Menu* menu)
156 menu->effect_start_time = real_time;
157 menu->effect_progress = 0.0f;
160 // just to be sure...
161 main_controller->reset();
164 MenuItem::MenuItem(MenuItemKind _kind, int _id)
165 : kind(_kind) , id(_id)
173 MenuItem::change_text(const std::string& text_)
179 MenuItem::change_input(const std::string& text_)
184 std::string MenuItem::get_input_with_symbol(bool active_item)
187 input_flickering = true;
189 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
194 snprintf(str, sizeof(str), "%s ",input.c_str());
196 snprintf(str, sizeof(str), "%s_",input.c_str());
198 std::string string = str;
205 for(std::vector<MenuItem*>::iterator i = items.begin();
206 i != items.end(); ++i)
215 menuaction = MENU_ACTION_NONE;
216 delete_character = 0;
217 mn_input_char = '\0';
219 pos_x = SCREEN_WIDTH/2;
220 pos_y = SCREEN_HEIGHT/2;
224 effect_progress = 0.0f;
225 effect_start_time = 0.0f;
227 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
228 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
229 back.reset(new Surface("images/engine/menu/arrow-back.png"));
230 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
231 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
234 void Menu::set_pos(float x, float y, float rw, float rh)
236 pos_x = x + get_width() * rw;
237 pos_y = y + get_height() * rh;
240 /* Add an item to a menu */
242 Menu::additem(MenuItem* item)
244 items.push_back(item);
246 /* If a new menu is being built, the active item shouldn't be set to
247 * something that isnt selectable. Set the active_item to the first
248 * selectable item added
250 if (active_item == -1
251 && item->kind != MN_HL
252 && item->kind != MN_LABEL
253 && item->kind != MN_DEACTIVE) {
254 active_item = items.size() - 1;
261 additem(new MenuItem(MN_HL));
265 Menu::add_label(const std::string& text)
267 MenuItem* item = new MenuItem(MN_LABEL);
273 Menu::add_controlfield(int id, const std::string& text,
274 const std::string& mapping)
276 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
277 item->change_text(text);
278 item->change_input(mapping);
283 Menu::add_entry(int id, const std::string& text)
285 MenuItem* item = new MenuItem(MN_ACTION, id);
291 Menu::add_deactive(int id, const std::string& text)
293 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
299 Menu::add_toggle(int id, const std::string& text, bool toogled)
301 MenuItem* item = new MenuItem(MN_TOGGLE, id);
303 item->toggled = toogled;
308 Menu::add_back(const std::string& text)
310 MenuItem* item = new MenuItem(MN_BACK);
316 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
318 MenuItem* item = new MenuItem(MN_GOTO, id);
320 item->target_menu = submenu;
327 for(std::vector<MenuItem*>::iterator i = items.begin();
328 i != items.end(); ++i) {
335 /* Process actions done on the menu */
339 effect_progress = (real_time - effect_start_time) * 6.0f;
341 if(effect_progress >= 1.0f) {
342 effect_progress = 1.0f;
343 } else if (effect_progress <= 0.0f) {
344 effect_progress = 0.0f;
347 /** check main input controller... */
348 if(main_controller->pressed(Controller::UP)) {
349 menuaction = MENU_ACTION_UP;
350 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
352 if(main_controller->hold(Controller::UP) &&
353 menu_repeat_time != 0 && real_time > menu_repeat_time) {
354 menuaction = MENU_ACTION_UP;
355 menu_repeat_time = real_time + MENU_REPEAT_RATE;
357 if(main_controller->pressed(Controller::DOWN)) {
358 menuaction = MENU_ACTION_DOWN;
359 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
361 if(main_controller->hold(Controller::DOWN) &&
362 menu_repeat_time != 0 && real_time > menu_repeat_time) {
363 menuaction = MENU_ACTION_DOWN;
364 menu_repeat_time = real_time + MENU_REPEAT_RATE;
366 if(main_controller->pressed(Controller::ACTION)
367 || main_controller->pressed(Controller::MENU_SELECT)) {
368 menuaction = MENU_ACTION_HIT;
370 if(main_controller->pressed(Controller::PAUSE_MENU)) {
371 menuaction = MENU_ACTION_BACK;
375 if(items.size() == 0)
378 int last_active_item = active_item;
385 active_item = int(items.size())-1;
386 } while ((items[active_item]->kind == MN_HL
387 || items[active_item]->kind == MN_LABEL
388 || items[active_item]->kind == MN_DEACTIVE)
389 && (active_item != last_active_item));
393 case MENU_ACTION_DOWN:
395 if(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_LEFT:
407 if(items[active_item]->kind == MN_STRINGSELECT) {
408 if(items[active_item]->selected > 0)
409 items[active_item]->selected--;
411 items[active_item]->selected = items[active_item]->list.size()-1;
415 case MENU_ACTION_RIGHT:
416 if(items[active_item]->kind == MN_STRINGSELECT) {
417 if(items[active_item]->selected+1 < items[active_item]->list.size())
418 items[active_item]->selected++;
420 items[active_item]->selected = 0;
424 case MENU_ACTION_HIT: {
425 hit_item = active_item;
426 switch (items[active_item]->kind) {
428 assert(items[active_item]->target_menu != 0);
429 Menu::push_current(items[active_item]->target_menu);
433 items[active_item]->toggled = !items[active_item]->toggled;
434 menu_action(items[active_item]);
437 case MN_CONTROLFIELD:
438 menu_action(items[active_item]);
442 menu_action(items[active_item]);
447 menuaction = MENU_ACTION_DOWN;
460 case MENU_ACTION_REMOVE:
461 if(items[active_item]->kind == MN_TEXTFIELD
462 || items[active_item]->kind == MN_NUMFIELD)
464 if(!items[active_item]->input.empty())
466 int i = items[active_item]->input.size();
468 while(delete_character > 0) /* remove charactes */
470 items[active_item]->input.resize(i-1);
477 case MENU_ACTION_INPUT:
478 if(items[active_item]->kind == MN_TEXTFIELD
479 || (items[active_item]->kind == MN_NUMFIELD
480 && mn_input_char >= '0' && mn_input_char <= '9'))
482 items[active_item]->input.push_back(mn_input_char);
486 case MENU_ACTION_BACK:
490 case MENU_ACTION_NONE:
493 menuaction = MENU_ACTION_NONE;
495 assert(active_item < int(items.size()));
502 return items[hit_item]->id;
508 Menu::menu_action(MenuItem* )
512 Menu::draw_item(DrawingContext& context, int index)
514 float menu_height = get_height();
515 float menu_width = get_width();
517 MenuItem& pitem = *(items[index]);
519 Font* text_font = default_font;
521 float y_pos = pos_y + 24*index - menu_height/2 + 12;
523 int text_width = int(text_font->get_text_width(pitem.text));
524 int input_width = int(text_font->get_text_width(pitem.input) + 10);
526 if(pitem.list.size() > 0) {
527 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
531 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
533 if(index == active_item)
536 text_font = active_font;
539 if(active_item == index)
541 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
542 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
543 Color(1.0f, 1.0f, 1.0f, 0.5f),
546 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
547 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
548 Color(1.0f, 1.0f, 1.0f, 0.5f),
557 context.draw_text(deactive_font, pitem.text,
558 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
559 ALIGN_CENTER, LAYER_GUI);
566 float x = pos_x - menu_width/2;
567 float y = y_pos - 12;
568 /* Draw a horizontal line with a little 3d effect */
569 context.draw_filled_rect(Vector(x, y + 6),
570 Vector(menu_width, 4),
571 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
572 context.draw_filled_rect(Vector(x, y + 6),
573 Vector(menu_width, 2),
574 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
579 context.draw_text(label_font, pitem.text,
580 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
581 ALIGN_CENTER, LAYER_GUI);
586 case MN_CONTROLFIELD:
588 float width = text_width + input_width + 5;
589 float text_pos = SCREEN_WIDTH/2 - width/2;
590 float input_pos = text_pos + text_width + 10;
592 context.draw_filled_rect(
593 Vector(input_pos - 5, y_pos - 10),
594 Vector(input_width + 10, 20),
595 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
596 context.draw_filled_rect(
597 Vector(input_pos - 4, y_pos - 9),
598 Vector(input_width + 8, 18),
599 Color(0, 0, 0, 0.5f), LAYER_GUI-4);
601 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
603 if(active_item == index)
604 context.draw_text(field_font,
605 pitem.get_input_with_symbol(true),
606 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
607 ALIGN_LEFT, LAYER_GUI);
609 context.draw_text(field_font,
610 pitem.get_input_with_symbol(false),
611 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
612 ALIGN_LEFT, LAYER_GUI);
615 context.draw_text(field_font, pitem.input,
616 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
617 ALIGN_LEFT, LAYER_GUI);
619 context.draw_text(text_font, pitem.text,
620 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
621 ALIGN_LEFT, LAYER_GUI);
624 case MN_STRINGSELECT:
626 int list_pos_2 = list_width + 16;
627 int list_pos = list_width/2;
628 int text_pos = (text_width + 16)/2;
631 context.draw_surface(arrow_left.get(),
632 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
634 context.draw_surface(arrow_right.get(),
635 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
638 /* Draw input background */
639 context.draw_filled_rect(
640 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
641 Vector(list_pos_2 + 2, 20),
642 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
643 context.draw_filled_rect(
644 Vector(x_pos - list_pos + text_pos, y_pos - 9),
645 Vector(list_pos_2, 18),
646 Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
648 context.draw_text(text_font, pitem.list[pitem.selected],
649 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
650 ALIGN_CENTER, LAYER_GUI);
651 context.draw_text(text_font, pitem.text,
652 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
653 ALIGN_CENTER, LAYER_GUI);
658 context.draw_text(text_font, pitem.text,
659 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
660 ALIGN_CENTER, LAYER_GUI);
661 context.draw_surface(back.get(),
662 Vector(x_pos + text_width/2 + 16, y_pos - 8),
669 context.draw_text(text_font, pitem.text,
670 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
671 ALIGN_CENTER, LAYER_GUI);
674 context.draw_surface(checkbox_checked.get(),
675 Vector(x_pos + (text_width+16)/2, y_pos - 8),
678 context.draw_surface(checkbox.get(),
679 Vector(x_pos + (text_width+16)/2, y_pos - 8),
684 context.draw_text(text_font, pitem.text,
685 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
686 ALIGN_CENTER, LAYER_GUI);
690 context.draw_text(text_font, pitem.text,
691 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
692 ALIGN_CENTER, LAYER_GUI);
697 float Menu::get_width() const
699 /* The width of the menu has to be more than the width of the text
700 with the most characters */
701 float menu_width = 0;
702 for(unsigned int i = 0; i < items.size(); ++i)
704 Font* font = default_font;
705 if(items[i]->kind == MN_LABEL)
708 float w = font->get_text_width(items[i]->text) +
709 label_font->get_text_width(items[i]->input) + 16;
710 if(items[i]->kind == MN_TOGGLE)
717 return menu_width + 24;
720 float Menu::get_height() const
722 return items.size() * 24;
725 /* Draw the current menu. */
727 Menu::draw(DrawingContext& context)
729 if(MouseCursor::current()) {
730 MouseCursor::current()->draw(context);
733 float menu_width = get_width();
734 float menu_height = get_height();
736 if (effect_progress != 1.0f)
740 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
741 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
742 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
746 menu_width *= effect_progress;
747 menu_height *= effect_progress;
751 /* Draw a transparent background */
752 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
753 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
754 Color(0.2f, 0.3f, 0.4f, 0.8f),
758 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
759 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
760 Color(0.6f, 0.7f, 0.8f, 0.5f),
764 if (effect_progress == 1.0f)
765 for(unsigned int i = 0; i < items.size(); ++i)
767 draw_item(context, i);
772 Menu::get_item_by_id(int id)
774 for(std::vector<MenuItem*>::iterator i = items.begin();
775 i != items.end(); ++i) {
776 MenuItem& item = **i;
782 throw std::runtime_error("MenuItem not found");
786 Menu::get_item_by_id(int id) const
788 for(std::vector<MenuItem*>::const_iterator i = items.begin();
789 i != items.end(); ++i) {
790 const MenuItem& item = **i;
796 throw std::runtime_error("MenuItem not found");
799 int Menu::get_active_item_id()
801 return items[active_item]->id;
805 Menu::is_toggled(int id) const
807 return get_item_by_id(id).toggled;
810 /* Check for menu event */
812 Menu::event(const SDL_Event& event)
814 if(effect_progress != 1.0f)
818 case SDL_MOUSEBUTTONDOWN:
820 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
821 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
823 if(x > pos_x - get_width()/2 &&
824 x < pos_x + get_width()/2 &&
825 y > pos_y - get_height()/2 &&
826 y < pos_y + get_height()/2)
828 menuaction = MENU_ACTION_HIT;
833 case SDL_MOUSEMOTION:
835 float x = event.motion.x * SCREEN_WIDTH/screen->w;
836 float y = event.motion.y * 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)
844 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
846 /* only change the mouse focus to a selectable item */
847 if ((items[new_active_item]->kind != MN_HL)
848 && (items[new_active_item]->kind != MN_LABEL)
849 && (items[new_active_item]->kind != MN_DEACTIVE))
850 active_item = new_active_item;
852 if(MouseCursor::current())
853 MouseCursor::current()->set_state(MC_LINK);
857 if(MouseCursor::current())
858 MouseCursor::current()->set_state(MC_NORMAL);
869 Menu::set_active_item(int id)
871 for(size_t i = 0; i < items.size(); ++i) {
872 MenuItem* item = items[i];