2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "gui/menu.hpp"
22 #include "control/joystickkeyboardcontroller.hpp"
23 #include "gui/menu_item.hpp"
24 #include "gui/menu_manager.hpp"
25 #include "gui/mousecursor.hpp"
26 #include "supertux/globals.hpp"
27 #include "supertux/screen_manager.hpp"
28 #include "supertux/resources.hpp"
29 #include "supertux/timer.hpp"
30 #include "util/gettext.hpp"
31 #include "video/drawing_context.hpp"
32 #include "video/font.hpp"
33 #include "video/renderer.hpp"
35 static const float MENU_REPEAT_INITIAL = 0.4f;
36 static const float MENU_REPEAT_RATE = 0.1f;
52 MenuManager::all_menus.push_back(this);
55 menuaction = MENU_ACTION_NONE;
59 pos.x = SCREEN_WIDTH/2;
60 pos.y = SCREEN_HEIGHT/2;
64 effect_progress = 0.0f;
65 effect_start_time = 0.0f;
70 MenuManager::all_menus.remove(this);
72 for(std::vector<MenuItem*>::iterator i = items.begin();
73 i != items.end(); ++i)
78 if (MenuManager::current_ == this)
79 MenuManager::current_ = NULL;
81 if (MenuManager::previous == this)
82 MenuManager::previous = NULL;
86 Menu::set_pos(float x, float y, float rw, float rh)
88 pos.x = x + get_width() * rw;
89 pos.y = y + get_height() * rh;
92 /* Add an item to a menu */
94 Menu::additem(MenuItem* item)
96 items.push_back(item);
98 /* If a new menu is being built, the active item shouldn't be set to
99 * something that isn't selectable. Set the active_item to the first
100 * selectable item added.
102 if (active_item == -1
103 && item->kind != MN_HL
104 && item->kind != MN_LABEL
105 && item->kind != MN_INACTIVE) {
106 active_item = items.size() - 1;
113 MenuItem* item = new MenuItem(MN_HL);
119 Menu::add_label(const std::string& text)
121 MenuItem* item = new MenuItem(MN_LABEL);
128 Menu::add_controlfield(int id, const std::string& text,
129 const std::string& mapping)
131 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
132 item->change_text(text);
133 item->change_input(mapping);
139 Menu::add_entry(int id, const std::string& text)
141 MenuItem* item = new MenuItem(MN_ACTION, id);
148 Menu::add_inactive(int id, const std::string& text)
150 MenuItem* item = new MenuItem(MN_INACTIVE, id);
157 Menu::add_toggle(int id, const std::string& text, bool toogled)
159 MenuItem* item = new MenuItem(MN_TOGGLE, id);
161 item->toggled = toogled;
167 Menu::add_string_select(int id, const std::string& text)
169 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
176 Menu::add_back(const std::string& text)
178 MenuItem* item = new MenuItem(MN_BACK);
185 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
187 MenuItem* item = new MenuItem(MN_GOTO, id);
189 item->target_menu = submenu;
197 for(std::vector<MenuItem*>::iterator i = items.begin();
198 i != items.end(); ++i) {
205 /* Process actions done on the menu */
209 int menu_height = (int) get_height();
210 if (menu_height > SCREEN_HEIGHT)
212 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
213 pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
216 effect_progress = (real_time - effect_start_time) * 6.0f;
218 if(effect_progress >= 1.0f) {
219 effect_progress = 1.0f;
222 MenuManager::current_ = 0;
226 else if (effect_progress <= 0.0f) {
227 effect_progress = 0.0f;
230 Controller* controller = g_jk_controller->get_controller();
231 /** check main input controller... */
232 if(controller->pressed(Controller::UP)) {
233 menuaction = MENU_ACTION_UP;
234 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
236 if(controller->hold(Controller::UP) &&
237 menu_repeat_time != 0 && real_time > menu_repeat_time) {
238 menuaction = MENU_ACTION_UP;
239 menu_repeat_time = real_time + MENU_REPEAT_RATE;
242 if(controller->pressed(Controller::DOWN)) {
243 menuaction = MENU_ACTION_DOWN;
244 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
246 if(controller->hold(Controller::DOWN) &&
247 menu_repeat_time != 0 && real_time > menu_repeat_time) {
248 menuaction = MENU_ACTION_DOWN;
249 menu_repeat_time = real_time + MENU_REPEAT_RATE;
252 if(controller->pressed(Controller::LEFT)) {
253 menuaction = MENU_ACTION_LEFT;
254 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
256 if(controller->hold(Controller::LEFT) &&
257 menu_repeat_time != 0 && real_time > menu_repeat_time) {
258 menuaction = MENU_ACTION_LEFT;
259 menu_repeat_time = real_time + MENU_REPEAT_RATE;
262 if(controller->pressed(Controller::RIGHT)) {
263 menuaction = MENU_ACTION_RIGHT;
264 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
266 if(controller->hold(Controller::RIGHT) &&
267 menu_repeat_time != 0 && real_time > menu_repeat_time) {
268 menuaction = MENU_ACTION_RIGHT;
269 menu_repeat_time = real_time + MENU_REPEAT_RATE;
272 if(controller->pressed(Controller::ACTION)
273 || controller->pressed(Controller::MENU_SELECT)) {
274 menuaction = MENU_ACTION_HIT;
276 if(controller->pressed(Controller::PAUSE_MENU)
277 || controller->pressed(Controller::MENU_BACK)) {
278 menuaction = MENU_ACTION_BACK;
282 if(items.size() == 0)
285 int last_active_item = active_item;
292 active_item = int(items.size())-1;
293 } while ((items[active_item]->kind == MN_HL
294 || items[active_item]->kind == MN_LABEL
295 || items[active_item]->kind == MN_INACTIVE)
296 && (active_item != last_active_item));
300 case MENU_ACTION_DOWN:
302 if(active_item < int(items.size())-1 )
306 } while ((items[active_item]->kind == MN_HL
307 || items[active_item]->kind == MN_LABEL
308 || items[active_item]->kind == MN_INACTIVE)
309 && (active_item != last_active_item));
313 case MENU_ACTION_LEFT:
314 if(items[active_item]->kind == MN_STRINGSELECT) {
315 if(items[active_item]->selected > 0)
316 items[active_item]->selected--;
318 items[active_item]->selected = items[active_item]->list.size()-1;
320 menu_action(items[active_item]);
324 case MENU_ACTION_RIGHT:
325 if(items[active_item]->kind == MN_STRINGSELECT) {
326 if(items[active_item]->selected+1 < items[active_item]->list.size())
327 items[active_item]->selected++;
329 items[active_item]->selected = 0;
331 menu_action(items[active_item]);
335 case MENU_ACTION_HIT: {
336 hit_item = active_item;
337 switch (items[active_item]->kind) {
339 assert(items[active_item]->target_menu != 0);
340 MenuManager::push_current(items[active_item]->target_menu);
344 items[active_item]->toggled = !items[active_item]->toggled;
345 menu_action(items[active_item]);
348 case MN_CONTROLFIELD:
349 menu_action(items[active_item]);
353 menu_action(items[active_item]);
356 case MN_STRINGSELECT:
357 if(items[active_item]->selected+1 < items[active_item]->list.size())
358 items[active_item]->selected++;
360 items[active_item]->selected = 0;
362 menu_action(items[active_item]);
367 menuaction = MENU_ACTION_DOWN;
372 MenuManager::pop_current();
380 case MENU_ACTION_REMOVE:
381 if(items[active_item]->kind == MN_TEXTFIELD
382 || items[active_item]->kind == MN_NUMFIELD)
384 if(!items[active_item]->input.empty())
386 int i = items[active_item]->input.size();
388 while(delete_character > 0) /* remove characters */
390 items[active_item]->input.resize(i-1);
397 case MENU_ACTION_INPUT:
398 if(items[active_item]->kind == MN_TEXTFIELD
399 || (items[active_item]->kind == MN_NUMFIELD
400 && mn_input_char >= '0' && mn_input_char <= '9'))
402 items[active_item]->input.push_back(mn_input_char);
406 case MENU_ACTION_BACK:
407 MenuManager::pop_current();
410 case MENU_ACTION_NONE:
413 menuaction = MENU_ACTION_NONE;
415 assert(active_item < int(items.size()));
423 int id = items[hit_item]->id;
424 // Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib)
433 Menu::menu_action(MenuItem* )
437 Menu::draw_item(DrawingContext& context, int index)
439 float menu_height = get_height();
440 float menu_width = get_width();
442 MenuItem& pitem = *(items[index]);
444 Color text_color = default_color;
446 float y_pos = pos.y + 24*index - menu_height/2 + 12;
447 int text_width = int(Resources::normal_font->get_text_width(pitem.text));
448 int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
451 float left = pos.x - menu_width/2 + 16;
452 float right = pos.x + menu_width/2 - 16;
454 if(pitem.list.size() > 0) {
455 list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
459 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
461 if(index == active_item)
463 text_color = active_color;
466 if(active_item == index)
468 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
469 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
470 Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
471 Color(1.0f, 1.0f, 1.0f, blink),
474 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
475 Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
476 Color(1.0f, 1.0f, 1.0f, 0.5f),
485 context.draw_text(Resources::normal_font, pitem.text,
486 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
487 ALIGN_CENTER, LAYER_GUI, inactive_color);
494 float x = pos.x - menu_width/2;
495 float y = y_pos - 12;
496 /* Draw a horizontal line with a little 3d effect */
497 context.draw_filled_rect(Vector(x, y + 6),
498 Vector(menu_width, 4),
499 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
500 context.draw_filled_rect(Vector(x, y + 6),
501 Vector(menu_width, 2),
502 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
507 context.draw_text(Resources::big_font, pitem.text,
508 Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
509 ALIGN_CENTER, LAYER_GUI, label_color);
514 case MN_CONTROLFIELD:
516 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
518 if(active_item == index)
519 context.draw_text(Resources::normal_font,
520 pitem.get_input_with_symbol(true),
521 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
522 ALIGN_RIGHT, LAYER_GUI, field_color);
524 context.draw_text(Resources::normal_font,
525 pitem.get_input_with_symbol(false),
526 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
527 ALIGN_RIGHT, LAYER_GUI, field_color);
530 context.draw_text(Resources::normal_font, pitem.input,
531 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
532 ALIGN_RIGHT, LAYER_GUI, field_color);
534 context.draw_text(Resources::normal_font, pitem.text,
535 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
536 ALIGN_LEFT, LAYER_GUI, text_color);
539 case MN_STRINGSELECT:
541 float roff = Resources::arrow_left->get_width();
543 context.draw_text(Resources::normal_font, pitem.text,
544 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
545 ALIGN_LEFT, LAYER_GUI, text_color);
548 context.draw_surface(Resources::arrow_left,
549 Vector(right - list_width - roff - roff, y_pos - 8),
551 context.draw_surface(Resources::arrow_right,
552 Vector(right - roff, y_pos - 8),
554 context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
555 Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
556 ALIGN_RIGHT, LAYER_GUI, text_color);
561 context.draw_text(Resources::Resources::normal_font, pitem.text,
562 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
563 ALIGN_CENTER, LAYER_GUI, text_color);
564 context.draw_surface(Resources::back,
565 Vector(x_pos + text_width/2 + 16, y_pos - 8),
572 context.draw_text(Resources::normal_font, pitem.text,
573 Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
574 ALIGN_LEFT, LAYER_GUI, text_color);
577 context.draw_surface(Resources::checkbox_checked,
578 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
581 context.draw_surface(Resources::checkbox,
582 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
587 context.draw_text(Resources::normal_font, pitem.text,
588 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
589 ALIGN_CENTER, LAYER_GUI, text_color);
593 context.draw_text(Resources::normal_font, pitem.text,
594 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
595 ALIGN_CENTER, LAYER_GUI, text_color);
601 Menu::get_width() const
603 /* The width of the menu has to be more than the width of the text
604 with the most characters */
605 float menu_width = 0;
606 for(unsigned int i = 0; i < items.size(); ++i)
608 FontPtr font = Resources::Resources::normal_font;
609 if(items[i]->kind == MN_LABEL)
610 font = Resources::big_font;
612 float w = font->get_text_width(items[i]->text) +
613 Resources::big_font->get_text_width(items[i]->input) + 16;
614 if(items[i]->kind == MN_TOGGLE)
616 if (items[i]->kind == MN_STRINGSELECT)
617 w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
624 return menu_width + 24;
628 Menu::get_height() const
630 return items.size() * 24;
633 /* Draw the current menu. */
635 Menu::draw(DrawingContext& context)
637 if(MouseCursor::current()) {
638 MouseCursor::current()->draw(context);
641 float menu_width = get_width();
642 float menu_height = get_height();
644 if (effect_progress != 1.0f)
648 menu_width = (MenuManager::current_->get_width() * (1.0f - effect_progress));
649 menu_height = (MenuManager::current_->get_height() * (1.0f - effect_progress));
651 else if (MenuManager::previous)
653 menu_width = (menu_width * effect_progress) + (MenuManager::previous->get_width() * (1.0f - effect_progress));
654 menu_height = (menu_height * effect_progress) + (MenuManager::previous->get_height() * (1.0f - effect_progress));
655 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
659 menu_width *= effect_progress;
660 menu_height *= effect_progress;
664 /* Draw a transparent background */
665 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4),
666 Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)),
667 Color(0.2f, 0.3f, 0.4f, 0.8f),
671 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2, pos.y - menu_height/2 - 10),
672 Vector(pos.x + menu_width/2, pos.y - menu_height/2 + 10 + menu_height)),
673 Color(0.6f, 0.7f, 0.8f, 0.5f),
677 if (!items[active_item]->help.empty())
679 int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
680 int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
682 Rectf text_rect(pos.x - text_width/2 - 8,
683 SCREEN_HEIGHT - 48 - text_height/2 - 4,
684 pos.x + text_width/2 + 8,
685 SCREEN_HEIGHT - 48 + text_height/2 + 4);
687 context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
688 text_rect.p2 + Vector(4,4)),
689 Color(0.2f, 0.3f, 0.4f, 0.8f),
693 context.draw_filled_rect(text_rect,
694 Color(0.6f, 0.7f, 0.8f, 0.5f),
698 context.draw_text(Resources::normal_font, items[active_item]->help,
699 Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
700 ALIGN_CENTER, LAYER_GUI);
703 if (effect_progress == 1.0f)
704 for(unsigned int i = 0; i < items.size(); ++i)
706 draw_item(context, i);
711 Menu::get_item_by_id(int id)
713 for(std::vector<MenuItem*>::iterator i = items.begin();
714 i != items.end(); ++i) {
715 MenuItem& item = **i;
721 throw std::runtime_error("MenuItem not found");
725 Menu::get_item_by_id(int id) const
727 for(std::vector<MenuItem*>::const_iterator i = items.begin();
728 i != items.end(); ++i) {
729 const MenuItem& item = **i;
735 throw std::runtime_error("MenuItem not found");
738 int Menu::get_active_item_id()
740 return items[active_item]->id;
744 Menu::is_toggled(int id) const
746 return get_item_by_id(id).toggled;
750 Menu::set_toggled(int id, bool toggled)
752 get_item_by_id(id).toggled = toggled;
756 Menu::get_parent() const
758 if (MenuManager::last_menus.empty())
761 return MenuManager::last_menus.back();
764 /* Check for menu event */
766 Menu::event(const SDL_Event& event)
768 if(effect_progress != 1.0f)
772 case SDL_MOUSEBUTTONDOWN:
773 if(event.button.button == SDL_BUTTON_LEFT)
775 Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
776 int x = int(mouse_pos.x);
777 int y = int(mouse_pos.y);
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 menuaction = MENU_ACTION_HIT;
789 case SDL_MOUSEMOTION:
791 Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
792 float x = mouse_pos.x;
793 float y = mouse_pos.y;
795 if(x > pos.x - get_width()/2 &&
796 x < pos.x + get_width()/2 &&
797 y > pos.y - get_height()/2 &&
798 y < pos.y + get_height()/2)
801 = static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
803 /* only change the mouse focus to a selectable item */
804 if ((items[new_active_item]->kind != MN_HL)
805 && (items[new_active_item]->kind != MN_LABEL)
806 && (items[new_active_item]->kind != MN_INACTIVE))
807 active_item = new_active_item;
809 if(MouseCursor::current())
810 MouseCursor::current()->set_state(MC_LINK);
814 if(MouseCursor::current())
815 MouseCursor::current()->set_state(MC_NORMAL);
826 Menu::set_active_item(int id)
828 for(size_t i = 0; i < items.size(); ++i) {
829 MenuItem* item = items[i];