add a dist target to jam
[supertux.git] / lib / gui / menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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 #ifndef WIN32
21 #include <config.h>
22
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include <iostream>
28 #include <sstream>
29 #include <cstdlib>
30 #include <cstdio>
31 #include <string>
32 #include <cassert>
33
34 #include "../app/globals.h"
35 #include "../gui/menu.h"
36 #include "../video/screen.h"
37 #include "../video/drawing_context.h"
38 #include "../app/setup.h"
39 #include "../app/gettext.h"
40 #include "../math/vector.h"
41
42 using namespace SuperTux;
43
44 #define FLICK_CURSOR_TIME 500
45
46 Surface* SuperTux::checkbox;
47 Surface* SuperTux::checkbox_checked;
48 Surface* SuperTux::back;
49 Surface* SuperTux::arrow_left;
50 Surface* SuperTux::arrow_right;
51
52 std::vector<Menu*> Menu::last_menus;
53 Menu* Menu::current_ = 0;
54 Font* Menu::default_font;
55 Font* Menu::active_font;
56 Font* Menu::deactive_font;
57 Font* Menu::label_font;
58 Font* Menu::field_font;
59
60 /* just displays a Yes/No text that can be used to confirm stuff */
61 bool SuperTux::confirm_dialog(Surface *background, std::string text)
62 {
63   //Surface* cap_screen = Surface::CaptureScreen();
64
65   Menu* dialog = new Menu;
66   dialog->additem(MN_DEACTIVE, text,0,0);
67   dialog->additem(MN_HL,"",0,0);
68   dialog->additem(MN_ACTION,_("Yes"),0,0,true);
69   dialog->additem(MN_ACTION,_("No"),0,0,false);
70   dialog->additem(MN_HL,"",0,0);
71
72   Menu::set_current(dialog);
73
74   DrawingContext context;
75
76   while(true)
77     {
78       SDL_Event event;
79
80       while (SDL_PollEvent(&event))
81         {
82           dialog->event(event);
83         }
84
85       if(background == NULL)
86         context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
87       else
88         context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
89
90       dialog->draw(context);
91       dialog->action();
92
93       switch (dialog->check())
94         {
95         case true:
96           //delete cap_screen;
97           Menu::set_current(0);
98           delete dialog;
99           return true;
100           break;
101         case false:
102           //delete cap_screen;
103           Menu::set_current(0);
104           delete dialog;
105           return false;
106           break;
107         default:
108           break;
109         }
110
111       mouse_cursor->draw(context);
112       context.do_drawing();
113       SDL_Delay(25);
114     }
115
116   return false;
117 }
118
119 void
120 Menu::push_current(Menu* pmenu)
121 {
122   if (current_)
123     last_menus.push_back(current_);
124
125   current_ = pmenu;
126   current_->effect.start(500);
127 }
128
129 void
130 Menu::pop_current()
131 {
132   if (!last_menus.empty())
133     {
134       current_ = last_menus.back();
135       current_->effect.start(500);
136
137       last_menus.pop_back();
138     }
139   else
140     {
141       current_ = 0;
142     }
143 }
144
145 void
146 Menu::set_current(Menu* menu)
147 {
148   last_menus.clear();
149
150   if (menu)
151     menu->effect.start(500);
152
153   current_ = menu;
154 }
155
156 MenuItem::MenuItem(MenuItemKind _kind, int _id)
157   : kind(_kind) , id(_id)
158 {
159 }
160
161 MenuItem::MenuItem(MenuItemKind _kind, int _id, const std::string& _text) 
162   : kind(_kind) , id(_id) , text(_text)
163 {
164 }
165
166 /* Return a pointer to a new menu item */
167 MenuItem*
168 MenuItem::create(MenuItemKind kind_, const std::string& text_, int init_toggle_, Menu* target_menu_, int id_, int* int_p_)
169 {
170   MenuItem *pnew_item = new MenuItem(kind_,id_);
171
172   pnew_item->text = text_;
173
174   if(kind_ == MN_TOGGLE)
175     pnew_item->toggled = init_toggle_;
176   else
177     pnew_item->toggled = false;
178
179   pnew_item->target_menu = target_menu_;
180
181   pnew_item->int_p = int_p_;
182
183   pnew_item->selected = 0;
184
185   pnew_item->input_flickering = false;
186   pnew_item->input_flickering_timer.init(true);
187   pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
188
189   return pnew_item;
190 }
191
192 void
193 MenuItem::change_text(const  std::string& text_)
194 {
195   text = text_;
196 }
197
198 void
199 MenuItem::change_input(const  std::string& text_)
200 {
201   input = text_;
202 }
203
204 std::string MenuItem::get_input_with_symbol(bool active_item)
205 {
206   if(!active_item)
207     input_flickering = true;
208   else
209     {
210       if(input_flickering_timer.get_left() < 0)
211         {
212           if(input_flickering)
213             input_flickering = false;
214           else
215             input_flickering = true;
216           input_flickering_timer.start(FLICK_CURSOR_TIME);
217         }
218     }
219
220   char str[1024];
221   if(input_flickering)
222     sprintf(str,"%s ",input.c_str());
223   else
224     sprintf(str,"%s_",input.c_str());
225
226   std::string string = str;
227
228   return string;
229 }
230
231 /* Set ControlField for keyboard key */
232 void Menu::get_controlfield_key_into_input(MenuItem *item)
233 {
234   switch(*item->int_p)
235     {
236     case SDLK_UP:
237       item->change_input(_("Up cursor"));
238       break;
239     case SDLK_DOWN:
240       item->change_input(_("Down cursor"));
241       break;
242     case SDLK_LEFT:
243       item->change_input(_("Left cursor"));
244       break;
245     case SDLK_RIGHT:
246       item->change_input(_("Right cursor"));
247       break;
248     case SDLK_RETURN:
249       item->change_input(_("Return"));
250       break;
251     case SDLK_SPACE:
252       item->change_input(_("Space"));
253       break;
254     case SDLK_RSHIFT:
255       item->change_input(_("Right Shift"));
256       break;
257     case SDLK_LSHIFT:
258       item->change_input(_("Left Shift"));
259       break;
260     case SDLK_RCTRL:
261       item->change_input(_("Right Control"));
262       break;
263     case SDLK_LCTRL:
264       item->change_input(_("Left Control"));
265       break;
266     case SDLK_RALT:
267       item->change_input(_("Right Alt"));
268       break;
269     case SDLK_LALT:
270       item->change_input(_("Left Alt"));
271       break;
272     default:
273       {
274         char tmp[64];
275         snprintf(tmp, 64, "%d", *item->int_p);
276         item->change_input(tmp);
277       }
278       break;
279     }
280 }
281
282 /* Set ControlField for joystick button */
283 void Menu::get_controlfield_js_into_input(MenuItem *item)
284 {
285   std::ostringstream oss;
286   oss << "Button " << *item->int_p;
287   item->change_input(oss.str().c_str());
288 }
289
290 Menu::~Menu()
291 {
292 }
293
294 Menu::Menu()
295 {
296   hit_item = -1;
297   menuaction = MENU_ACTION_NONE;
298   delete_character = 0;
299   mn_input_char = '\0';
300
301   pos_x        = screen->w/2;
302   pos_y        = screen->h/2;
303   arrange_left = 0;
304   active_item  = 0;
305   effect.init(false);
306
307   joystick_timer.init(true);
308 }
309
310 void Menu::set_pos(int x, int y, float rw, float rh)
311 {
312   pos_x = x + (int)((float)get_width() * rw);
313   pos_y = y + (int)((float)get_height() * rh);
314 }
315
316 void
317 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
318 {
319   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
320   
321     /* If a new menu is being built, the active item shouldn't be set to something
322        that isnt selectable.  Keep setting the active item to the most recently
323        added item until a selectable entry is found.
324     */
325     if (item[active_item].kind == MN_HL 
326            || item[active_item].kind == MN_LABEL
327            || item[active_item].kind == MN_DEACTIVE)
328            active_item = item.size() - 1;
329 }
330
331 /* Add an item to a menu */
332 void
333 Menu::additem(MenuItem* pmenu_item)
334 {
335   item.push_back(*pmenu_item);
336   delete pmenu_item;
337 }
338
339 /* Add an item to a menu */
340 void
341 Menu::additem(const MenuItem& pmenu_item)
342 {
343   item.push_back(pmenu_item);
344 }
345
346 void
347 Menu::clear()
348 {
349   item.clear();
350 }
351
352 /* Process actions done on the menu */
353 void
354 Menu::action()
355 {
356   hit_item = -1;
357   if(item.size() != 0)
358     {
359       int last_active_item = active_item;
360       switch(menuaction)
361         {
362         case MENU_ACTION_UP:
363           do {
364                   if (active_item > 0)
365                     --active_item;
366                   else
367                     active_item = int(item.size())-1;
368                } while ((item[active_item].kind == MN_HL 
369                         || item[active_item].kind == MN_LABEL
370                         || item[active_item].kind == MN_DEACTIVE)
371                         && (active_item != last_active_item));
372                
373           break;
374
375         case MENU_ACTION_DOWN:
376           do {
377                   if(active_item < int(item.size())-1 )
378                     ++active_item;
379                   else
380                     active_item = 0;
381                } while ((item[active_item].kind == MN_HL
382                         || item[active_item].kind == MN_LABEL
383                         || item[active_item].kind == MN_DEACTIVE)
384                         && (active_item != last_active_item));
385
386           break;
387
388         case MENU_ACTION_LEFT:
389           if(item[active_item].kind == MN_STRINGSELECT) {
390             if(item[active_item].selected > 0)
391               item[active_item].selected--;
392             else
393               item[active_item].selected = item[active_item].list.size()-1;
394           }
395           break;
396
397         case MENU_ACTION_RIGHT:
398           if(item[active_item].kind == MN_STRINGSELECT) {
399             if(item[active_item].selected+1 < item[active_item].list.size())
400               item[active_item].selected++;
401             else
402               item[active_item].selected = 0;
403           }
404           break;
405
406         case MENU_ACTION_HIT:
407           {
408             hit_item = active_item;
409             switch (item[active_item].kind)
410               {
411               case MN_GOTO:
412                 if (item[active_item].target_menu != NULL)
413                   Menu::push_current(item[active_item].target_menu);
414                 else
415                   puts("NULLL");
416                 break;
417
418               case MN_TOGGLE:
419                 item[active_item].toggled = !item[active_item].toggled;
420                 break;
421
422               case MN_ACTION:
423                 Menu::set_current(0);
424                 item[active_item].toggled = true;
425                 break;
426               case MN_TEXTFIELD:
427               case MN_NUMFIELD:
428                 menuaction = MENU_ACTION_DOWN;
429                 action();
430                 break;
431
432               case MN_BACK:
433                 Menu::pop_current();
434                 break;
435               default:
436                 break;
437               }
438           }
439           break;
440
441         case MENU_ACTION_REMOVE:
442           if(item[active_item].kind == MN_TEXTFIELD
443               || item[active_item].kind == MN_NUMFIELD)
444             {
445               if(!item[active_item].input.empty())
446                 {
447                   int i = item[active_item].input.size();
448
449                   while(delete_character > 0)   /* remove charactes */
450                     {
451                       item[active_item].input.resize(i-1);
452                       delete_character--;
453                     }
454                 }
455             }
456           break;
457
458         case MENU_ACTION_INPUT:
459           if(item[active_item].kind == MN_TEXTFIELD
460               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
461             {
462               item[active_item].input.push_back(mn_input_char);
463             }
464
465         case MENU_ACTION_NONE:
466           break;
467         }
468     }
469
470
471   menuaction = MENU_ACTION_NONE;
472
473   if (active_item >= int(item.size()))
474     active_item = int(item.size()) - 1;
475 }
476
477 int
478 Menu::check()
479 {
480   if (hit_item != -1)
481     return item[hit_item].id;
482   else
483     return -1;
484 }
485
486 void
487 Menu::draw_item(DrawingContext& context,
488                 int index, // Position of the current item in the menu
489                 int menu_width, int menu_height)
490 {
491   MenuItem& pitem = item[index];
492
493   int effect_offset = 0;
494   {
495     int effect_time = 0;
496
497     if(effect.check())
498       effect_time = effect.get_left() / 4;
499
500     effect_offset = (index % 2) ? effect_time : -effect_time;
501   }
502
503   Font* text_font = default_font;
504   int x_pos       = pos_x;
505   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
506   int shadow_size = 2;
507   int text_width  = int(text_font->get_text_width(pitem.text));
508   int input_width = int(text_font->get_text_width(pitem.input) + 10);
509   int list_width = 0;
510   if(pitem.list.size() > 0) {
511     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
512   }
513   
514   if (arrange_left)
515     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
516
517   if(index == active_item)
518     {
519       shadow_size = 3;
520       text_font = active_font;
521     }
522
523   switch (pitem.kind)
524     {
525     case MN_DEACTIVE:
526       {
527         context.draw_text(deactive_font, pitem.text,
528                           Vector(screen->w/2, y_pos - int(deactive_font->get_height()/2)),
529                           CENTER_ALLIGN, LAYER_GUI);
530         break;
531       }
532
533     case MN_HL:
534       {
535         // TODO
536         int x = pos_x - menu_width/2;
537         int y = y_pos - 12 - effect_offset;
538         /* Draw a horizontal line with a little 3d effect */
539         context.draw_filled_rect(Vector(x, y + 6),
540                                  Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
541         context.draw_filled_rect(Vector(x, y + 6),
542                                  Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
543         break;
544       }
545     case MN_LABEL:
546       {
547         context.draw_text(label_font, pitem.text,
548                           Vector(screen->w/2, y_pos - int(label_font->get_height()/2)),
549                           CENTER_ALLIGN, LAYER_GUI);
550         break;
551       }
552     case MN_TEXTFIELD:
553     case MN_NUMFIELD:
554     case MN_CONTROLFIELD_KB:
555     case MN_CONTROLFIELD_JS:
556       {
557         int width = text_width + input_width + 5;
558         int text_pos = screen->w/2 - width/2;
559         int input_pos = text_pos + text_width + 10;
560
561         context.draw_filled_rect(
562           Vector(input_pos - 5, y_pos - 10),
563           Vector(input_width + 10, 20),
564           Color(255,255,255,255), LAYER_GUI-5);
565         context.draw_filled_rect(
566           Vector(input_pos - 4, y_pos - 9),
567           Vector(input_width + 8, 18),
568           Color(0,0,0,128), LAYER_GUI-4);
569
570         if(pitem.kind == MN_CONTROLFIELD_KB)
571           get_controlfield_key_into_input(&pitem);
572         else if (pitem.kind == MN_CONTROLFIELD_JS)
573           get_controlfield_js_into_input(&pitem);
574
575         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
576           {
577             if(active_item == index)
578               context.draw_text(field_font,
579                                 pitem.get_input_with_symbol(true),
580                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
581                                 LEFT_ALLIGN, LAYER_GUI);
582             else
583               context.draw_text(field_font,
584                                 pitem.get_input_with_symbol(false),
585                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
586                                 LEFT_ALLIGN, LAYER_GUI);
587           }
588         else
589           context.draw_text(field_font, pitem.input,
590                             Vector(input_pos, y_pos - int(field_font->get_height()/2)),
591                             LEFT_ALLIGN, LAYER_GUI);
592
593         context.draw_text(text_font, pitem.text,
594                           Vector(text_pos, y_pos - int(text_font->get_height()/2)),
595                           LEFT_ALLIGN, LAYER_GUI);
596         break;
597       }
598     case MN_STRINGSELECT:
599       {
600         int list_pos_2 = list_width + 16;
601         int list_pos   = list_width/2;
602         int text_pos   = (text_width + 16)/2;
603
604         /* Draw arrows */
605         context.draw_surface(arrow_left,
606                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
607                              LAYER_GUI);
608         context.draw_surface(arrow_right,
609                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
610                              LAYER_GUI);
611
612         /* Draw input background */
613         context.draw_filled_rect(
614           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
615           Vector(list_pos_2 + 2, 20),
616           Color(255,255,255,255), LAYER_GUI - 4);
617         context.draw_filled_rect(
618           Vector(x_pos - list_pos + text_pos, y_pos - 9),
619           Vector(list_pos_2, 18),
620           Color(0,0,0,128), LAYER_GUI - 5);
621
622         context.draw_text(text_font, pitem.list[pitem.selected],
623                                  Vector(screen->w/2 + text_pos, y_pos - int(text_font->get_height()/2)),
624                                  CENTER_ALLIGN, LAYER_GUI);
625         context.draw_text(text_font, pitem.text,
626                                  Vector(screen->w/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
627                                  CENTER_ALLIGN, LAYER_GUI);
628         break;
629       }
630     case MN_BACK:
631       {
632         context.draw_text(text_font, pitem.text,
633                           Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
634                           CENTER_ALLIGN, LAYER_GUI);
635         context.draw_surface(back,
636                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
637                              LAYER_GUI);
638         break;
639       }
640
641     case MN_TOGGLE:
642       {
643         context.draw_text(text_font, pitem.text,
644                           Vector(screen->w/2, y_pos - (text_font->get_height()/2)),
645                           CENTER_ALLIGN, LAYER_GUI);
646
647         if(pitem.toggled)
648           context.draw_surface(checkbox_checked,
649                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
650                                LAYER_GUI + 1);
651         else
652           context.draw_surface(checkbox,
653                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
654                                LAYER_GUI + 1);
655         break;
656       }
657     case MN_ACTION:
658       context.draw_text(text_font, pitem.text,
659                         Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
660                         CENTER_ALLIGN, LAYER_GUI);
661       break;
662
663     case MN_GOTO:
664       context.draw_text(text_font, pitem.text,
665                         Vector(screen->w/2, y_pos - int(text_font->get_height()/2)),
666                         CENTER_ALLIGN, LAYER_GUI);
667       break;
668     }
669 }
670
671 int Menu::get_width() const
672   {
673     /* The width of the menu has to be more than the width of the text
674        with the most characters */
675     int menu_width = 0;
676     for(unsigned int i = 0; i < item.size(); ++i)
677       {
678         int w = item[i].text.size() + item[i].input.size() + 1;
679         if( w > menu_width )
680           {
681             menu_width = w;
682             if( item[i].kind == MN_TOGGLE)
683               menu_width += 2;
684           }
685       }
686
687     return (menu_width * 16 + 24);
688   }
689
690 int Menu::get_height() const
691   {
692     return item.size() * 24;
693   }
694
695 /* Draw the current menu. */
696 void
697 Menu::draw(DrawingContext& context)
698 {
699
700   int menu_height = get_height();
701   int menu_width = get_width();  
702
703   /* Draw a transparent background */
704   context.draw_filled_rect(
705     Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
706     Vector(menu_width,menu_height + 20),
707     Color(150,180,200,125), LAYER_GUI-10);
708
709   for(unsigned int i = 0; i < item.size(); ++i)
710     {
711       draw_item(context, i, menu_width, menu_height);
712     }
713 }
714
715 MenuItem&
716 Menu::get_item_by_id(int id)
717 {
718   for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
719     {
720       if(i->id == id)
721         return *i;
722     }
723
724   assert(false);
725   static MenuItem dummyitem;
726   return dummyitem;
727 }
728
729 int Menu::get_active_item_id()
730 {
731   return item[active_item].id;
732 }
733
734 bool
735 Menu::isToggled(int id)
736 {
737   return get_item_by_id(id).toggled;
738 }
739
740 /* Check for menu event */
741 void
742 Menu::event(SDL_Event& event)
743 {
744   switch(event.type)
745     {
746     case SDL_KEYDOWN:
747       {
748         SDLKey key = key = event.key.keysym.sym;
749         SDLMod keymod;
750         char ch[2];
751         keymod = SDL_GetModState();
752
753         /* If the current unicode character is an ASCII character,
754            assign it to ch. */
755         if ( (event.key.keysym.unicode & 0xFF80) == 0 )
756           {
757             ch[0] = event.key.keysym.unicode & 0x7F;
758             ch[1] = '\0';
759           }
760         else
761           {
762             /* An International Character. */
763           }
764
765         if(item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_KB)
766           {
767             if(key == SDLK_ESCAPE)
768               {
769                 Menu::pop_current();
770                 return;
771               }
772             *item[active_item].int_p = key;
773             menuaction = MENU_ACTION_DOWN;
774             return;
775           }
776
777
778         switch(key)
779           {
780           case SDLK_UP:         /* Menu Up */
781             menuaction = MENU_ACTION_UP;
782             break;
783           case SDLK_DOWN:               /* Menu Down */
784             menuaction = MENU_ACTION_DOWN;
785             break;
786           case SDLK_LEFT:               /* Menu Up */
787             menuaction = MENU_ACTION_LEFT;
788             break;
789           case SDLK_RIGHT:              /* Menu Down */
790             menuaction = MENU_ACTION_RIGHT;
791             break;
792           case SDLK_SPACE:
793             if(item.size() > 0 && item[active_item].kind == MN_TEXTFIELD)
794               {
795                 menuaction = MENU_ACTION_INPUT;
796                 mn_input_char = ' ';
797                 break;
798               }
799           case SDLK_RETURN: /* Menu Hit */
800             menuaction = MENU_ACTION_HIT;
801             break;
802           case SDLK_DELETE:
803           case SDLK_BACKSPACE:
804             menuaction = MENU_ACTION_REMOVE;
805             delete_character++;
806             break;
807           case SDLK_ESCAPE:
808             Menu::pop_current();
809             break;
810           default:
811             if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
812               {
813                 menuaction = MENU_ACTION_INPUT;
814                 mn_input_char = *ch;
815               }
816             else
817               {
818                 mn_input_char = '\0';
819               }
820             break;
821           }
822       }
823       break;
824
825     case  SDL_JOYAXISMOTION:
826       if(event.jaxis.axis == joystick_keymap.y_axis)
827         {
828           if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
829             {
830               menuaction = MENU_ACTION_DOWN;
831               joystick_timer.start(JOYSTICK_MENU_DELAY);
832             }
833           else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
834             {
835               menuaction = MENU_ACTION_UP;
836               joystick_timer.start(JOYSTICK_MENU_DELAY);
837             }
838           else
839             joystick_timer.stop();
840         }
841       break;
842     case SDL_JOYHATMOTION:
843       if(event.jhat.value & SDL_HAT_UP) {
844           menuaction = MENU_ACTION_UP;
845       } else if(event.jhat.value & SDL_HAT_DOWN) {
846           menuaction = MENU_ACTION_DOWN;
847       }
848       break;
849     case  SDL_JOYBUTTONDOWN:
850       if (item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_JS)
851         {
852           // FIXME: This next line does nothing useable, right?
853           // *item[active_item].int_p = key;
854           menuaction = MENU_ACTION_DOWN;
855         }
856       menuaction = MENU_ACTION_HIT;
857       break;
858
859     case SDL_MOUSEBUTTONDOWN:
860       {
861         int x = event.motion.x;
862         int y = event.motion.y;
863
864         if(x > pos_x - get_width()/2 &&
865             x < pos_x + get_width()/2 &&
866             y > pos_y - get_height()/2 &&
867             y < pos_y + get_height()/2)
868           {
869             menuaction = MENU_ACTION_HIT;
870           }
871       }
872       break;
873
874     case SDL_MOUSEMOTION:
875       {
876         int x = event.motion.x;
877         int y = event.motion.y;
878
879         if(x > pos_x - get_width()/2 &&
880             x < pos_x + get_width()/2 &&
881             y > pos_y - get_height()/2 &&
882             y < pos_y + get_height()/2)
883           {
884             int new_active_item = (y - (pos_y - get_height()/2)) / 24;
885           
886             /* only change the mouse focus to a selectable item */
887                 if ((item[new_active_item].kind != MN_HL)
888                       && (item[new_active_item].kind != MN_LABEL)
889                       && (item[new_active_item].kind != MN_DEACTIVE))                
890               active_item = new_active_item;
891             
892                  if(MouseCursor::current())
893               MouseCursor::current()->set_state(MC_LINK);
894           }
895         else
896           {
897                  if(MouseCursor::current())
898               MouseCursor::current()->set_state(MC_NORMAL);
899           }
900       }
901       break;
902
903     default:
904       break;
905     }
906 }
907