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"
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;
79 while (SDL_PollEvent(&event)) {
80 if(event.type == SDL_QUIT)
81 throw std::runtime_error("received window close event");
82 main_controller->process_event(event);
86 if(background == NULL)
87 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
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)
124 last_menus.push_back(current_);
127 current_->effect_ticks = SDL_GetTicks();
133 if (last_menus.size() >= 1) {
134 current_ = last_menus.back();
135 current_->effect_ticks = SDL_GetTicks();
136 last_menus.pop_back();
143 Menu::set_current(Menu* menu)
148 menu->effect_ticks = SDL_GetTicks();
151 // just to be sure...
152 main_controller->reset();
155 MenuItem::MenuItem(MenuItemKind _kind, int _id)
156 : kind(_kind) , id(_id)
164 MenuItem::change_text(const std::string& text_)
170 MenuItem::change_input(const std::string& text_)
175 std::string MenuItem::get_input_with_symbol(bool active_item)
178 input_flickering = true;
180 input_flickering = (SDL_GetTicks() / FLICK_CURSOR_TIME) % 2;
185 sprintf(str,"%s ",input.c_str());
187 sprintf(str,"%s_",input.c_str());
189 std::string string = str;
196 for(std::vector<MenuItem*>::iterator i = items.begin();
197 i != items.end(); ++i)
204 menuaction = MENU_ACTION_NONE;
205 delete_character = 0;
206 mn_input_char = '\0';
208 pos_x = SCREEN_WIDTH/2;
209 pos_y = SCREEN_HEIGHT/2;
214 void Menu::set_pos(int x, int y, float rw, float rh)
216 pos_x = x + (int)((float)get_width() * rw);
217 pos_y = y + (int)((float)get_height() * rh);
220 /* Add an item to a menu */
222 Menu::additem(MenuItem* item)
224 items.push_back(item);
226 /* If a new menu is being built, the active item shouldn't be set to
227 * something that isnt selectable. Set the active_item to the first
228 * selectable item added
230 if (active_item == -1
231 && item->kind != MN_HL
232 && item->kind != MN_LABEL
233 && item->kind != MN_DEACTIVE) {
234 active_item = items.size() - 1;
241 additem(new MenuItem(MN_HL));
245 Menu::add_label(const std::string& text)
247 MenuItem* item = new MenuItem(MN_LABEL);
253 Menu::add_controlfield(int id, const std::string& text,
254 const std::string& mapping)
256 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
257 item->change_text(text);
258 item->change_input(mapping);
263 Menu::add_entry(int id, const std::string& text)
265 MenuItem* item = new MenuItem(MN_ACTION, id);
271 Menu::add_deactive(int id, const std::string& text)
273 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
279 Menu::add_toggle(int id, const std::string& text, bool toogled)
281 MenuItem* item = new MenuItem(MN_TOGGLE, id);
283 item->toggled = toogled;
288 Menu::add_back(const std::string& text)
290 MenuItem* item = new MenuItem(MN_BACK);
296 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
298 MenuItem* item = new MenuItem(MN_GOTO, id);
300 item->target_menu = submenu;
307 for(std::vector<MenuItem*>::iterator i = items.begin();
308 i != items.end(); ++i) {
315 /* Process actions done on the menu */
319 /** check main input controller... */
320 Uint32 ticks = SDL_GetTicks();
321 if(main_controller->pressed(Controller::UP)) {
322 menuaction = MENU_ACTION_UP;
323 menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
325 if(main_controller->hold(Controller::UP) &&
326 menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
327 menuaction = MENU_ACTION_UP;
328 menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
330 if(main_controller->pressed(Controller::DOWN)) {
331 menuaction = MENU_ACTION_DOWN;
332 menu_repeat_ticks = ticks + MENU_REPEAT_INITIAL;
334 if(main_controller->hold(Controller::DOWN) &&
335 menu_repeat_ticks != 0 && ticks > menu_repeat_ticks) {
336 menuaction = MENU_ACTION_DOWN;
337 menu_repeat_ticks = ticks + MENU_REPEAT_RATE;
339 if(main_controller->pressed(Controller::JUMP)
340 || main_controller->pressed(Controller::ACTION)
341 || main_controller->pressed(Controller::MENU_SELECT)) {
342 menuaction = MENU_ACTION_HIT;
344 if(main_controller->pressed(Controller::PAUSE_MENU)) {
345 menuaction = MENU_ACTION_BACK;
349 if(items.size() == 0)
352 int last_active_item = active_item;
359 active_item = int(items.size())-1;
360 } while ((items[active_item]->kind == MN_HL
361 || items[active_item]->kind == MN_LABEL
362 || items[active_item]->kind == MN_DEACTIVE)
363 && (active_item != last_active_item));
367 case MENU_ACTION_DOWN:
369 if(active_item < int(items.size())-1 )
373 } while ((items[active_item]->kind == MN_HL
374 || items[active_item]->kind == MN_LABEL
375 || items[active_item]->kind == MN_DEACTIVE)
376 && (active_item != last_active_item));
380 case MENU_ACTION_LEFT:
381 if(items[active_item]->kind == MN_STRINGSELECT) {
382 if(items[active_item]->selected > 0)
383 items[active_item]->selected--;
385 items[active_item]->selected = items[active_item]->list.size()-1;
389 case MENU_ACTION_RIGHT:
390 if(items[active_item]->kind == MN_STRINGSELECT) {
391 if(items[active_item]->selected+1 < items[active_item]->list.size())
392 items[active_item]->selected++;
394 items[active_item]->selected = 0;
398 case MENU_ACTION_HIT: {
399 hit_item = active_item;
400 switch (items[active_item]->kind) {
402 assert(items[active_item]->target_menu != 0);
403 Menu::push_current(items[active_item]->target_menu);
407 items[active_item]->toggled = !items[active_item]->toggled;
408 menu_action(items[active_item]);
411 case MN_CONTROLFIELD:
412 menu_action(items[active_item]);
416 menu_action(items[active_item]);
421 menuaction = MENU_ACTION_DOWN;
434 case MENU_ACTION_REMOVE:
435 if(items[active_item]->kind == MN_TEXTFIELD
436 || items[active_item]->kind == MN_NUMFIELD)
438 if(!items[active_item]->input.empty())
440 int i = items[active_item]->input.size();
442 while(delete_character > 0) /* remove charactes */
444 items[active_item]->input.resize(i-1);
451 case MENU_ACTION_INPUT:
452 if(items[active_item]->kind == MN_TEXTFIELD
453 || (items[active_item]->kind == MN_NUMFIELD
454 && mn_input_char >= '0' && mn_input_char <= '9'))
456 items[active_item]->input.push_back(mn_input_char);
460 case MENU_ACTION_BACK:
464 case MENU_ACTION_NONE:
467 menuaction = MENU_ACTION_NONE;
469 assert(active_item < int(items.size()));
476 return items[hit_item]->id;
482 Menu::menu_action(MenuItem* )
486 Menu::draw_item(DrawingContext& context, int index)
488 int menu_height = get_height();
489 int menu_width = get_width();
491 MenuItem& pitem = *(items[index]);
493 int effect_offset = 0;
494 if(effect_ticks != 0) {
495 if(SDL_GetTicks() - effect_ticks > 500) {
498 Uint32 effect_time = (500 - (SDL_GetTicks() - effect_ticks)) / 4;
499 effect_offset = (index % 2) ? effect_time : -effect_time;
503 Font* text_font = default_font;
505 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
507 int text_width = int(text_font->get_text_width(pitem.text));
508 int input_width = int(text_font->get_text_width(pitem.input) + 10);
510 if(pitem.list.size() > 0) {
511 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
515 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
517 if(index == active_item)
520 text_font = active_font;
527 context.draw_text(deactive_font, pitem.text,
528 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
529 CENTER_ALLIGN, LAYER_GUI);
536 int x = pos_x - menu_width/2;
537 int y = y_pos - 12 - effect_offset;
538 /* Draw a horizontal line with a little 3d effect */
539 context.draw_filled_rect(Vector(x, y + 6),
540 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
541 context.draw_filled_rect(Vector(x, y + 6),
542 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
547 context.draw_text(label_font, pitem.text,
548 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
549 CENTER_ALLIGN, LAYER_GUI);
554 case MN_CONTROLFIELD:
556 int width = text_width + input_width + 5;
557 int text_pos = SCREEN_WIDTH/2 - width/2;
558 int input_pos = text_pos + text_width + 10;
560 context.draw_filled_rect(
561 Vector(input_pos - 5, y_pos - 10),
562 Vector(input_width + 10, 20),
563 Color(255,255,255,255), LAYER_GUI-5);
564 context.draw_filled_rect(
565 Vector(input_pos - 4, y_pos - 9),
566 Vector(input_width + 8, 18),
567 Color(0,0,0,128), LAYER_GUI-4);
569 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
571 if(active_item == index)
572 context.draw_text(field_font,
573 pitem.get_input_with_symbol(true),
574 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
575 LEFT_ALLIGN, LAYER_GUI);
577 context.draw_text(field_font,
578 pitem.get_input_with_symbol(false),
579 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
580 LEFT_ALLIGN, LAYER_GUI);
583 context.draw_text(field_font, pitem.input,
584 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
585 LEFT_ALLIGN, LAYER_GUI);
587 context.draw_text(text_font, pitem.text,
588 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
589 LEFT_ALLIGN, LAYER_GUI);
592 case MN_STRINGSELECT:
594 int list_pos_2 = list_width + 16;
595 int list_pos = list_width/2;
596 int text_pos = (text_width + 16)/2;
599 context.draw_surface(arrow_left,
600 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
602 context.draw_surface(arrow_right,
603 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
606 /* Draw input background */
607 context.draw_filled_rect(
608 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
609 Vector(list_pos_2 + 2, 20),
610 Color(255,255,255,255), LAYER_GUI - 4);
611 context.draw_filled_rect(
612 Vector(x_pos - list_pos + text_pos, y_pos - 9),
613 Vector(list_pos_2, 18),
614 Color(0,0,0,128), LAYER_GUI - 5);
616 context.draw_text(text_font, pitem.list[pitem.selected],
617 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
618 CENTER_ALLIGN, LAYER_GUI);
619 context.draw_text(text_font, pitem.text,
620 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
621 CENTER_ALLIGN, LAYER_GUI);
626 context.draw_text(text_font, pitem.text,
627 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
628 CENTER_ALLIGN, LAYER_GUI);
629 context.draw_surface(back,
630 Vector(x_pos + text_width/2 + 16, y_pos - 8),
637 context.draw_text(text_font, pitem.text,
638 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
639 CENTER_ALLIGN, LAYER_GUI);
642 context.draw_surface(checkbox_checked,
643 Vector(x_pos + (text_width+16)/2, y_pos - 8),
646 context.draw_surface(checkbox,
647 Vector(x_pos + (text_width+16)/2, y_pos - 8),
652 context.draw_text(text_font, pitem.text,
653 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
654 CENTER_ALLIGN, LAYER_GUI);
658 context.draw_text(text_font, pitem.text,
659 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
660 CENTER_ALLIGN, LAYER_GUI);
665 int Menu::get_width() const
667 /* The width of the menu has to be more than the width of the text
668 with the most characters */
670 for(unsigned int i = 0; i < items.size(); ++i)
672 int w = items[i]->text.size() + items[i]->input.size() + 1;
676 if( items[i]->kind == MN_TOGGLE)
681 return (menu_width * 16 + 24);
684 int Menu::get_height() const
686 return items.size() * 24;
689 /* Draw the current menu. */
691 Menu::draw(DrawingContext& context)
693 if(MouseCursor::current()) {
694 MouseCursor::current()->draw(context);
697 int menu_height = get_height();
698 int menu_width = get_width();
700 /* Draw a transparent background */
701 context.draw_filled_rect(
702 Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
703 Vector(menu_width,menu_height + 20),
704 Color(150,180,200,125), LAYER_GUI-10);
706 for(unsigned int i = 0; i < items.size(); ++i)
708 draw_item(context, i);
713 Menu::get_item_by_id(int id)
715 for(std::vector<MenuItem*>::iterator i = items.begin();
716 i != items.end(); ++i) {
717 MenuItem& item = **i;
723 throw std::runtime_error("MenuItem not found");
727 Menu::get_item_by_id(int id) const
729 for(std::vector<MenuItem*>::const_iterator i = items.begin();
730 i != items.end(); ++i) {
731 const MenuItem& item = **i;
737 throw std::runtime_error("MenuItem not found");
740 int Menu::get_active_item_id()
742 return items[active_item]->id;
746 Menu::is_toggled(int id) const
748 return get_item_by_id(id).toggled;
751 /* Check for menu event */
753 Menu::event(const SDL_Event& event)
755 if(effect_ticks != 0)
759 case SDL_MOUSEBUTTONDOWN:
761 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
762 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
764 if(x > pos_x - get_width()/2 &&
765 x < pos_x + get_width()/2 &&
766 y > pos_y - get_height()/2 &&
767 y < pos_y + get_height()/2)
769 menuaction = MENU_ACTION_HIT;
774 case SDL_MOUSEMOTION:
776 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
777 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
779 if(x > pos_x - get_width()/2 &&
780 x < pos_x + get_width()/2 &&
781 y > pos_y - get_height()/2 &&
782 y < pos_y + get_height()/2)
784 int new_active_item = (y - (pos_y - get_height()/2)) / 24;
786 /* only change the mouse focus to a selectable item */
787 if ((items[new_active_item]->kind != MN_HL)
788 && (items[new_active_item]->kind != MN_LABEL)
789 && (items[new_active_item]->kind != MN_DEACTIVE))
790 active_item = new_active_item;
792 if(MouseCursor::current())
793 MouseCursor::current()->set_state(MC_LINK);
797 if(MouseCursor::current())
798 MouseCursor::current()->set_state(MC_NORMAL);
809 Menu::set_active_item(int id)
811 for(size_t i = 0; i < items.size(); ++i) {
812 MenuItem* item = items[i];