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