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)
216 menuaction = MENU_ACTION_NONE;
217 delete_character = 0;
218 mn_input_char = '\0';
220 pos_x = SCREEN_WIDTH/2;
221 pos_y = SCREEN_HEIGHT/2;
225 effect_progress = 0.0f;
226 effect_start_time = 0.0f;
228 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
229 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
230 back.reset(new Surface("images/engine/menu/arrow-back.png"));
231 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
232 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
236 Menu::set_pos(float x, float y, float rw, float rh)
238 pos_x = x + get_width() * rw;
239 pos_y = y + get_height() * rh;
242 /* Add an item to a menu */
244 Menu::additem(MenuItem* item)
246 items.push_back(item);
248 /* If a new menu is being built, the active item shouldn't be set to
249 * something that isnt selectable. Set the active_item to the first
250 * selectable item added
252 if (active_item == -1
253 && item->kind != MN_HL
254 && item->kind != MN_LABEL
255 && item->kind != MN_DEACTIVE) {
256 active_item = items.size() - 1;
263 additem(new MenuItem(MN_HL));
267 Menu::add_label(const std::string& text)
269 MenuItem* item = new MenuItem(MN_LABEL);
275 Menu::add_controlfield(int id, const std::string& text,
276 const std::string& mapping)
278 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
279 item->change_text(text);
280 item->change_input(mapping);
285 Menu::add_entry(int id, const std::string& text)
287 MenuItem* item = new MenuItem(MN_ACTION, id);
293 Menu::add_deactive(int id, const std::string& text)
295 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
301 Menu::add_toggle(int id, const std::string& text, bool toogled)
303 MenuItem* item = new MenuItem(MN_TOGGLE, id);
305 item->toggled = toogled;
310 Menu::add_back(const std::string& text)
312 MenuItem* item = new MenuItem(MN_BACK);
318 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
320 MenuItem* item = new MenuItem(MN_GOTO, id);
322 item->target_menu = submenu;
329 for(std::vector<MenuItem*>::iterator i = items.begin();
330 i != items.end(); ++i) {
337 /* Process actions done on the menu */
341 effect_progress = (real_time - effect_start_time) * 6.0f;
343 if(effect_progress >= 1.0f) {
344 effect_progress = 1.0f;
345 } else if (effect_progress <= 0.0f) {
346 effect_progress = 0.0f;
349 /** check main input controller... */
350 if(main_controller->pressed(Controller::UP)) {
351 menuaction = MENU_ACTION_UP;
352 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
354 if(main_controller->hold(Controller::UP) &&
355 menu_repeat_time != 0 && real_time > menu_repeat_time) {
356 menuaction = MENU_ACTION_UP;
357 menu_repeat_time = real_time + MENU_REPEAT_RATE;
359 if(main_controller->pressed(Controller::DOWN)) {
360 menuaction = MENU_ACTION_DOWN;
361 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
363 if(main_controller->hold(Controller::DOWN) &&
364 menu_repeat_time != 0 && real_time > menu_repeat_time) {
365 menuaction = MENU_ACTION_DOWN;
366 menu_repeat_time = real_time + MENU_REPEAT_RATE;
368 if(main_controller->pressed(Controller::ACTION)
369 || main_controller->pressed(Controller::MENU_SELECT)) {
370 menuaction = MENU_ACTION_HIT;
372 if(main_controller->pressed(Controller::PAUSE_MENU)) {
373 menuaction = MENU_ACTION_BACK;
377 if(items.size() == 0)
380 int last_active_item = active_item;
387 active_item = int(items.size())-1;
388 } while ((items[active_item]->kind == MN_HL
389 || items[active_item]->kind == MN_LABEL
390 || items[active_item]->kind == MN_DEACTIVE)
391 && (active_item != last_active_item));
395 case MENU_ACTION_DOWN:
397 if(active_item < int(items.size())-1 )
401 } while ((items[active_item]->kind == MN_HL
402 || items[active_item]->kind == MN_LABEL
403 || items[active_item]->kind == MN_DEACTIVE)
404 && (active_item != last_active_item));
408 case MENU_ACTION_LEFT:
409 if(items[active_item]->kind == MN_STRINGSELECT) {
410 if(items[active_item]->selected > 0)
411 items[active_item]->selected--;
413 items[active_item]->selected = items[active_item]->list.size()-1;
417 case MENU_ACTION_RIGHT:
418 if(items[active_item]->kind == MN_STRINGSELECT) {
419 if(items[active_item]->selected+1 < items[active_item]->list.size())
420 items[active_item]->selected++;
422 items[active_item]->selected = 0;
426 case MENU_ACTION_HIT: {
427 hit_item = active_item;
428 switch (items[active_item]->kind) {
430 assert(items[active_item]->target_menu != 0);
431 Menu::push_current(items[active_item]->target_menu);
435 items[active_item]->toggled = !items[active_item]->toggled;
436 menu_action(items[active_item]);
439 case MN_CONTROLFIELD:
440 menu_action(items[active_item]);
444 menu_action(items[active_item]);
449 menuaction = MENU_ACTION_DOWN;
462 case MENU_ACTION_REMOVE:
463 if(items[active_item]->kind == MN_TEXTFIELD
464 || items[active_item]->kind == MN_NUMFIELD)
466 if(!items[active_item]->input.empty())
468 int i = items[active_item]->input.size();
470 while(delete_character > 0) /* remove charactes */
472 items[active_item]->input.resize(i-1);
479 case MENU_ACTION_INPUT:
480 if(items[active_item]->kind == MN_TEXTFIELD
481 || (items[active_item]->kind == MN_NUMFIELD
482 && mn_input_char >= '0' && mn_input_char <= '9'))
484 items[active_item]->input.push_back(mn_input_char);
488 case MENU_ACTION_BACK:
492 case MENU_ACTION_NONE:
495 menuaction = MENU_ACTION_NONE;
497 assert(active_item < int(items.size()));
504 return items[hit_item]->id;
510 Menu::menu_action(MenuItem* )
514 Menu::draw_item(DrawingContext& context, int index)
516 float menu_height = get_height();
517 float menu_width = get_width();
519 MenuItem& pitem = *(items[index]);
521 Font* text_font = default_font;
523 float y_pos = pos_y + 24*index - menu_height/2 + 12;
525 int text_width = int(text_font->get_text_width(pitem.text));
526 int input_width = int(text_font->get_text_width(pitem.input) + 10);
528 if(pitem.list.size() > 0) {
529 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
533 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
535 if(index == active_item)
538 text_font = active_font;
541 if(active_item == index)
543 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
544 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
545 Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
546 Color(1.0f, 1.0f, 1.0f, blink),
549 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
550 Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
551 Color(1.0f, 1.0f, 1.0f, 0.5f),
560 context.draw_text(deactive_font, pitem.text,
561 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
562 ALIGN_CENTER, LAYER_GUI);
569 float x = pos_x - menu_width/2;
570 float y = y_pos - 12;
571 /* Draw a horizontal line with a little 3d effect */
572 context.draw_filled_rect(Vector(x, y + 6),
573 Vector(menu_width, 4),
574 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
575 context.draw_filled_rect(Vector(x, y + 6),
576 Vector(menu_width, 2),
577 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
582 context.draw_text(label_font, pitem.text,
583 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
584 ALIGN_CENTER, LAYER_GUI);
589 case MN_CONTROLFIELD:
591 float left = SCREEN_WIDTH/2 - menu_width/2 + 16;
592 float right = SCREEN_WIDTH/2 + menu_width/2 - 16;
594 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
596 if(active_item == index)
597 context.draw_text(field_font,
598 pitem.get_input_with_symbol(true),
599 Vector(right, y_pos - int(field_font->get_height()/2)),
600 ALIGN_RIGHT, LAYER_GUI);
602 context.draw_text(field_font,
603 pitem.get_input_with_symbol(false),
604 Vector(right, y_pos - int(field_font->get_height()/2)),
605 ALIGN_RIGHT, LAYER_GUI);
608 context.draw_text(field_font, pitem.input,
609 Vector(right, y_pos - int(field_font->get_height()/2)),
610 ALIGN_RIGHT, LAYER_GUI);
612 context.draw_text(text_font, pitem.text,
613 Vector(left, y_pos - int(text_font->get_height()/2)),
614 ALIGN_LEFT, LAYER_GUI);
617 case MN_STRINGSELECT:
619 int list_pos_2 = list_width + 16;
620 int list_pos = list_width/2;
621 int text_pos = (text_width + 16)/2;
624 context.draw_surface(arrow_left.get(),
625 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
627 context.draw_surface(arrow_right.get(),
628 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
631 /* Draw input background */
632 context.draw_filled_rect(
633 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
634 Vector(list_pos_2 + 2, 20),
635 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
636 context.draw_filled_rect(
637 Vector(x_pos - list_pos + text_pos, y_pos - 9),
638 Vector(list_pos_2, 18),
639 Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
641 context.draw_text(text_font, pitem.list[pitem.selected],
642 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
643 ALIGN_CENTER, LAYER_GUI);
644 context.draw_text(text_font, pitem.text,
645 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
646 ALIGN_CENTER, LAYER_GUI);
651 context.draw_text(text_font, pitem.text,
652 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
653 ALIGN_CENTER, LAYER_GUI);
654 context.draw_surface(back.get(),
655 Vector(x_pos + text_width/2 + 16, y_pos - 8),
662 context.draw_text(text_font, pitem.text,
663 Vector(SCREEN_WIDTH/2 - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
664 ALIGN_LEFT, LAYER_GUI);
667 context.draw_surface(checkbox_checked.get(),
668 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
671 context.draw_surface(checkbox.get(),
672 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
677 context.draw_text(text_font, pitem.text,
678 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
679 ALIGN_CENTER, LAYER_GUI);
683 context.draw_text(text_font, pitem.text,
684 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
685 ALIGN_CENTER, LAYER_GUI);
691 Menu::get_width() const
693 /* The width of the menu has to be more than the width of the text
694 with the most characters */
695 float menu_width = 0;
696 for(unsigned int i = 0; i < items.size(); ++i)
698 Font* font = default_font;
699 if(items[i]->kind == MN_LABEL)
702 float w = font->get_text_width(items[i]->text) +
703 label_font->get_text_width(items[i]->input) + 16;
704 if(items[i]->kind == MN_TOGGLE)
711 return menu_width + 24;
715 Menu::get_height() const
717 return items.size() * 24;
720 /* Draw the current menu. */
722 Menu::draw(DrawingContext& context)
724 if(MouseCursor::current()) {
725 MouseCursor::current()->draw(context);
728 float menu_width = get_width();
729 float menu_height = get_height();
731 if (effect_progress != 1.0f)
735 menu_width = (menu_width * effect_progress) + (Menu::previous->get_width() * (1.0f - effect_progress));
736 menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
737 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
741 menu_width *= effect_progress;
742 menu_height *= effect_progress;
746 /* Draw a transparent background */
747 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
748 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
749 Color(0.2f, 0.3f, 0.4f, 0.8f),
753 context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
754 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
755 Color(0.6f, 0.7f, 0.8f, 0.5f),
759 if (effect_progress == 1.0f)
760 for(unsigned int i = 0; i < items.size(); ++i)
762 draw_item(context, i);
767 Menu::get_item_by_id(int id)
769 for(std::vector<MenuItem*>::iterator i = items.begin();
770 i != items.end(); ++i) {
771 MenuItem& item = **i;
777 throw std::runtime_error("MenuItem not found");
781 Menu::get_item_by_id(int id) const
783 for(std::vector<MenuItem*>::const_iterator i = items.begin();
784 i != items.end(); ++i) {
785 const MenuItem& item = **i;
791 throw std::runtime_error("MenuItem not found");
794 int Menu::get_active_item_id()
796 return items[active_item]->id;
800 Menu::is_toggled(int id) const
802 return get_item_by_id(id).toggled;
805 /* Check for menu event */
807 Menu::event(const SDL_Event& event)
809 if(effect_progress != 1.0f)
813 case SDL_MOUSEBUTTONDOWN:
815 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
816 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
818 if(x > pos_x - get_width()/2 &&
819 x < pos_x + get_width()/2 &&
820 y > pos_y - get_height()/2 &&
821 y < pos_y + get_height()/2)
823 menuaction = MENU_ACTION_HIT;
828 case SDL_MOUSEMOTION:
830 float x = event.motion.x * SCREEN_WIDTH/screen->w;
831 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
833 if(x > pos_x - get_width()/2 &&
834 x < pos_x + get_width()/2 &&
835 y > pos_y - get_height()/2 &&
836 y < pos_y + get_height()/2)
839 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
841 /* only change the mouse focus to a selectable item */
842 if ((items[new_active_item]->kind != MN_HL)
843 && (items[new_active_item]->kind != MN_LABEL)
844 && (items[new_active_item]->kind != MN_DEACTIVE))
845 active_item = new_active_item;
847 if(MouseCursor::current())
848 MouseCursor::current()->set_state(MC_LINK);
852 if(MouseCursor::current())
853 MouseCursor::current()->set_state(MC_NORMAL);
864 Menu::set_active_item(int id)
866 for(size_t i = 0; i < items.size(); ++i) {
867 MenuItem* item = items[i];