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