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