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