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"
34 static const float MENU_REPEAT_INITIAL = 0.4f;
35 static const float MENU_REPEAT_RATE = 0.1f;
56 MenuManager::all_menus.push_back(this);
59 menuaction = MENU_ACTION_NONE;
63 pos.x = SCREEN_WIDTH/2;
64 pos.y = SCREEN_HEIGHT/2;
68 effect_progress = 0.0f;
69 effect_start_time = 0.0f;
71 checkbox = Surface::create("images/engine/menu/checkbox-unchecked.png");
72 checkbox_checked = Surface::create("images/engine/menu/checkbox-checked.png");
73 back = Surface::create("images/engine/menu/arrow-back.png");
74 arrow_left = Surface::create("images/engine/menu/arrow-left.png");
75 arrow_right = Surface::create("images/engine/menu/arrow-right.png");
80 MenuManager::all_menus.remove(this);
82 for(std::vector<MenuItem*>::iterator i = items.begin();
83 i != items.end(); ++i)
88 if (MenuManager::current_ == this)
89 MenuManager::current_ = NULL;
91 if (MenuManager::previous == this)
92 MenuManager::previous = NULL;
96 Menu::set_pos(float x, float y, float rw, float rh)
98 pos.x = x + get_width() * rw;
99 pos.y = y + get_height() * rh;
102 /* Add an item to a menu */
104 Menu::additem(MenuItem* item)
106 items.push_back(item);
108 /* If a new menu is being built, the active item shouldn't be set to
109 * something that isn't selectable. Set the active_item to the first
110 * selectable item added.
112 if (active_item == -1
113 && item->kind != MN_HL
114 && item->kind != MN_LABEL
115 && item->kind != MN_INACTIVE) {
116 active_item = items.size() - 1;
123 MenuItem* item = new MenuItem(MN_HL);
129 Menu::add_label(const std::string& text)
131 MenuItem* item = new MenuItem(MN_LABEL);
138 Menu::add_controlfield(int id, const std::string& text,
139 const std::string& mapping)
141 MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
142 item->change_text(text);
143 item->change_input(mapping);
149 Menu::add_entry(int id, const std::string& text)
151 MenuItem* item = new MenuItem(MN_ACTION, id);
158 Menu::add_inactive(int id, const std::string& text)
160 MenuItem* item = new MenuItem(MN_INACTIVE, id);
167 Menu::add_toggle(int id, const std::string& text, bool toogled)
169 MenuItem* item = new MenuItem(MN_TOGGLE, id);
171 item->toggled = toogled;
177 Menu::add_string_select(int id, const std::string& text)
179 MenuItem* item = new MenuItem(MN_STRINGSELECT, id);
186 Menu::add_back(const std::string& text)
188 MenuItem* item = new MenuItem(MN_BACK);
195 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
197 MenuItem* item = new MenuItem(MN_GOTO, id);
199 item->target_menu = submenu;
207 for(std::vector<MenuItem*>::iterator i = items.begin();
208 i != items.end(); ++i) {
215 /* Process actions done on the menu */
219 int menu_height = (int) get_height();
220 if (menu_height > SCREEN_HEIGHT)
222 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
223 pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
226 effect_progress = (real_time - effect_start_time) * 6.0f;
228 if(effect_progress >= 1.0f) {
229 effect_progress = 1.0f;
232 MenuManager::current_ = 0;
236 else if (effect_progress <= 0.0f) {
237 effect_progress = 0.0f;
240 Controller *controller = g_jk_controller->get_main_controller();
241 /** check main input controller... */
242 if(controller->pressed(Controller::UP)) {
243 menuaction = MENU_ACTION_UP;
244 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
246 if(controller->hold(Controller::UP) &&
247 menu_repeat_time != 0 && real_time > menu_repeat_time) {
248 menuaction = MENU_ACTION_UP;
249 menu_repeat_time = real_time + MENU_REPEAT_RATE;
252 if(controller->pressed(Controller::DOWN)) {
253 menuaction = MENU_ACTION_DOWN;
254 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
256 if(controller->hold(Controller::DOWN) &&
257 menu_repeat_time != 0 && real_time > menu_repeat_time) {
258 menuaction = MENU_ACTION_DOWN;
259 menu_repeat_time = real_time + MENU_REPEAT_RATE;
262 if(controller->pressed(Controller::LEFT)) {
263 menuaction = MENU_ACTION_LEFT;
264 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
266 if(controller->hold(Controller::LEFT) &&
267 menu_repeat_time != 0 && real_time > menu_repeat_time) {
268 menuaction = MENU_ACTION_LEFT;
269 menu_repeat_time = real_time + MENU_REPEAT_RATE;
272 if(controller->pressed(Controller::RIGHT)) {
273 menuaction = MENU_ACTION_RIGHT;
274 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
276 if(controller->hold(Controller::RIGHT) &&
277 menu_repeat_time != 0 && real_time > menu_repeat_time) {
278 menuaction = MENU_ACTION_RIGHT;
279 menu_repeat_time = real_time + MENU_REPEAT_RATE;
282 if(controller->pressed(Controller::ACTION)
283 || controller->pressed(Controller::MENU_SELECT)) {
284 menuaction = MENU_ACTION_HIT;
286 if(controller->pressed(Controller::PAUSE_MENU)) {
287 menuaction = MENU_ACTION_BACK;
291 if(items.size() == 0)
294 int last_active_item = active_item;
301 active_item = int(items.size())-1;
302 } while ((items[active_item]->kind == MN_HL
303 || items[active_item]->kind == MN_LABEL
304 || items[active_item]->kind == MN_INACTIVE)
305 && (active_item != last_active_item));
309 case MENU_ACTION_DOWN:
311 if(active_item < int(items.size())-1 )
315 } while ((items[active_item]->kind == MN_HL
316 || items[active_item]->kind == MN_LABEL
317 || items[active_item]->kind == MN_INACTIVE)
318 && (active_item != last_active_item));
322 case MENU_ACTION_LEFT:
323 if(items[active_item]->kind == MN_STRINGSELECT) {
324 if(items[active_item]->selected > 0)
325 items[active_item]->selected--;
327 items[active_item]->selected = items[active_item]->list.size()-1;
329 menu_action(items[active_item]);
333 case MENU_ACTION_RIGHT:
334 if(items[active_item]->kind == MN_STRINGSELECT) {
335 if(items[active_item]->selected+1 < items[active_item]->list.size())
336 items[active_item]->selected++;
338 items[active_item]->selected = 0;
340 menu_action(items[active_item]);
344 case MENU_ACTION_HIT: {
345 hit_item = active_item;
346 switch (items[active_item]->kind) {
348 assert(items[active_item]->target_menu != 0);
349 MenuManager::push_current(items[active_item]->target_menu);
353 items[active_item]->toggled = !items[active_item]->toggled;
354 menu_action(items[active_item]);
357 case MN_CONTROLFIELD:
358 menu_action(items[active_item]);
362 menu_action(items[active_item]);
365 case MN_STRINGSELECT:
366 if(items[active_item]->selected+1 < items[active_item]->list.size())
367 items[active_item]->selected++;
369 items[active_item]->selected = 0;
371 menu_action(items[active_item]);
376 menuaction = MENU_ACTION_DOWN;
381 MenuManager::pop_current();
389 case MENU_ACTION_REMOVE:
390 if(items[active_item]->kind == MN_TEXTFIELD
391 || items[active_item]->kind == MN_NUMFIELD)
393 if(!items[active_item]->input.empty())
395 int i = items[active_item]->input.size();
397 while(delete_character > 0) /* remove characters */
399 items[active_item]->input.resize(i-1);
406 case MENU_ACTION_INPUT:
407 if(items[active_item]->kind == MN_TEXTFIELD
408 || (items[active_item]->kind == MN_NUMFIELD
409 && mn_input_char >= '0' && mn_input_char <= '9'))
411 items[active_item]->input.push_back(mn_input_char);
415 case MENU_ACTION_BACK:
416 MenuManager::pop_current();
419 case MENU_ACTION_NONE:
422 menuaction = MENU_ACTION_NONE;
424 assert(active_item < int(items.size()));
432 int id = items[hit_item]->id;
433 // Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib)
442 Menu::menu_action(MenuItem* )
446 Menu::draw_item(DrawingContext& context, int index)
448 float menu_height = get_height();
449 float menu_width = get_width();
451 MenuItem& pitem = *(items[index]);
453 Color text_color = default_color;
455 float y_pos = pos.y + 24*index - menu_height/2 + 12;
456 int text_width = int(Resources::normal_font->get_text_width(pitem.text));
457 int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
460 float left = pos.x - menu_width/2 + 16;
461 float right = pos.x + menu_width/2 - 16;
463 if(pitem.list.size() > 0) {
464 list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
468 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
470 if(index == active_item)
472 text_color = active_color;
475 if(active_item == index)
477 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
478 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
479 Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
480 Color(1.0f, 1.0f, 1.0f, blink),
483 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
484 Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
485 Color(1.0f, 1.0f, 1.0f, 0.5f),
494 context.draw_text(Resources::normal_font, pitem.text,
495 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
496 ALIGN_CENTER, LAYER_GUI, inactive_color);
503 float x = pos.x - menu_width/2;
504 float y = y_pos - 12;
505 /* Draw a horizontal line with a little 3d effect */
506 context.draw_filled_rect(Vector(x, y + 6),
507 Vector(menu_width, 4),
508 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
509 context.draw_filled_rect(Vector(x, y + 6),
510 Vector(menu_width, 2),
511 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
516 context.draw_text(Resources::big_font, pitem.text,
517 Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
518 ALIGN_CENTER, LAYER_GUI, label_color);
523 case MN_CONTROLFIELD:
525 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
527 if(active_item == index)
528 context.draw_text(Resources::normal_font,
529 pitem.get_input_with_symbol(true),
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,
534 pitem.get_input_with_symbol(false),
535 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
536 ALIGN_RIGHT, LAYER_GUI, field_color);
539 context.draw_text(Resources::normal_font, pitem.input,
540 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
541 ALIGN_RIGHT, LAYER_GUI, field_color);
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 case MN_STRINGSELECT:
550 float roff = arrow_left->get_width();
552 context.draw_text(Resources::normal_font, pitem.text,
553 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
554 ALIGN_LEFT, LAYER_GUI, text_color);
557 context.draw_surface(arrow_left,
558 Vector(right - list_width - roff - roff, y_pos - 8),
560 context.draw_surface(arrow_right,
561 Vector(right - roff, y_pos - 8),
563 context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
564 Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
565 ALIGN_RIGHT, LAYER_GUI, text_color);
570 context.draw_text(Resources::Resources::normal_font, pitem.text,
571 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
572 ALIGN_CENTER, LAYER_GUI, text_color);
573 context.draw_surface(back,
574 Vector(x_pos + text_width/2 + 16, y_pos - 8),
581 context.draw_text(Resources::normal_font, pitem.text,
582 Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
583 ALIGN_LEFT, LAYER_GUI, text_color);
586 context.draw_surface(checkbox_checked,
587 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
590 context.draw_surface(checkbox,
591 Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
596 context.draw_text(Resources::normal_font, pitem.text,
597 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
598 ALIGN_CENTER, LAYER_GUI, text_color);
602 context.draw_text(Resources::normal_font, pitem.text,
603 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
604 ALIGN_CENTER, LAYER_GUI, text_color);
610 Menu::get_width() const
612 /* The width of the menu has to be more than the width of the text
613 with the most characters */
614 float menu_width = 0;
615 for(unsigned int i = 0; i < items.size(); ++i)
617 FontPtr font = Resources::Resources::normal_font;
618 if(items[i]->kind == MN_LABEL)
619 font = Resources::big_font;
621 float w = font->get_text_width(items[i]->text) +
622 Resources::big_font->get_text_width(items[i]->input) + 16;
623 if(items[i]->kind == MN_TOGGLE)
625 if (items[i]->kind == MN_STRINGSELECT)
626 w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
633 return menu_width + 24;
637 Menu::get_height() const
639 return items.size() * 24;
642 /* Draw the current menu. */
644 Menu::draw(DrawingContext& context)
646 if(MouseCursor::current()) {
647 MouseCursor::current()->draw(context);
650 float menu_width = get_width();
651 float menu_height = get_height();
653 if (effect_progress != 1.0f)
657 menu_width = (MenuManager::current_->get_width() * (1.0f - effect_progress));
658 menu_height = (MenuManager::current_->get_height() * (1.0f - effect_progress));
660 else if (MenuManager::previous)
662 menu_width = (menu_width * effect_progress) + (MenuManager::previous->get_width() * (1.0f - effect_progress));
663 menu_height = (menu_height * effect_progress) + (MenuManager::previous->get_height() * (1.0f - effect_progress));
664 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
668 menu_width *= effect_progress;
669 menu_height *= effect_progress;
673 /* Draw a transparent background */
674 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4),
675 Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)),
676 Color(0.2f, 0.3f, 0.4f, 0.8f),
680 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2, pos.y - menu_height/2 - 10),
681 Vector(pos.x + menu_width/2, pos.y - menu_height/2 + 10 + menu_height)),
682 Color(0.6f, 0.7f, 0.8f, 0.5f),
686 if (!items[active_item]->help.empty())
688 int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
689 int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
691 Rectf text_rect(pos.x - text_width/2 - 8,
692 SCREEN_HEIGHT - 48 - text_height/2 - 4,
693 pos.x + text_width/2 + 8,
694 SCREEN_HEIGHT - 48 + text_height/2 + 4);
696 context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
697 text_rect.p2 + Vector(4,4)),
698 Color(0.2f, 0.3f, 0.4f, 0.8f),
702 context.draw_filled_rect(text_rect,
703 Color(0.6f, 0.7f, 0.8f, 0.5f),
707 context.draw_text(Resources::normal_font, items[active_item]->help,
708 Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
709 ALIGN_CENTER, LAYER_GUI);
712 if (effect_progress == 1.0f)
713 for(unsigned int i = 0; i < items.size(); ++i)
715 draw_item(context, i);
720 Menu::get_item_by_id(int id)
722 for(std::vector<MenuItem*>::iterator i = items.begin();
723 i != items.end(); ++i) {
724 MenuItem& item = **i;
730 throw std::runtime_error("MenuItem not found");
734 Menu::get_item_by_id(int id) const
736 for(std::vector<MenuItem*>::const_iterator i = items.begin();
737 i != items.end(); ++i) {
738 const MenuItem& item = **i;
744 throw std::runtime_error("MenuItem not found");
747 int Menu::get_active_item_id()
749 return items[active_item]->id;
753 Menu::is_toggled(int id) const
755 return get_item_by_id(id).toggled;
759 Menu::set_toggled(int id, bool toggled)
761 get_item_by_id(id).toggled = toggled;
765 Menu::get_parent() const
767 if (MenuManager::last_menus.empty())
770 return MenuManager::last_menus.back();
773 /* Check for menu event */
775 Menu::event(const SDL_Event& event)
777 if(effect_progress != 1.0f)
781 case SDL_MOUSEBUTTONDOWN:
782 if(event.button.button == SDL_BUTTON_LEFT)
784 int x = int(event.motion.x * float(SCREEN_WIDTH)/g_screen->w);
785 int y = int(event.motion.y * float(SCREEN_HEIGHT)/g_screen->h);
787 if(x > pos.x - get_width()/2 &&
788 x < pos.x + get_width()/2 &&
789 y > pos.y - get_height()/2 &&
790 y < pos.y + get_height()/2)
792 menuaction = MENU_ACTION_HIT;
797 case SDL_MOUSEMOTION:
799 float x = event.motion.x * SCREEN_WIDTH/g_screen->w;
800 float y = event.motion.y * SCREEN_HEIGHT/g_screen->h;
802 if(x > pos.x - get_width()/2 &&
803 x < pos.x + get_width()/2 &&
804 y > pos.y - get_height()/2 &&
805 y < pos.y + get_height()/2)
808 = static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
810 /* only change the mouse focus to a selectable item */
811 if ((items[new_active_item]->kind != MN_HL)
812 && (items[new_active_item]->kind != MN_LABEL)
813 && (items[new_active_item]->kind != MN_INACTIVE))
814 active_item = new_active_item;
816 if(MouseCursor::current())
817 MouseCursor::current()->set_state(MC_LINK);
821 if(MouseCursor::current())
822 MouseCursor::current()->set_state(MC_NORMAL);
833 Menu::set_active_item(int id)
835 for(size_t i = 0; i < items.size(); ++i) {
836 MenuItem* item = items[i];