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