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