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