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_main_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 menuaction = MENU_ACTION_BACK;
281 if(items.size() == 0)
284 int last_active_item = active_item;
291 active_item = int(items.size())-1;
292 } while ((items[active_item]->kind == MN_HL
293 || items[active_item]->kind == MN_LABEL
294 || items[active_item]->kind == MN_INACTIVE)
295 && (active_item != last_active_item));
299 case MENU_ACTION_DOWN:
301 if(active_item < int(items.size())-1 )
305 } while ((items[active_item]->kind == MN_HL
306 || items[active_item]->kind == MN_LABEL
307 || items[active_item]->kind == MN_INACTIVE)
308 && (active_item != last_active_item));
312 case MENU_ACTION_LEFT:
313 if(items[active_item]->kind == MN_STRINGSELECT) {
314 if(items[active_item]->selected > 0)
315 items[active_item]->selected--;
317 items[active_item]->selected = items[active_item]->list.size()-1;
319 menu_action(items[active_item]);
323 case MENU_ACTION_RIGHT:
324 if(items[active_item]->kind == MN_STRINGSELECT) {
325 if(items[active_item]->selected+1 < items[active_item]->list.size())
326 items[active_item]->selected++;
328 items[active_item]->selected = 0;
330 menu_action(items[active_item]);
334 case MENU_ACTION_HIT: {
335 hit_item = active_item;
336 switch (items[active_item]->kind) {
338 assert(items[active_item]->target_menu != 0);
339 MenuManager::push_current(items[active_item]->target_menu);
343 items[active_item]->toggled = !items[active_item]->toggled;
344 menu_action(items[active_item]);
347 case MN_CONTROLFIELD:
348 menu_action(items[active_item]);
352 menu_action(items[active_item]);
355 case MN_STRINGSELECT:
356 if(items[active_item]->selected+1 < items[active_item]->list.size())
357 items[active_item]->selected++;
359 items[active_item]->selected = 0;
361 menu_action(items[active_item]);
366 menuaction = MENU_ACTION_DOWN;
371 MenuManager::pop_current();
379 case MENU_ACTION_REMOVE:
380 if(items[active_item]->kind == MN_TEXTFIELD
381 || items[active_item]->kind == MN_NUMFIELD)
383 if(!items[active_item]->input.empty())
385 int i = items[active_item]->input.size();
387 while(delete_character > 0) /* remove characters */
389 items[active_item]->input.resize(i-1);
396 case MENU_ACTION_INPUT:
397 if(items[active_item]->kind == MN_TEXTFIELD
398 || (items[active_item]->kind == MN_NUMFIELD
399 && mn_input_char >= '0' && mn_input_char <= '9'))
401 items[active_item]->input.push_back(mn_input_char);
405 case MENU_ACTION_BACK:
406 MenuManager::pop_current();
409 case MENU_ACTION_NONE:
412 menuaction = MENU_ACTION_NONE;
414 assert(active_item < int(items.size()));
422 int id = items[hit_item]->id;
423 // Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib)
432 Menu::menu_action(MenuItem* )
436 Menu::draw_item(DrawingContext& context, int index)
438 float menu_height = get_height();
439 float menu_width = get_width();
441 MenuItem& pitem = *(items[index]);
443 Color text_color = default_color;
445 float y_pos = pos.y + 24*index - menu_height/2 + 12;
446 int text_width = int(Resources::normal_font->get_text_width(pitem.text));
447 int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
450 float left = pos.x - menu_width/2 + 16;
451 float right = pos.x + menu_width/2 - 16;
453 if(pitem.list.size() > 0) {
454 list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
458 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
460 if(index == active_item)
462 text_color = active_color;
465 if(active_item == index)
467 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
468 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
469 Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
470 Color(1.0f, 1.0f, 1.0f, blink),
473 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
474 Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
475 Color(1.0f, 1.0f, 1.0f, 0.5f),
484 context.draw_text(Resources::normal_font, pitem.text,
485 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
486 ALIGN_CENTER, LAYER_GUI, inactive_color);
493 float x = pos.x - menu_width/2;
494 float y = y_pos - 12;
495 /* Draw a horizontal line with a little 3d effect */
496 context.draw_filled_rect(Vector(x, y + 6),
497 Vector(menu_width, 4),
498 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
499 context.draw_filled_rect(Vector(x, y + 6),
500 Vector(menu_width, 2),
501 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
506 context.draw_text(Resources::big_font, pitem.text,
507 Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
508 ALIGN_CENTER, LAYER_GUI, label_color);
513 case MN_CONTROLFIELD:
515 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
517 if(active_item == index)
518 context.draw_text(Resources::normal_font,
519 pitem.get_input_with_symbol(true),
520 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
521 ALIGN_RIGHT, LAYER_GUI, field_color);
523 context.draw_text(Resources::normal_font,
524 pitem.get_input_with_symbol(false),
525 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
526 ALIGN_RIGHT, LAYER_GUI, field_color);
529 context.draw_text(Resources::normal_font, pitem.input,
530 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
531 ALIGN_RIGHT, LAYER_GUI, field_color);
533 context.draw_text(Resources::normal_font, pitem.text,
534 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
535 ALIGN_LEFT, LAYER_GUI, text_color);
538 case MN_STRINGSELECT:
540 float roff = Resources::arrow_left->get_width();
542 context.draw_text(Resources::normal_font, pitem.text,
543 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
544 ALIGN_LEFT, LAYER_GUI, text_color);
547 context.draw_surface(Resources::arrow_left,
548 Vector(right - list_width - roff - roff, y_pos - 8),
550 context.draw_surface(Resources::arrow_right,
551 Vector(right - roff, y_pos - 8),
553 context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
554 Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
555 ALIGN_RIGHT, LAYER_GUI, text_color);
560 context.draw_text(Resources::Resources::normal_font, pitem.text,
561 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
562 ALIGN_CENTER, LAYER_GUI, text_color);
563 context.draw_surface(Resources::back,
564 Vector(x_pos + text_width/2 + 16, y_pos - 8),
571 context.draw_text(Resources::normal_font, pitem.text,
572 Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
573 ALIGN_LEFT, LAYER_GUI, text_color);
576 context.draw_surface(Resources::checkbox_checked,
577 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
580 context.draw_surface(Resources::checkbox,
581 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
586 context.draw_text(Resources::normal_font, pitem.text,
587 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
588 ALIGN_CENTER, LAYER_GUI, text_color);
592 context.draw_text(Resources::normal_font, pitem.text,
593 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
594 ALIGN_CENTER, LAYER_GUI, text_color);
600 Menu::get_width() const
602 /* The width of the menu has to be more than the width of the text
603 with the most characters */
604 float menu_width = 0;
605 for(unsigned int i = 0; i < items.size(); ++i)
607 FontPtr font = Resources::Resources::normal_font;
608 if(items[i]->kind == MN_LABEL)
609 font = Resources::big_font;
611 float w = font->get_text_width(items[i]->text) +
612 Resources::big_font->get_text_width(items[i]->input) + 16;
613 if(items[i]->kind == MN_TOGGLE)
615 if (items[i]->kind == MN_STRINGSELECT)
616 w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
623 return menu_width + 24;
627 Menu::get_height() const
629 return items.size() * 24;
632 /* Draw the current menu. */
634 Menu::draw(DrawingContext& context)
636 if(MouseCursor::current()) {
637 MouseCursor::current()->draw(context);
640 float menu_width = get_width();
641 float menu_height = get_height();
643 if (effect_progress != 1.0f)
647 menu_width = (MenuManager::current_->get_width() * (1.0f - effect_progress));
648 menu_height = (MenuManager::current_->get_height() * (1.0f - effect_progress));
650 else if (MenuManager::previous)
652 menu_width = (menu_width * effect_progress) + (MenuManager::previous->get_width() * (1.0f - effect_progress));
653 menu_height = (menu_height * effect_progress) + (MenuManager::previous->get_height() * (1.0f - effect_progress));
654 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
658 menu_width *= effect_progress;
659 menu_height *= effect_progress;
663 /* Draw a transparent background */
664 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4),
665 Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)),
666 Color(0.2f, 0.3f, 0.4f, 0.8f),
670 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2, pos.y - menu_height/2 - 10),
671 Vector(pos.x + menu_width/2, pos.y - menu_height/2 + 10 + menu_height)),
672 Color(0.6f, 0.7f, 0.8f, 0.5f),
676 if (!items[active_item]->help.empty())
678 int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
679 int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
681 Rectf text_rect(pos.x - text_width/2 - 8,
682 SCREEN_HEIGHT - 48 - text_height/2 - 4,
683 pos.x + text_width/2 + 8,
684 SCREEN_HEIGHT - 48 + text_height/2 + 4);
686 context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
687 text_rect.p2 + Vector(4,4)),
688 Color(0.2f, 0.3f, 0.4f, 0.8f),
692 context.draw_filled_rect(text_rect,
693 Color(0.6f, 0.7f, 0.8f, 0.5f),
697 context.draw_text(Resources::normal_font, items[active_item]->help,
698 Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
699 ALIGN_CENTER, LAYER_GUI);
702 if (effect_progress == 1.0f)
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;
749 Menu::set_toggled(int id, bool toggled)
751 get_item_by_id(id).toggled = toggled;
755 Menu::get_parent() const
757 if (MenuManager::last_menus.empty())
760 return MenuManager::last_menus.back();
763 /* Check for menu event */
765 Menu::event(const SDL_Event& event)
767 if(effect_progress != 1.0f)
771 case SDL_MOUSEBUTTONUP:
773 Vector mouse_pos = Renderer::instance()->to_logical(event.button.x, event.button.y);
775 if(MouseCursor::current())
777 MouseCursor::current()->set_pos(mouse_pos);
782 case SDL_MOUSEBUTTONDOWN:
784 Vector mouse_pos = Renderer::instance()->to_logical(event.button.x, event.button.y);
786 if(MouseCursor::current())
788 MouseCursor::current()->set_pos(mouse_pos);
791 if(event.button.button == SDL_BUTTON_LEFT)
793 int x = int(mouse_pos.x);
794 int y = int(mouse_pos.y);
796 if(x > pos.x - get_width()/2 &&
797 x < pos.x + get_width()/2 &&
798 y > pos.y - get_height()/2 &&
799 y < pos.y + get_height()/2)
801 menuaction = MENU_ACTION_HIT;
807 case SDL_MOUSEMOTION:
809 Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
811 if(MouseCursor::current())
813 MouseCursor::current()->set_pos(mouse_pos);
816 float x = mouse_pos.x;
817 float y = mouse_pos.y;
819 if(x > pos.x - get_width()/2 &&
820 x < pos.x + get_width()/2 &&
821 y > pos.y - get_height()/2 &&
822 y < pos.y + get_height()/2)
825 = static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
827 /* only change the mouse focus to a selectable item */
828 if ((items[new_active_item]->kind != MN_HL)
829 && (items[new_active_item]->kind != MN_LABEL)
830 && (items[new_active_item]->kind != MN_INACTIVE))
831 active_item = new_active_item;
833 if(MouseCursor::current())
834 MouseCursor::current()->set_state(MC_LINK);
838 if(MouseCursor::current())
839 MouseCursor::current()->set_state(MC_NORMAL);
850 Menu::set_active_item(int id)
852 for(size_t i = 0; i < items.size(); ++i) {
853 MenuItem* item = items[i];