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