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