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