Fixed the ever-elusive crash when using lightmaps: Discard all lightmap requests...
[supertux.git] / src / gui / menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <ctype.h>
24
25 #include <iostream>
26 #include <sstream>
27 #include <cstdlib>
28 #include <cstdio>
29 #include <string>
30 #include <cassert>
31 #include <stdexcept>
32
33 #include "menu.hpp"
34 #include "mainloop.hpp"
35 #include "video/drawing_context.hpp"
36 #include "gettext.hpp"
37 #include "math/vector.hpp"
38 #include "main.hpp"
39 #include "resources.hpp"
40 #include "timer.hpp"
41 #include "control/joystickkeyboardcontroller.hpp"
42
43 static const float MENU_REPEAT_INITIAL = 0.4f;
44 static const float MENU_REPEAT_RATE = 0.2f;
45 static const float FLICK_CURSOR_TIME = 0.5f;
46
47 extern SDL_Surface* screen;
48
49 std::vector<Menu*> Menu::last_menus;
50 Menu* Menu::current_ = 0;
51 Font* Menu::default_font;
52 Font* Menu::active_font;
53 Font* Menu::deactive_font;
54 Font* Menu::label_font;
55 Font* Menu::field_font;
56
57 /* just displays a Yes/No text that can be used to confirm stuff */
58 bool confirm_dialog(Surface *background, std::string text)
59 {
60   //Surface* cap_screen = Surface::CaptureScreen();
61   Menu* dialog = new Menu;
62   dialog->add_deactive(-1, text);
63   dialog->add_hl();
64   dialog->add_entry(true, _("Yes"));
65   dialog->add_entry(false, _("No"));
66   dialog->add_hl();
67
68   Menu::set_current(dialog);
69
70   DrawingContext context;
71
72   // TODO make this a screen and not another mainloop...
73   while(true)
74     {
75       SDL_Event event;
76       while (SDL_PollEvent(&event)) {
77         if(event.type == SDL_QUIT)
78           main_loop->quit();
79         main_controller->process_event(event);
80         dialog->event(event);
81       }
82
83       if(background == NULL)
84         context.draw_gradient(Color(0.8f, 0.95f, 0.85f), Color(0.8f, 0.8f, 0.8f),
85                               LAYER_BACKGROUND0);
86       else
87         context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
88
89       dialog->draw(context);
90       dialog->update();
91
92       switch (dialog->check())
93         {
94         case true:
95           //delete cap_screen;
96           Menu::set_current(0);
97           delete dialog;
98           return true;
99           break;
100         case false:
101           //delete cap_screen;
102           Menu::set_current(0);
103           delete dialog;
104           return false;
105           break;
106         default:
107           break;
108         }
109
110       mouse_cursor->draw(context);
111       context.do_drawing();
112       SDL_Delay(25);
113     }
114
115   return false;
116 }
117
118 void
119 Menu::push_current(Menu* pmenu)
120 {
121   if (current_)
122     last_menus.push_back(current_);
123
124   current_ = pmenu;
125   current_->effect_time = real_time;
126 }
127
128 void
129 Menu::pop_current()
130 {
131   if (last_menus.size() >= 1) {
132     current_ = last_menus.back();
133     current_->effect_time = real_time;
134     last_menus.pop_back();
135   } else {
136     current_ = 0;
137   }
138 }
139
140 void
141 Menu::set_current(Menu* menu)
142 {
143   last_menus.clear();
144
145   if (menu)
146     menu->effect_time = real_time;
147
148   current_ = menu;
149   // just to be sure...
150   main_controller->reset();
151 }
152
153 MenuItem::MenuItem(MenuItemKind _kind, int _id)
154   : kind(_kind) , id(_id)
155 {
156   toggled = false;
157   selected = false;
158   target_menu = 0;
159 }
160
161 void
162 MenuItem::change_text(const  std::string& text_)
163 {
164   text = text_;
165 }
166
167 void
168 MenuItem::change_input(const  std::string& text_)
169 {
170   input = text_;
171 }
172
173 std::string MenuItem::get_input_with_symbol(bool active_item)
174 {
175   if(!active_item) {
176     input_flickering = true;
177   } else {
178     input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
179   }
180
181   char str[1024];
182   if(input_flickering)
183     snprintf(str, sizeof(str), "%s ",input.c_str());
184   else
185     snprintf(str, sizeof(str), "%s_",input.c_str());
186
187   std::string string = str;
188
189   return string;
190 }
191
192 Menu::~Menu()
193 {
194   for(std::vector<MenuItem*>::iterator i = items.begin();
195       i != items.end(); ++i)
196     delete *i;
197   if(current_ == this)
198     current_ = NULL;
199 }
200
201 Menu::Menu()
202 {
203   hit_item = -1;
204   menuaction = MENU_ACTION_NONE;
205   delete_character = 0;
206   mn_input_char = '\0';
207
208   pos_x        = SCREEN_WIDTH/2;
209   pos_y        = SCREEN_HEIGHT/2;
210   arrange_left = 0;
211   active_item  = -1;
212
213   checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
214   checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
215   back.reset(new Surface("images/engine/menu/arrow-back.png"));
216   arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
217   arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
218 }
219
220 void Menu::set_pos(float x, float y, float rw, float rh)
221 {
222   pos_x = x + get_width() * rw;
223   pos_y = y + get_height() * rh;
224 }
225
226 /* Add an item to a menu */
227 void
228 Menu::additem(MenuItem* item)
229 {
230   items.push_back(item);
231
232   /* If a new menu is being built, the active item shouldn't be set to
233    * something that isnt selectable. Set the active_item to the first
234    * selectable item added
235    */
236   if (active_item == -1
237       && item->kind != MN_HL
238       && item->kind != MN_LABEL
239       && item->kind != MN_DEACTIVE) {
240     active_item = items.size() - 1;
241   }
242 }
243
244 void
245 Menu::add_hl()
246 {
247   additem(new MenuItem(MN_HL));
248 }
249
250 void
251 Menu::add_label(const std::string& text)
252 {
253   MenuItem* item = new MenuItem(MN_LABEL);
254   item->text = text;
255   additem(item);
256 }
257
258 void
259 Menu::add_controlfield(int id, const std::string& text,
260                 const std::string& mapping)
261 {
262   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
263   item->change_text(text);
264         item->change_input(mapping);
265   additem(item);
266 }
267
268 void
269 Menu::add_entry(int id, const std::string& text)
270 {
271   MenuItem* item = new MenuItem(MN_ACTION, id);
272   item->text = text;
273   additem(item);
274 }
275
276 void
277 Menu::add_deactive(int id, const std::string& text)
278 {
279   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
280   item->text = text;
281   additem(item);
282 }
283
284 void
285 Menu::add_toggle(int id, const std::string& text, bool toogled)
286 {
287   MenuItem* item = new MenuItem(MN_TOGGLE, id);
288   item->text = text;
289   item->toggled = toogled;
290   additem(item);
291 }
292
293 void
294 Menu::add_back(const std::string& text)
295 {
296   MenuItem* item = new MenuItem(MN_BACK);
297   item->text = text;
298   additem(item);
299 }
300
301 void
302 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
303 {
304   MenuItem* item = new MenuItem(MN_GOTO, id);
305   item->text = text;
306   item->target_menu = submenu;
307   additem(item);
308 }
309
310 void
311 Menu::clear()
312 {
313   for(std::vector<MenuItem*>::iterator i = items.begin();
314       i != items.end(); ++i) {
315     delete *i;
316   }
317   items.clear();
318   active_item = -1;
319 }
320
321 /* Process actions done on the menu */
322 void
323 Menu::update()
324 {
325   /** check main input controller... */
326   if(main_controller->pressed(Controller::UP)) {
327     menuaction = MENU_ACTION_UP;
328     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
329   }
330   if(main_controller->hold(Controller::UP) &&
331       menu_repeat_time != 0 && real_time > menu_repeat_time) {
332     menuaction = MENU_ACTION_UP;
333     menu_repeat_time = real_time + MENU_REPEAT_RATE;
334   }
335   if(main_controller->pressed(Controller::DOWN)) {
336     menuaction = MENU_ACTION_DOWN;
337     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
338   }
339   if(main_controller->hold(Controller::DOWN) &&
340       menu_repeat_time != 0 && real_time > menu_repeat_time) {
341     menuaction = MENU_ACTION_DOWN;
342     menu_repeat_time = real_time + MENU_REPEAT_RATE;
343   }
344   if(main_controller->pressed(Controller::ACTION)
345      || main_controller->pressed(Controller::MENU_SELECT)) {
346     menuaction = MENU_ACTION_HIT;
347   }
348   if(main_controller->pressed(Controller::PAUSE_MENU)) {
349     menuaction = MENU_ACTION_BACK;
350   }
351
352   hit_item = -1;
353   if(items.size() == 0)
354     return;
355
356   int last_active_item = active_item;
357   switch(menuaction) {
358     case MENU_ACTION_UP:
359       do {
360         if (active_item > 0)
361           --active_item;
362         else
363           active_item = int(items.size())-1;
364       } while ((items[active_item]->kind == MN_HL
365                 || items[active_item]->kind == MN_LABEL
366                 || items[active_item]->kind == MN_DEACTIVE)
367                && (active_item != last_active_item));
368
369       break;
370
371     case MENU_ACTION_DOWN:
372       do {
373         if(active_item < int(items.size())-1 )
374           ++active_item;
375         else
376           active_item = 0;
377       } while ((items[active_item]->kind == MN_HL
378                 || items[active_item]->kind == MN_LABEL
379                 || items[active_item]->kind == MN_DEACTIVE)
380                && (active_item != last_active_item));
381
382       break;
383
384     case MENU_ACTION_LEFT:
385       if(items[active_item]->kind == MN_STRINGSELECT) {
386         if(items[active_item]->selected > 0)
387           items[active_item]->selected--;
388         else
389           items[active_item]->selected = items[active_item]->list.size()-1;
390       }
391       break;
392
393     case MENU_ACTION_RIGHT:
394       if(items[active_item]->kind == MN_STRINGSELECT) {
395         if(items[active_item]->selected+1 < items[active_item]->list.size())
396           items[active_item]->selected++;
397         else
398           items[active_item]->selected = 0;
399       }
400       break;
401
402     case MENU_ACTION_HIT: {
403       hit_item = active_item;
404       switch (items[active_item]->kind) {
405         case MN_GOTO:
406           assert(items[active_item]->target_menu != 0);
407           Menu::push_current(items[active_item]->target_menu);
408           break;
409
410         case MN_TOGGLE:
411           items[active_item]->toggled = !items[active_item]->toggled;
412           menu_action(items[active_item]);
413           break;
414
415         case MN_CONTROLFIELD:
416           menu_action(items[active_item]);
417           break;
418
419         case MN_ACTION:
420           menu_action(items[active_item]);
421           break;
422
423         case MN_TEXTFIELD:
424         case MN_NUMFIELD:
425           menuaction = MENU_ACTION_DOWN;
426           update();
427           break;
428
429         case MN_BACK:
430           Menu::pop_current();
431           break;
432         default:
433           break;
434       }
435       break;
436     }
437
438     case MENU_ACTION_REMOVE:
439       if(items[active_item]->kind == MN_TEXTFIELD
440          || items[active_item]->kind == MN_NUMFIELD)
441       {
442         if(!items[active_item]->input.empty())
443         {
444           int i = items[active_item]->input.size();
445
446           while(delete_character > 0)   /* remove charactes */
447           {
448             items[active_item]->input.resize(i-1);
449             delete_character--;
450           }
451         }
452       }
453       break;
454
455     case MENU_ACTION_INPUT:
456       if(items[active_item]->kind == MN_TEXTFIELD
457          || (items[active_item]->kind == MN_NUMFIELD
458              && mn_input_char >= '0' && mn_input_char <= '9'))
459       {
460         items[active_item]->input.push_back(mn_input_char);
461       }
462       break;
463
464     case MENU_ACTION_BACK:
465       Menu::pop_current();
466       break;
467
468     case MENU_ACTION_NONE:
469       break;
470   }
471   menuaction = MENU_ACTION_NONE;
472
473   assert(active_item < int(items.size()));
474 }
475
476 int
477 Menu::check()
478 {
479   if (hit_item != -1)
480     return items[hit_item]->id;
481   else
482     return -1;
483 }
484
485 void
486 Menu::menu_action(MenuItem* )
487 {}
488
489 void
490 Menu::draw_item(DrawingContext& context, int index)
491 {
492   float menu_height = get_height();
493   float menu_width  = get_width();
494
495   MenuItem& pitem = *(items[index]);
496
497   int effect_offset = 0;
498   if(effect_time != 0) {
499     if(real_time - effect_time > 0.5) {
500       effect_time = 0;
501     } else {
502       float effect_delta = (0.5 - (real_time - effect_time)) * 250;
503       effect_offset = (int) ((index % 2) ? effect_delta : -effect_delta);
504     }
505   }
506
507   Font* text_font = default_font;
508   float x_pos       = pos_x;
509   float y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
510   int shadow_size = 2;
511   int text_width  = int(text_font->get_text_width(pitem.text));
512   int input_width = int(text_font->get_text_width(pitem.input) + 10);
513   int list_width = 0;
514   if(pitem.list.size() > 0) {
515     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
516   }
517
518   if (arrange_left)
519     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
520
521   if(index == active_item)
522     {
523       shadow_size = 3;
524       text_font = active_font;
525     }
526
527   if(active_item == index)
528     {
529       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10 - 2, y_pos - 12 - 2),
530                                     Vector(pos_x + menu_width/2 - 10 + 2, y_pos + 12 + 2)),
531                                Color(1.0f, 1.0f, 1.0f, 0.5f),
532                                16.0f,
533                                LAYER_GUI-10);
534       context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2 + 10, y_pos - 12),
535                                     Vector(pos_x + menu_width/2 - 10, y_pos + 12)),
536                                Color(1.0f, 1.0f, 1.0f, 0.5f),
537                                16.0f,
538                                LAYER_GUI-10);
539     }
540
541   switch (pitem.kind)
542     {
543     case MN_DEACTIVE:
544       {
545         context.draw_text(deactive_font, pitem.text,
546                           Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
547                           ALIGN_CENTER, LAYER_GUI);
548         break;
549       }
550
551     case MN_HL:
552       {
553         // TODO
554         float x = pos_x - menu_width/2;
555         float y = y_pos - 12 - effect_offset;
556         /* Draw a horizontal line with a little 3d effect */
557         context.draw_filled_rect(Vector(x, y + 6),
558                                  Vector(menu_width, 4),
559                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
560         context.draw_filled_rect(Vector(x, y + 6),
561                                  Vector(menu_width, 2),
562                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
563         break;
564       }
565     case MN_LABEL:
566       {
567         context.draw_text(label_font, pitem.text,
568                           Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
569                           ALIGN_CENTER, LAYER_GUI);
570         break;
571       }
572     case MN_TEXTFIELD:
573     case MN_NUMFIELD:
574     case MN_CONTROLFIELD:
575       {
576         float width = text_width + input_width + 5;
577         float text_pos = SCREEN_WIDTH/2 - width/2;
578         float input_pos = text_pos + text_width + 10;
579
580         context.draw_filled_rect(
581           Vector(input_pos - 5, y_pos - 10),
582           Vector(input_width + 10, 20),
583           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
584         context.draw_filled_rect(
585           Vector(input_pos - 4, y_pos - 9),
586           Vector(input_width + 8, 18),
587           Color(0, 0, 0, 0.5f), LAYER_GUI-4);
588
589         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
590           {
591             if(active_item == index)
592               context.draw_text(field_font,
593                                 pitem.get_input_with_symbol(true),
594                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
595                                 ALIGN_LEFT, LAYER_GUI);
596             else
597               context.draw_text(field_font,
598                                 pitem.get_input_with_symbol(false),
599                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
600                                 ALIGN_LEFT, LAYER_GUI);
601           }
602         else
603           context.draw_text(field_font, pitem.input,
604                             Vector(input_pos, y_pos - int(field_font->get_height()/2)),
605                             ALIGN_LEFT, LAYER_GUI);
606
607         context.draw_text(text_font, pitem.text,
608                           Vector(text_pos, y_pos - int(text_font->get_height()/2)),
609                           ALIGN_LEFT, LAYER_GUI);
610         break;
611       }
612     case MN_STRINGSELECT:
613       {
614         int list_pos_2 = list_width + 16;
615         int list_pos   = list_width/2;
616         int text_pos   = (text_width + 16)/2;
617
618         /* Draw arrows */
619         context.draw_surface(arrow_left.get(),
620                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
621                              LAYER_GUI);
622         context.draw_surface(arrow_right.get(),
623                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
624                              LAYER_GUI);
625
626         /* Draw input background */
627         context.draw_filled_rect(
628           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
629           Vector(list_pos_2 + 2, 20),
630           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
631         context.draw_filled_rect(
632           Vector(x_pos - list_pos + text_pos, y_pos - 9),
633           Vector(list_pos_2, 18),
634           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
635
636         context.draw_text(text_font, pitem.list[pitem.selected],
637                                  Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
638                                  ALIGN_CENTER, LAYER_GUI);
639         context.draw_text(text_font, pitem.text,
640                                  Vector(SCREEN_WIDTH/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
641                                  ALIGN_CENTER, LAYER_GUI);
642         break;
643       }
644     case MN_BACK:
645       {
646         context.draw_text(text_font, pitem.text,
647                           Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
648                           ALIGN_CENTER, LAYER_GUI);
649         context.draw_surface(back.get(),
650                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
651                              LAYER_GUI);
652         break;
653       }
654
655     case MN_TOGGLE:
656       {
657         context.draw_text(text_font, pitem.text,
658                           Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
659                           ALIGN_CENTER, LAYER_GUI);
660
661         if(pitem.toggled)
662           context.draw_surface(checkbox_checked.get(),
663                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
664                                LAYER_GUI + 1);
665         else
666           context.draw_surface(checkbox.get(),
667                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
668                                LAYER_GUI + 1);
669         break;
670       }
671     case MN_ACTION:
672       context.draw_text(text_font, pitem.text,
673                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
674                         ALIGN_CENTER, LAYER_GUI);
675       break;
676
677     case MN_GOTO:
678       context.draw_text(text_font, pitem.text,
679                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
680                         ALIGN_CENTER, LAYER_GUI);
681       break;
682     }
683 }
684
685 float Menu::get_width() const
686 {
687   /* The width of the menu has to be more than the width of the text
688      with the most characters */
689   float menu_width = 0;
690   for(unsigned int i = 0; i < items.size(); ++i)
691   {
692     Font* font = default_font;
693     if(items[i]->kind == MN_LABEL)
694       font = label_font;
695
696     float w = font->get_text_width(items[i]->text) +
697         label_font->get_text_width(items[i]->input) + 16;
698     if(items[i]->kind == MN_TOGGLE)
699       w += 32;
700
701     if(w > menu_width)
702       menu_width = w;
703   }
704
705   return menu_width + 24;
706 }
707
708 float Menu::get_height() const
709 {
710   return items.size() * 24;
711 }
712
713 /* Draw the current menu. */
714 void
715 Menu::draw(DrawingContext& context)
716 {
717   if(MouseCursor::current()) {
718     MouseCursor::current()->draw(context);
719   }
720
721   float menu_height = get_height();
722   float menu_width  = get_width();
723
724   /* Draw a transparent background */
725   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2-4, pos_y - 24*items.size()/2 - 10-4),
726                                 Vector(pos_x + menu_width/2+4, pos_y - 24*items.size()/2 + 10 + menu_height+4)),
727                            Color(0.2f, 0.3f, 0.4f, 0.8f), 
728                            16.0f,
729                            LAYER_GUI-10);
730
731   context.draw_filled_rect(Rect(Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
732                                 Vector(pos_x + menu_width/2, pos_y - 24*items.size()/2 + 10 + menu_height)),
733                            Color(0.6f, 0.7f, 0.8f, 0.5f), 
734                            16.0f,
735                            LAYER_GUI-10);
736
737   for(unsigned int i = 0; i < items.size(); ++i)
738     {
739       draw_item(context, i);
740     }
741 }
742
743 MenuItem&
744 Menu::get_item_by_id(int id)
745 {
746   for(std::vector<MenuItem*>::iterator i = items.begin();
747       i != items.end(); ++i) {
748     MenuItem& item = **i;
749
750     if(item.id == id)
751       return item;
752   }
753
754   throw std::runtime_error("MenuItem not found");
755 }
756
757 const MenuItem&
758 Menu::get_item_by_id(int id) const
759 {
760   for(std::vector<MenuItem*>::const_iterator i = items.begin();
761       i != items.end(); ++i) {
762     const MenuItem& item = **i;
763
764     if(item.id == id)
765       return item;
766   }
767
768   throw std::runtime_error("MenuItem not found");
769 }
770
771 int Menu::get_active_item_id()
772 {
773   return items[active_item]->id;
774 }
775
776 bool
777 Menu::is_toggled(int id) const
778 {
779   return get_item_by_id(id).toggled;
780 }
781
782 /* Check for menu event */
783 void
784 Menu::event(const SDL_Event& event)
785 {
786   if(effect_time != 0)
787     return;
788
789   switch(event.type) {
790     case SDL_MOUSEBUTTONDOWN:
791       {
792         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
793         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
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             menuaction = MENU_ACTION_HIT;
801           }
802       }
803       break;
804
805     case SDL_MOUSEMOTION:
806       {
807         float x = event.motion.x * SCREEN_WIDTH/screen->w;
808         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
809
810         if(x > pos_x - get_width()/2 &&
811             x < pos_x + get_width()/2 &&
812             y > pos_y - get_height()/2 &&
813             y < pos_y + get_height()/2)
814           {
815             int new_active_item
816               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
817
818             /* only change the mouse focus to a selectable item */
819             if ((items[new_active_item]->kind != MN_HL)
820                 && (items[new_active_item]->kind != MN_LABEL)
821                 && (items[new_active_item]->kind != MN_DEACTIVE))
822               active_item = new_active_item;
823
824             if(MouseCursor::current())
825               MouseCursor::current()->set_state(MC_LINK);
826           }
827         else
828         {
829           if(MouseCursor::current())
830             MouseCursor::current()->set_state(MC_NORMAL);
831         }
832       }
833       break;
834
835     default:
836       break;
837     }
838 }
839
840 void
841 Menu::set_active_item(int id)
842 {
843   for(size_t i = 0; i < items.size(); ++i) {
844     MenuItem* item = items[i];
845     if(item->id == id) {
846       active_item = i;
847       break;
848     }
849   }
850 }