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