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/input_manager.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/resources.hpp"
28 #include "supertux/screen_manager.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;
53 menuaction = MENU_ACTION_NONE;
57 pos.x = SCREEN_WIDTH/2;
58 pos.y = SCREEN_HEIGHT/2;
62 effect_progress = 0.0f;
63 effect_start_time = 0.0f;
71 Menu::set_pos(float x, float y, float rw, float rh)
73 pos.x = x + get_width() * rw;
74 pos.y = y + get_height() * rh;
77 /* Add an item to a menu */
79 Menu::add_item(std::unique_ptr<MenuItem> new_item)
81 items.push_back(std::move(new_item));
82 MenuItem* item = items.back().get();
84 /* If a new menu is being built, the active item shouldn't be set to
85 * something that isn't selectable. Set the active_item to the first
86 * selectable item added.
89 && item->kind != MN_HL
90 && item->kind != MN_LABEL
91 && item->kind != MN_INACTIVE)
93 active_item = items.size() - 1;
102 std::unique_ptr<MenuItem> item(new MenuItem(MN_HL));
103 return add_item(std::move(item));
107 Menu::add_label(const std::string& text)
109 std::unique_ptr<MenuItem> item(new MenuItem(MN_LABEL));
111 return add_item(std::move(item));
115 Menu::add_controlfield(int id, const std::string& text,
116 const std::string& mapping)
118 std::unique_ptr<MenuItem> item(new MenuItem(MN_CONTROLFIELD, id));
119 item->change_text(text);
120 item->change_input(mapping);
121 return add_item(std::move(item));
125 Menu::add_entry(int id, const std::string& text)
127 std::unique_ptr<MenuItem> item(new MenuItem(MN_ACTION, id));
129 return add_item(std::move(item));
133 Menu::add_inactive(int id, const std::string& text)
135 std::unique_ptr<MenuItem> item(new MenuItem(MN_INACTIVE, id));
137 return add_item(std::move(item));
141 Menu::add_toggle(int id, const std::string& text, bool toogled)
143 std::unique_ptr<MenuItem> item(new MenuItem(MN_TOGGLE, id));
145 item->toggled = toogled;
146 return add_item(std::move(item));
150 Menu::add_string_select(int id, const std::string& text)
152 std::unique_ptr<MenuItem> item(new MenuItem(MN_STRINGSELECT, id));
154 return add_item(std::move(item));
158 Menu::add_back(const std::string& text)
160 std::unique_ptr<MenuItem> item(new MenuItem(MN_BACK));
162 return add_item(std::move(item));
166 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
168 std::unique_ptr<MenuItem> item(new MenuItem(MN_GOTO, id));
170 item->target_menu = submenu;
171 return add_item(std::move(item));
181 /* Process actions done on the menu */
185 int menu_height = (int) get_height();
186 if (menu_height > SCREEN_HEIGHT)
188 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
189 pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
192 effect_progress = (real_time - effect_start_time) * 6.0f;
194 if(effect_progress >= 1.0f) {
195 effect_progress = 1.0f;
198 MenuManager::instance().current(0);
202 else if (effect_progress <= 0.0f) {
203 effect_progress = 0.0f;
206 Controller* controller = g_input_manager->get_controller();
207 /** check main input controller... */
208 if(controller->pressed(Controller::UP)) {
209 menuaction = MENU_ACTION_UP;
210 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
212 if(controller->hold(Controller::UP) &&
213 menu_repeat_time != 0 && real_time > menu_repeat_time) {
214 menuaction = MENU_ACTION_UP;
215 menu_repeat_time = real_time + MENU_REPEAT_RATE;
218 if(controller->pressed(Controller::DOWN)) {
219 menuaction = MENU_ACTION_DOWN;
220 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
222 if(controller->hold(Controller::DOWN) &&
223 menu_repeat_time != 0 && real_time > menu_repeat_time) {
224 menuaction = MENU_ACTION_DOWN;
225 menu_repeat_time = real_time + MENU_REPEAT_RATE;
228 if(controller->pressed(Controller::LEFT)) {
229 menuaction = MENU_ACTION_LEFT;
230 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
232 if(controller->hold(Controller::LEFT) &&
233 menu_repeat_time != 0 && real_time > menu_repeat_time) {
234 menuaction = MENU_ACTION_LEFT;
235 menu_repeat_time = real_time + MENU_REPEAT_RATE;
238 if(controller->pressed(Controller::RIGHT)) {
239 menuaction = MENU_ACTION_RIGHT;
240 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
242 if(controller->hold(Controller::RIGHT) &&
243 menu_repeat_time != 0 && real_time > menu_repeat_time) {
244 menuaction = MENU_ACTION_RIGHT;
245 menu_repeat_time = real_time + MENU_REPEAT_RATE;
248 if(controller->pressed(Controller::ACTION)
249 || controller->pressed(Controller::MENU_SELECT)) {
250 menuaction = MENU_ACTION_HIT;
252 if(controller->pressed(Controller::PAUSE_MENU)
253 || controller->pressed(Controller::MENU_BACK)) {
254 menuaction = MENU_ACTION_BACK;
258 if(items.size() == 0)
261 int last_active_item = active_item;
268 active_item = int(items.size())-1;
269 } while ((items[active_item]->kind == MN_HL
270 || items[active_item]->kind == MN_LABEL
271 || items[active_item]->kind == MN_INACTIVE)
272 && (active_item != last_active_item));
276 case MENU_ACTION_DOWN:
278 if(active_item < int(items.size())-1 )
282 } while ((items[active_item]->kind == MN_HL
283 || items[active_item]->kind == MN_LABEL
284 || items[active_item]->kind == MN_INACTIVE)
285 && (active_item != last_active_item));
289 case MENU_ACTION_LEFT:
290 if(items[active_item]->kind == MN_STRINGSELECT) {
291 if(items[active_item]->selected > 0)
292 items[active_item]->selected--;
294 items[active_item]->selected = items[active_item]->list.size()-1;
296 menu_action(items[active_item].get());
300 case MENU_ACTION_RIGHT:
301 if(items[active_item]->kind == MN_STRINGSELECT) {
302 if(items[active_item]->selected+1 < items[active_item]->list.size())
303 items[active_item]->selected++;
305 items[active_item]->selected = 0;
307 menu_action(items[active_item].get());
311 case MENU_ACTION_HIT: {
312 hit_item = active_item;
313 switch (items[active_item]->kind) {
315 assert(items[active_item]->target_menu != 0);
316 MenuManager::instance().push_current(items[active_item]->target_menu);
320 items[active_item]->toggled = !items[active_item]->toggled;
321 menu_action(items[active_item].get());
324 case MN_CONTROLFIELD:
325 menu_action(items[active_item].get());
329 menu_action(items[active_item].get());
332 case MN_STRINGSELECT:
333 if(items[active_item]->selected+1 < items[active_item]->list.size())
334 items[active_item]->selected++;
336 items[active_item]->selected = 0;
338 menu_action(items[active_item].get());
343 menuaction = MENU_ACTION_DOWN;
348 MenuManager::instance().pop_current();
356 case MENU_ACTION_REMOVE:
357 if(items[active_item]->kind == MN_TEXTFIELD
358 || items[active_item]->kind == MN_NUMFIELD)
360 if(!items[active_item]->input.empty())
362 int i = items[active_item]->input.size();
364 while(delete_character > 0) /* remove characters */
366 items[active_item]->input.resize(i-1);
373 case MENU_ACTION_INPUT:
374 if(items[active_item]->kind == MN_TEXTFIELD
375 || (items[active_item]->kind == MN_NUMFIELD
376 && mn_input_char >= '0' && mn_input_char <= '9'))
378 items[active_item]->input.push_back(mn_input_char);
382 case MENU_ACTION_BACK:
383 MenuManager::instance().pop_current();
386 case MENU_ACTION_NONE:
389 menuaction = MENU_ACTION_NONE;
391 assert(active_item < int(items.size()));
399 int id = items[hit_item]->id;
400 // Clear event when checked out.. (we would end up in a loop when we try to leave "fake" submenu like Addons or Contrib)
409 Menu::menu_action(MenuItem* )
413 Menu::draw_item(DrawingContext& context, int index)
415 float menu_height = get_height();
416 float menu_width = get_width();
418 MenuItem& pitem = *(items[index]);
420 Color text_color = default_color;
422 float y_pos = pos.y + 24*index - menu_height/2 + 12;
423 int text_width = int(Resources::normal_font->get_text_width(pitem.text));
424 int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
427 float left = pos.x - menu_width/2 + 16;
428 float right = pos.x + menu_width/2 - 16;
430 if(pitem.list.size() > 0) {
431 list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
435 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
437 if(index == active_item)
439 text_color = active_color;
442 if(active_item == index)
444 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
445 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
446 Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
447 Color(1.0f, 1.0f, 1.0f, blink),
450 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
451 Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
452 Color(1.0f, 1.0f, 1.0f, 0.5f),
461 context.draw_text(Resources::normal_font, pitem.text,
462 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
463 ALIGN_CENTER, LAYER_GUI, inactive_color);
470 float x = pos.x - menu_width/2;
471 float y = y_pos - 12;
472 /* Draw a horizontal line with a little 3d effect */
473 context.draw_filled_rect(Vector(x, y + 6),
474 Vector(menu_width, 4),
475 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
476 context.draw_filled_rect(Vector(x, y + 6),
477 Vector(menu_width, 2),
478 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
483 context.draw_text(Resources::big_font, pitem.text,
484 Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
485 ALIGN_CENTER, LAYER_GUI, label_color);
490 case MN_CONTROLFIELD:
492 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
494 if(active_item == index)
495 context.draw_text(Resources::normal_font,
496 pitem.get_input_with_symbol(true),
497 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
498 ALIGN_RIGHT, LAYER_GUI, field_color);
500 context.draw_text(Resources::normal_font,
501 pitem.get_input_with_symbol(false),
502 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
503 ALIGN_RIGHT, LAYER_GUI, field_color);
506 context.draw_text(Resources::normal_font, pitem.input,
507 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
508 ALIGN_RIGHT, LAYER_GUI, field_color);
510 context.draw_text(Resources::normal_font, pitem.text,
511 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
512 ALIGN_LEFT, LAYER_GUI, text_color);
515 case MN_STRINGSELECT:
517 float roff = Resources::arrow_left->get_width();
519 context.draw_text(Resources::normal_font, pitem.text,
520 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
521 ALIGN_LEFT, LAYER_GUI, text_color);
524 context.draw_surface(Resources::arrow_left,
525 Vector(right - list_width - roff - roff, y_pos - 8),
527 context.draw_surface(Resources::arrow_right,
528 Vector(right - roff, y_pos - 8),
530 context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
531 Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
532 ALIGN_RIGHT, LAYER_GUI, text_color);
537 context.draw_text(Resources::Resources::normal_font, pitem.text,
538 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
539 ALIGN_CENTER, LAYER_GUI, text_color);
540 context.draw_surface(Resources::back,
541 Vector(x_pos + text_width/2 + 16, y_pos - 8),
548 context.draw_text(Resources::normal_font, pitem.text,
549 Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
550 ALIGN_LEFT, LAYER_GUI, text_color);
553 context.draw_surface(Resources::checkbox_checked,
554 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
557 context.draw_surface(Resources::checkbox,
558 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
563 context.draw_text(Resources::normal_font, pitem.text,
564 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
565 ALIGN_CENTER, LAYER_GUI, text_color);
569 context.draw_text(Resources::normal_font, pitem.text,
570 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
571 ALIGN_CENTER, LAYER_GUI, text_color);
577 Menu::get_width() const
579 /* The width of the menu has to be more than the width of the text
580 with the most characters */
581 float menu_width = 0;
582 for(unsigned int i = 0; i < items.size(); ++i)
584 FontPtr font = Resources::Resources::normal_font;
585 if(items[i]->kind == MN_LABEL)
586 font = Resources::big_font;
588 float w = font->get_text_width(items[i]->text) +
589 Resources::big_font->get_text_width(items[i]->input) + 16;
590 if(items[i]->kind == MN_TOGGLE)
592 if (items[i]->kind == MN_STRINGSELECT)
593 w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
600 return menu_width + 24;
604 Menu::get_height() const
606 return items.size() * 24;
609 /* Draw the current menu. */
611 Menu::draw(DrawingContext& context)
613 if(MouseCursor::current()) {
614 MouseCursor::current()->draw(context);
617 float menu_width = get_width();
618 float menu_height = get_height();
620 if (effect_progress != 1.0f)
624 menu_width = (MenuManager::instance().current()->get_width() * (1.0f - effect_progress));
625 menu_height = (MenuManager::instance().current()->get_height() * (1.0f - effect_progress));
627 else if (MenuManager::instance().get_previous())
629 menu_width = (menu_width * effect_progress) + (MenuManager::instance().get_previous()->get_width() * (1.0f - effect_progress));
630 menu_height = (menu_height * effect_progress) + (MenuManager::instance().get_previous()->get_height() * (1.0f - effect_progress));
631 //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
635 menu_width *= effect_progress;
636 menu_height *= effect_progress;
640 /* Draw a transparent background */
641 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2-4, pos.y - menu_height/2 - 10-4),
642 Vector(pos.x + menu_width/2+4, pos.y - menu_height/2 + 10 + menu_height+4)),
643 Color(0.2f, 0.3f, 0.4f, 0.8f),
647 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2, pos.y - menu_height/2 - 10),
648 Vector(pos.x + menu_width/2, pos.y - menu_height/2 + 10 + menu_height)),
649 Color(0.6f, 0.7f, 0.8f, 0.5f),
653 if (!items[active_item]->help.empty())
655 int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
656 int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
658 Rectf text_rect(pos.x - text_width/2 - 8,
659 SCREEN_HEIGHT - 48 - text_height/2 - 4,
660 pos.x + text_width/2 + 8,
661 SCREEN_HEIGHT - 48 + text_height/2 + 4);
663 context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
664 text_rect.p2 + Vector(4,4)),
665 Color(0.2f, 0.3f, 0.4f, 0.8f),
669 context.draw_filled_rect(text_rect,
670 Color(0.6f, 0.7f, 0.8f, 0.5f),
674 context.draw_text(Resources::normal_font, items[active_item]->help,
675 Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
676 ALIGN_CENTER, LAYER_GUI);
679 if (effect_progress == 1.0f)
680 for(unsigned int i = 0; i < items.size(); ++i)
682 draw_item(context, i);
687 Menu::get_item_by_id(int id)
689 for (const auto& item : items)
697 throw std::runtime_error("MenuItem not found");
701 Menu::get_item_by_id(int id) const
703 for (const auto& item : items)
711 throw std::runtime_error("MenuItem not found");
714 int Menu::get_active_item_id()
716 return items[active_item]->id;
720 Menu::is_toggled(int id) const
722 return get_item_by_id(id).toggled;
726 Menu::set_toggled(int id, bool toggled)
728 get_item_by_id(id).toggled = toggled;
731 /* Check for menu event */
733 Menu::event(const SDL_Event& event)
735 if(effect_progress != 1.0f)
739 case SDL_MOUSEBUTTONDOWN:
740 if(event.button.button == SDL_BUTTON_LEFT)
742 Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
743 int x = int(mouse_pos.x);
744 int y = int(mouse_pos.y);
746 if(x > pos.x - get_width()/2 &&
747 x < pos.x + get_width()/2 &&
748 y > pos.y - get_height()/2 &&
749 y < pos.y + get_height()/2)
751 menuaction = MENU_ACTION_HIT;
756 case SDL_MOUSEMOTION:
758 Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
759 float x = mouse_pos.x;
760 float y = mouse_pos.y;
762 if(x > pos.x - get_width()/2 &&
763 x < pos.x + get_width()/2 &&
764 y > pos.y - get_height()/2 &&
765 y < pos.y + get_height()/2)
768 = static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
770 /* only change the mouse focus to a selectable item */
771 if ((items[new_active_item]->kind != MN_HL)
772 && (items[new_active_item]->kind != MN_LABEL)
773 && (items[new_active_item]->kind != MN_INACTIVE))
774 active_item = new_active_item;
776 if(MouseCursor::current())
777 MouseCursor::current()->set_state(MC_LINK);
781 if(MouseCursor::current())
782 MouseCursor::current()->set_state(MC_NORMAL);
793 Menu::set_active_item(int id)
795 for(size_t i = 0; i < items.size(); ++i) {
796 if(items[i]->id == id) {