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