Menu items in Option menu are aligned right and left
[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
236 Menu::set_pos(float x, float y, float rw, float rh)
237 {
238   pos_x = x + get_width() * rw;
239   pos_y = y + get_height() * rh;
240 }
241
242 /* Add an item to a menu */
243 void
244 Menu::additem(MenuItem* item)
245 {
246   items.push_back(item);
247
248   /* If a new menu is being built, the active item shouldn't be set to
249    * something that isnt selectable. Set the active_item to the first
250    * selectable item added
251    */
252   if (active_item == -1
253       && item->kind != MN_HL
254       && item->kind != MN_LABEL
255       && item->kind != MN_DEACTIVE) {
256     active_item = items.size() - 1;
257   }
258 }
259
260 void
261 Menu::add_hl()
262 {
263   additem(new MenuItem(MN_HL));
264 }
265
266 void
267 Menu::add_label(const std::string& text)
268 {
269   MenuItem* item = new MenuItem(MN_LABEL);
270   item->text = text;
271   additem(item);
272 }
273
274 void
275 Menu::add_controlfield(int id, const std::string& text,
276                 const std::string& mapping)
277 {
278   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
279   item->change_text(text);
280         item->change_input(mapping);
281   additem(item);
282 }
283
284 void
285 Menu::add_entry(int id, const std::string& text)
286 {
287   MenuItem* item = new MenuItem(MN_ACTION, id);
288   item->text = text;
289   additem(item);
290 }
291
292 void
293 Menu::add_deactive(int id, const std::string& text)
294 {
295   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
296   item->text = text;
297   additem(item);
298 }
299
300 void
301 Menu::add_toggle(int id, const std::string& text, bool toogled)
302 {
303   MenuItem* item = new MenuItem(MN_TOGGLE, id);
304   item->text = text;
305   item->toggled = toogled;
306   additem(item);
307 }
308
309 void
310 Menu::add_back(const std::string& text)
311 {
312   MenuItem* item = new MenuItem(MN_BACK);
313   item->text = text;
314   additem(item);
315 }
316
317 void
318 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
319 {
320   MenuItem* item = new MenuItem(MN_GOTO, id);
321   item->text = text;
322   item->target_menu = submenu;
323   additem(item);
324 }
325
326 void
327 Menu::clear()
328 {
329   for(std::vector<MenuItem*>::iterator i = items.begin();
330       i != items.end(); ++i) {
331     delete *i;
332   }
333   items.clear();
334   active_item = -1;
335 }
336
337 /* Process actions done on the menu */
338 void
339 Menu::update()
340 {
341   effect_progress = (real_time - effect_start_time) * 6.0f;
342
343   if(effect_progress >= 1.0f) {
344     effect_progress = 1.0f;
345   } else if (effect_progress <= 0.0f) {
346     effect_progress = 0.0f;
347   }
348
349   /** check main input controller... */
350   if(main_controller->pressed(Controller::UP)) {
351     menuaction = MENU_ACTION_UP;
352     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
353   }
354   if(main_controller->hold(Controller::UP) &&
355       menu_repeat_time != 0 && real_time > menu_repeat_time) {
356     menuaction = MENU_ACTION_UP;
357     menu_repeat_time = real_time + MENU_REPEAT_RATE;
358   }
359   if(main_controller->pressed(Controller::DOWN)) {
360     menuaction = MENU_ACTION_DOWN;
361     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
362   }
363   if(main_controller->hold(Controller::DOWN) &&
364       menu_repeat_time != 0 && real_time > menu_repeat_time) {
365     menuaction = MENU_ACTION_DOWN;
366     menu_repeat_time = real_time + MENU_REPEAT_RATE;
367   }
368   if(main_controller->pressed(Controller::ACTION)
369      || main_controller->pressed(Controller::MENU_SELECT)) {
370     menuaction = MENU_ACTION_HIT;
371   }
372   if(main_controller->pressed(Controller::PAUSE_MENU)) {
373     menuaction = MENU_ACTION_BACK;
374   }
375
376   hit_item = -1;
377   if(items.size() == 0)
378     return;
379
380   int last_active_item = active_item;
381   switch(menuaction) {
382     case MENU_ACTION_UP:
383       do {
384         if (active_item > 0)
385           --active_item;
386         else
387           active_item = int(items.size())-1;
388       } while ((items[active_item]->kind == MN_HL
389                 || items[active_item]->kind == MN_LABEL
390                 || items[active_item]->kind == MN_DEACTIVE)
391                && (active_item != last_active_item));
392
393       break;
394
395     case MENU_ACTION_DOWN:
396       do {
397         if(active_item < int(items.size())-1 )
398           ++active_item;
399         else
400           active_item = 0;
401       } while ((items[active_item]->kind == MN_HL
402                 || items[active_item]->kind == MN_LABEL
403                 || items[active_item]->kind == MN_DEACTIVE)
404                && (active_item != last_active_item));
405
406       break;
407
408     case MENU_ACTION_LEFT:
409       if(items[active_item]->kind == MN_STRINGSELECT) {
410         if(items[active_item]->selected > 0)
411           items[active_item]->selected--;
412         else
413           items[active_item]->selected = items[active_item]->list.size()-1;
414       }
415       break;
416
417     case MENU_ACTION_RIGHT:
418       if(items[active_item]->kind == MN_STRINGSELECT) {
419         if(items[active_item]->selected+1 < items[active_item]->list.size())
420           items[active_item]->selected++;
421         else
422           items[active_item]->selected = 0;
423       }
424       break;
425
426     case MENU_ACTION_HIT: {
427       hit_item = active_item;
428       switch (items[active_item]->kind) {
429         case MN_GOTO:
430           assert(items[active_item]->target_menu != 0);
431           Menu::push_current(items[active_item]->target_menu);
432           break;
433
434         case MN_TOGGLE:
435           items[active_item]->toggled = !items[active_item]->toggled;
436           menu_action(items[active_item]);
437           break;
438
439         case MN_CONTROLFIELD:
440           menu_action(items[active_item]);
441           break;
442
443         case MN_ACTION:
444           menu_action(items[active_item]);
445           break;
446
447         case MN_TEXTFIELD:
448         case MN_NUMFIELD:
449           menuaction = MENU_ACTION_DOWN;
450           update();
451           break;
452
453         case MN_BACK:
454           Menu::pop_current();
455           break;
456         default:
457           break;
458       }
459       break;
460     }
461
462     case MENU_ACTION_REMOVE:
463       if(items[active_item]->kind == MN_TEXTFIELD
464          || items[active_item]->kind == MN_NUMFIELD)
465       {
466         if(!items[active_item]->input.empty())
467         {
468           int i = items[active_item]->input.size();
469
470           while(delete_character > 0)   /* remove charactes */
471           {
472             items[active_item]->input.resize(i-1);
473             delete_character--;
474           }
475         }
476       }
477       break;
478
479     case MENU_ACTION_INPUT:
480       if(items[active_item]->kind == MN_TEXTFIELD
481          || (items[active_item]->kind == MN_NUMFIELD
482              && mn_input_char >= '0' && mn_input_char <= '9'))
483       {
484         items[active_item]->input.push_back(mn_input_char);
485       }
486       break;
487
488     case MENU_ACTION_BACK:
489       Menu::pop_current();
490       break;
491
492     case MENU_ACTION_NONE:
493       break;
494   }
495   menuaction = MENU_ACTION_NONE;
496
497   assert(active_item < int(items.size()));
498 }
499
500 int
501 Menu::check()
502 {
503   if (hit_item != -1)
504     return items[hit_item]->id;
505   else
506     return -1;
507 }
508
509 void
510 Menu::menu_action(MenuItem* )
511 {}
512
513 void
514 Menu::draw_item(DrawingContext& context, int index)
515 {
516   float menu_height = get_height();
517   float menu_width  = get_width();
518
519   MenuItem& pitem = *(items[index]);
520
521   Font* text_font = default_font;
522   float x_pos       = pos_x;
523   float y_pos       = pos_y + 24*index - menu_height/2 + 12;
524   int shadow_size = 2;
525   int text_width  = int(text_font->get_text_width(pitem.text));
526   int input_width = int(text_font->get_text_width(pitem.input) + 10);
527   int list_width = 0;
528   if(pitem.list.size() > 0) {
529     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
530   }
531
532   if (arrange_left)
533     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
534
535   if(index == active_item)
536     {
537       shadow_size = 3;
538       text_font = active_font;
539     }
540
541   if(active_item == index)
542     {
543       float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
544       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
545                                     Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
546                                Color(1.0f, 1.0f, 1.0f, blink),
547                                14.0f,
548                                LAYER_GUI-10);
549       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
550                                     Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
551                                Color(1.0f, 1.0f, 1.0f, 0.5f),
552                                12.0f,
553                                LAYER_GUI-10);
554     }
555
556   switch (pitem.kind)
557     {
558     case MN_DEACTIVE:
559       {
560         context.draw_text(deactive_font, pitem.text,
561                           Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
562                           ALIGN_CENTER, LAYER_GUI);
563         break;
564       }
565
566     case MN_HL:
567       {
568         // TODO
569         float x = pos_x - menu_width/2;
570         float y = y_pos - 12;
571         /* Draw a horizontal line with a little 3d effect */
572         context.draw_filled_rect(Vector(x, y + 6),
573                                  Vector(menu_width, 4),
574                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
575         context.draw_filled_rect(Vector(x, y + 6),
576                                  Vector(menu_width, 2),
577                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
578         break;
579       }
580     case MN_LABEL:
581       {
582         context.draw_text(label_font, pitem.text,
583                           Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
584                           ALIGN_CENTER, LAYER_GUI);
585         break;
586       }
587     case MN_TEXTFIELD:
588     case MN_NUMFIELD:
589     case MN_CONTROLFIELD:
590       {
591         float left  = SCREEN_WIDTH/2 - menu_width/2 + 16;
592         float right = SCREEN_WIDTH/2 + menu_width/2 - 16;
593
594         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
595           {
596             if(active_item == index)
597               context.draw_text(field_font,
598                                 pitem.get_input_with_symbol(true),
599                                 Vector(right, y_pos - int(field_font->get_height()/2)),
600                                 ALIGN_RIGHT, LAYER_GUI);
601             else
602               context.draw_text(field_font,
603                                 pitem.get_input_with_symbol(false),
604                                 Vector(right, y_pos - int(field_font->get_height()/2)),
605                                 ALIGN_RIGHT, LAYER_GUI);
606           }
607         else
608           context.draw_text(field_font, pitem.input,
609                             Vector(right, y_pos - int(field_font->get_height()/2)),
610                             ALIGN_RIGHT, LAYER_GUI);
611
612         context.draw_text(text_font, pitem.text,
613                           Vector(left, y_pos - int(text_font->get_height()/2)),
614                           ALIGN_LEFT, LAYER_GUI);
615         break;
616       }
617     case MN_STRINGSELECT:
618       {
619         int list_pos_2 = list_width + 16;
620         int list_pos   = list_width/2;
621         int text_pos   = (text_width + 16)/2;
622
623         /* Draw arrows */
624         context.draw_surface(arrow_left.get(),
625                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
626                              LAYER_GUI);
627         context.draw_surface(arrow_right.get(),
628                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
629                              LAYER_GUI);
630
631         /* Draw input background */
632         context.draw_filled_rect(
633           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
634           Vector(list_pos_2 + 2, 20),
635           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
636         context.draw_filled_rect(
637           Vector(x_pos - list_pos + text_pos, y_pos - 9),
638           Vector(list_pos_2, 18),
639           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
640
641         context.draw_text(text_font, pitem.list[pitem.selected],
642                                  Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
643                                  ALIGN_CENTER, LAYER_GUI);
644         context.draw_text(text_font, pitem.text,
645                                  Vector(SCREEN_WIDTH/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
646                                  ALIGN_CENTER, LAYER_GUI);
647         break;
648       }
649     case MN_BACK:
650       {
651         context.draw_text(text_font, pitem.text,
652                           Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
653                           ALIGN_CENTER, LAYER_GUI);
654         context.draw_surface(back.get(),
655                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
656                              LAYER_GUI);
657         break;
658       }
659
660     case MN_TOGGLE:
661       {
662         context.draw_text(text_font, pitem.text,
663                           Vector(SCREEN_WIDTH/2 - menu_width/2 + 16, y_pos - (text_font->get_height()/2)),
664                           ALIGN_LEFT, LAYER_GUI);
665
666         if(pitem.toggled)
667           context.draw_surface(checkbox_checked.get(),
668                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
669                                LAYER_GUI + 1);
670         else
671           context.draw_surface(checkbox.get(),
672                                Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
673                                LAYER_GUI + 1);
674         break;
675       }
676     case MN_ACTION:
677       context.draw_text(text_font, pitem.text,
678                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
679                         ALIGN_CENTER, LAYER_GUI);
680       break;
681
682     case MN_GOTO:
683       context.draw_text(text_font, pitem.text,
684                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
685                         ALIGN_CENTER, LAYER_GUI);
686       break;
687     }
688 }
689
690 float
691 Menu::get_width() const
692 {
693   /* The width of the menu has to be more than the width of the text
694      with the most characters */
695   float menu_width = 0;
696   for(unsigned int i = 0; i < items.size(); ++i)
697   {
698     Font* font = default_font;
699     if(items[i]->kind == MN_LABEL)
700       font = label_font;
701
702     float w = font->get_text_width(items[i]->text) +
703         label_font->get_text_width(items[i]->input) + 16;
704     if(items[i]->kind == MN_TOGGLE)
705       w += 32;
706
707     if(w > menu_width)
708       menu_width = w;
709   }
710
711   return menu_width + 24;
712 }
713
714 float
715 Menu::get_height() const
716 {
717   return items.size() * 24;
718 }
719
720 /* Draw the current menu. */
721 void
722 Menu::draw(DrawingContext& context)
723 {
724   if(MouseCursor::current()) {
725     MouseCursor::current()->draw(context);
726   }
727
728   float menu_width  = get_width();
729   float menu_height = get_height();
730
731   if (effect_progress != 1.0f)
732     {
733     if (Menu::previous)
734       {
735         menu_width  = (menu_width  * effect_progress) + (Menu::previous->get_width()  * (1.0f - effect_progress));
736         menu_height = (menu_height * effect_progress) + (Menu::previous->get_height() * (1.0f - effect_progress));
737         //std::cout << effect_progress << " " << this << " " << last_menus.back() << std::endl;
738       }
739     else
740       {
741         menu_width  *= effect_progress;
742         menu_height *= effect_progress;
743       }
744     }
745
746   /* Draw a transparent background */
747   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - menu_height/2 - 10-4),
748                                 Vector(pos_x + menu_width/2+4, pos_y - menu_height/2 + 10 + menu_height+4)),
749                            Color(0.2f, 0.3f, 0.4f, 0.8f), 
750                            20.0f,
751                            LAYER_GUI-10);
752
753   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - menu_height/2 - 10),
754                                 Vector(pos_x + menu_width/2, pos_y - menu_height/2 + 10 + menu_height)),
755                            Color(0.6f, 0.7f, 0.8f, 0.5f), 
756                            16.0f,
757                            LAYER_GUI-10);
758
759   if (effect_progress == 1.0f)
760     for(unsigned int i = 0; i < items.size(); ++i)
761       {
762         draw_item(context, i);
763       }
764 }
765
766 MenuItem&
767 Menu::get_item_by_id(int id)
768 {
769   for(std::vector<MenuItem*>::iterator i = items.begin();
770       i != items.end(); ++i) {
771     MenuItem& item = **i;
772
773     if(item.id == id)
774       return item;
775   }
776
777   throw std::runtime_error("MenuItem not found");
778 }
779
780 const MenuItem&
781 Menu::get_item_by_id(int id) const
782 {
783   for(std::vector<MenuItem*>::const_iterator i = items.begin();
784       i != items.end(); ++i) {
785     const MenuItem& item = **i;
786
787     if(item.id == id)
788       return item;
789   }
790
791   throw std::runtime_error("MenuItem not found");
792 }
793
794 int Menu::get_active_item_id()
795 {
796   return items[active_item]->id;
797 }
798
799 bool
800 Menu::is_toggled(int id) const
801 {
802   return get_item_by_id(id).toggled;
803 }
804
805 /* Check for menu event */
806 void
807 Menu::event(const SDL_Event& event)
808 {
809   if(effect_progress != 1.0f)
810     return;
811
812   switch(event.type) {
813     case SDL_MOUSEBUTTONDOWN:
814       {
815         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
816         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
817
818         if(x > pos_x - get_width()/2 &&
819             x < pos_x + get_width()/2 &&
820             y > pos_y - get_height()/2 &&
821             y < pos_y + get_height()/2)
822           {
823             menuaction = MENU_ACTION_HIT;
824           }
825       }
826       break;
827
828     case SDL_MOUSEMOTION:
829       {
830         float x = event.motion.x * SCREEN_WIDTH/screen->w;
831         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
832
833         if(x > pos_x - get_width()/2 &&
834             x < pos_x + get_width()/2 &&
835             y > pos_y - get_height()/2 &&
836             y < pos_y + get_height()/2)
837           {
838             int new_active_item
839               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
840
841             /* only change the mouse focus to a selectable item */
842             if ((items[new_active_item]->kind != MN_HL)
843                 && (items[new_active_item]->kind != MN_LABEL)
844                 && (items[new_active_item]->kind != MN_DEACTIVE))
845               active_item = new_active_item;
846
847             if(MouseCursor::current())
848               MouseCursor::current()->set_state(MC_LINK);
849           }
850         else
851         {
852           if(MouseCursor::current())
853             MouseCursor::current()->set_state(MC_NORMAL);
854         }
855       }
856       break;
857
858     default:
859       break;
860     }
861 }
862
863 void
864 Menu::set_active_item(int id)
865 {
866   for(size_t i = 0; i < items.size(); ++i) {
867     MenuItem* item = items[i];
868     if(item->id == id) {
869       active_item = i;
870       break;
871     }
872   }
873 }