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