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>
34 #include "mainloop.hpp"
35 #include "video/drawing_context.hpp"
36 #include "gettext.hpp"
37 #include "math/vector.hpp"
39 #include "resources.hpp"
41 #include "control/joystickkeyboardcontroller.hpp"
43 static const float MENU_REPEAT_INITIAL = 0.4f;
44 static const float MENU_REPEAT_RATE = 0.2f;
45 static const float FLICK_CURSOR_TIME = 0.5f;
47 extern SDL_Surface* screen;
49 std::vector<Menu*> Menu::last_menus;
50 Menu* Menu::current_ = 0;
51 Font* Menu::default_font;
52 Font* Menu::active_font;
53 Font* Menu::deactive_font;
54 Font* Menu::label_font;
55 Font* Menu::field_font;
57 /* just displays a Yes/No text that can be used to confirm stuff */
58 bool confirm_dialog(Surface *background, std::string text)
60 //Surface* cap_screen = Surface::CaptureScreen();
61 Menu* dialog = new Menu;
62 dialog->add_deactive(-1, text);
64 dialog->add_entry(true, _("Yes"));
65 dialog->add_entry(false, _("No"));
68 Menu::set_current(dialog);
70 DrawingContext context;
72 // TODO make this a screen and not another mainloop...
76 while (SDL_PollEvent(&event)) {
77 if(event.type == SDL_QUIT)
79 main_controller->process_event(event);
83 if(background == NULL)
84 context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
87 context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
89 dialog->draw(context);
92 switch (dialog->check())
102 Menu::set_current(0);
110 mouse_cursor->draw(context);
111 context.do_drawing();
119 Menu::push_current(Menu* pmenu)
122 last_menus.push_back(current_);
125 current_->effect_time = real_time;
131 if (last_menus.size() >= 1) {
132 current_ = last_menus.back();
133 current_->effect_time = real_time;
134 last_menus.pop_back();
141 Menu::set_current(Menu* menu)
146 menu->effect_time = real_time;
149 // just to be sure...
150 main_controller->reset();
153 MenuItem::MenuItem(MenuItemKind _kind, int _id)
154 : kind(_kind) , id(_id)
162 MenuItem::change_text(const std::string& text_)
168 MenuItem::change_input(const std::string& text_)
173 std::string MenuItem::get_input_with_symbol(bool active_item)
176 input_flickering = true;
178 input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
183 snprintf(str, sizeof(str), "%s ",input.c_str());
185 snprintf(str, sizeof(str), "%s_",input.c_str());
187 std::string string = str;
194 for(std::vector<MenuItem*>::iterator i = items.begin();
195 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;
213 checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
214 checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
215 back.reset(new Surface("images/engine/menu/arrow-back.png"));
216 arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
217 arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
220 void Menu::set_pos(float x, float y, float rw, float rh)
222 pos_x = x + get_width() * rw;
223 pos_y = y + get_height() * rh;
226 /* Add an item to a menu */
228 Menu::additem(MenuItem* item)
230 items.push_back(item);
232 /* If a new menu is being built, the active item shouldn't be set to
233 * something that isnt selectable. Set the active_item to the first
234 * selectable item added
236 if (active_item == -1
237 && item->kind != MN_HL
238 && item->kind != MN_LABEL
239 && item->kind != MN_DEACTIVE) {
240 active_item = items.size() - 1;
247 additem(new MenuItem(MN_HL));
251 Menu::add_label(const std::string& text)
253 MenuItem* item = new MenuItem(MN_LABEL);
259 Menu::add_controlfield(int id, const std::string& text,
260 const std::string& mapping)
262 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
263 item->change_text(text);
264 item->change_input(mapping);
269 Menu::add_entry(int id, const std::string& text)
271 MenuItem* item = new MenuItem(MN_ACTION, id);
277 Menu::add_deactive(int id, const std::string& text)
279 MenuItem* item = new MenuItem(MN_DEACTIVE, id);
285 Menu::add_toggle(int id, const std::string& text, bool toogled)
287 MenuItem* item = new MenuItem(MN_TOGGLE, id);
289 item->toggled = toogled;
294 Menu::add_back(const std::string& text)
296 MenuItem* item = new MenuItem(MN_BACK);
302 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
304 MenuItem* item = new MenuItem(MN_GOTO, id);
306 item->target_menu = submenu;
313 for(std::vector<MenuItem*>::iterator i = items.begin();
314 i != items.end(); ++i) {
321 /* Process actions done on the menu */
325 /** check main input controller... */
326 if(main_controller->pressed(Controller::UP)) {
327 menuaction = MENU_ACTION_UP;
328 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
330 if(main_controller->hold(Controller::UP) &&
331 menu_repeat_time != 0 && real_time > menu_repeat_time) {
332 menuaction = MENU_ACTION_UP;
333 menu_repeat_time = real_time + MENU_REPEAT_RATE;
335 if(main_controller->pressed(Controller::DOWN)) {
336 menuaction = MENU_ACTION_DOWN;
337 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
339 if(main_controller->hold(Controller::DOWN) &&
340 menu_repeat_time != 0 && real_time > menu_repeat_time) {
341 menuaction = MENU_ACTION_DOWN;
342 menu_repeat_time = real_time + MENU_REPEAT_RATE;
344 if(main_controller->pressed(Controller::JUMP)
345 || main_controller->pressed(Controller::ACTION)
346 || main_controller->pressed(Controller::MENU_SELECT)) {
347 menuaction = MENU_ACTION_HIT;
349 if(main_controller->pressed(Controller::PAUSE_MENU)) {
350 menuaction = MENU_ACTION_BACK;
354 if(items.size() == 0)
357 int last_active_item = active_item;
364 active_item = int(items.size())-1;
365 } while ((items[active_item]->kind == MN_HL
366 || items[active_item]->kind == MN_LABEL
367 || items[active_item]->kind == MN_DEACTIVE)
368 && (active_item != last_active_item));
372 case MENU_ACTION_DOWN:
374 if(active_item < int(items.size())-1 )
378 } while ((items[active_item]->kind == MN_HL
379 || items[active_item]->kind == MN_LABEL
380 || items[active_item]->kind == MN_DEACTIVE)
381 && (active_item != last_active_item));
385 case MENU_ACTION_LEFT:
386 if(items[active_item]->kind == MN_STRINGSELECT) {
387 if(items[active_item]->selected > 0)
388 items[active_item]->selected--;
390 items[active_item]->selected = items[active_item]->list.size()-1;
394 case MENU_ACTION_RIGHT:
395 if(items[active_item]->kind == MN_STRINGSELECT) {
396 if(items[active_item]->selected+1 < items[active_item]->list.size())
397 items[active_item]->selected++;
399 items[active_item]->selected = 0;
403 case MENU_ACTION_HIT: {
404 hit_item = active_item;
405 switch (items[active_item]->kind) {
407 assert(items[active_item]->target_menu != 0);
408 Menu::push_current(items[active_item]->target_menu);
412 items[active_item]->toggled = !items[active_item]->toggled;
413 menu_action(items[active_item]);
416 case MN_CONTROLFIELD:
417 menu_action(items[active_item]);
421 menu_action(items[active_item]);
426 menuaction = MENU_ACTION_DOWN;
439 case MENU_ACTION_REMOVE:
440 if(items[active_item]->kind == MN_TEXTFIELD
441 || items[active_item]->kind == MN_NUMFIELD)
443 if(!items[active_item]->input.empty())
445 int i = items[active_item]->input.size();
447 while(delete_character > 0) /* remove charactes */
449 items[active_item]->input.resize(i-1);
456 case MENU_ACTION_INPUT:
457 if(items[active_item]->kind == MN_TEXTFIELD
458 || (items[active_item]->kind == MN_NUMFIELD
459 && mn_input_char >= '0' && mn_input_char <= '9'))
461 items[active_item]->input.push_back(mn_input_char);
465 case MENU_ACTION_BACK:
469 case MENU_ACTION_NONE:
472 menuaction = MENU_ACTION_NONE;
474 assert(active_item < int(items.size()));
481 return items[hit_item]->id;
487 Menu::menu_action(MenuItem* )
491 Menu::draw_item(DrawingContext& context, int index)
493 float menu_height = get_height();
494 float menu_width = get_width();
496 MenuItem& pitem = *(items[index]);
498 int effect_offset = 0;
499 if(effect_time != 0) {
500 if(real_time - effect_time > 0.5) {
503 float effect_delta = (0.5 - (real_time - effect_time)) * 250;
504 effect_offset = (int) ((index % 2) ? effect_delta : -effect_delta);
508 Font* text_font = default_font;
510 float y_pos = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
512 int text_width = int(text_font->get_text_width(pitem.text));
513 int input_width = int(text_font->get_text_width(pitem.input) + 10);
515 if(pitem.list.size() > 0) {
516 list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
520 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
522 if(index == active_item)
525 text_font = active_font;
532 context.draw_text(deactive_font, pitem.text,
533 Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
534 ALIGN_CENTER, LAYER_GUI);
541 float x = pos_x - menu_width/2;
542 float y = y_pos - 12 - effect_offset;
543 /* Draw a horizontal line with a little 3d effect */
544 context.draw_filled_rect(Vector(x, y + 6),
545 Vector(menu_width, 4),
546 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
547 context.draw_filled_rect(Vector(x, y + 6),
548 Vector(menu_width, 2),
549 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
554 context.draw_text(label_font, pitem.text,
555 Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
556 ALIGN_CENTER, LAYER_GUI);
561 case MN_CONTROLFIELD:
563 float width = text_width + input_width + 5;
564 float text_pos = SCREEN_WIDTH/2 - width/2;
565 float input_pos = text_pos + text_width + 10;
567 context.draw_filled_rect(
568 Vector(input_pos - 5, y_pos - 10),
569 Vector(input_width + 10, 20),
570 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
571 context.draw_filled_rect(
572 Vector(input_pos - 4, y_pos - 9),
573 Vector(input_width + 8, 18),
574 Color(0, 0, 0, 0.5f), LAYER_GUI-4);
576 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
578 if(active_item == index)
579 context.draw_text(field_font,
580 pitem.get_input_with_symbol(true),
581 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
582 ALIGN_LEFT, LAYER_GUI);
584 context.draw_text(field_font,
585 pitem.get_input_with_symbol(false),
586 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
587 ALIGN_LEFT, LAYER_GUI);
590 context.draw_text(field_font, pitem.input,
591 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
592 ALIGN_LEFT, LAYER_GUI);
594 context.draw_text(text_font, pitem.text,
595 Vector(text_pos, y_pos - int(text_font->get_height()/2)),
596 ALIGN_LEFT, LAYER_GUI);
599 case MN_STRINGSELECT:
601 int list_pos_2 = list_width + 16;
602 int list_pos = list_width/2;
603 int text_pos = (text_width + 16)/2;
606 context.draw_surface(arrow_left.get(),
607 Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
609 context.draw_surface(arrow_right.get(),
610 Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
613 /* Draw input background */
614 context.draw_filled_rect(
615 Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
616 Vector(list_pos_2 + 2, 20),
617 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
618 context.draw_filled_rect(
619 Vector(x_pos - list_pos + text_pos, y_pos - 9),
620 Vector(list_pos_2, 18),
621 Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
623 context.draw_text(text_font, pitem.list[pitem.selected],
624 Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
625 ALIGN_CENTER, LAYER_GUI);
626 context.draw_text(text_font, pitem.text,
627 Vector(SCREEN_WIDTH/2 + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
628 ALIGN_CENTER, LAYER_GUI);
633 context.draw_text(text_font, pitem.text,
634 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
635 ALIGN_CENTER, LAYER_GUI);
636 context.draw_surface(back.get(),
637 Vector(x_pos + text_width/2 + 16, y_pos - 8),
644 context.draw_text(text_font, pitem.text,
645 Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
646 ALIGN_CENTER, LAYER_GUI);
649 context.draw_surface(checkbox_checked.get(),
650 Vector(x_pos + (text_width+16)/2, y_pos - 8),
653 context.draw_surface(checkbox.get(),
654 Vector(x_pos + (text_width+16)/2, y_pos - 8),
659 context.draw_text(text_font, pitem.text,
660 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
661 ALIGN_CENTER, LAYER_GUI);
665 context.draw_text(text_font, pitem.text,
666 Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
667 ALIGN_CENTER, LAYER_GUI);
672 float Menu::get_width() const
674 /* The width of the menu has to be more than the width of the text
675 with the most characters */
676 float menu_width = 0;
677 for(unsigned int i = 0; i < items.size(); ++i)
679 Font* font = default_font;
680 if(items[i]->kind == MN_LABEL)
683 float w = font->get_text_width(items[i]->text) +
684 label_font->get_text_width(items[i]->input) + 16;
685 if(items[i]->kind == MN_TOGGLE)
692 return menu_width + 24;
695 float Menu::get_height() const
697 return items.size() * 24;
700 /* Draw the current menu. */
702 Menu::draw(DrawingContext& context)
704 if(MouseCursor::current()) {
705 MouseCursor::current()->draw(context);
708 float menu_height = get_height();
709 float menu_width = get_width();
711 /* Draw a transparent background */
712 context.draw_filled_rect(
713 Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
714 Vector(menu_width,menu_height + 20),
715 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-10);
717 for(unsigned int i = 0; i < items.size(); ++i)
719 draw_item(context, i);
724 Menu::get_item_by_id(int id)
726 for(std::vector<MenuItem*>::iterator i = items.begin();
727 i != items.end(); ++i) {
728 MenuItem& item = **i;
734 throw std::runtime_error("MenuItem not found");
738 Menu::get_item_by_id(int id) const
740 for(std::vector<MenuItem*>::const_iterator i = items.begin();
741 i != items.end(); ++i) {
742 const MenuItem& item = **i;
748 throw std::runtime_error("MenuItem not found");
751 int Menu::get_active_item_id()
753 return items[active_item]->id;
757 Menu::is_toggled(int id) const
759 return get_item_by_id(id).toggled;
762 /* Check for menu event */
764 Menu::event(const SDL_Event& event)
770 case SDL_MOUSEBUTTONDOWN:
772 int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
773 int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
775 if(x > pos_x - get_width()/2 &&
776 x < pos_x + get_width()/2 &&
777 y > pos_y - get_height()/2 &&
778 y < pos_y + get_height()/2)
780 menuaction = MENU_ACTION_HIT;
785 case SDL_MOUSEMOTION:
787 float x = event.motion.x * SCREEN_WIDTH/screen->w;
788 float y = event.motion.y * SCREEN_HEIGHT/screen->h;
790 if(x > pos_x - get_width()/2 &&
791 x < pos_x + get_width()/2 &&
792 y > pos_y - get_height()/2 &&
793 y < pos_y + get_height()/2)
796 = static_cast<int> ((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];