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>
32 #include "app/globals.h"
34 #include "video/screen.h"
35 #include "video/drawing_context.h"
36 #include "app/setup.h"
37 #include "app/gettext.h"
38 #include "math/vector.h"
40 #include "control/joystickkeyboardcontroller.h"
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;
48 using namespace SuperTux;
51 Surface* checkbox_checked;
56 std::vector<Menu*> Menu::last_menus;
57 Menu* Menu::current_ = 0;
58 Font* Menu::default_font;
59 Font* Menu::active_font;
60 Font* Menu::deactive_font;
61 Font* Menu::label_font;
62 Font* Menu::field_font;
64 /* just displays a Yes/No text that can be used to confirm stuff */
65 bool confirm_dialog(Surface *background, std::string text)
67 //Surface* cap_screen = Surface::CaptureScreen();
68 Menu* dialog = new Menu;
69 dialog->add_deactive(-1, text);
71 dialog->add_entry(true, _("Yes"));
72 dialog->add_entry(false, _("No"));
75 Menu::set_current(dialog);
77 DrawingContext context;
83 if(event.type == SDL_QUIT)
84 throw std::runtime_error("received window close event");
86 while (SDL_PollEvent(&event)) {
90 if(background == NULL)
91 context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
93 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
95 dialog->draw(context);
98 switch (dialog->check())
102 Menu::set_current(0);
108 Menu::set_current(0);
116 mouse_cursor->draw(context);
117 context.do_drawing();
125 Menu::push_current(Menu* pmenu)
128 last_menus.push_back(current_);
131 current_->effect.start(500);
137 if (last_menus.size() >= 1) {
138 current_ = last_menus.back();
139 current_->effect.start(500);
141 last_menus.pop_back();
148 Menu::set_current(Menu* menu)
153 menu->effect.start(500);
156 // just to be sure...
157 main_controller->reset();
160 MenuItem::MenuItem(MenuItemKind _kind, int _id)
161 : kind(_kind) , id(_id)
166 input_flickering = false;
167 input_flickering_timer.init(true);
168 input_flickering_timer.start(FLICK_CURSOR_TIME);
172 MenuItem::change_text(const std::string& text_)
178 MenuItem::change_input(const std::string& text_)
183 std::string MenuItem::get_input_with_symbol(bool active_item)
186 input_flickering = true;
189 if(input_flickering_timer.get_left() < 0)
192 input_flickering = false;
194 input_flickering = true;
195 input_flickering_timer.start(FLICK_CURSOR_TIME);
201 sprintf(str,"%s ",input.c_str());
203 sprintf(str,"%s_",input.c_str());
205 std::string string = str;
212 for(std::vector<MenuItem*>::iterator i = items.begin();
213 i != items.end(); ++i)
220 menuaction = MENU_ACTION_NONE;
221 delete_character = 0;
222 mn_input_char = '\0';
224 pos_x = SCREEN_WIDTH/2;
225 pos_y = SCREEN_HEIGHT/2;
230 repeat_timer.init(true);
233 void Menu::set_pos(int x, int y, float rw, float rh)
235 pos_x = x + (int)((float)get_width() * rw);
236 pos_y = y + (int)((float)get_height() * rh);
239 /* Add an item to a menu */
241 Menu::additem(MenuItem* item)
243 items.push_back(item);
245 /* If a new menu is being built, the active item shouldn't be set to
246 * something that isnt selectable. Set the active_item to the first
247 * selectable item added
249 if (active_item == -1
250 && item->kind != MN_HL
251 && item->kind != MN_LABEL
252 && item->kind != MN_DEACTIVE) {
253 active_item = items.size() - 1;
260 additem(new MenuItem(MN_HL));
264 Menu::add_label(const std::string& text)
266 MenuItem* item = new MenuItem(MN_LABEL);
272 Menu::add_controlfield(int id, const std::string& text,
273 const std::string& mapping)
275 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
276 item->change_text(text);
277 item->change_input(mapping);
282 Menu::add_entry(int id, const std::string& text)
284 MenuItem* item = new MenuItem(MN_ACTION, id);
290 Menu::add_deactive(int id, const std::string& text)
292 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
298 Menu::add_toggle(int id, const std::string& text, bool toogled)
300 MenuItem* item = new MenuItem(MN_TOGGLE, id);
302 item->toggled = toogled;
307 Menu::add_back(const std::string& text)
309 MenuItem* item = new MenuItem(MN_BACK);
315 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
317 MenuItem* item = new MenuItem(MN_GOTO, id);
319 item->target_menu = submenu;
326 for(std::vector<MenuItem*>::iterator i = items.begin();
327 i != items.end(); ++i) {
334 /* Process actions done on the menu */
338 /** check main input controller... */
339 if(main_controller->pressed(Controller::UP)) {
340 menuaction = MENU_ACTION_UP;
341 repeat_timer.start(MENU_REPEAT_INITIAL);
343 if(main_controller->hold(Controller::UP) && !repeat_timer.check()) {
344 menuaction = MENU_ACTION_UP;
345 repeat_timer.start(MENU_REPEAT_RATE);
347 if(main_controller->pressed(Controller::DOWN)) {
348 menuaction = MENU_ACTION_DOWN;
349 repeat_timer.start(MENU_REPEAT_INITIAL);
351 if(main_controller->hold(Controller::DOWN) && !repeat_timer.check()) {
352 menuaction = MENU_ACTION_DOWN;
353 repeat_timer.start(MENU_REPEAT_RATE);
355 if(main_controller->pressed(Controller::JUMP)
356 || main_controller->pressed(Controller::ACTION)
357 || main_controller->pressed(Controller::MENU_SELECT)) {
358 menuaction = MENU_ACTION_HIT;
360 if(main_controller->pressed(Controller::PAUSE_MENU)) {
361 menuaction = MENU_ACTION_BACK;
365 if(items.size() == 0)
368 int last_active_item = active_item;
375 active_item = int(items.size())-1;
376 } while ((items[active_item]->kind == MN_HL
377 || items[active_item]->kind == MN_LABEL
378 || items[active_item]->kind == MN_DEACTIVE)
379 && (active_item != last_active_item));
383 case MENU_ACTION_DOWN:
385 if(active_item < int(items.size())-1 )
389 } while ((items[active_item]->kind == MN_HL
390 || items[active_item]->kind == MN_LABEL
391 || items[active_item]->kind == MN_DEACTIVE)
392 && (active_item != last_active_item));
396 case MENU_ACTION_LEFT:
397 if(items[active_item]->kind == MN_STRINGSELECT) {
398 if(items[active_item]->selected > 0)
399 items[active_item]->selected--;
401 items[active_item]->selected = items[active_item]->list.size()-1;
405 case MENU_ACTION_RIGHT:
406 if(items[active_item]->kind == MN_STRINGSELECT) {
407 if(items[active_item]->selected+1 < items[active_item]->list.size())
408 items[active_item]->selected++;
410 items[active_item]->selected = 0;
414 case MENU_ACTION_HIT: {
415 hit_item = active_item;
416 switch (items[active_item]->kind) {
418 assert(items[active_item]->target_menu != 0);
419 Menu::push_current(items[active_item]->target_menu);
423 items[active_item]->toggled = !items[active_item]->toggled;
424 menu_action(items[active_item]);
427 case MN_CONTROLFIELD:
428 menu_action(items[active_item]);
432 menu_action(items[active_item]);
437 menuaction = MENU_ACTION_DOWN;
450 case MENU_ACTION_REMOVE:
451 if(items[active_item]->kind == MN_TEXTFIELD
452 || items[active_item]->kind == MN_NUMFIELD)
454 if(!items[active_item]->input.empty())
456 int i = items[active_item]->input.size();
458 while(delete_character > 0) /* remove charactes */
460 items[active_item]->input.resize(i-1);
467 case MENU_ACTION_INPUT:
468 if(items[active_item]->kind == MN_TEXTFIELD
469 || (items[active_item]->kind == MN_NUMFIELD
470 && mn_input_char >= '0' && mn_input_char <= '9'))
472 items[active_item]->input.push_back(mn_input_char);
476 case MENU_ACTION_BACK:
480 case MENU_ACTION_NONE:
483 menuaction = MENU_ACTION_NONE;
485 assert(active_item < int(items.size()));
492 return items[hit_item]->id;
498 Menu::menu_action(MenuItem* )
502 Menu::draw_item(DrawingContext& context, int index)
504 int menu_height = get_height();
505 int menu_width = get_width();
507 MenuItem& pitem = *(items[index]);
509 int effect_offset = 0;
514 effect_time = effect.get_left() / 4;
516 effect_offset = (index % 2) ? effect_time : -effect_time;
519 Font* text_font = default_font;
521 int y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
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;
543 context.draw_text(deactive_font, pitem.text,
544 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
545 CENTER_ALLIGN, LAYER_GUI);
552 int x = pos_x - menu_width/2;
553 int y = y_pos - 12 - effect_offset;
554 /* Draw a horizontal line with a little 3d effect */
555 context.draw_filled_rect(Vector(x, y + 6),
556 Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
557 context.draw_filled_rect(Vector(x, y + 6),
558 Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
563 context.draw_text(label_font, pitem.text,
564 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
565 CENTER_ALLIGN, LAYER_GUI);
570 case MN_CONTROLFIELD:
572 int width = text_width + input_width + 5;
573 int text_pos = SCREEN_WIDTH/2 - width/2;
574 int input_pos = text_pos + text_width + 10;
576 context.draw_filled_rect(
577 Vector(input_pos - 5, y_pos - 10),
578 Vector(input_width + 10, 20),
579 Color(255,255,255,255), LAYER_GUI-5);
580 context.draw_filled_rect(
581 Vector(input_pos - 4, y_pos - 9),
582 Vector(input_width + 8, 18),
583 Color(0,0,0,128), LAYER_GUI-4);
585 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
587 if(active_item == index)
588 context.draw_text(field_font,
589 pitem.get_input_with_symbol(true),
590 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
591 LEFT_ALLIGN, LAYER_GUI);
593 context.draw_text(field_font,
594 pitem.get_input_with_symbol(false),
595 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
596 LEFT_ALLIGN, LAYER_GUI);
599 context.draw_text(field_font, pitem.input,
600 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
601 LEFT_ALLIGN, LAYER_GUI);
603 context.draw_text(text_font, pitem.text,
604 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
605 LEFT_ALLIGN, LAYER_GUI);
608 case MN_STRINGSELECT:
610 int list_pos_2 = list_width + 16;
611 int list_pos = list_width/2;
612 int text_pos = (text_width + 16)/2;
615 context.draw_surface(arrow_left,
616 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
618 context.draw_surface(arrow_right,
619 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
622 /* Draw input background */
623 context.draw_filled_rect(
624 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
625 Vector(list_pos_2 + 2, 20),
626 Color(255,255,255,255), LAYER_GUI - 4);
627 context.draw_filled_rect(
628 Vector(x_pos - list_pos + text_pos, y_pos - 9),
629 Vector(list_pos_2, 18),
630 Color(0,0,0,128), LAYER_GUI - 5);
632 context.draw_text(text_font, pitem.list[pitem.selected],
633 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
634 CENTER_ALLIGN, LAYER_GUI);
635 context.draw_text(text_font, pitem.text,
636 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
637 CENTER_ALLIGN, LAYER_GUI);
642 context.draw_text(text_font, pitem.text,
643 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
644 CENTER_ALLIGN, LAYER_GUI);
645 context.draw_surface(back,
646 Vector(x_pos + text_width/2 + 16, y_pos - 8),
653 context.draw_text(text_font, pitem.text,
654 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
655 CENTER_ALLIGN, LAYER_GUI);
658 context.draw_surface(checkbox_checked,
659 Vector(x_pos + (text_width+16)/2, y_pos - 8),
662 context.draw_surface(checkbox,
663 Vector(x_pos + (text_width+16)/2, y_pos - 8),
668 context.draw_text(text_font, pitem.text,
669 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
670 CENTER_ALLIGN, LAYER_GUI);
674 context.draw_text(text_font, pitem.text,
675 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
676 CENTER_ALLIGN, LAYER_GUI);
681 int Menu::get_width() const
683 /* The width of the menu has to be more than the width of the text
684 with the most characters */
686 for(unsigned int i = 0; i < items.size(); ++i)
688 int w = items[i]->text.size() + items[i]->input.size() + 1;
692 if( items[i]->kind == MN_TOGGLE)
697 return (menu_width * 16 + 24);
700 int Menu::get_height() const
702 return items.size() * 24;
705 /* Draw the current menu. */
707 Menu::draw(DrawingContext& context)
709 int menu_height = get_height();
710 int menu_width = get_width();
712 /* Draw a transparent background */
713 context.draw_filled_rect(
714 Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
715 Vector(menu_width,menu_height + 20),
716 Color(150,180,200,125), LAYER_GUI-10);
718 for(unsigned int i = 0; i < items.size(); ++i)
720 draw_item(context, i);
725 Menu::get_item_by_id(int id)
727 for(std::vector<MenuItem*>::iterator i = items.begin();
728 i != items.end(); ++i) {
729 MenuItem& item = **i;
735 throw std::runtime_error("MenuItem not found");
739 Menu::get_item_by_id(int id) const
741 for(std::vector<MenuItem*>::const_iterator i = items.begin();
742 i != items.end(); ++i) {
743 const MenuItem& item = **i;
749 throw std::runtime_error("MenuItem not found");
752 int Menu::get_active_item_id()
754 return items[active_item]->id;
758 Menu::is_toggled(int id) const
760 return get_item_by_id(id).toggled;
763 /* Check for menu event */
765 Menu::event(const SDL_Event& event)
771 case SDL_MOUSEBUTTONDOWN:
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 menuaction = MENU_ACTION_HIT;
786 case SDL_MOUSEMOTION:
788 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
789 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
791 if(x > pos_x - get_width()/2 &&
792 x < pos_x + get_width()/2 &&
793 y > pos_y - get_height()/2 &&
794 y < pos_y + get_height()/2)
796 int new_active_item = (y - (pos_y - get_height()/2)) / 24;
798 /* only change the mouse focus to a selectable item */
799 if ((items[new_active_item]->kind != MN_HL)
800 && (items[new_active_item]->kind != MN_LABEL)
801 && (items[new_active_item]->kind != MN_DEACTIVE))
802 active_item = new_active_item;
804 if(MouseCursor::current())
805 MouseCursor::current()->set_state(MC_LINK);
809 if(MouseCursor::current())
810 MouseCursor::current()->set_state(MC_NORMAL);
821 Menu::set_active_item(int id)
823 for(size_t i = 0; i < items.size(); ++i) {
824 MenuItem* item = items[i];