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/colorscheme.hpp"
27 #include "supertux/globals.hpp"
28 #include "supertux/resources.hpp"
29 #include "supertux/screen_manager.hpp"
30 #include "supertux/timer.hpp"
31 #include "util/gettext.hpp"
32 #include "video/drawing_context.hpp"
33 #include "video/font.hpp"
34 #include "video/renderer.hpp"
35 #include "video/video_system.hpp"
37 static const float MENU_REPEAT_INITIAL = 0.4f;
38 static const float MENU_REPEAT_RATE = 0.1f;
52 pos.x = SCREEN_WIDTH/2;
53 pos.y = SCREEN_HEIGHT/2;
63 Menu::set_center_pos(float x, float y)
69 /* Add an item to a menu */
71 Menu::add_item(std::unique_ptr<MenuItem> new_item)
73 items.push_back(std::move(new_item));
74 MenuItem* item = items.back().get();
76 /* If a new menu is being built, the active item shouldn't be set to
77 * something that isn't selectable. Set the active_item to the first
78 * selectable item added.
81 && item->kind != MN_HL
82 && item->kind != MN_LABEL
83 && item->kind != MN_INACTIVE)
85 active_item = items.size() - 1;
94 std::unique_ptr<MenuItem> item(new MenuItem(MN_HL));
95 return add_item(std::move(item));
99 Menu::add_label(const std::string& text)
101 std::unique_ptr<MenuItem> item(new MenuItem(MN_LABEL));
103 return add_item(std::move(item));
107 Menu::add_controlfield(int id, const std::string& text,
108 const std::string& mapping)
110 std::unique_ptr<MenuItem> item(new MenuItem(MN_CONTROLFIELD, id));
111 item->change_text(text);
112 item->change_input(mapping);
113 return add_item(std::move(item));
117 Menu::add_entry(int id, const std::string& text)
119 std::unique_ptr<MenuItem> item(new MenuItem(MN_ACTION, id));
121 return add_item(std::move(item));
125 Menu::add_inactive(int id, const std::string& text)
127 std::unique_ptr<MenuItem> item(new MenuItem(MN_INACTIVE, id));
129 return add_item(std::move(item));
133 Menu::add_toggle(int id, const std::string& text, bool toogled)
135 std::unique_ptr<MenuItem> item(new MenuItem(MN_TOGGLE, id));
137 item->toggled = toogled;
138 return add_item(std::move(item));
142 Menu::add_string_select(int id, const std::string& text)
144 std::unique_ptr<MenuItem> item(new MenuItem(MN_STRINGSELECT, id));
146 return add_item(std::move(item));
150 Menu::add_back(const std::string& text)
152 std::unique_ptr<MenuItem> item(new MenuItem(MN_BACK));
154 return add_item(std::move(item));
158 Menu::add_submenu(const std::string& text, int submenu)
160 std::unique_ptr<MenuItem> item(new MenuItem(MN_GOTO));
162 item->target_menu = submenu;
163 return add_item(std::move(item));
174 Menu::process_input()
176 int menu_height = (int) get_height();
177 if (menu_height > SCREEN_HEIGHT)
179 int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
180 pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
183 MenuAction menuaction = MENU_ACTION_NONE;
184 Controller* controller = InputManager::current()->get_controller();
185 /** check main input controller... */
186 if(controller->pressed(Controller::UP)) {
187 menuaction = MENU_ACTION_UP;
188 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
190 if(controller->hold(Controller::UP) &&
191 menu_repeat_time != 0 && real_time > menu_repeat_time) {
192 menuaction = MENU_ACTION_UP;
193 menu_repeat_time = real_time + MENU_REPEAT_RATE;
196 if(controller->pressed(Controller::DOWN)) {
197 menuaction = MENU_ACTION_DOWN;
198 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
200 if(controller->hold(Controller::DOWN) &&
201 menu_repeat_time != 0 && real_time > menu_repeat_time) {
202 menuaction = MENU_ACTION_DOWN;
203 menu_repeat_time = real_time + MENU_REPEAT_RATE;
206 if(controller->pressed(Controller::LEFT)) {
207 menuaction = MENU_ACTION_LEFT;
208 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
210 if(controller->hold(Controller::LEFT) &&
211 menu_repeat_time != 0 && real_time > menu_repeat_time) {
212 menuaction = MENU_ACTION_LEFT;
213 menu_repeat_time = real_time + MENU_REPEAT_RATE;
216 if(controller->pressed(Controller::RIGHT)) {
217 menuaction = MENU_ACTION_RIGHT;
218 menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
220 if(controller->hold(Controller::RIGHT) &&
221 menu_repeat_time != 0 && real_time > menu_repeat_time) {
222 menuaction = MENU_ACTION_RIGHT;
223 menu_repeat_time = real_time + MENU_REPEAT_RATE;
226 if(controller->pressed(Controller::ACTION)
227 || controller->pressed(Controller::MENU_SELECT)) {
228 menuaction = MENU_ACTION_HIT;
230 if(controller->pressed(Controller::PAUSE_MENU)
231 || controller->pressed(Controller::MENU_BACK)) {
232 menuaction = MENU_ACTION_BACK;
235 if(items.size() == 0)
238 // The menu_action() call can pop() the menu from the stack and thus
239 // delete it, so it's important that no further member variables are
240 // accessed after this call
241 process_action(menuaction);
245 Menu::process_action(MenuAction menuaction)
247 int last_active_item = active_item;
254 active_item = int(items.size())-1;
255 } while ((items[active_item]->kind == MN_HL
256 || items[active_item]->kind == MN_LABEL
257 || items[active_item]->kind == MN_INACTIVE)
258 && (active_item != last_active_item));
262 case MENU_ACTION_DOWN:
264 if(active_item < int(items.size())-1 )
268 } while ((items[active_item]->kind == MN_HL
269 || items[active_item]->kind == MN_LABEL
270 || items[active_item]->kind == MN_INACTIVE)
271 && (active_item != last_active_item));
275 case MENU_ACTION_LEFT:
276 if(items[active_item]->kind == MN_STRINGSELECT) {
277 if(items[active_item]->selected > 0)
278 items[active_item]->selected--;
280 items[active_item]->selected = items[active_item]->list.size()-1;
282 menu_action(items[active_item].get());
286 case MENU_ACTION_RIGHT:
287 if(items[active_item]->kind == MN_STRINGSELECT) {
288 if(items[active_item]->selected+1 < items[active_item]->list.size())
289 items[active_item]->selected++;
291 items[active_item]->selected = 0;
293 menu_action(items[active_item].get());
297 case MENU_ACTION_HIT: {
298 switch (items[active_item]->kind) {
300 assert(items[active_item]->target_menu != 0);
301 MenuManager::instance().push_menu(items[active_item]->target_menu);
305 items[active_item]->toggled = !items[active_item]->toggled;
306 menu_action(items[active_item].get());
309 case MN_CONTROLFIELD:
310 menu_action(items[active_item].get());
314 menu_action(items[active_item].get());
317 case MN_STRINGSELECT:
318 if(items[active_item]->selected+1 < items[active_item]->list.size())
319 items[active_item]->selected++;
321 items[active_item]->selected = 0;
323 menu_action(items[active_item].get());
328 menuaction = MENU_ACTION_DOWN;
333 MenuManager::instance().pop_menu();
342 case MENU_ACTION_REMOVE:
343 if(items[active_item]->kind == MN_TEXTFIELD
344 || items[active_item]->kind == MN_NUMFIELD)
346 if(!items[active_item]->input.empty())
348 int i = items[active_item]->input.size();
350 while(delete_character > 0) /* remove characters */
352 items[active_item]->input.resize(i-1);
359 case MENU_ACTION_INPUT:
360 if(items[active_item]->kind == MN_TEXTFIELD
361 || (items[active_item]->kind == MN_NUMFIELD
362 && mn_input_char >= '0' && mn_input_char <= '9'))
364 items[active_item]->input.push_back(mn_input_char);
368 case MENU_ACTION_BACK:
369 MenuManager::instance().pop_menu();
372 case MENU_ACTION_NONE:
378 Menu::draw_item(DrawingContext& context, int index)
380 float menu_height = get_height();
381 float menu_width = get_width();
383 MenuItem& pitem = *(items[index]);
385 Color text_color = ColorScheme::Menu::default_color;
387 float y_pos = pos.y + 24*index - menu_height/2 + 12;
388 int text_width = int(Resources::normal_font->get_text_width(pitem.text));
389 int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
392 float left = pos.x - menu_width/2 + 16;
393 float right = pos.x + menu_width/2 - 16;
395 if(pitem.list.size() > 0) {
396 list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
400 x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
402 if(index == active_item)
404 text_color = ColorScheme::Menu::active_color;
407 if(active_item == index)
409 float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
410 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
411 Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
412 Color(1.0f, 1.0f, 1.0f, blink),
415 context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
416 Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
417 Color(1.0f, 1.0f, 1.0f, 0.5f),
426 context.draw_text(Resources::normal_font, pitem.text,
427 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
428 ALIGN_CENTER, LAYER_GUI, ColorScheme::Menu::inactive_color);
435 float x = pos.x - menu_width/2;
436 float y = y_pos - 12;
437 /* Draw a horizontal line with a little 3d effect */
438 context.draw_filled_rect(Vector(x, y + 6),
439 Vector(menu_width, 4),
440 Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
441 context.draw_filled_rect(Vector(x, y + 6),
442 Vector(menu_width, 2),
443 Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
448 context.draw_text(Resources::big_font, pitem.text,
449 Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
450 ALIGN_CENTER, LAYER_GUI, ColorScheme::Menu::label_color);
455 case MN_CONTROLFIELD:
457 if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
459 if(active_item == index)
460 context.draw_text(Resources::normal_font,
461 pitem.get_input_with_symbol(true),
462 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
463 ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::field_color);
465 context.draw_text(Resources::normal_font,
466 pitem.get_input_with_symbol(false),
467 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
468 ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::field_color);
471 context.draw_text(Resources::normal_font, pitem.input,
472 Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
473 ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::field_color);
475 context.draw_text(Resources::normal_font, pitem.text,
476 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
477 ALIGN_LEFT, LAYER_GUI, text_color);
480 case MN_STRINGSELECT:
482 float roff = Resources::arrow_left->get_width();
484 context.draw_text(Resources::normal_font, pitem.text,
485 Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
486 ALIGN_LEFT, LAYER_GUI, text_color);
489 context.draw_surface(Resources::arrow_left,
490 Vector(right - list_width - roff - roff, y_pos - 8),
492 context.draw_surface(Resources::arrow_right,
493 Vector(right - roff, y_pos - 8),
495 context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
496 Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
497 ALIGN_RIGHT, LAYER_GUI, text_color);
502 context.draw_text(Resources::Resources::normal_font, pitem.text,
503 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
504 ALIGN_CENTER, LAYER_GUI, text_color);
505 context.draw_surface(Resources::back,
506 Vector(x_pos + text_width/2 + 16, y_pos - 8),
513 context.draw_text(Resources::normal_font, pitem.text,
514 Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
515 ALIGN_LEFT, LAYER_GUI, text_color);
518 context.draw_surface(Resources::checkbox_checked,
519 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
522 context.draw_surface(Resources::checkbox,
523 Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
528 context.draw_text(Resources::normal_font, pitem.text,
529 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
530 ALIGN_CENTER, LAYER_GUI, text_color);
534 context.draw_text(Resources::normal_font, pitem.text,
535 Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
536 ALIGN_CENTER, LAYER_GUI, text_color);
542 Menu::get_width() const
544 /* The width of the menu has to be more than the width of the text
545 with the most characters */
546 float menu_width = 0;
547 for(unsigned int i = 0; i < items.size(); ++i)
549 FontPtr font = Resources::Resources::normal_font;
550 if(items[i]->kind == MN_LABEL)
551 font = Resources::big_font;
553 float w = font->get_text_width(items[i]->text) +
554 Resources::big_font->get_text_width(items[i]->input) + 16;
555 if(items[i]->kind == MN_TOGGLE)
557 if (items[i]->kind == MN_STRINGSELECT)
558 w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
565 return menu_width + 24;
569 Menu::get_height() const
571 return items.size() * 24;
575 Menu::on_window_resize()
577 pos.x = SCREEN_WIDTH / 2;
578 pos.y = SCREEN_HEIGHT / 2;
582 Menu::draw(DrawingContext& context)
584 if (!items[active_item]->help.empty())
586 int text_width = (int) Resources::normal_font->get_text_width(items[active_item]->help);
587 int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
589 Rectf text_rect(pos.x - text_width/2 - 8,
590 SCREEN_HEIGHT - 48 - text_height/2 - 4,
591 pos.x + text_width/2 + 8,
592 SCREEN_HEIGHT - 48 + text_height/2 + 4);
594 context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
595 text_rect.p2 + Vector(4,4)),
596 Color(0.2f, 0.3f, 0.4f, 0.8f),
600 context.draw_filled_rect(text_rect,
601 Color(0.6f, 0.7f, 0.8f, 0.5f),
605 context.draw_text(Resources::normal_font, items[active_item]->help,
606 Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
607 ALIGN_CENTER, LAYER_GUI);
610 for(unsigned int i = 0; i < items.size(); ++i)
612 draw_item(context, i);
617 Menu::get_item_by_id(int id)
619 for (const auto& item : items)
627 throw std::runtime_error("MenuItem not found");
631 Menu::get_item_by_id(int id) const
633 for (const auto& item : items)
641 throw std::runtime_error("MenuItem not found");
644 int Menu::get_active_item_id()
646 return items[active_item]->id;
650 Menu::is_toggled(int id) const
652 return get_item_by_id(id).toggled;
656 Menu::set_toggled(int id, bool toggled)
658 get_item_by_id(id).toggled = toggled;
662 Menu::event(const SDL_Event& ev)
665 case SDL_MOUSEBUTTONDOWN:
666 if(ev.button.button == SDL_BUTTON_LEFT)
668 Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
669 int x = int(mouse_pos.x);
670 int y = int(mouse_pos.y);
672 if(x > pos.x - get_width()/2 &&
673 x < pos.x + get_width()/2 &&
674 y > pos.y - get_height()/2 &&
675 y < pos.y + get_height()/2)
677 process_action(MENU_ACTION_HIT);
682 case SDL_MOUSEMOTION:
684 Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y);
685 float x = mouse_pos.x;
686 float y = mouse_pos.y;
688 if(x > pos.x - get_width()/2 &&
689 x < pos.x + get_width()/2 &&
690 y > pos.y - get_height()/2 &&
691 y < pos.y + get_height()/2)
694 = static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
696 /* only change the mouse focus to a selectable item */
697 if ((items[new_active_item]->kind != MN_HL)
698 && (items[new_active_item]->kind != MN_LABEL)
699 && (items[new_active_item]->kind != MN_INACTIVE))
700 active_item = new_active_item;
702 if(MouseCursor::current())
703 MouseCursor::current()->set_state(MC_LINK);
707 if(MouseCursor::current())
708 MouseCursor::current()->set_state(MC_NORMAL);
719 Menu::set_active_item(int id)
721 for(size_t i = 0; i < items.size(); ++i) {
722 if(items[i]->id == id) {