Added scrolling to the menu (a little crude, but working)
[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   int menu_height = get_height();
346   if (menu_height > SCREEN_HEIGHT)
347     { // Scrolling
348       int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
349       pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
350     }
351
352   effect_progress = (real_time - effect_start_time) * 6.0f;
353
354   if(effect_progress >= 1.0f) {
355     effect_progress = 1.0f;
356   } else if (effect_progress <= 0.0f) {
357     effect_progress = 0.0f;
358   }
359
360   /** check main input controller... */
361   if(main_controller->pressed(Controller::UP)) {
362     menuaction = MENU_ACTION_UP;
363     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
364   }
365   if(main_controller->hold(Controller::UP) &&
366       menu_repeat_time != 0 && real_time > menu_repeat_time) {
367     menuaction = MENU_ACTION_UP;
368     menu_repeat_time = real_time + MENU_REPEAT_RATE;
369   }
370   if(main_controller->pressed(Controller::DOWN)) {
371     menuaction = MENU_ACTION_DOWN;
372     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
373   }
374   if(main_controller->hold(Controller::DOWN) &&
375       menu_repeat_time != 0 && real_time > menu_repeat_time) {
376     menuaction = MENU_ACTION_DOWN;
377     menu_repeat_time = real_time + MENU_REPEAT_RATE;
378   }
379   if(main_controller->pressed(Controller::ACTION)
380      || main_controller->pressed(Controller::MENU_SELECT)) {
381     menuaction = MENU_ACTION_HIT;
382   }
383   if(main_controller->pressed(Controller::PAUSE_MENU)) {
384     menuaction = MENU_ACTION_BACK;
385   }
386
387   hit_item = -1;
388   if(items.size() == 0)
389     return;
390
391   int last_active_item = active_item;
392   switch(menuaction) {
393     case MENU_ACTION_UP:
394       do {
395         if (active_item > 0)
396           --active_item;
397         else
398           active_item = int(items.size())-1;
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_DOWN:
407       do {
408         if(active_item < int(items.size())-1 )
409           ++active_item;
410         else
411           active_item = 0;
412       } while ((items[active_item]->kind == MN_HL
413                 || items[active_item]->kind == MN_LABEL
414                 || items[active_item]->kind == MN_DEACTIVE)
415                && (active_item != last_active_item));
416
417       break;
418
419     case MENU_ACTION_LEFT:
420       if(items[active_item]->kind == MN_STRINGSELECT) {
421         if(items[active_item]->selected > 0)
422           items[active_item]->selected--;
423         else
424           items[active_item]->selected = items[active_item]->list.size()-1;
425       }
426       break;
427
428     case MENU_ACTION_RIGHT:
429       if(items[active_item]->kind == MN_STRINGSELECT) {
430         if(items[active_item]->selected+1 < items[active_item]->list.size())
431           items[active_item]->selected++;
432         else
433           items[active_item]->selected = 0;
434       }
435       break;
436
437     case MENU_ACTION_HIT: {
438       hit_item = active_item;
439       switch (items[active_item]->kind) {
440         case MN_GOTO:
441           assert(items[active_item]->target_menu != 0);
442           Menu::push_current(items[active_item]->target_menu);
443           break;
444
445         case MN_TOGGLE:
446           items[active_item]->toggled = !items[active_item]->toggled;
447           menu_action(items[active_item]);
448           break;
449
450         case MN_CONTROLFIELD:
451           menu_action(items[active_item]);
452           break;
453
454         case MN_ACTION:
455           menu_action(items[active_item]);
456           break;
457
458         case MN_TEXTFIELD:
459         case MN_NUMFIELD:
460           menuaction = MENU_ACTION_DOWN;
461           update();
462           break;
463
464         case MN_BACK:
465           Menu::pop_current();
466           break;
467         default:
468           break;
469       }
470       break;
471     }
472
473     case MENU_ACTION_REMOVE:
474       if(items[active_item]->kind == MN_TEXTFIELD
475          || items[active_item]->kind == MN_NUMFIELD)
476       {
477         if(!items[active_item]->input.empty())
478         {
479           int i = items[active_item]->input.size();
480
481           while(delete_character > 0)   /* remove charactes */
482           {
483             items[active_item]->input.resize(i-1);
484             delete_character--;
485           }
486         }
487       }
488       break;
489
490     case MENU_ACTION_INPUT:
491       if(items[active_item]->kind == MN_TEXTFIELD
492          || (items[active_item]->kind == MN_NUMFIELD
493              && mn_input_char >= '0' && mn_input_char <= '9'))
494       {
495         items[active_item]->input.push_back(mn_input_char);
496       }
497       break;
498
499     case MENU_ACTION_BACK:
500       Menu::pop_current();
501       break;
502
503     case MENU_ACTION_NONE:
504       break;
505   }
506   menuaction = MENU_ACTION_NONE;
507
508   assert(active_item < int(items.size()));
509 }
510
511 int
512 Menu::check()
513 {
514   if (hit_item != -1)
515     return items[hit_item]->id;
516   else
517     return -1;
518 }
519
520 void
521 Menu::menu_action(MenuItem* )
522 {}
523
524 void
525 Menu::draw_item(DrawingContext& context, int index)
526 {
527   float menu_height = get_height();
528   float menu_width  = get_width();
529
530   MenuItem& pitem = *(items[index]);
531
532   Font* text_font = default_font;
533   float x_pos       = pos_x;
534   float y_pos       = pos_y + 24*index - menu_height/2 + 12;
535   int shadow_size = 2;
536   int text_width  = int(text_font->get_text_width(pitem.text));
537   int input_width = int(text_font->get_text_width(pitem.input) + 10);
538   int list_width = 0;
539   if(pitem.list.size() > 0) {
540     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
541   }
542
543   if (arrange_left)
544     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
545
546   if(index == active_item)
547     {
548       shadow_size = 3;
549       text_font = active_font;
550     }
551
552   if(active_item == index)
553     {
554       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
555       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
556                                     Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
557                                Color(1.0f, 1.0f, 1.0f, blink),
558                                14.0f,
559                                LAYER_GUI-10);
560       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
561                                     Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
562                                Color(1.0f, 1.0f, 1.0f, 0.5f),
563                                12.0f,
564                                LAYER_GUI-10);
565     }
566
567   switch (pitem.kind)
568     {
569     case MN_DEACTIVE:
570       {
571         context.draw_text(deactive_font, pitem.text,
572                           Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
573                           ALIGN_CENTER, LAYER_GUI);
574         break;
575       }
576
577     case MN_HL:
578       {
579         // TODO
580         float x = pos_x - menu_width/2;
581         float y = y_pos - 12;
582         /* Draw a horizontal line with a little 3d effect */
583         context.draw_filled_rect(Vector(x, y + 6),
584                                  Vector(menu_width, 4),
585                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
586         context.draw_filled_rect(Vector(x, y + 6),
587                                  Vector(menu_width, 2),
588                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
589         break;
590       }
591     case MN_LABEL:
592       {
593         context.draw_text(label_font, pitem.text,
594                           Vector(pos_x, y_pos - int(label_font->get_height()/2)),
595                           ALIGN_CENTER, LAYER_GUI);
596         break;
597       }
598     case MN_TEXTFIELD:
599     case MN_NUMFIELD:
600     case MN_CONTROLFIELD:
601       {
602         float left  = pos_x - menu_width/2 + 16;
603         float right = pos_x + menu_width/2 - 16;
604
605         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
606           {
607             if(active_item == index)
608               context.draw_text(field_font,
609                                 pitem.get_input_with_symbol(true),
610                                 Vector(right, y_pos - int(field_font->get_height()/2)),
611                                 ALIGN_RIGHT, LAYER_GUI);
612             else
613               context.draw_text(field_font,
614                                 pitem.get_input_with_symbol(false),
615                                 Vector(right, y_pos - int(field_font->get_height()/2)),
616                                 ALIGN_RIGHT, LAYER_GUI);
617           }
618         else
619           context.draw_text(field_font, pitem.input,
620                             Vector(right, y_pos - int(field_font->get_height()/2)),
621                             ALIGN_RIGHT, LAYER_GUI);
622
623         context.draw_text(text_font, pitem.text,
624                           Vector(left, y_pos - int(text_font->get_height()/2)),
625                           ALIGN_LEFT, LAYER_GUI);
626         break;
627       }
628     case MN_STRINGSELECT:
629       {
630         int list_pos_2 = list_width + 16;
631         int list_pos   = list_width/2;
632         int text_pos   = (text_width + 16)/2;
633
634         /* Draw arrows */
635         context.draw_surface(arrow_left.get(),
636                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
637                              LAYER_GUI);
638         context.draw_surface(arrow_right.get(),
639                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
640                              LAYER_GUI);
641
642         /* Draw input background */
643         context.draw_filled_rect(
644           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
645           Vector(list_pos_2 + 2, 20),
646           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
647         context.draw_filled_rect(
648           Vector(x_pos - list_pos + text_pos, y_pos - 9),
649           Vector(list_pos_2, 18),
650           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
651
652         context.draw_text(text_font, pitem.list[pitem.selected],
653                                  Vector(pos_x + text_pos, y_pos - int(text_font->get_height()/2)),
654                                  ALIGN_CENTER, LAYER_GUI);
655         context.draw_text(text_font, pitem.text,
656                                  Vector(pos_x  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
657                                  ALIGN_CENTER, LAYER_GUI);
658         break;
659       }
660     case MN_BACK:
661       {
662         context.draw_text(text_font, pitem.text,
663                           Vector(pos_x, y_pos - int(text_font->get_height()/2)),
664                           ALIGN_CENTER, LAYER_GUI);
665         context.draw_surface(back.get(),
666                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
667                              LAYER_GUI);
668         break;
669       }
670
671     case MN_TOGGLE:
672       {
673         context.draw_text(text_font, pitem.text,
674                           Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
675                           ALIGN_LEFT, LAYER_GUI);
676
677         if(pitem.toggled)
678           context.draw_surface(checkbox_checked.get(),
679                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
680                                LAYER_GUI + 1);
681         else
682           context.draw_surface(checkbox.get(),
683                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
684                                LAYER_GUI + 1);
685         break;
686       }
687     case MN_ACTION:
688       context.draw_text(text_font, pitem.text,
689                         Vector(pos_x, y_pos - int(text_font->get_height()/2)),
690                         ALIGN_CENTER, LAYER_GUI);
691       break;
692
693     case MN_GOTO:
694       context.draw_text(text_font, pitem.text,
695                         Vector(pos_x, y_pos - int(text_font->get_height()/2)),
696                         ALIGN_CENTER, LAYER_GUI);
697       break;
698     }
699 }
700
701 float
702 Menu::get_width() const
703 {
704   /* The width of the menu has to be more than the width of the text
705      with the most characters */
706   float menu_width = 0;
707   for(unsigned int i = 0; i < items.size(); ++i)
708   {
709     Font* font = default_font;
710     if(items[i]->kind == MN_LABEL)
711       font = label_font;
712
713     float w = font->get_text_width(items[i]->text) +
714         label_font->get_text_width(items[i]->input) + 16;
715     if(items[i]->kind == MN_TOGGLE)
716       w += 32;
717
718     if(w > menu_width)
719       menu_width = w;
720   }
721
722   return menu_width + 24;
723 }
724
725 float
726 Menu::get_height() const
727 {
728   return items.size() * 24;
729 }
730
731 /* Draw the current menu. */
732 void
733 Menu::draw(DrawingContext& context)
734 {
735   if(MouseCursor::current()) {
736     MouseCursor::current()->draw(context);
737   }
738
739   float menu_width  = get_width();
740   float menu_height = get_height();
741
742   if (effect_progress != 1.0f)
743     {
744     if (Menu::previous)
745       {
746         menu_width  = (menu_width  * effect_progress) + (Menu::previous->get_width()  * (1.0f - effect_progress));
747         menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
748         //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
749       }
750     else
751       {
752         menu_width  *= effect_progress;
753         menu_height *= effect_progress;
754       }
755     }
756
757   /* Draw a transparent background */
758   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
759                                 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
760                            Color(0.2f, 0.3f, 0.4f, 0.8f), 
761                            20.0f,
762                            LAYER_GUI-10);
763
764   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
765                                 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
766                            Color(0.6f, 0.7f, 0.8f, 0.5f), 
767                            16.0f,
768                            LAYER_GUI-10);
769
770   if (effect_progress == 1.0f)
771     for(unsigned int i = 0; i < items.size(); ++i)
772       {
773         draw_item(context, i);
774       }
775 }
776
777 MenuItem&
778 Menu::get_item_by_id(int id)
779 {
780   for(std::vector<MenuItem*>::iterator i = items.begin();
781       i != items.end(); ++i) {
782     MenuItem& item = **i;
783
784     if(item.id == id)
785       return item;
786   }
787
788   throw std::runtime_error("MenuItem not found");
789 }
790
791 const MenuItem&
792 Menu::get_item_by_id(int id) const
793 {
794   for(std::vector<MenuItem*>::const_iterator i = items.begin();
795       i != items.end(); ++i) {
796     const MenuItem& item = **i;
797
798     if(item.id == id)
799       return item;
800   }
801
802   throw std::runtime_error("MenuItem not found");
803 }
804
805 int Menu::get_active_item_id()
806 {
807   return items[active_item]->id;
808 }
809
810 bool
811 Menu::is_toggled(int id) const
812 {
813   return get_item_by_id(id).toggled;
814 }
815
816 Menu*
817 Menu::get_parent() const
818 {
819   if (last_menus.empty())
820     return 0;
821   else
822     return last_menus.back();
823 }
824
825 /* Check for menu event */
826 void
827 Menu::event(const SDL_Event& event)
828 {
829   if(effect_progress != 1.0f)
830     return;
831
832   switch(event.type) {
833     case SDL_MOUSEBUTTONDOWN:
834       {
835         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
836         int y = int(event.motion.y * float(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             menuaction = MENU_ACTION_HIT;
844           }
845       }
846       break;
847
848     case SDL_MOUSEMOTION:
849       {
850         float x = event.motion.x * SCREEN_WIDTH/screen->w;
851         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
852
853         if(x > pos_x - get_width()/2 &&
854             x < pos_x + get_width()/2 &&
855             y > pos_y - get_height()/2 &&
856             y < pos_y + get_height()/2)
857           {
858             int new_active_item
859               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
860
861             /* only change the mouse focus to a selectable item */
862             if ((items[new_active_item]->kind != MN_HL)
863                 && (items[new_active_item]->kind != MN_LABEL)
864                 && (items[new_active_item]->kind != MN_DEACTIVE))
865               active_item = new_active_item;
866
867             if(MouseCursor::current())
868               MouseCursor::current()->set_state(MC_LINK);
869           }
870         else
871         {
872           if(MouseCursor::current())
873             MouseCursor::current()->set_state(MC_NORMAL);
874         }
875       }
876       break;
877
878     default:
879       break;
880     }
881 }
882
883 void
884 Menu::set_active_item(int id)
885 {
886   for(size_t i = 0; i < items.size(); ++i) {
887     MenuItem* item = items[i];
888     if(item->id == id) {
889       active_item = i;
890       break;
891     }
892   }
893 }