4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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.
21 #include <sys/types.h>
33 #include "video/screen.hpp"
34 #include "video/drawing_context.hpp"
35 #include "gettext.hpp"
36 #include "math/vector.hpp"
38 #include "resources.hpp"
39 #include "control/joystickkeyboardcontroller.hpp"
40 #include "exceptions.hpp"
42 static const int MENU_REPEAT_INITIAL = 400;
43 static const int MENU_REPEAT_RATE = 200;
44 static const int FLICK_CURSOR_TIME = 500;
46 extern SDL_Surface* screen;
49 Surface* checkbox_checked;
54 std::vector<Menu*> Menu::last_menus;
55 Menu* Menu::current_ = 0;
56 Font* Menu::default_font;
57 Font* Menu::active_font;
58 Font* Menu::deactive_font;
59 Font* Menu::label_font;
60 Font* Menu::field_font;
62 /* just displays a Yes/No text that can be used to confirm stuff */
63 bool confirm_dialog(Surface *background, std::string text)
65 //Surface* cap_screen = Surface::CaptureScreen();
66 Menu* dialog = new Menu;
67 dialog->add_deactive(-1, text);
69 dialog->add_entry(true, _("Yes"));
70 dialog->add_entry(false, _("No"));
73 Menu::set_current(dialog);
75 DrawingContext context;
80 while (SDL_PollEvent(&event)) {
81 if(event.type == SDL_QUIT)
82 throw graceful_shutdown();
83 main_controller->process_event(event);
87 if(background == NULL)
88 context.draw_gradient(Color(0.8, 0.95, 0.85), Color(0.8, 0.8, 0.8),
91 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
93 dialog->draw(context);
96 switch (dialog->check())
100 Menu::set_current(0);
106 Menu::set_current(0);
114 mouse_cursor->draw(context);
115 context.do_drawing();
123 Menu::push_current(Menu* pmenu)
126 last_menus.push_back(current_);
129 current_->effect_ticks = SDL_GetTicks();
135 if (last_menus.size() >= 1) {
136 current_ = last_menus.back();
137 current_->effect_ticks = SDL_GetTicks();
138 last_menus.pop_back();
145 Menu::set_current(Menu* menu)
150 menu->effect_ticks = SDL_GetTicks();
153 // just to be sure...
154 main_controller->reset();
157 MenuItem::MenuItem(MenuItemKind _kind, int _id)
158 : kind(_kind) , id(_id)
166 MenuItem::change_text(const std::string& text_)
172 MenuItem::change_input(const std::string& text_)
177 std::string MenuItem::get_input_with_symbol(bool active_item)
180 input_flickering = true;
182 input_flickering = (SDL_GetTicks() / FLICK_CURSOR_TIME) % 2;
187 sprintf(str,"%s ",input.c_str());
189 sprintf(str,"%s_",input.c_str());
191 std::string string = str;
198 for(std::vector<MenuItem*>::iterator i = items.begin();
199 i != items.end(); ++i)
206 menuaction = MENU_ACTION_NONE;
207 delete_character = 0;
208 mn_input_char = '\0';
210 pos_x = SCREEN_WIDTH/2;
211 pos_y = SCREEN_HEIGHT/2;
216 void Menu::set_pos(float x, float y, float rw, float rh)
218 pos_x = x + get_width() * rw;
219 pos_y = y + get_height() * rh;
222 /* Add an item to a menu */
224 Menu::additem(MenuItem* item)
226 items.push_back(item);
228 /* If a new menu is being built, the active item shouldn't be set to
229 * something that isnt selectable. Set the active_item to the first
230 * selectable item added
232 if (active_item == -1
233 && item->kind != MN_HL
234 && item->kind != MN_LABEL
235 && item->kind != MN_DEACTIVE) {
236 active_item = items.size() - 1;
243 additem(new MenuItem(MN_HL));
247 Menu::add_label(const std::string& text)
249 MenuItem* item = new MenuItem(MN_LABEL);
255 Menu::add_controlfield(int id, const std::string& text,
256 const std::string& mapping)
258 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
259 item->change_text(text);
260 item->change_input(mapping);
265 Menu::add_entry(int id, const std::string& text)
267 MenuItem* item = new MenuItem(MN_ACTION, id);
273 Menu::add_deactive(int id, const std::string& text)
275 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
281 Menu::add_toggle(int id, const std::string& text, bool toogled)
283 MenuItem* item = new MenuItem(MN_TOGGLE, id);
285 item->toggled = toogled;
290 Menu::add_back(const std::string& text)
292 MenuItem* item = new MenuItem(MN_BACK);
298 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
300 MenuItem* item = new MenuItem(MN_GOTO, id);
302 item->target_menu = submenu;
309 for(std::vector<MenuItem*>::iterator i = items.begin();
310 i != items.end(); ++i) {
317 /* Process actions done on the menu */
321 /** check main input controller... */
322 Uint32 ticks = SDL_GetTicks();
323 if(main_controller->pressed(Controller::UP)) {
324 menuaction = MENU_ACTION_UP;
325 menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
327 if(main_controller->hold(Controller::UP) &&
328 menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
329 menuaction = MENU_ACTION_UP;
330 menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
332 if(main_controller->pressed(Controller::DOWN)) {
333 menuaction = MENU_ACTION_DOWN;
334 menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
336 if(main_controller->hold(Controller::DOWN) &&
337 menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
338 menuaction = MENU_ACTION_DOWN;
339 menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
341 if(main_controller->pressed(Controller::JUMP)
342 || main_controller->pressed(Controller::ACTION)
343 || main_controller->pressed(Controller::MENU_SELECT)) {
344 menuaction = MENU_ACTION_HIT;
346 if(main_controller->pressed(Controller::PAUSE_MENU)) {
347 menuaction = MENU_ACTION_BACK;
351 if(items.size() == 0)
354 int last_active_item = active_item;
361 active_item = int(items.size())-1;
362 } while ((items[active_item]->kind == MN_HL
363 || items[active_item]->kind == MN_LABEL
364 || items[active_item]->kind == MN_DEACTIVE)
365 && (active_item != last_active_item));
369 case MENU_ACTION_DOWN:
371 if(active_item < int(items.size())-1 )
375 } while ((items[active_item]->kind == MN_HL
376 || items[active_item]->kind == MN_LABEL
377 || items[active_item]->kind == MN_DEACTIVE)
378 && (active_item != last_active_item));
382 case MENU_ACTION_LEFT:
383 if(items[active_item]->kind == MN_STRINGSELECT) {
384 if(items[active_item]->selected > 0)
385 items[active_item]->selected--;
387 items[active_item]->selected = items[active_item]->list.size()-1;
391 case MENU_ACTION_RIGHT:
392 if(items[active_item]->kind == MN_STRINGSELECT) {
393 if(items[active_item]->selected+1 < items[active_item]->list.size())
394 items[active_item]->selected++;
396 items[active_item]->selected = 0;
400 case MENU_ACTION_HIT: {
401 hit_item = active_item;
402 switch (items[active_item]->kind) {
404 assert(items[active_item]->target_menu != 0);
405 Menu::push_current(items[active_item]->target_menu);
409 items[active_item]->toggled = !items[active_item]->toggled;
410 menu_action(items[active_item]);
413 case MN_CONTROLFIELD:
414 menu_action(items[active_item]);
418 menu_action(items[active_item]);
423 menuaction = MENU_ACTION_DOWN;
436 case MENU_ACTION_REMOVE:
437 if(items[active_item]->kind == MN_TEXTFIELD
438 || items[active_item]->kind == MN_NUMFIELD)
440 if(!items[active_item]->input.empty())
442 int i = items[active_item]->input.size();
444 while(delete_character > 0) /* remove charactes */
446 items[active_item]->input.resize(i-1);
453 case MENU_ACTION_INPUT:
454 if(items[active_item]->kind == MN_TEXTFIELD
455 || (items[active_item]->kind == MN_NUMFIELD
456 && mn_input_char >= '0' && mn_input_char <= '9'))
458 items[active_item]->input.push_back(mn_input_char);
462 case MENU_ACTION_BACK:
466 case MENU_ACTION_NONE:
469 menuaction = MENU_ACTION_NONE;
471 assert(active_item < int(items.size()));
478 return items[hit_item]->id;
484 Menu::menu_action(MenuItem* )
488 Menu::draw_item(DrawingContext& context, int index)
490 int menu_height = get_height();
491 int menu_width = get_width();
493 MenuItem& pitem = *(items[index]);
495 int effect_offset = 0;
496 if(effect_ticks != 0) {
497 if(SDL_GetTicks() - effect_ticks > 500) {
500 Uint32 effect_time = (500 - (SDL_GetTicks() - effect_ticks)) / 4;
501 effect_offset = (index % 2) ? effect_time : -effect_time;
505 Font* text_font = default_font;
507 float y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
509 int text_width = int(text_font->get_text_width(pitem.text));
510 int input_width = int(text_font->get_text_width(pitem.input) + 10);
512 if(pitem.list.size() > 0) {
513 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
517 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
519 if(index == active_item)
522 text_font = active_font;
529 context.draw_text(deactive_font, pitem.text,
530 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
531 CENTER_ALLIGN, LAYER_GUI);
538 float x = pos_x - menu_width/2;
539 float y = y_pos - 12 - effect_offset;
540 /* Draw a horizontal line with a little 3d effect */
541 context.draw_filled_rect(Vector(x, y + 6),
542 Vector(menu_width, 4),
543 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
544 context.draw_filled_rect(Vector(x, y + 6),
545 Vector(menu_width, 2),
546 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
551 context.draw_text(label_font, pitem.text,
552 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
553 CENTER_ALLIGN, LAYER_GUI);
558 case MN_CONTROLFIELD:
560 float width = text_width + input_width + 5;
561 float text_pos = SCREEN_WIDTH/2 - width/2;
562 float input_pos = text_pos + text_width + 10;
564 context.draw_filled_rect(
565 Vector(input_pos - 5, y_pos - 10),
566 Vector(input_width + 10, 20),
567 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
568 context.draw_filled_rect(
569 Vector(input_pos - 4, y_pos - 9),
570 Vector(input_width + 8, 18),
571 Color(0, 0, 0, 0.5f), LAYER_GUI-4);
573 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
575 if(active_item == index)
576 context.draw_text(field_font,
577 pitem.get_input_with_symbol(true),
578 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
579 LEFT_ALLIGN, LAYER_GUI);
581 context.draw_text(field_font,
582 pitem.get_input_with_symbol(false),
583 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
584 LEFT_ALLIGN, LAYER_GUI);
587 context.draw_text(field_font, pitem.input,
588 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
589 LEFT_ALLIGN, LAYER_GUI);
591 context.draw_text(text_font, pitem.text,
592 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
593 LEFT_ALLIGN, LAYER_GUI);
596 case MN_STRINGSELECT:
598 int list_pos_2 = list_width + 16;
599 int list_pos = list_width/2;
600 int text_pos = (text_width + 16)/2;
603 context.draw_surface(arrow_left,
604 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
606 context.draw_surface(arrow_right,
607 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
610 /* Draw input background */
611 context.draw_filled_rect(
612 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
613 Vector(list_pos_2 + 2, 20),
614 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
615 context.draw_filled_rect(
616 Vector(x_pos - list_pos + text_pos, y_pos - 9),
617 Vector(list_pos_2, 18),
618 Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
620 context.draw_text(text_font, pitem.list[pitem.selected],
621 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
622 CENTER_ALLIGN, LAYER_GUI);
623 context.draw_text(text_font, pitem.text,
624 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
625 CENTER_ALLIGN, LAYER_GUI);
630 context.draw_text(text_font, pitem.text,
631 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
632 CENTER_ALLIGN, LAYER_GUI);
633 context.draw_surface(back,
634 Vector(x_pos + text_width/2 + 16, y_pos - 8),
641 context.draw_text(text_font, pitem.text,
642 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
643 CENTER_ALLIGN, LAYER_GUI);
646 context.draw_surface(checkbox_checked,
647 Vector(x_pos + (text_width+16)/2, y_pos - 8),
650 context.draw_surface(checkbox,
651 Vector(x_pos + (text_width+16)/2, y_pos - 8),
656 context.draw_text(text_font, pitem.text,
657 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
658 CENTER_ALLIGN, LAYER_GUI);
662 context.draw_text(text_font, pitem.text,
663 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
664 CENTER_ALLIGN, LAYER_GUI);
669 int Menu::get_width() const
671 /* The width of the menu has to be more than the width of the text
672 with the most characters */
674 for(unsigned int i = 0; i < items.size(); ++i)
676 int w = items[i]->text.size() + items[i]->input.size() + 1;
680 if( items[i]->kind == MN_TOGGLE)
685 return (menu_width * 16 + 24);
688 int Menu::get_height() const
690 return items.size() * 24;
693 /* Draw the current menu. */
695 Menu::draw(DrawingContext& context)
697 if(MouseCursor::current()) {
698 MouseCursor::current()->draw(context);
701 int menu_height = get_height();
702 int menu_width = get_width();
704 /* Draw a transparent background */
705 context.draw_filled_rect(
706 Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
707 Vector(menu_width,menu_height + 20),
708 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-10);
710 for(unsigned int i = 0; i < items.size(); ++i)
712 draw_item(context, i);
717 Menu::get_item_by_id(int id)
719 for(std::vector<MenuItem*>::iterator i = items.begin();
720 i != items.end(); ++i) {
721 MenuItem& item = **i;
727 throw std::runtime_error("MenuItem not found");
731 Menu::get_item_by_id(int id) const
733 for(std::vector<MenuItem*>::const_iterator i = items.begin();
734 i != items.end(); ++i) {
735 const MenuItem& item = **i;
741 throw std::runtime_error("MenuItem not found");
744 int Menu::get_active_item_id()
746 return items[active_item]->id;
750 Menu::is_toggled(int id) const
752 return get_item_by_id(id).toggled;
755 /* Check for menu event */
757 Menu::event(const SDL_Event& event)
759 if(effect_ticks != 0)
763 case SDL_MOUSEBUTTONDOWN:
765 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
766 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
768 if(x > pos_x - get_width()/2 &&
769 x < pos_x + get_width()/2 &&
770 y > pos_y - get_height()/2 &&
771 y < pos_y + get_height()/2)
773 menuaction = MENU_ACTION_HIT;
778 case SDL_MOUSEMOTION:
780 float x = event.motion.x * SCREEN_WIDTH/screen->w;
781 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
783 if(x > pos_x - get_width()/2 &&
784 x < pos_x + get_width()/2 &&
785 y > pos_y - get_height()/2 &&
786 y < pos_y + get_height()/2)
789 = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
791 /* only change the mouse focus to a selectable item */
792 if ((items[new_active_item]->kind != MN_HL)
793 && (items[new_active_item]->kind != MN_LABEL)
794 && (items[new_active_item]->kind != MN_DEACTIVE))
795 active_item = new_active_item;
797 if(MouseCursor::current())
798 MouseCursor::current()->set_state(MC_LINK);
802 if(MouseCursor::current())
803 MouseCursor::current()->set_state(MC_NORMAL);
814 Menu::set_active_item(int id)
816 for(size_t i = 0; i < items.size(); ++i) {
817 MenuItem* item = items[i];