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