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