Make it compile with 4.6.1.
[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 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     text_color = active_color;
473   }
474
475   if(active_item == index)
476   {
477     float blink = (sinf(real_time * M_PI * 1.0f)/2.0f + 0.5f) * 0.5f + 0.25f;
478     context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
479                                    Vector(pos.x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
480                              Color(1.0f, 1.0f, 1.0f, blink),
481                              14.0f,
482                              LAYER_GUI-10);
483     context.draw_filled_rect(Rectf(Vector(pos.x - menu_width/2 + 10, y_pos - 12),
484                                    Vector(pos.x + menu_width/2 - 10, y_pos + 12)),
485                              Color(1.0f, 1.0f, 1.0f, 0.5f),
486                              12.0f,
487                              LAYER_GUI-10);
488   }
489
490   switch (pitem.kind)
491   {
492     case MN_INACTIVE:
493     {
494       context.draw_text(Resources::normal_font, pitem.text,
495                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
496                         ALIGN_CENTER, LAYER_GUI, inactive_color);
497       break;
498     }
499
500     case MN_HL:
501     {
502       // TODO
503       float x = pos.x - menu_width/2;
504       float y = y_pos - 12;
505       /* Draw a horizontal line with a little 3d effect */
506       context.draw_filled_rect(Vector(x, y + 6),
507                                Vector(menu_width, 4),
508                                Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
509       context.draw_filled_rect(Vector(x, y + 6),
510                                Vector(menu_width, 2),
511                                Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
512       break;
513     }
514     case MN_LABEL:
515     {
516       context.draw_text(Resources::big_font, pitem.text,
517                         Vector(pos.x, y_pos - int(Resources::big_font->get_height()/2)),
518                         ALIGN_CENTER, LAYER_GUI, label_color);
519       break;
520     }
521     case MN_TEXTFIELD:
522     case MN_NUMFIELD:
523     case MN_CONTROLFIELD:
524     {
525       if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
526       {
527         if(active_item == index)
528           context.draw_text(Resources::normal_font,
529                             pitem.get_input_with_symbol(true),
530                             Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
531                             ALIGN_RIGHT, LAYER_GUI, field_color);
532         else
533           context.draw_text(Resources::normal_font,
534                             pitem.get_input_with_symbol(false),
535                             Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
536                             ALIGN_RIGHT, LAYER_GUI, field_color);
537       }
538       else
539         context.draw_text(Resources::normal_font, pitem.input,
540                           Vector(right, y_pos - int(Resources::normal_font->get_height()/2)),
541                           ALIGN_RIGHT, LAYER_GUI, field_color);
542
543       context.draw_text(Resources::normal_font, pitem.text,
544                         Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
545                         ALIGN_LEFT, LAYER_GUI, text_color);
546       break;
547     }
548     case MN_STRINGSELECT:
549     {
550       float roff = arrow_left->get_width();
551       // Draw left side
552       context.draw_text(Resources::normal_font, pitem.text,
553                         Vector(left, y_pos - int(Resources::normal_font->get_height()/2)),
554                         ALIGN_LEFT, LAYER_GUI, text_color);
555
556       // Draw right side
557       context.draw_surface(arrow_left,
558                            Vector(right - list_width - roff - roff, y_pos - 8),
559                            LAYER_GUI);
560       context.draw_surface(arrow_right,
561                            Vector(right - roff, y_pos - 8),
562                            LAYER_GUI);
563       context.draw_text(Resources::normal_font, pitem.list[pitem.selected],
564                         Vector(right - roff, y_pos - int(Resources::normal_font->get_height()/2)),
565                         ALIGN_RIGHT, LAYER_GUI, text_color);
566       break;
567     }
568     case MN_BACK:
569     {
570       context.draw_text(Resources::Resources::normal_font, pitem.text,
571                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
572                         ALIGN_CENTER, LAYER_GUI, text_color);
573       context.draw_surface(back,
574                            Vector(x_pos + text_width/2  + 16, y_pos - 8),
575                            LAYER_GUI);
576       break;
577     }
578
579     case MN_TOGGLE:
580     {
581       context.draw_text(Resources::normal_font, pitem.text,
582                         Vector(pos.x - menu_width/2 + 16, y_pos - (Resources::normal_font->get_height()/2)),
583                         ALIGN_LEFT, LAYER_GUI, text_color);
584
585       if(pitem.toggled)
586         context.draw_surface(checkbox_checked,
587                              Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
588                              LAYER_GUI + 1);
589       else
590         context.draw_surface(checkbox,
591                              Vector(x_pos + (menu_width/2-16) - checkbox->get_width(), y_pos - 8),
592                              LAYER_GUI + 1);
593       break;
594     }
595     case MN_ACTION:
596       context.draw_text(Resources::normal_font, pitem.text,
597                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
598                         ALIGN_CENTER, LAYER_GUI, text_color);
599       break;
600
601     case MN_GOTO:
602       context.draw_text(Resources::normal_font, pitem.text,
603                         Vector(pos.x, y_pos - int(Resources::normal_font->get_height()/2)),
604                         ALIGN_CENTER, LAYER_GUI, text_color);
605       break;
606   }
607 }
608
609 float
610 Menu::get_width() const
611 {
612   /* The width of the menu has to be more than the width of the text
613      with the most characters */
614   float menu_width = 0;
615   for(unsigned int i = 0; i < items.size(); ++i)
616   {
617     FontPtr font = Resources::Resources::normal_font;
618     if(items[i]->kind == MN_LABEL)
619       font = Resources::big_font;
620
621     float w = font->get_text_width(items[i]->text) +
622       Resources::big_font->get_text_width(items[i]->input) + 16;
623     if(items[i]->kind == MN_TOGGLE)
624       w += 32;
625     if (items[i]->kind == MN_STRINGSELECT)
626       w += font->get_text_width(items[i]->list[items[i]->selected]) + 32;
627     
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(Rectf(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(Rectf(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     Rectf 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(Rectf(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 */