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