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