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