Removed assert(active_item < int(items.size())); as that is no longer save to call...
[supertux.git] / src / gui / menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "gui/menu.hpp"
18
19 #include <math.h>
20 #include <stdexcept>
21
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"
34
35 static const float MENU_REPEAT_INITIAL = 0.4f;
36 static const float MENU_REPEAT_RATE    = 0.1f;
37
38 Menu::Menu() :
39   pos(),
40   delete_character(),
41   mn_input_char(),
42   menu_repeat_time(),
43   items(),
44   arrange_left(),
45   active_item()
46 {
47   delete_character = 0;
48   mn_input_char = '\0';
49
50   pos.x        = SCREEN_WIDTH/2;
51   pos.y        = SCREEN_HEIGHT/2;
52   arrange_left = 0;
53   active_item  = -1;
54 }
55
56 Menu::~Menu()
57 {
58 }
59
60 void
61 Menu::set_center_pos(float x, float y)
62 {
63   pos.x = x;
64   pos.y = y;
65 }
66
67 /* Add an item to a menu */
68 MenuItem*
69 Menu::add_item(std::unique_ptr<MenuItem> new_item)
70 {
71   items.push_back(std::move(new_item));
72   MenuItem* item = items.back().get();
73
74   /* If a new menu is being built, the active item shouldn't be set to
75    * something that isn't selectable. Set the active_item to the first
76    * selectable item added.
77    */
78   if (active_item == -1
79       && item->kind != MN_HL
80       && item->kind != MN_LABEL
81       && item->kind != MN_INACTIVE)
82   {
83     active_item = items.size() - 1;
84   }
85
86   return item;
87 }
88
89 MenuItem*
90 Menu::add_hl()
91 {
92   std::unique_ptr<MenuItem> item(new MenuItem(MN_HL));
93   return add_item(std::move(item));
94 }
95
96 MenuItem*
97 Menu::add_label(const std::string& text)
98 {
99   std::unique_ptr<MenuItem> item(new MenuItem(MN_LABEL));
100   item->text = text;
101   return add_item(std::move(item));
102 }
103
104 MenuItem*
105 Menu::add_controlfield(int id, const std::string& text,
106                        const std::string& mapping)
107 {
108   std::unique_ptr<MenuItem> item(new MenuItem(MN_CONTROLFIELD, id));
109   item->change_text(text);
110   item->change_input(mapping);
111   return add_item(std::move(item));
112 }
113
114 MenuItem*
115 Menu::add_entry(int id, const std::string& text)
116 {
117   std::unique_ptr<MenuItem> item(new MenuItem(MN_ACTION, id));
118   item->text = text;
119   return add_item(std::move(item));
120 }
121
122 MenuItem*
123 Menu::add_inactive(int id, const std::string& text)
124 {
125   std::unique_ptr<MenuItem> item(new MenuItem(MN_INACTIVE, id));
126   item->text = text;
127   return add_item(std::move(item));
128 }
129
130 MenuItem*
131 Menu::add_toggle(int id, const std::string& text, bool toogled)
132 {
133   std::unique_ptr<MenuItem> item(new MenuItem(MN_TOGGLE, id));
134   item->text = text;
135   item->toggled = toogled;
136   return add_item(std::move(item));
137 }
138
139 MenuItem*
140 Menu::add_string_select(int id, const std::string& text)
141 {
142   std::unique_ptr<MenuItem> item(new MenuItem(MN_STRINGSELECT, id));
143   item->text = text;
144   return add_item(std::move(item));
145 }
146
147 MenuItem*
148 Menu::add_back(const std::string& text)
149 {
150   std::unique_ptr<MenuItem> item(new MenuItem(MN_BACK));
151   item->text = text;
152   return add_item(std::move(item));
153 }
154
155 MenuItem*
156 Menu::add_submenu(const std::string& text, int submenu)
157 {
158   std::unique_ptr<MenuItem> item(new MenuItem(MN_GOTO));
159   item->text = text;
160   item->target_menu = submenu;
161   return add_item(std::move(item));
162 }
163
164 void
165 Menu::clear()
166 {
167   items.clear();
168   active_item = -1;
169 }
170
171 void
172 Menu::process_input()
173 {
174   int menu_height = (int) get_height();
175   if (menu_height > SCREEN_HEIGHT)
176   { // Scrolling
177     int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
178     pos.y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
179   }
180
181   MenuAction menuaction = MENU_ACTION_NONE;
182   Controller* controller = g_input_manager->get_controller();
183   /** check main input controller... */
184   if(controller->pressed(Controller::UP)) {
185     menuaction = MENU_ACTION_UP;
186     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
187   }
188   if(controller->hold(Controller::UP) &&
189      menu_repeat_time != 0 && real_time > menu_repeat_time) {
190     menuaction = MENU_ACTION_UP;
191     menu_repeat_time = real_time + MENU_REPEAT_RATE;
192   }
193
194   if(controller->pressed(Controller::DOWN)) {
195     menuaction = MENU_ACTION_DOWN;
196     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
197   }
198   if(controller->hold(Controller::DOWN) &&
199      menu_repeat_time != 0 && real_time > menu_repeat_time) {
200     menuaction = MENU_ACTION_DOWN;
201     menu_repeat_time = real_time + MENU_REPEAT_RATE;
202   }
203
204   if(controller->pressed(Controller::LEFT)) {
205     menuaction = MENU_ACTION_LEFT;
206     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
207   }
208   if(controller->hold(Controller::LEFT) &&
209      menu_repeat_time != 0 && real_time > menu_repeat_time) {
210     menuaction = MENU_ACTION_LEFT;
211     menu_repeat_time = real_time + MENU_REPEAT_RATE;
212   }
213
214   if(controller->pressed(Controller::RIGHT)) {
215     menuaction = MENU_ACTION_RIGHT;
216     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
217   }
218   if(controller->hold(Controller::RIGHT) &&
219      menu_repeat_time != 0 && real_time > menu_repeat_time) {
220     menuaction = MENU_ACTION_RIGHT;
221     menu_repeat_time = real_time + MENU_REPEAT_RATE;
222   }
223
224   if(controller->pressed(Controller::ACTION)
225      || controller->pressed(Controller::MENU_SELECT)) {
226     menuaction = MENU_ACTION_HIT;
227   }
228   if(controller->pressed(Controller::PAUSE_MENU)
229     || controller->pressed(Controller::MENU_BACK)) {
230     menuaction = MENU_ACTION_BACK;
231   }
232
233   if(items.size() == 0)
234     return;
235
236   // The menu_action() call can pop() the menu from the stack and thus
237   // delete it, so it's important that no further member variables are
238   // accessed after this call
239   process_action(menuaction);
240 }
241
242 void
243 Menu::process_action(MenuAction menuaction)
244 {
245   int last_active_item = active_item;
246   switch(menuaction) {
247     case MENU_ACTION_UP:
248       do {
249         if (active_item > 0)
250           --active_item;
251         else
252           active_item = int(items.size())-1;
253       } while ((items[active_item]->kind == MN_HL
254                 || items[active_item]->kind == MN_LABEL
255                 || items[active_item]->kind == MN_INACTIVE)
256                && (active_item != last_active_item));
257
258       break;
259
260     case MENU_ACTION_DOWN:
261       do {
262         if(active_item < int(items.size())-1 )
263           ++active_item;
264         else
265           active_item = 0;
266       } while ((items[active_item]->kind == MN_HL
267                 || items[active_item]->kind == MN_LABEL
268                 || items[active_item]->kind == MN_INACTIVE)
269                && (active_item != last_active_item));
270
271       break;
272
273     case MENU_ACTION_LEFT:
274       if(items[active_item]->kind == MN_STRINGSELECT) {
275         if(items[active_item]->selected > 0)
276           items[active_item]->selected--;
277         else
278           items[active_item]->selected = items[active_item]->list.size()-1;
279
280         menu_action(items[active_item].get());
281       }
282       break;
283
284     case MENU_ACTION_RIGHT:
285       if(items[active_item]->kind == MN_STRINGSELECT) {
286         if(items[active_item]->selected+1 < items[active_item]->list.size())
287           items[active_item]->selected++;
288         else
289           items[active_item]->selected = 0;
290
291         menu_action(items[active_item].get());
292       }
293       break;
294
295     case MENU_ACTION_HIT: {
296       switch (items[active_item]->kind) {
297         case MN_GOTO:
298           assert(items[active_item]->target_menu != 0);
299           MenuManager::instance().push_menu(items[active_item]->target_menu);
300           break;
301
302         case MN_TOGGLE:
303           items[active_item]->toggled = !items[active_item]->toggled;
304           menu_action(items[active_item].get());
305           break;
306
307         case MN_CONTROLFIELD:
308           menu_action(items[active_item].get());
309           break;
310
311         case MN_ACTION:
312           menu_action(items[active_item].get());
313           break;
314
315         case MN_STRINGSELECT:
316           if(items[active_item]->selected+1 < items[active_item]->list.size())
317             items[active_item]->selected++;
318           else
319             items[active_item]->selected = 0;
320
321           menu_action(items[active_item].get());
322           break;
323
324         case MN_TEXTFIELD:
325         case MN_NUMFIELD:
326           menuaction = MENU_ACTION_DOWN;
327           process_input();
328           break;
329
330         case MN_BACK:
331           MenuManager::instance().pop_menu();
332           return;
333
334         default:
335           break;
336       }
337       break;
338     }
339
340     case MENU_ACTION_REMOVE:
341       if(items[active_item]->kind == MN_TEXTFIELD
342          || items[active_item]->kind == MN_NUMFIELD)
343       {
344         if(!items[active_item]->input.empty())
345         {
346           int i = items[active_item]->input.size();
347
348           while(delete_character > 0)        /* remove characters */
349           {
350             items[active_item]->input.resize(i-1);
351             delete_character--;
352           }
353         }
354       }
355       break;
356
357     case MENU_ACTION_INPUT:
358       if(items[active_item]->kind == MN_TEXTFIELD
359          || (items[active_item]->kind == MN_NUMFIELD
360              && mn_input_char >= '0' && mn_input_char <= '9'))
361       {
362         items[active_item]->input.push_back(mn_input_char);
363       }
364       break;
365
366     case MENU_ACTION_BACK:
367       MenuManager::instance().pop_menu();
368       return;
369
370     case MENU_ACTION_NONE:
371       break;
372   }
373 }
374
375 void
376 Menu::draw_item(DrawingContext& context, int index)
377 {
378   float menu_height = get_height();
379   float menu_width  = get_width();
380
381   MenuItem& pitem = *(items[index]);
382
383   Color text_color = default_color;
384   float x_pos       = pos.x;
385   float y_pos       = pos.y + 24*index - menu_height/2 + 12;
386   int text_width  = int(Resources::normal_font->get_text_width(pitem.text));
387   int input_width = int(Resources::normal_font->get_text_width(pitem.input) + 10);
388   int list_width = 0;
389
390   float left  = pos.x - menu_width/2 + 16;
391   float right = pos.x + menu_width/2 - 16;
392
393   if(pitem.list.size() > 0) {
394     list_width = (int) Resources::normal_font->get_text_width(pitem.list[pitem.selected]);
395   }
396
397   if (arrange_left)
398     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
399
400   if(index == active_item)
401   {
402     text_color = active_color;
403   }
404
405   if(active_item == index)
406   {
407     float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
408     context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
409                                    Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
410                              Color(1.0f, 1.0f, 1.0f, blink),
411                              14.0f,
412                              LAYER_GUI-10);
413     context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
414                                    Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
415                              Color(1.0f, 1.0f, 1.0f, 0.5f),
416                              12.0f,
417                              LAYER_GUI-10);
418   }
419
420   switch (pitem.kind)
421   {
422     case MN_INACTIVE:
423     {
424       context.draw_text(Resources::normal_font, pitem.text,
425                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
426                         ALIGN_CENTER, LAYER_GUI, inactive_color);
427       break;
428     }
429
430     case MN_HL:
431     {
432       // TODO
433       float x = pos.x - menu_width/2;
434       float y = y_pos - 12;
435       /* Draw a horizontal line with a little 3d effect */
436       context.draw_filled_rect(Vector(x, y + 6),
437                                Vector(menu_width, 4),
438                                Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
439       context.draw_filled_rect(Vector(x, y + 6),
440                                Vector(menu_width, 2),
441                                Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
442       break;
443     }
444     case MN_LABEL:
445     {
446       context.draw_text(Resources::big_font, pitem.text,
447                         Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
448                         ALIGN_CENTER, LAYER_GUI, label_color);
449       break;
450     }
451     case MN_TEXTFIELD:
452     case MN_NUMFIELD:
453     case MN_CONTROLFIELD:
454     {
455       if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
456       {
457         if(active_item == index)
458           context.draw_text(Resources::normal_font,
459                             pitem.get_input_with_symbol(true),
460                             Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
461                             ALIGN_RIGHT, LAYER_GUI, field_color);
462         else
463           context.draw_text(Resources::normal_font,
464                             pitem.get_input_with_symbol(false),
465                             Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
466                             ALIGN_RIGHT, LAYER_GUI, field_color);
467       }
468       else
469         context.draw_text(Resources::normal_font, pitem.input,
470                           Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
471                           ALIGN_RIGHT, LAYER_GUI, field_color);
472
473       context.draw_text(Resources::normal_font, pitem.text,
474                         Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
475                         ALIGN_LEFT, LAYER_GUI, text_color);
476       break;
477     }
478     case MN_STRINGSELECT:
479     {
480       float roff = Resources::arrow_left->get_width();
481       // Draw left side
482       context.draw_text(Resources::normal_font, pitem.text,
483                         Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
484                         ALIGN_LEFT, LAYER_GUI, text_color);
485
486       // Draw right side
487       context.draw_surface(Resources::arrow_left,
488                            Vector(right - list_width - roff - roff, y_pos - 8),
489                            LAYER_GUI);
490       context.draw_surface(Resources::arrow_right,
491                            Vector(right - roff, y_pos - 8),
492                            LAYER_GUI);
493       context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
494                         Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
495                         ALIGN_RIGHT, LAYER_GUI, text_color);
496       break;
497     }
498     case MN_BACK:
499     {
500       context.draw_text(Resources::Resources::normal_font, pitem.text,
501                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
502                         ALIGN_CENTER, LAYER_GUI, text_color);
503       context.draw_surface(Resources::back,
504                            Vector(x_pos + text_width/2  + 16, y_pos - 8),
505                            LAYER_GUI);
506       break;
507     }
508
509     case MN_TOGGLE:
510     {
511       context.draw_text(Resources::normal_font, pitem.text,
512                         Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
513                         ALIGN_LEFT, LAYER_GUI, text_color);
514
515       if(pitem.toggled)
516         context.draw_surface(Resources::checkbox_checked,
517                              Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
518                              LAYER_GUI + 1);
519       else
520         context.draw_surface(Resources::checkbox,
521                              Vector(x_pos + (menu_width/2-16) - Resources::checkbox->get_width(), y_pos - 8),
522                              LAYER_GUI + 1);
523       break;
524     }
525     case MN_ACTION:
526       context.draw_text(Resources::normal_font, pitem.text,
527                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
528                         ALIGN_CENTER, LAYER_GUI, text_color);
529       break;
530
531     case MN_GOTO:
532       context.draw_text(Resources::normal_font, pitem.text,
533                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
534                         ALIGN_CENTER, LAYER_GUI, text_color);
535       break;
536   }
537 }
538
539 float
540 Menu::get_width() const
541 {
542   /* The width of the menu has to be more than the width of the text
543      with the most characters */
544   float menu_width = 0;
545   for(unsigned int i = 0; i < items.size(); ++i)
546   {
547     FontPtr font = Resources::Resources::normal_font;
548     if(items[i]->kind == MN_LABEL)
549       font = Resources::big_font;
550
551     float w = font->get_text_width(items[i]->text) +
552       Resources::big_font->get_text_width(items[i]->input) + 16;
553     if(items[i]->kind == MN_TOGGLE)
554       w += 32;
555     if (items[i]->kind == MN_STRINGSELECT)
556       w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
557
558
559     if(w > menu_width)
560       menu_width = w;
561   }
562
563   return menu_width + 24;
564 }
565
566 float
567 Menu::get_height() const
568 {
569   return items.size() * 24;
570 }
571
572 void
573 Menu::on_window_resize()
574 {
575   pos.x = SCREEN_WIDTH / 2;
576   pos.y = SCREEN_HEIGHT / 2;
577 }
578
579 void
580 Menu::draw(DrawingContext& context)
581 {
582   if (!items[active_item]->help.empty())
583   {
584     int text_width  = (int) Resources::normal_font->get_text_width(items[active_item]->help);
585     int text_height = (int) Resources::normal_font->get_text_height(items[active_item]->help);
586
587     Rectf text_rect(pos.x - text_width/2 - 8,
588                    SCREEN_HEIGHT - 48 - text_height/2 - 4,
589                    pos.x + text_width/2 + 8,
590                    SCREEN_HEIGHT - 48 + text_height/2 + 4);
591
592     context.draw_filled_rect(Rectf(text_rect.p1 - Vector(4,4),
593                                    text_rect.p2 + Vector(4,4)),
594                              Color(0.2f, 0.3f, 0.4f, 0.8f),
595                              16.0f,
596                              LAYER_GUI-10);
597
598     context.draw_filled_rect(text_rect,
599                              Color(0.6f, 0.7f, 0.8f, 0.5f),
600                              16.0f,
601                              LAYER_GUI-10);
602
603     context.draw_text(Resources::normal_font, items[active_item]->help,
604                       Vector(pos.x, SCREEN_HEIGHT - 48 - text_height/2),
605                       ALIGN_CENTER, LAYER_GUI);
606   }
607
608   for(unsigned int i = 0; i < items.size(); ++i)
609   {
610     draw_item(context, i);
611   }
612 }
613
614 MenuItem&
615 Menu::get_item_by_id(int id)
616 {
617   for (const auto& item : items)
618   {
619     if (item->id == id)
620     {
621       return *item;
622     }
623   }
624
625   throw std::runtime_error("MenuItem not found");
626 }
627
628 const MenuItem&
629 Menu::get_item_by_id(int id) const
630 {
631   for (const auto& item : items)
632   {
633     if (item->id == id)
634     {
635       return *item;
636     }
637   }
638
639   throw std::runtime_error("MenuItem not found");
640 }
641
642 int Menu::get_active_item_id()
643 {
644   return items[active_item]->id;
645 }
646
647 bool
648 Menu::is_toggled(int id) const
649 {
650   return get_item_by_id(id).toggled;
651 }
652
653 void
654 Menu::set_toggled(int id, bool toggled)
655 {
656   get_item_by_id(id).toggled = toggled;
657 }
658
659 void
660 Menu::event(const SDL_Event& event)
661 {
662   switch(event.type) {
663     case SDL_MOUSEBUTTONDOWN:
664     if(event.button.button == SDL_BUTTON_LEFT)
665     {
666       Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
667       int x = int(mouse_pos.x);
668       int y = int(mouse_pos.y);
669
670       if(x > pos.x - get_width()/2 &&
671          x < pos.x + get_width()/2 &&
672          y > pos.y - get_height()/2 &&
673          y < pos.y + get_height()/2)
674       {
675         process_action(MENU_ACTION_HIT);
676       }
677     }
678     break;
679
680     case SDL_MOUSEMOTION:
681     {
682       Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y);
683       float x = mouse_pos.x;
684       float y = mouse_pos.y;
685
686       if(x > pos.x - get_width()/2 &&
687          x < pos.x + get_width()/2 &&
688          y > pos.y - get_height()/2 &&
689          y < pos.y + get_height()/2)
690       {
691         int new_active_item
692           = static_cast<int> ((y - (pos.y - get_height()/2)) / 24);
693
694         /* only change the mouse focus to a selectable item */
695         if ((items[new_active_item]->kind != MN_HL)
696             && (items[new_active_item]->kind != MN_LABEL)
697             && (items[new_active_item]->kind != MN_INACTIVE))
698           active_item = new_active_item;
699
700         if(MouseCursor::current())
701           MouseCursor::current()->set_state(MC_LINK);
702       }
703       else
704       {
705         if(MouseCursor::current())
706           MouseCursor::current()->set_state(MC_NORMAL);
707       }
708     }
709     break;
710
711     default:
712       break;
713   }
714 }
715
716 void
717 Menu::set_active_item(int id)
718 {
719   for(size_t i = 0; i < items.size(); ++i) {
720     if(items[i]->id == id) {
721       active_item = i;
722       break;
723     }
724   }
725 }
726
727 /* EOF */