Return pointer to MenuItem when creating one
[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 void
186 MenuItem::set_help(const std::string& help_text)
187 {
188   help = help_text;
189 }
190
191 std::string MenuItem::get_input_with_symbol(bool active_item)
192 {
193   if(!active_item) {
194     input_flickering = true;
195   } else {
196     input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
197   }
198
199   char str[1024];
200   if(input_flickering)
201     snprintf(str, sizeof(str), "%s ",input.c_str());
202   else
203     snprintf(str, sizeof(str), "%s_",input.c_str());
204
205   std::string string = str;
206
207   return string;
208 }
209 \f
210 Menu::~Menu()
211 {
212   for(std::vector<MenuItem*>::iterator i = items.begin();
213       i != items.end(); ++i)
214     delete *i;
215
216   if(current_ == this)
217     current_ = NULL;
218
219   if (previous == this)
220     previous = NULL;
221 }
222
223 Menu::Menu()
224 {
225   hit_item = -1;
226   menuaction = MENU_ACTION_NONE;
227   delete_character = 0;
228   mn_input_char = '\0';
229
230   pos_x        = SCREEN_WIDTH/2;
231   pos_y        = SCREEN_HEIGHT/2;
232   arrange_left = 0;
233   active_item  = -1;
234
235   effect_progress   = 0.0f;
236   effect_start_time = 0.0f;
237
238   checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
239   checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
240   back.reset(new Surface("images/engine/menu/arrow-back.png"));
241   arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
242   arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
243 }
244
245 void
246 Menu::set_pos(float x, float y, float rw, float rh)
247 {
248   pos_x = x + get_width() * rw;
249   pos_y = y + get_height() * rh;
250 }
251
252 /* Add an item to a menu */
253 void
254 Menu::additem(MenuItem* item)
255 {
256   items.push_back(item);
257
258   /* If a new menu is being built, the active item shouldn't be set to
259    * something that isnt selectable. Set the active_item to the first
260    * selectable item added
261    */
262   if (active_item == -1
263       && item->kind != MN_HL
264       && item->kind != MN_LABEL
265       && item->kind != MN_DEACTIVE) {
266     active_item = items.size() - 1;
267   }
268 }
269
270 MenuItem*
271 Menu::add_hl()
272 {
273   MenuItem* item = new MenuItem(MN_HL);
274   additem(item);
275   return item;
276 }
277
278 MenuItem*
279 Menu::add_label(const std::string& text)
280 {
281   MenuItem* item = new MenuItem(MN_LABEL);
282   item->text = text;
283   additem(item);
284   return item;
285 }
286
287 MenuItem*
288 Menu::add_controlfield(int id, const std::string& text,
289                 const std::string& mapping)
290 {
291   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
292   item->change_text(text);
293         item->change_input(mapping);
294   additem(item);
295   return item;
296 }
297
298 MenuItem*
299 Menu::add_entry(int id, const std::string& text)
300 {
301   MenuItem* item = new MenuItem(MN_ACTION, id);
302   item->text = text;
303   additem(item);
304   return item;
305 }
306
307 MenuItem*
308 Menu::add_deactive(int id, const std::string& text)
309 {
310   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
311   item->text = text;
312   additem(item);
313   return item;
314 }
315
316 MenuItem*
317 Menu::add_toggle(int id, const std::string& text, bool toogled)
318 {
319   MenuItem* item = new MenuItem(MN_TOGGLE, id);
320   item->text = text;
321   item->toggled = toogled;
322   additem(item);
323   return item;
324 }
325
326 MenuItem*
327 Menu::add_back(const std::string& text)
328 {
329   MenuItem* item = new MenuItem(MN_BACK);
330   item->text = text;
331   additem(item);
332   return item;
333 }
334
335 MenuItem*
336 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
337 {
338   MenuItem* item = new MenuItem(MN_GOTO, id);
339   item->text = text;
340   item->target_menu = submenu;
341   additem(item);
342   return item;
343 }
344
345 void
346 Menu::clear()
347 {
348   for(std::vector<MenuItem*>::iterator i = items.begin();
349       i != items.end(); ++i) {
350     delete *i;
351   }
352   items.clear();
353   active_item = -1;
354 }
355
356 /* Process actions done on the menu */
357 void
358 Menu::update()
359 {
360   int menu_height = get_height();
361   if (menu_height > SCREEN_HEIGHT)
362     { // Scrolling
363       int scroll_offset = (menu_height - SCREEN_HEIGHT) / 2 + 32;
364       pos_y = SCREEN_HEIGHT/2 - scroll_offset * ((float(active_item) / (items.size()-1)) - 0.5f) * 2.0f;
365     }
366
367   effect_progress = (real_time - effect_start_time) * 6.0f;
368
369   if(effect_progress >= 1.0f) {
370     effect_progress = 1.0f;
371   } else if (effect_progress <= 0.0f) {
372     effect_progress = 0.0f;
373   }
374
375   /** check main input controller... */
376   if(main_controller->pressed(Controller::UP)) {
377     menuaction = MENU_ACTION_UP;
378     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
379   }
380   if(main_controller->hold(Controller::UP) &&
381       menu_repeat_time != 0 && real_time > menu_repeat_time) {
382     menuaction = MENU_ACTION_UP;
383     menu_repeat_time = real_time + MENU_REPEAT_RATE;
384   }
385   if(main_controller->pressed(Controller::DOWN)) {
386     menuaction = MENU_ACTION_DOWN;
387     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
388   }
389   if(main_controller->hold(Controller::DOWN) &&
390       menu_repeat_time != 0 && real_time > menu_repeat_time) {
391     menuaction = MENU_ACTION_DOWN;
392     menu_repeat_time = real_time + MENU_REPEAT_RATE;
393   }
394   if(main_controller->pressed(Controller::ACTION)
395      || main_controller->pressed(Controller::MENU_SELECT)) {
396     menuaction = MENU_ACTION_HIT;
397   }
398   if(main_controller->pressed(Controller::PAUSE_MENU)) {
399     menuaction = MENU_ACTION_BACK;
400   }
401
402   hit_item = -1;
403   if(items.size() == 0)
404     return;
405
406   int last_active_item = active_item;
407   switch(menuaction) {
408     case MENU_ACTION_UP:
409       do {
410         if (active_item > 0)
411           --active_item;
412         else
413           active_item = int(items.size())-1;
414       } while ((items[active_item]->kind == MN_HL
415                 || items[active_item]->kind == MN_LABEL
416                 || items[active_item]->kind == MN_DEACTIVE)
417                && (active_item != last_active_item));
418
419       break;
420
421     case MENU_ACTION_DOWN:
422       do {
423         if(active_item < int(items.size())-1 )
424           ++active_item;
425         else
426           active_item = 0;
427       } while ((items[active_item]->kind == MN_HL
428                 || items[active_item]->kind == MN_LABEL
429                 || items[active_item]->kind == MN_DEACTIVE)
430                && (active_item != last_active_item));
431
432       break;
433
434     case MENU_ACTION_LEFT:
435       if(items[active_item]->kind == MN_STRINGSELECT) {
436         if(items[active_item]->selected > 0)
437           items[active_item]->selected--;
438         else
439           items[active_item]->selected = items[active_item]->list.size()-1;
440       }
441       break;
442
443     case MENU_ACTION_RIGHT:
444       if(items[active_item]->kind == MN_STRINGSELECT) {
445         if(items[active_item]->selected+1 < items[active_item]->list.size())
446           items[active_item]->selected++;
447         else
448           items[active_item]->selected = 0;
449       }
450       break;
451
452     case MENU_ACTION_HIT: {
453       hit_item = active_item;
454       switch (items[active_item]->kind) {
455         case MN_GOTO:
456           assert(items[active_item]->target_menu != 0);
457           Menu::push_current(items[active_item]->target_menu);
458           break;
459
460         case MN_TOGGLE:
461           items[active_item]->toggled = !items[active_item]->toggled;
462           menu_action(items[active_item]);
463           break;
464
465         case MN_CONTROLFIELD:
466           menu_action(items[active_item]);
467           break;
468
469         case MN_ACTION:
470           menu_action(items[active_item]);
471           break;
472
473         case MN_TEXTFIELD:
474         case MN_NUMFIELD:
475           menuaction = MENU_ACTION_DOWN;
476           update();
477           break;
478
479         case MN_BACK:
480           Menu::pop_current();
481           break;
482         default:
483           break;
484       }
485       break;
486     }
487
488     case MENU_ACTION_REMOVE:
489       if(items[active_item]->kind == MN_TEXTFIELD
490          || items[active_item]->kind == MN_NUMFIELD)
491       {
492         if(!items[active_item]->input.empty())
493         {
494           int i = items[active_item]->input.size();
495
496           while(delete_character > 0)   /* remove charactes */
497           {
498             items[active_item]->input.resize(i-1);
499             delete_character--;
500           }
501         }
502       }
503       break;
504
505     case MENU_ACTION_INPUT:
506       if(items[active_item]->kind == MN_TEXTFIELD
507          || (items[active_item]->kind == MN_NUMFIELD
508              && mn_input_char >= '0' && mn_input_char <= '9'))
509       {
510         items[active_item]->input.push_back(mn_input_char);
511       }
512       break;
513
514     case MENU_ACTION_BACK:
515       Menu::pop_current();
516       break;
517
518     case MENU_ACTION_NONE:
519       break;
520   }
521   menuaction = MENU_ACTION_NONE;
522
523   assert(active_item < int(items.size()));
524 }
525
526 int
527 Menu::check()
528 {
529   if (hit_item != -1)
530     return items[hit_item]->id;
531   else
532     return -1;
533 }
534
535 void
536 Menu::menu_action(MenuItem* )
537 {}
538
539 void
540 Menu::draw_item(DrawingContext& context, int index)
541 {
542   float menu_height = get_height();
543   float menu_width  = get_width();
544
545   MenuItem& pitem = *(items[index]);
546
547   Font* text_font = default_font;
548   float x_pos       = pos_x;
549   float y_pos       = pos_y + 24*index - menu_height/2 + 12;
550   int shadow_size = 2;
551   int text_width  = int(text_font->get_text_width(pitem.text));
552   int input_width = int(text_font->get_text_width(pitem.input) + 10);
553   int list_width = 0;
554   if(pitem.list.size() > 0) {
555     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
556   }
557
558   if (arrange_left)
559     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
560
561   if(index == active_item)
562     {
563       shadow_size = 3;
564       text_font = active_font;
565     }
566
567   if(active_item == index)
568     {
569       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
570       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
571                                     Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
572                                Color(1.0f, 1.0f, 1.0f, blink),
573                                14.0f,
574                                LAYER_GUI-10);
575       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
576                                     Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
577                                Color(1.0f, 1.0f, 1.0f, 0.5f),
578                                12.0f,
579                                LAYER_GUI-10);
580     }
581
582   switch (pitem.kind)
583     {
584     case MN_DEACTIVE:
585       {
586         context.draw_text(deactive_font, pitem.text,
587                           Vector(pos_x, y_pos - int(deactive_font->get_height()/2)),
588                           ALIGN_CENTER, LAYER_GUI);
589         break;
590       }
591
592     case MN_HL:
593       {
594         // TODO
595         float x = pos_x - menu_width/2;
596         float y = y_pos - 12;
597         /* Draw a horizontal line with a little 3d effect */
598         context.draw_filled_rect(Vector(x, y + 6),
599                                  Vector(menu_width, 4),
600                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
601         context.draw_filled_rect(Vector(x, y + 6),
602                                  Vector(menu_width, 2),
603                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
604         break;
605       }
606     case MN_LABEL:
607       {
608         context.draw_text(label_font, pitem.text,
609                           Vector(pos_x, y_pos - int(label_font->get_height()/2)),
610                           ALIGN_CENTER, LAYER_GUI);
611         break;
612       }
613     case MN_TEXTFIELD:
614     case MN_NUMFIELD:
615     case MN_CONTROLFIELD:
616       {
617         float left  = pos_x - menu_width/2 + 16;
618         float right = pos_x + menu_width/2 - 16;
619
620         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
621           {
622             if(active_item == index)
623               context.draw_text(field_font,
624                                 pitem.get_input_with_symbol(true),
625                                 Vector(right, y_pos - int(field_font->get_height()/2)),
626                                 ALIGN_RIGHT, LAYER_GUI);
627             else
628               context.draw_text(field_font,
629                                 pitem.get_input_with_symbol(false),
630                                 Vector(right, y_pos - int(field_font->get_height()/2)),
631                                 ALIGN_RIGHT, LAYER_GUI);
632           }
633         else
634           context.draw_text(field_font, pitem.input,
635                             Vector(right, y_pos - int(field_font->get_height()/2)),
636                             ALIGN_RIGHT, LAYER_GUI);
637
638         context.draw_text(text_font, pitem.text,
639                           Vector(left, y_pos - int(text_font->get_height()/2)),
640                           ALIGN_LEFT, LAYER_GUI);
641         break;
642       }
643     case MN_STRINGSELECT:
644       {
645         int list_pos_2 = list_width + 16;
646         int list_pos   = list_width/2;
647         int text_pos   = (text_width + 16)/2;
648
649         /* Draw arrows */
650         context.draw_surface(arrow_left.get(),
651                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
652                              LAYER_GUI);
653         context.draw_surface(arrow_right.get(),
654                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
655                              LAYER_GUI);
656
657         /* Draw input background */
658         context.draw_filled_rect(
659           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
660           Vector(list_pos_2 + 2, 20),
661           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
662         context.draw_filled_rect(
663           Vector(x_pos - list_pos + text_pos, y_pos - 9),
664           Vector(list_pos_2, 18),
665           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
666
667         context.draw_text(text_font, pitem.list[pitem.selected],
668                                  Vector(pos_x + text_pos, y_pos - int(text_font->get_height()/2)),
669                                  ALIGN_CENTER, LAYER_GUI);
670         context.draw_text(text_font, pitem.text,
671                                  Vector(pos_x  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
672                                  ALIGN_CENTER, LAYER_GUI);
673         break;
674       }
675     case MN_BACK:
676       {
677         context.draw_text(text_font, pitem.text,
678                           Vector(pos_x, y_pos - int(text_font->get_height()/2)),
679                           ALIGN_CENTER, LAYER_GUI);
680         context.draw_surface(back.get(),
681                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
682                              LAYER_GUI);
683         break;
684       }
685
686     case MN_TOGGLE:
687       {
688         context.draw_text(text_font, pitem.text,
689                           Vector(pos_x - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
690                           ALIGN_LEFT, LAYER_GUI);
691
692         if(pitem.toggled)
693           context.draw_surface(checkbox_checked.get(),
694                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
695                                LAYER_GUI + 1);
696         else
697           context.draw_surface(checkbox.get(),
698                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
699                                LAYER_GUI + 1);
700         break;
701       }
702     case MN_ACTION:
703       context.draw_text(text_font, pitem.text,
704                         Vector(pos_x, y_pos - int(text_font->get_height()/2)),
705                         ALIGN_CENTER, LAYER_GUI);
706       break;
707
708     case MN_GOTO:
709       context.draw_text(text_font, pitem.text,
710                         Vector(pos_x, y_pos - int(text_font->get_height()/2)),
711                         ALIGN_CENTER, LAYER_GUI);
712       break;
713     }
714 }
715
716 float
717 Menu::get_width() const
718 {
719   /* The width of the menu has to be more than the width of the text
720      with the most characters */
721   float menu_width = 0;
722   for(unsigned int i = 0; i < items.size(); ++i)
723   {
724     Font* font = default_font;
725     if(items[i]->kind == MN_LABEL)
726       font = label_font;
727
728     float w = font->get_text_width(items[i]->text) +
729         label_font->get_text_width(items[i]->input) + 16;
730     if(items[i]->kind == MN_TOGGLE)
731       w += 32;
732
733     if(w > menu_width)
734       menu_width = w;
735   }
736
737   return menu_width + 24;
738 }
739
740 float
741 Menu::get_height() const
742 {
743   return items.size() * 24;
744 }
745
746 /* Draw the current menu. */
747 void
748 Menu::draw(DrawingContext& context)
749 {
750   if(MouseCursor::current()) {
751     MouseCursor::current()->draw(context);
752   }
753
754   float menu_width  = get_width();
755   float menu_height = get_height();
756
757   if (effect_progress != 1.0f)
758     {
759     if (Menu::previous)
760       {
761         menu_width  = (menu_width  * effect_progress) + (Menu::previous->get_width()  * (1.0f - effect_progress));
762         menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
763         //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
764       }
765     else
766       {
767         menu_width  *= effect_progress;
768         menu_height *= effect_progress;
769       }
770     }
771
772   /* Draw a transparent background */
773   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
774                                 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
775                            Color(0.2f, 0.3f, 0.4f, 0.8f), 
776                            20.0f,
777                            LAYER_GUI-10);
778
779   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
780                                 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
781                            Color(0.6f, 0.7f, 0.8f, 0.5f), 
782                            16.0f,
783                            LAYER_GUI-10);
784
785   if (effect_progress == 1.0f)
786     for(unsigned int i = 0; i < items.size(); ++i)
787       {
788         draw_item(context, i);
789       }
790 }
791
792 MenuItem&
793 Menu::get_item_by_id(int id)
794 {
795   for(std::vector<MenuItem*>::iterator i = items.begin();
796       i != items.end(); ++i) {
797     MenuItem& item = **i;
798
799     if(item.id == id)
800       return item;
801   }
802
803   throw std::runtime_error("MenuItem not found");
804 }
805
806 const MenuItem&
807 Menu::get_item_by_id(int id) const
808 {
809   for(std::vector<MenuItem*>::const_iterator i = items.begin();
810       i != items.end(); ++i) {
811     const MenuItem& item = **i;
812
813     if(item.id == id)
814       return item;
815   }
816
817   throw std::runtime_error("MenuItem not found");
818 }
819
820 int Menu::get_active_item_id()
821 {
822   return items[active_item]->id;
823 }
824
825 bool
826 Menu::is_toggled(int id) const
827 {
828   return get_item_by_id(id).toggled;
829 }
830
831 Menu*
832 Menu::get_parent() const
833 {
834   if (last_menus.empty())
835     return 0;
836   else
837     return last_menus.back();
838 }
839
840 /* Check for menu event */
841 void
842 Menu::event(const SDL_Event& event)
843 {
844   if(effect_progress != 1.0f)
845     return;
846
847   switch(event.type) {
848     case SDL_MOUSEBUTTONDOWN:
849       {
850         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
851         int y = int(event.motion.y * float(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             menuaction = MENU_ACTION_HIT;
859           }
860       }
861       break;
862
863     case SDL_MOUSEMOTION:
864       {
865         float x = event.motion.x * SCREEN_WIDTH/screen->w;
866         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
867
868         if(x > pos_x - get_width()/2 &&
869             x < pos_x + get_width()/2 &&
870             y > pos_y - get_height()/2 &&
871             y < pos_y + get_height()/2)
872           {
873             int new_active_item
874               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
875
876             /* only change the mouse focus to a selectable item */
877             if ((items[new_active_item]->kind != MN_HL)
878                 && (items[new_active_item]->kind != MN_LABEL)
879                 && (items[new_active_item]->kind != MN_DEACTIVE))
880               active_item = new_active_item;
881
882             if(MouseCursor::current())
883               MouseCursor::current()->set_state(MC_LINK);
884           }
885         else
886         {
887           if(MouseCursor::current())
888             MouseCursor::current()->set_state(MC_NORMAL);
889         }
890       }
891       break;
892
893     default:
894       break;
895     }
896 }
897
898 void
899 Menu::set_active_item(int id)
900 {
901   for(size_t i = 0; i < items.size(); ++i) {
902     MenuItem* item = items[i];
903     if(item->id == id) {
904       active_item = i;
905       break;
906     }
907   }
908 }