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.h"
34 #include "video/drawing_context.h"
36 #include "math/vector.h"
38 #include "resources.h"
39 #include "control/joystickkeyboardcontroller.h"
41 static const int MENU_REPEAT_INITIAL = 400;
42 static const int MENU_REPEAT_RATE = 200;
43 static const int FLICK_CURSOR_TIME = 500;
45 extern SDL_Surface* screen;
48 Surface* checkbox_checked;
53 std::vector<Menu*> Menu::last_menus;
54 Menu* Menu::current_ = 0;
55 Font* Menu::default_font;
56 Font* Menu::active_font;
57 Font* Menu::deactive_font;
58 Font* Menu::label_font;
59 Font* Menu::field_font;
61 /* just displays a Yes/No text that can be used to confirm stuff */
62 bool confirm_dialog(Surface *background, std::string text)
64 //Surface* cap_screen = Surface::CaptureScreen();
65 Menu* dialog = new Menu;
66 dialog->add_deactive(-1, text);
68 dialog->add_entry(true, _("Yes"));
69 dialog->add_entry(false, _("No"));
72 Menu::set_current(dialog);
74 DrawingContext context;
80 if(event.type == SDL_QUIT)
81 throw std::runtime_error("received window close event");
83 while (SDL_PollEvent(&event)) {
87 if(background == NULL)
88 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
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)
125 last_menus.push_back(current_);
128 current_->effect_ticks = SDL_GetTicks();
134 if (last_menus.size() >= 1) {
135 current_ = last_menus.back();
136 current_->effect_ticks = SDL_GetTicks();
137 last_menus.pop_back();
144 Menu::set_current(Menu* menu)
149 menu->effect_ticks = SDL_GetTicks();
152 // just to be sure...
153 main_controller->reset();
156 MenuItem::MenuItem(MenuItemKind _kind, int _id)
157 : kind(_kind) , id(_id)
165 MenuItem::change_text(const std::string& text_)
171 MenuItem::change_input(const std::string& text_)
176 std::string MenuItem::get_input_with_symbol(bool active_item)
179 input_flickering = true;
181 input_flickering = (SDL_GetTicks() / FLICK_CURSOR_TIME) % 2;
186 sprintf(str,"%s ",input.c_str());
188 sprintf(str,"%s_",input.c_str());
190 std::string string = str;
197 for(std::vector<MenuItem*>::iterator i = items.begin();
198 i != items.end(); ++i)
205 menuaction = MENU_ACTION_NONE;
206 delete_character = 0;
207 mn_input_char = '\0';
209 pos_x = SCREEN_WIDTH/2;
210 pos_y = SCREEN_HEIGHT/2;
215 void Menu::set_pos(int x, int y, float rw, float rh)
217 pos_x = x + (int)((float)get_width() * rw);
218 pos_y = y + (int)((float)get_height() * rh);
221 /* Add an item to a menu */
223 Menu::additem(MenuItem* item)
225 items.push_back(item);
227 /* If a new menu is being built, the active item shouldn't be set to
228 * something that isnt selectable. Set the active_item to the first
229 * selectable item added
231 if (active_item == -1
232 && item->kind != MN_HL
233 && item->kind != MN_LABEL
234 && item->kind != MN_DEACTIVE) {
235 active_item = items.size() - 1;
242 additem(new MenuItem(MN_HL));
246 Menu::add_label(const std::string& text)
248 MenuItem* item = new MenuItem(MN_LABEL);
254 Menu::add_controlfield(int id, const std::string& text,
255 const std::string& mapping)
257 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
258 item->change_text(text);
259 item->change_input(mapping);
264 Menu::add_entry(int id, const std::string& text)
266 MenuItem* item = new MenuItem(MN_ACTION, id);
272 Menu::add_deactive(int id, const std::string& text)
274 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
280 Menu::add_toggle(int id, const std::string& text, bool toogled)
282 MenuItem* item = new MenuItem(MN_TOGGLE, id);
284 item->toggled = toogled;
289 Menu::add_back(const std::string& text)
291 MenuItem* item = new MenuItem(MN_BACK);
297 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
299 MenuItem* item = new MenuItem(MN_GOTO, id);
301 item->target_menu = submenu;
308 for(std::vector<MenuItem*>::iterator i = items.begin();
309 i != items.end(); ++i) {
316 /* Process actions done on the menu */
320 /** check main input controller... */
321 Uint32 ticks = SDL_GetTicks();
322 if(main_controller->pressed(Controller::UP)) {
323 menuaction = MENU_ACTION_UP;
324 menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
326 if(main_controller->hold(Controller::UP) &&
327 menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
328 menuaction = MENU_ACTION_UP;
329 menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
331 if(main_controller->pressed(Controller::DOWN)) {
332 menuaction = MENU_ACTION_DOWN;
333 menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
335 if(main_controller->hold(Controller::DOWN) &&
336 menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
337 menuaction = MENU_ACTION_DOWN;
338 menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
340 if(main_controller->pressed(Controller::JUMP)
341 || main_controller->pressed(Controller::ACTION)
342 || main_controller->pressed(Controller::MENU_SELECT)) {
343 menuaction = MENU_ACTION_HIT;
345 if(main_controller->pressed(Controller::PAUSE_MENU)) {
346 menuaction = MENU_ACTION_BACK;
350 if(items.size() == 0)
353 int last_active_item = active_item;
360 active_item = int(items.size())-1;
361 } while ((items[active_item]->kind == MN_HL
362 || items[active_item]->kind == MN_LABEL
363 || items[active_item]->kind == MN_DEACTIVE)
364 && (active_item != last_active_item));
368 case MENU_ACTION_DOWN:
370 if(active_item < int(items.size())-1 )
374 } while ((items[active_item]->kind == MN_HL
375 || items[active_item]->kind == MN_LABEL
376 || items[active_item]->kind == MN_DEACTIVE)
377 && (active_item != last_active_item));
381 case MENU_ACTION_LEFT:
382 if(items[active_item]->kind == MN_STRINGSELECT) {
383 if(items[active_item]->selected > 0)
384 items[active_item]->selected--;
386 items[active_item]->selected = items[active_item]->list.size()-1;
390 case MENU_ACTION_RIGHT:
391 if(items[active_item]->kind == MN_STRINGSELECT) {
392 if(items[active_item]->selected+1 < items[active_item]->list.size())
393 items[active_item]->selected++;
395 items[active_item]->selected = 0;
399 case MENU_ACTION_HIT: {
400 hit_item = active_item;
401 switch (items[active_item]->kind) {
403 assert(items[active_item]->target_menu != 0);
404 Menu::push_current(items[active_item]->target_menu);
408 items[active_item]->toggled = !items[active_item]->toggled;
409 menu_action(items[active_item]);
412 case MN_CONTROLFIELD:
413 menu_action(items[active_item]);
417 menu_action(items[active_item]);
422 menuaction = MENU_ACTION_DOWN;
435 case MENU_ACTION_REMOVE:
436 if(items[active_item]->kind == MN_TEXTFIELD
437 || items[active_item]->kind == MN_NUMFIELD)
439 if(!items[active_item]->input.empty())
441 int i = items[active_item]->input.size();
443 while(delete_character > 0) /* remove charactes */
445 items[active_item]->input.resize(i-1);
452 case MENU_ACTION_INPUT:
453 if(items[active_item]->kind == MN_TEXTFIELD
454 || (items[active_item]->kind == MN_NUMFIELD
455 && mn_input_char >= '0' && mn_input_char <= '9'))
457 items[active_item]->input.push_back(mn_input_char);
461 case MENU_ACTION_BACK:
465 case MENU_ACTION_NONE:
468 menuaction = MENU_ACTION_NONE;
470 assert(active_item < int(items.size()));
477 return items[hit_item]->id;
483 Menu::menu_action(MenuItem* )
487 Menu::draw_item(DrawingContext& context, int index)
489 int menu_height = get_height();
490 int menu_width = get_width();
492 MenuItem& pitem = *(items[index]);
494 int effect_offset = 0;
495 if(effect_ticks != 0) {
496 if(SDL_GetTicks() - effect_ticks > 500) {
499 Uint32 effect_time = (500 - (SDL_GetTicks() - effect_ticks)) / 4;
500 effect_offset = (index % 2) ? effect_time : -effect_time;
504 Font* text_font = default_font;
506 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
508 int text_width = int(text_font->get_text_width(pitem.text));
509 int input_width = int(text_font->get_text_width(pitem.input) + 10);
511 if(pitem.list.size() > 0) {
512 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
516 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
518 if(index == active_item)
521 text_font = active_font;
528 context.draw_text(deactive_font, pitem.text,
529 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
530 CENTER_ALLIGN, LAYER_GUI);
537 int x = pos_x - menu_width/2;
538 int y = y_pos - 12 - effect_offset;
539 /* Draw a horizontal line with a little 3d effect */
540 context.draw_filled_rect(Vector(x, y + 6),
541 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
542 context.draw_filled_rect(Vector(x, y + 6),
543 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
548 context.draw_text(label_font, pitem.text,
549 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
550 CENTER_ALLIGN, LAYER_GUI);
555 case MN_CONTROLFIELD:
557 int width = text_width + input_width + 5;
558 int text_pos = SCREEN_WIDTH/2 - width/2;
559 int input_pos = text_pos + text_width + 10;
561 context.draw_filled_rect(
562 Vector(input_pos - 5, y_pos - 10),
563 Vector(input_width + 10, 20),
564 Color(255,255,255,255), LAYER_GUI-5);
565 context.draw_filled_rect(
566 Vector(input_pos - 4, y_pos - 9),
567 Vector(input_width + 8, 18),
568 Color(0,0,0,128), LAYER_GUI-4);
570 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
572 if(active_item == index)
573 context.draw_text(field_font,
574 pitem.get_input_with_symbol(true),
575 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
576 LEFT_ALLIGN, LAYER_GUI);
578 context.draw_text(field_font,
579 pitem.get_input_with_symbol(false),
580 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
581 LEFT_ALLIGN, LAYER_GUI);
584 context.draw_text(field_font, pitem.input,
585 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
586 LEFT_ALLIGN, LAYER_GUI);
588 context.draw_text(text_font, pitem.text,
589 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
590 LEFT_ALLIGN, LAYER_GUI);
593 case MN_STRINGSELECT:
595 int list_pos_2 = list_width + 16;
596 int list_pos = list_width/2;
597 int text_pos = (text_width + 16)/2;
600 context.draw_surface(arrow_left,
601 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
603 context.draw_surface(arrow_right,
604 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
607 /* Draw input background */
608 context.draw_filled_rect(
609 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
610 Vector(list_pos_2 + 2, 20),
611 Color(255,255,255,255), LAYER_GUI - 4);
612 context.draw_filled_rect(
613 Vector(x_pos - list_pos + text_pos, y_pos - 9),
614 Vector(list_pos_2, 18),
615 Color(0,0,0,128), LAYER_GUI - 5);
617 context.draw_text(text_font, pitem.list[pitem.selected],
618 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
619 CENTER_ALLIGN, LAYER_GUI);
620 context.draw_text(text_font, pitem.text,
621 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
622 CENTER_ALLIGN, LAYER_GUI);
627 context.draw_text(text_font, pitem.text,
628 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
629 CENTER_ALLIGN, LAYER_GUI);
630 context.draw_surface(back,
631 Vector(x_pos + text_width/2 + 16, y_pos - 8),
638 context.draw_text(text_font, pitem.text,
639 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
640 CENTER_ALLIGN, LAYER_GUI);
643 context.draw_surface(checkbox_checked,
644 Vector(x_pos + (text_width+16)/2, y_pos - 8),
647 context.draw_surface(checkbox,
648 Vector(x_pos + (text_width+16)/2, y_pos - 8),
653 context.draw_text(text_font, pitem.text,
654 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
655 CENTER_ALLIGN, LAYER_GUI);
659 context.draw_text(text_font, pitem.text,
660 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
661 CENTER_ALLIGN, LAYER_GUI);
666 int Menu::get_width() const
668 /* The width of the menu has to be more than the width of the text
669 with the most characters */
671 for(unsigned int i = 0; i < items.size(); ++i)
673 int w = items[i]->text.size() + items[i]->input.size() + 1;
677 if( items[i]->kind == MN_TOGGLE)
682 return (menu_width * 16 + 24);
685 int Menu::get_height() const
687 return items.size() * 24;
690 /* Draw the current menu. */
692 Menu::draw(DrawingContext& context)
694 int menu_height = get_height();
695 int menu_width = get_width();
697 /* Draw a transparent background */
698 context.draw_filled_rect(
699 Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
700 Vector(menu_width,menu_height + 20),
701 Color(150,180,200,125), LAYER_GUI-10);
703 for(unsigned int i = 0; i < items.size(); ++i)
705 draw_item(context, i);
710 Menu::get_item_by_id(int id)
712 for(std::vector<MenuItem*>::iterator i = items.begin();
713 i != items.end(); ++i) {
714 MenuItem& item = **i;
720 throw std::runtime_error("MenuItem not found");
724 Menu::get_item_by_id(int id) const
726 for(std::vector<MenuItem*>::const_iterator i = items.begin();
727 i != items.end(); ++i) {
728 const MenuItem& item = **i;
734 throw std::runtime_error("MenuItem not found");
737 int Menu::get_active_item_id()
739 return items[active_item]->id;
743 Menu::is_toggled(int id) const
745 return get_item_by_id(id).toggled;
748 /* Check for menu event */
750 Menu::event(const SDL_Event& event)
752 if(effect_ticks != 0)
756 case SDL_MOUSEBUTTONDOWN:
758 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
759 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
761 if(x > pos_x - get_width()/2 &&
762 x < pos_x + get_width()/2 &&
763 y > pos_y - get_height()/2 &&
764 y < pos_y + get_height()/2)
766 menuaction = MENU_ACTION_HIT;
771 case SDL_MOUSEMOTION:
773 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
774 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
776 if(x > pos_x - get_width()/2 &&
777 x < pos_x + get_width()/2 &&
778 y > pos_y - get_height()/2 &&
779 y < pos_y + get_height()/2)
781 int new_active_item = (y - (pos_y - get_height()/2)) / 24;
783 /* only change the mouse focus to a selectable item */
784 if ((items[new_active_item]->kind != MN_HL)
785 && (items[new_active_item]->kind != MN_LABEL)
786 && (items[new_active_item]->kind != MN_DEACTIVE))
787 active_item = new_active_item;
789 if(MouseCursor::current())
790 MouseCursor::current()->set_state(MC_LINK);
794 if(MouseCursor::current())
795 MouseCursor::current()->set_state(MC_NORMAL);
806 Menu::set_active_item(int id)
808 for(size_t i = 0; i < items.size(); ++i) {
809 MenuItem* item = items[i];