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