Fixed game crashing when leaving the worldmap
[supertux.git] / src / gui / menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <ctype.h>
24
25 #include <iostream>
26 #include <sstream>
27 #include <math.h>
28 #include <cstdlib>
29 #include <cstdio>
30 #include <string>
31 #include <cassert>
32 #include <stdexcept>
33
34 #include "menu.hpp"
35 #include "mainloop.hpp"
36 #include "video/drawing_context.hpp"
37 #include "gettext.hpp"
38 #include "math/vector.hpp"
39 #include "main.hpp"
40 #include "resources.hpp"
41 #include "timer.hpp"
42 #include "control/joystickkeyboardcontroller.hpp"
43
44 static const float MENU_REPEAT_INITIAL = 0.4f;
45 static const float MENU_REPEAT_RATE = 0.2f;
46 static const float FLICK_CURSOR_TIME = 0.5f;
47
48 extern SDL_Surface* screen;
49
50 std::vector<Menu*> Menu::last_menus;
51 Menu* Menu::current_ = 0;
52 Menu* Menu::previous = 0;
53 Font* Menu::default_font;
54 Font* Menu::active_font;
55 Font* Menu::deactive_font;
56 Font* Menu::label_font;
57 Font* Menu::field_font;
58
59 /* just displays a Yes/No text that can be used to confirm stuff */
60 bool confirm_dialog(Surface *background, std::string text)
61 {
62   //Surface* cap_screen = Surface::CaptureScreen();
63   Menu* dialog = new Menu;
64   dialog->add_deactive(-1, text);
65   dialog->add_hl();
66   dialog->add_entry(true, _("Yes"));
67   dialog->add_entry(false, _("No"));
68   dialog->add_hl();
69
70   Menu::set_current(dialog);
71
72   DrawingContext context;
73
74   // TODO make this a screen and not another mainloop...
75   while(true)
76     {
77       SDL_Event event;
78       while (SDL_PollEvent(&event)) {
79         if(event.type == SDL_QUIT)
80           main_loop->quit();
81         main_controller->process_event(event);
82         dialog->event(event);
83       }
84
85       if(background == NULL)
86         context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
87                               LAYER_BACKGROUND0);
88       else
89         context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
90
91       dialog->draw(context);
92       dialog->update();
93
94       switch (dialog->check())
95         {
96         case true:
97           //delete cap_screen;
98           Menu::set_current(0);
99           delete dialog;
100           return true;
101           break;
102         case false:
103           //delete cap_screen;
104           Menu::set_current(0);
105           delete dialog;
106           return false;
107           break;
108         default:
109           break;
110         }
111
112       mouse_cursor->draw(context);
113       context.do_drawing();
114       SDL_Delay(25);
115     }
116
117   return false;
118 }
119 \f
120 void
121 Menu::push_current(Menu* pmenu)
122 {
123   previous = current_;
124
125   if (current_)
126     last_menus.push_back(current_);
127
128   current_ = pmenu;
129   current_->effect_start_time = real_time;
130   current_->effect_progress   = 0.0f;
131 }
132
133 void
134 Menu::pop_current()
135 {
136   previous = current_;
137
138   if (last_menus.size() >= 1) {
139     current_ = last_menus.back();
140     current_->effect_start_time = real_time;
141     current_->effect_progress   = 0.0f;
142     last_menus.pop_back();
143   } else {
144     current_ = 0;
145   }
146 }
147
148 void
149 Menu::set_current(Menu* menu)
150 {
151   previous = current_;
152
153   last_menus.clear();
154
155   if (menu)
156     {
157       menu->effect_start_time = real_time;
158       menu->effect_progress = 0.0f;
159     }
160   current_ = menu;
161   // just to be sure...
162   main_controller->reset();
163 }
164 \f
165 MenuItem::MenuItem(MenuItemKind _kind, int _id)
166   : kind(_kind) , id(_id)
167 {
168   toggled = false;
169   selected = false;
170   target_menu = 0;
171 }
172
173 void
174 MenuItem::change_text(const  std::string& text_)
175 {
176   text = text_;
177 }
178
179 void
180 MenuItem::change_input(const  std::string& text_)
181 {
182   input = text_;
183 }
184
185 std::string MenuItem::get_input_with_symbol(bool active_item)
186 {
187   if(!active_item) {
188     input_flickering = true;
189   } else {
190     input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
191   }
192
193   char str[1024];
194   if(input_flickering)
195     snprintf(str, sizeof(str), "%s ",input.c_str());
196   else
197     snprintf(str, sizeof(str), "%s_",input.c_str());
198
199   std::string string = str;
200
201   return string;
202 }
203 \f
204 Menu::~Menu()
205 {
206   for(std::vector<MenuItem*>::iterator i = items.begin();
207       i != items.end(); ++i)
208     delete *i;
209
210   if(current_ == this)
211     current_ = NULL;
212
213   if (previous == this)
214     previous = NULL;
215 }
216
217 Menu::Menu()
218 {
219   hit_item = -1;
220   menuaction = MENU_ACTION_NONE;
221   delete_character = 0;
222   mn_input_char = '\0';
223
224   pos_x        = SCREEN_WIDTH/2;
225   pos_y        = SCREEN_HEIGHT/2;
226   arrange_left = 0;
227   active_item  = -1;
228
229   effect_progress   = 0.0f;
230   effect_start_time = 0.0f;
231
232   checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
233   checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
234   back.reset(new Surface("images/engine/menu/arrow-back.png"));
235   arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
236   arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
237 }
238
239 void
240 Menu::set_pos(float x, float y, float rw, float rh)
241 {
242   pos_x = x + get_width() * rw;
243   pos_y = y + get_height() * rh;
244 }
245
246 /* Add an item to a menu */
247 void
248 Menu::additem(MenuItem* item)
249 {
250   items.push_back(item);
251
252   /* If a new menu is being built, the active item shouldn't be set to
253    * something that isnt selectable. Set the active_item to the first
254    * selectable item added
255    */
256   if (active_item == -1
257       && item->kind != MN_HL
258       && item->kind != MN_LABEL
259       && item->kind != MN_DEACTIVE) {
260     active_item = items.size() - 1;
261   }
262 }
263
264 void
265 Menu::add_hl()
266 {
267   additem(new MenuItem(MN_HL));
268 }
269
270 void
271 Menu::add_label(const std::string& text)
272 {
273   MenuItem* item = new MenuItem(MN_LABEL);
274   item->text = text;
275   additem(item);
276 }
277
278 void
279 Menu::add_controlfield(int id, const std::string& text,
280                 const std::string& mapping)
281 {
282   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
283   item->change_text(text);
284         item->change_input(mapping);
285   additem(item);
286 }
287
288 void
289 Menu::add_entry(int id, const std::string& text)
290 {
291   MenuItem* item = new MenuItem(MN_ACTION, id);
292   item->text = text;
293   additem(item);
294 }
295
296 void
297 Menu::add_deactive(int id, const std::string& text)
298 {
299   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
300   item->text = text;
301   additem(item);
302 }
303
304 void
305 Menu::add_toggle(int id, const std::string& text, bool toogled)
306 {
307   MenuItem* item = new MenuItem(MN_TOGGLE, id);
308   item->text = text;
309   item->toggled = toogled;
310   additem(item);
311 }
312
313 void
314 Menu::add_back(const std::string& text)
315 {
316   MenuItem* item = new MenuItem(MN_BACK);
317   item->text = text;
318   additem(item);
319 }
320
321 void
322 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
323 {
324   MenuItem* item = new MenuItem(MN_GOTO, id);
325   item->text = text;
326   item->target_menu = submenu;
327   additem(item);
328 }
329
330 void
331 Menu::clear()
332 {
333   for(std::vector<MenuItem*>::iterator i = items.begin();
334       i != items.end(); ++i) {
335     delete *i;
336   }
337   items.clear();
338   active_item = -1;
339 }
340
341 /* Process actions done on the menu */
342 void
343 Menu::update()
344 {
345   effect_progress = (real_time - effect_start_time) * 6.0f;
346
347   if(effect_progress >= 1.0f) {
348     effect_progress = 1.0f;
349   } else if (effect_progress <= 0.0f) {
350     effect_progress = 0.0f;
351   }
352
353   /** check main input controller... */
354   if(main_controller->pressed(Controller::UP)) {
355     menuaction = MENU_ACTION_UP;
356     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
357   }
358   if(main_controller->hold(Controller::UP) &&
359       menu_repeat_time != 0 && real_time > menu_repeat_time) {
360     menuaction = MENU_ACTION_UP;
361     menu_repeat_time = real_time + MENU_REPEAT_RATE;
362   }
363   if(main_controller->pressed(Controller::DOWN)) {
364     menuaction = MENU_ACTION_DOWN;
365     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
366   }
367   if(main_controller->hold(Controller::DOWN) &&
368       menu_repeat_time != 0 && real_time > menu_repeat_time) {
369     menuaction = MENU_ACTION_DOWN;
370     menu_repeat_time = real_time + MENU_REPEAT_RATE;
371   }
372   if(main_controller->pressed(Controller::ACTION)
373      || main_controller->pressed(Controller::MENU_SELECT)) {
374     menuaction = MENU_ACTION_HIT;
375   }
376   if(main_controller->pressed(Controller::PAUSE_MENU)) {
377     menuaction = MENU_ACTION_BACK;
378   }
379
380   hit_item = -1;
381   if(items.size() == 0)
382     return;
383
384   int last_active_item = active_item;
385   switch(menuaction) {
386     case MENU_ACTION_UP:
387       do {
388         if (active_item > 0)
389           --active_item;
390         else
391           active_item = int(items.size())-1;
392       } while ((items[active_item]->kind == MN_HL
393                 || items[active_item]->kind == MN_LABEL
394                 || items[active_item]->kind == MN_DEACTIVE)
395                && (active_item != last_active_item));
396
397       break;
398
399     case MENU_ACTION_DOWN:
400       do {
401         if(active_item < int(items.size())-1 )
402           ++active_item;
403         else
404           active_item = 0;
405       } while ((items[active_item]->kind == MN_HL
406                 || items[active_item]->kind == MN_LABEL
407                 || items[active_item]->kind == MN_DEACTIVE)
408                && (active_item != last_active_item));
409
410       break;
411
412     case MENU_ACTION_LEFT:
413       if(items[active_item]->kind == MN_STRINGSELECT) {
414         if(items[active_item]->selected > 0)
415           items[active_item]->selected--;
416         else
417           items[active_item]->selected = items[active_item]->list.size()-1;
418       }
419       break;
420
421     case MENU_ACTION_RIGHT:
422       if(items[active_item]->kind == MN_STRINGSELECT) {
423         if(items[active_item]->selected+1 < items[active_item]->list.size())
424           items[active_item]->selected++;
425         else
426           items[active_item]->selected = 0;
427       }
428       break;
429
430     case MENU_ACTION_HIT: {
431       hit_item = active_item;
432       switch (items[active_item]->kind) {
433         case MN_GOTO:
434           assert(items[active_item]->target_menu != 0);
435           Menu::push_current(items[active_item]->target_menu);
436           break;
437
438         case MN_TOGGLE:
439           items[active_item]->toggled = !items[active_item]->toggled;
440           menu_action(items[active_item]);
441           break;
442
443         case MN_CONTROLFIELD:
444           menu_action(items[active_item]);
445           break;
446
447         case MN_ACTION:
448           menu_action(items[active_item]);
449           break;
450
451         case MN_TEXTFIELD:
452         case MN_NUMFIELD:
453           menuaction = MENU_ACTION_DOWN;
454           update();
455           break;
456
457         case MN_BACK:
458           Menu::pop_current();
459           break;
460         default:
461           break;
462       }
463       break;
464     }
465
466     case MENU_ACTION_REMOVE:
467       if(items[active_item]->kind == MN_TEXTFIELD
468          || items[active_item]->kind == MN_NUMFIELD)
469       {
470         if(!items[active_item]->input.empty())
471         {
472           int i = items[active_item]->input.size();
473
474           while(delete_character > 0)   /* remove charactes */
475           {
476             items[active_item]->input.resize(i-1);
477             delete_character--;
478           }
479         }
480       }
481       break;
482
483     case MENU_ACTION_INPUT:
484       if(items[active_item]->kind == MN_TEXTFIELD
485          || (items[active_item]->kind == MN_NUMFIELD
486              && mn_input_char >= '0' && mn_input_char <= '9'))
487       {
488         items[active_item]->input.push_back(mn_input_char);
489       }
490       break;
491
492     case MENU_ACTION_BACK:
493       Menu::pop_current();
494       break;
495
496     case MENU_ACTION_NONE:
497       break;
498   }
499   menuaction = MENU_ACTION_NONE;
500
501   assert(active_item < int(items.size()));
502 }
503
504 int
505 Menu::check()
506 {
507   if (hit_item != -1)
508     return items[hit_item]->id;
509   else
510     return -1;
511 }
512
513 void
514 Menu::menu_action(MenuItem* )
515 {}
516
517 void
518 Menu::draw_item(DrawingContext& context, int index)
519 {
520   float menu_height = get_height();
521   float menu_width  = get_width();
522
523   MenuItem& pitem = *(items[index]);
524
525   Font* text_font = default_font;
526   float x_pos       = pos_x;
527   float y_pos       = pos_y + 24*index - menu_height/2 + 12;
528   int shadow_size = 2;
529   int text_width  = int(text_font->get_text_width(pitem.text));
530   int input_width = int(text_font->get_text_width(pitem.input) + 10);
531   int list_width = 0;
532   if(pitem.list.size() > 0) {
533     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
534   }
535
536   if (arrange_left)
537     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
538
539   if(index == active_item)
540     {
541       shadow_size = 3;
542       text_font = active_font;
543     }
544
545   if(active_item == index)
546     {
547       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
548       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
549                                     Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
550                                Color(1.0f, 1.0f, 1.0f, blink),
551                                14.0f,
552                                LAYER_GUI-10);
553       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
554                                     Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
555                                Color(1.0f, 1.0f, 1.0f, 0.5f),
556                                12.0f,
557                                LAYER_GUI-10);
558     }
559
560   switch (pitem.kind)
561     {
562     case MN_DEACTIVE:
563       {
564         context.draw_text(deactive_font, pitem.text,
565                           Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
566                           ALIGN_CENTER, LAYER_GUI);
567         break;
568       }
569
570     case MN_HL:
571       {
572         // TODO
573         float x = pos_x - menu_width/2;
574         float y = y_pos - 12;
575         /* Draw a horizontal line with a little 3d effect */
576         context.draw_filled_rect(Vector(x, y + 6),
577                                  Vector(menu_width, 4),
578                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
579         context.draw_filled_rect(Vector(x, y + 6),
580                                  Vector(menu_width, 2),
581                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
582         break;
583       }
584     case MN_LABEL:
585       {
586         context.draw_text(label_font, pitem.text,
587                           Vector(pos_x, y_pos - int(label_font->get_height()/2)),
588                           ALIGN_CENTER, LAYER_GUI);
589         break;
590       }
591     case MN_TEXTFIELD:
592     case MN_NUMFIELD:
593     case MN_CONTROLFIELD:
594       {
595         float left  = pos_x - menu_width/2 + 16;
596         float right = pos_x + menu_width/2 - 16;
597
598         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
599           {
600             if(active_item == index)
601               context.draw_text(field_font,
602                                 pitem.get_input_with_symbol(true),
603                                 Vector(right, y_pos - int(field_font->get_height()/2)),
604                                 ALIGN_RIGHT, LAYER_GUI);
605             else
606               context.draw_text(field_font,
607                                 pitem.get_input_with_symbol(false),
608                                 Vector(right, y_pos - int(field_font->get_height()/2)),
609                                 ALIGN_RIGHT, LAYER_GUI);
610           }
611         else
612           context.draw_text(field_font, pitem.input,
613                             Vector(right, y_pos - int(field_font->get_height()/2)),
614                             ALIGN_RIGHT, LAYER_GUI);
615
616         context.draw_text(text_font, pitem.text,
617                           Vector(left, y_pos - int(text_font->get_height()/2)),
618                           ALIGN_LEFT, LAYER_GUI);
619         break;
620       }
621     case MN_STRINGSELECT:
622       {
623         int list_pos_2 = list_width + 16;
624         int list_pos   = list_width/2;
625         int text_pos   = (text_width + 16)/2;
626
627         /* Draw arrows */
628         context.draw_surface(arrow_left.get(),
629                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
630                              LAYER_GUI);
631         context.draw_surface(arrow_right.get(),
632                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
633                              LAYER_GUI);
634
635         /* Draw input background */
636         context.draw_filled_rect(
637           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
638           Vector(list_pos_2 + 2, 20),
639           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
640         context.draw_filled_rect(
641           Vector(x_pos - list_pos + text_pos, y_pos - 9),
642           Vector(list_pos_2, 18),
643           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
644
645         context.draw_text(text_font, pitem.list[pitem.selected],
646                                  Vector(pos_x + text_pos, y_pos - int(text_font->get_height()/2)),
647                                  ALIGN_CENTER, LAYER_GUI);
648         context.draw_text(text_font, pitem.text,
649                                  Vector(pos_x  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
650                                  ALIGN_CENTER, LAYER_GUI);
651         break;
652       }
653     case MN_BACK:
654       {
655         context.draw_text(text_font, pitem.text,
656                           Vector(pos_x, y_pos - int(text_font->get_height()/2)),
657                           ALIGN_CENTER, LAYER_GUI);
658         context.draw_surface(back.get(),
659                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
660                              LAYER_GUI);
661         break;
662       }
663
664     case MN_TOGGLE:
665       {
666         context.draw_text(text_font, pitem.text,
667                           Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
668                           ALIGN_LEFT, LAYER_GUI);
669
670         if(pitem.toggled)
671           context.draw_surface(checkbox_checked.get(),
672                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
673                                LAYER_GUI + 1);
674         else
675           context.draw_surface(checkbox.get(),
676                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
677                                LAYER_GUI + 1);
678         break;
679       }
680     case MN_ACTION:
681       context.draw_text(text_font, pitem.text,
682                         Vector(pos_x, y_pos - int(text_font->get_height()/2)),
683                         ALIGN_CENTER, LAYER_GUI);
684       break;
685
686     case MN_GOTO:
687       context.draw_text(text_font, pitem.text,
688                         Vector(pos_x, y_pos - int(text_font->get_height()/2)),
689                         ALIGN_CENTER, LAYER_GUI);
690       break;
691     }
692 }
693
694 float
695 Menu::get_width() const
696 {
697   /* The width of the menu has to be more than the width of the text
698      with the most characters */
699   float menu_width = 0;
700   for(unsigned int i = 0; i < items.size(); ++i)
701   {
702     Font* font = default_font;
703     if(items[i]->kind == MN_LABEL)
704       font = label_font;
705
706     float w = font->get_text_width(items[i]->text) +
707         label_font->get_text_width(items[i]->input) + 16;
708     if(items[i]->kind == MN_TOGGLE)
709       w += 32;
710
711     if(w > menu_width)
712       menu_width = w;
713   }
714
715   return menu_width + 24;
716 }
717
718 float
719 Menu::get_height() const
720 {
721   return items.size() * 24;
722 }
723
724 /* Draw the current menu. */
725 void
726 Menu::draw(DrawingContext& context)
727 {
728   if(MouseCursor::current()) {
729     MouseCursor::current()->draw(context);
730   }
731
732   float menu_width  = get_width();
733   float menu_height = get_height();
734
735   if (effect_progress != 1.0f)
736     {
737     if (Menu::previous)
738       {
739         menu_width  = (menu_width  * effect_progress) + (Menu::previous->get_width()  * (1.0f - effect_progress));
740         menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
741         //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
742       }
743     else
744       {
745         menu_width  *= effect_progress;
746         menu_height *= effect_progress;
747       }
748     }
749
750   /* Draw a transparent background */
751   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
752                                 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
753                            Color(0.2f, 0.3f, 0.4f, 0.8f), 
754                            20.0f,
755                            LAYER_GUI-10);
756
757   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
758                                 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
759                            Color(0.6f, 0.7f, 0.8f, 0.5f), 
760                            16.0f,
761                            LAYER_GUI-10);
762
763   if (effect_progress == 1.0f)
764     for(unsigned int i = 0; i < items.size(); ++i)
765       {
766         draw_item(context, i);
767       }
768 }
769
770 MenuItem&
771 Menu::get_item_by_id(int id)
772 {
773   for(std::vector<MenuItem*>::iterator i = items.begin();
774       i != items.end(); ++i) {
775     MenuItem& item = **i;
776
777     if(item.id == id)
778       return item;
779   }
780
781   throw std::runtime_error("MenuItem not found");
782 }
783
784 const MenuItem&
785 Menu::get_item_by_id(int id) const
786 {
787   for(std::vector<MenuItem*>::const_iterator i = items.begin();
788       i != items.end(); ++i) {
789     const MenuItem& item = **i;
790
791     if(item.id == id)
792       return item;
793   }
794
795   throw std::runtime_error("MenuItem not found");
796 }
797
798 int Menu::get_active_item_id()
799 {
800   return items[active_item]->id;
801 }
802
803 bool
804 Menu::is_toggled(int id) const
805 {
806   return get_item_by_id(id).toggled;
807 }
808
809 Menu*
810 Menu::get_parent() const
811 {
812   if (last_menus.empty())
813     return 0;
814   else
815     return last_menus.back();
816 }
817
818 /* Check for menu event */
819 void
820 Menu::event(const SDL_Event& event)
821 {
822   if(effect_progress != 1.0f)
823     return;
824
825   switch(event.type) {
826     case SDL_MOUSEBUTTONDOWN:
827       {
828         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
829         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
830
831         if(x > pos_x - get_width()/2 &&
832             x < pos_x + get_width()/2 &&
833             y > pos_y - get_height()/2 &&
834             y < pos_y + get_height()/2)
835           {
836             menuaction = MENU_ACTION_HIT;
837           }
838       }
839       break;
840
841     case SDL_MOUSEMOTION:
842       {
843         float x = event.motion.x * SCREEN_WIDTH/screen->w;
844         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
845
846         if(x > pos_x - get_width()/2 &&
847             x < pos_x + get_width()/2 &&
848             y > pos_y - get_height()/2 &&
849             y < pos_y + get_height()/2)
850           {
851             int new_active_item
852               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
853
854             /* only change the mouse focus to a selectable item */
855             if ((items[new_active_item]->kind != MN_HL)
856                 && (items[new_active_item]->kind != MN_LABEL)
857                 && (items[new_active_item]->kind != MN_DEACTIVE))
858               active_item = new_active_item;
859
860             if(MouseCursor::current())
861               MouseCursor::current()->set_state(MC_LINK);
862           }
863         else
864         {
865           if(MouseCursor::current())
866             MouseCursor::current()->set_state(MC_NORMAL);
867         }
868       }
869       break;
870
871     default:
872       break;
873     }
874 }
875
876 void
877 Menu::set_active_item(int id)
878 {
879   for(size_t i = 0; i < items.size(); ++i) {
880     MenuItem* item = items[i];
881     if(item->id == id) {
882       active_item = i;
883       break;
884     }
885   }
886 }