updating Nolok contrib templates
[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 "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_WIDTH/2;
302   pos_y        = SCREEN_HEIGHT/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   active_item = 0;
351 }
352
353 /* Process actions done on the menu */
354 void
355 Menu::action()
356 {
357   hit_item = -1;
358   if(item.size() != 0)
359     {
360       int last_active_item = active_item;
361       switch(menuaction)
362         {
363         case MENU_ACTION_UP:
364           do {
365                   if (active_item > 0)
366                     --active_item;
367                   else
368                     active_item = int(item.size())-1;
369                } while ((item[active_item].kind == MN_HL 
370                         || item[active_item].kind == MN_LABEL
371                         || item[active_item].kind == MN_DEACTIVE)
372                         && (active_item != last_active_item));
373                
374           break;
375
376         case MENU_ACTION_DOWN:
377           do {
378                   if(active_item < int(item.size())-1 )
379                     ++active_item;
380                   else
381                     active_item = 0;
382                } while ((item[active_item].kind == MN_HL
383                         || item[active_item].kind == MN_LABEL
384                         || item[active_item].kind == MN_DEACTIVE)
385                         && (active_item != last_active_item));
386
387           break;
388
389         case MENU_ACTION_LEFT:
390           if(item[active_item].kind == MN_STRINGSELECT) {
391             if(item[active_item].selected > 0)
392               item[active_item].selected--;
393             else
394               item[active_item].selected = item[active_item].list.size()-1;
395           }
396           break;
397
398         case MENU_ACTION_RIGHT:
399           if(item[active_item].kind == MN_STRINGSELECT) {
400             if(item[active_item].selected+1 < item[active_item].list.size())
401               item[active_item].selected++;
402             else
403               item[active_item].selected = 0;
404           }
405           break;
406
407         case MENU_ACTION_HIT:
408           {
409             hit_item = active_item;
410             switch (item[active_item].kind)
411               {
412               case MN_GOTO:
413                 if (item[active_item].target_menu != NULL)
414                   Menu::push_current(item[active_item].target_menu);
415                 else
416                   puts("NULLL");
417                 break;
418
419               case MN_TOGGLE:
420                 item[active_item].toggled = !item[active_item].toggled;
421                 break;
422
423               case MN_ACTION:
424                 Menu::set_current(0);
425                 item[active_item].toggled = true;
426                 break;
427               case MN_TEXTFIELD:
428               case MN_NUMFIELD:
429                 menuaction = MENU_ACTION_DOWN;
430                 action();
431                 break;
432
433               case MN_BACK:
434                 Menu::pop_current();
435                 break;
436               default:
437                 break;
438               }
439           }
440           break;
441
442         case MENU_ACTION_REMOVE:
443           if(item[active_item].kind == MN_TEXTFIELD
444               || item[active_item].kind == MN_NUMFIELD)
445             {
446               if(!item[active_item].input.empty())
447                 {
448                   int i = item[active_item].input.size();
449
450                   while(delete_character > 0)   /* remove charactes */
451                     {
452                       item[active_item].input.resize(i-1);
453                       delete_character--;
454                     }
455                 }
456             }
457           break;
458
459         case MENU_ACTION_INPUT:
460           if(item[active_item].kind == MN_TEXTFIELD
461               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
462             {
463               item[active_item].input.push_back(mn_input_char);
464             }
465
466         case MENU_ACTION_NONE:
467           break;
468         }
469     }
470
471
472   menuaction = MENU_ACTION_NONE;
473
474   if (active_item >= int(item.size()))
475     active_item = int(item.size()) - 1;
476 }
477
478 int
479 Menu::check()
480 {
481   if (hit_item != -1)
482     return item[hit_item].id;
483   else
484     return -1;
485 }
486
487 void
488 Menu::draw_item(DrawingContext& context,
489                 int index, // Position of the current item in the menu
490                 int menu_width, int menu_height)
491 {
492   MenuItem& pitem = item[index];
493
494   int effect_offset = 0;
495   {
496     int effect_time = 0;
497
498     if(effect.check())
499       effect_time = effect.get_left() / 4;
500
501     effect_offset = (index % 2) ? effect_time : -effect_time;
502   }
503
504   Font* text_font = default_font;
505   int x_pos       = pos_x;
506   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
507   int shadow_size = 2;
508   int text_width  = int(text_font->get_text_width(pitem.text));
509   int input_width = int(text_font->get_text_width(pitem.input) + 10);
510   int list_width = 0;
511   if(pitem.list.size() > 0) {
512     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
513   }
514   
515   if (arrange_left)
516     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
517
518   if(index == active_item)
519     {
520       shadow_size = 3;
521       text_font = active_font;
522     }
523
524   switch (pitem.kind)
525     {
526     case MN_DEACTIVE:
527       {
528         context.draw_text(deactive_font, pitem.text,
529                           Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
530                           CENTER_ALLIGN, LAYER_GUI);
531         break;
532       }
533
534     case MN_HL:
535       {
536         // TODO
537         int x = pos_x - menu_width/2;
538         int y = y_pos - 12 - effect_offset;
539         /* Draw a horizontal line with a little 3d effect */
540         context.draw_filled_rect(Vector(x, y + 6),
541                                  Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
542         context.draw_filled_rect(Vector(x, y + 6),
543                                  Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
544         break;
545       }
546     case MN_LABEL:
547       {
548         context.draw_text(label_font, pitem.text,
549                           Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
550                           CENTER_ALLIGN, LAYER_GUI);
551         break;
552       }
553     case MN_TEXTFIELD:
554     case MN_NUMFIELD:
555     case MN_CONTROLFIELD_KB:
556     case MN_CONTROLFIELD_JS:
557       {
558         int width = text_width + input_width + 5;
559         int text_pos = SCREEN_WIDTH/2 - width/2;
560         int input_pos = text_pos + text_width + 10;
561
562         context.draw_filled_rect(
563           Vector(input_pos - 5, y_pos - 10),
564           Vector(input_width + 10, 20),
565           Color(255,255,255,255), LAYER_GUI-5);
566         context.draw_filled_rect(
567           Vector(input_pos - 4, y_pos - 9),
568           Vector(input_width + 8, 18),
569           Color(0,0,0,128), LAYER_GUI-4);
570
571         if(pitem.kind == MN_CONTROLFIELD_KB)
572           get_controlfield_key_into_input(&pitem);
573         else if (pitem.kind == MN_CONTROLFIELD_JS)
574           get_controlfield_js_into_input(&pitem);
575
576         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
577           {
578             if(active_item == index)
579               context.draw_text(field_font,
580                                 pitem.get_input_with_symbol(true),
581                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
582                                 LEFT_ALLIGN, LAYER_GUI);
583             else
584               context.draw_text(field_font,
585                                 pitem.get_input_with_symbol(false),
586                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
587                                 LEFT_ALLIGN, LAYER_GUI);
588           }
589         else
590           context.draw_text(field_font, pitem.input,
591                             Vector(input_pos, y_pos - int(field_font->get_height()/2)),
592                             LEFT_ALLIGN, LAYER_GUI);
593
594         context.draw_text(text_font, pitem.text,
595                           Vector(text_pos, y_pos - int(text_font->get_height()/2)),
596                           LEFT_ALLIGN, LAYER_GUI);
597         break;
598       }
599     case MN_STRINGSELECT:
600       {
601         int list_pos_2 = list_width + 16;
602         int list_pos   = list_width/2;
603         int text_pos   = (text_width + 16)/2;
604
605         /* Draw arrows */
606         context.draw_surface(arrow_left,
607                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
608                              LAYER_GUI);
609         context.draw_surface(arrow_right,
610                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
611                              LAYER_GUI);
612
613         /* Draw input background */
614         context.draw_filled_rect(
615           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
616           Vector(list_pos_2 + 2, 20),
617           Color(255,255,255,255), LAYER_GUI - 4);
618         context.draw_filled_rect(
619           Vector(x_pos - list_pos + text_pos, y_pos - 9),
620           Vector(list_pos_2, 18),
621           Color(0,0,0,128), LAYER_GUI - 5);
622
623         context.draw_text(text_font, pitem.list[pitem.selected],
624                                  Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
625                                  CENTER_ALLIGN, LAYER_GUI);
626         context.draw_text(text_font, pitem.text,
627                                  Vector(SCREEN_WIDTH/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
628                                  CENTER_ALLIGN, LAYER_GUI);
629         break;
630       }
631     case MN_BACK:
632       {
633         context.draw_text(text_font, pitem.text,
634                           Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
635                           CENTER_ALLIGN, LAYER_GUI);
636         context.draw_surface(back,
637                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
638                              LAYER_GUI);
639         break;
640       }
641
642     case MN_TOGGLE:
643       {
644         context.draw_text(text_font, pitem.text,
645                           Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
646                           CENTER_ALLIGN, LAYER_GUI);
647
648         if(pitem.toggled)
649           context.draw_surface(checkbox_checked,
650                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
651                                LAYER_GUI + 1);
652         else
653           context.draw_surface(checkbox,
654                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
655                                LAYER_GUI + 1);
656         break;
657       }
658     case MN_ACTION:
659       context.draw_text(text_font, pitem.text,
660                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
661                         CENTER_ALLIGN, LAYER_GUI);
662       break;
663
664     case MN_GOTO:
665       context.draw_text(text_font, pitem.text,
666                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
667                         CENTER_ALLIGN, LAYER_GUI);
668       break;
669     }
670 }
671
672 int Menu::get_width() const
673   {
674     /* The width of the menu has to be more than the width of the text
675        with the most characters */
676     int menu_width = 0;
677     for(unsigned int i = 0; i < item.size(); ++i)
678       {
679         int w = item[i].text.size() + item[i].input.size() + 1;
680         if( w > menu_width )
681           {
682             menu_width = w;
683             if( item[i].kind == MN_TOGGLE)
684               menu_width += 2;
685           }
686       }
687
688     return (menu_width * 16 + 24);
689   }
690
691 int Menu::get_height() const
692   {
693     return item.size() * 24;
694   }
695
696 /* Draw the current menu. */
697 void
698 Menu::draw(DrawingContext& context)
699 {
700
701   int menu_height = get_height();
702   int menu_width = get_width();  
703
704   /* Draw a transparent background */
705   context.draw_filled_rect(
706     Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
707     Vector(menu_width,menu_height + 20),
708     Color(150,180,200,125), LAYER_GUI-10);
709
710   for(unsigned int i = 0; i < item.size(); ++i)
711     {
712       draw_item(context, i, menu_width, menu_height);
713     }
714 }
715
716 MenuItem&
717 Menu::get_item_by_id(int id)
718 {
719   for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i)
720     {
721       if(i->id == id)
722         return *i;
723     }
724
725   assert(false);
726   static MenuItem dummyitem;
727   return dummyitem;
728 }
729
730 int Menu::get_active_item_id()
731 {
732   return item[active_item].id;
733 }
734
735 bool
736 Menu::isToggled(int id)
737 {
738   return get_item_by_id(id).toggled;
739 }
740
741 /* Check for menu event */
742 void
743 Menu::event(SDL_Event& event)
744 {
745   switch(event.type)
746     {
747     case SDL_KEYDOWN:
748       {
749         SDLKey key = event.key.keysym.sym;
750         SDLMod keymod;
751         char ch[2];
752         keymod = SDL_GetModState();
753
754         /* If the current unicode character is an ASCII character,
755            assign it to ch. */
756         if ( (event.key.keysym.unicode & 0xFF80) == 0 )
757           {
758             ch[0] = event.key.keysym.unicode & 0x7F;
759             ch[1] = '\0';
760           }
761         else
762           {
763             /* An International Character. */
764           }
765
766         if(item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_KB)
767           {
768             if(key == SDLK_ESCAPE)
769               {
770                 Menu::pop_current();
771                 return;
772               }
773             *item[active_item].int_p = key;
774             menuaction = MENU_ACTION_DOWN;
775             return;
776           }
777
778
779         switch(key)
780           {
781           case SDLK_UP:         /* Menu Up */
782             menuaction = MENU_ACTION_UP;
783             break;
784           case SDLK_DOWN:               /* Menu Down */
785             menuaction = MENU_ACTION_DOWN;
786             break;
787           case SDLK_LEFT:               /* Menu Up */
788             menuaction = MENU_ACTION_LEFT;
789             break;
790           case SDLK_RIGHT:              /* Menu Down */
791             menuaction = MENU_ACTION_RIGHT;
792             break;
793           case SDLK_SPACE:
794             if(item.size() > 0 && item[active_item].kind == MN_TEXTFIELD)
795               {
796                 menuaction = MENU_ACTION_INPUT;
797                 mn_input_char = ' ';
798                 break;
799               }
800           case SDLK_RETURN: /* Menu Hit */
801             menuaction = MENU_ACTION_HIT;
802             break;
803           case SDLK_DELETE:
804           case SDLK_BACKSPACE:
805             menuaction = MENU_ACTION_REMOVE;
806             delete_character++;
807             break;
808           case SDLK_ESCAPE:
809             Menu::pop_current();
810             break;
811           default:
812             if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
813               {
814                 menuaction = MENU_ACTION_INPUT;
815                 mn_input_char = *ch;
816               }
817             else
818               {
819                 mn_input_char = '\0';
820               }
821             break;
822           }
823       }
824       break;
825
826     case  SDL_JOYAXISMOTION:
827       if(event.jaxis.axis == joystick_keymap.y_axis)
828         {
829           if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
830             {
831               menuaction = MENU_ACTION_DOWN;
832               joystick_timer.start(JOYSTICK_MENU_DELAY);
833             }
834           else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
835             {
836               menuaction = MENU_ACTION_UP;
837               joystick_timer.start(JOYSTICK_MENU_DELAY);
838             }
839           else
840             joystick_timer.stop();
841         }
842       break;
843     case SDL_JOYHATMOTION:
844       if(event.jhat.value & SDL_HAT_UP) {
845           menuaction = MENU_ACTION_UP;
846       } else if(event.jhat.value & SDL_HAT_DOWN) {
847           menuaction = MENU_ACTION_DOWN;
848       }
849       break;
850     case  SDL_JOYBUTTONDOWN:
851       if (item.size() > 0 && item[active_item].kind == MN_CONTROLFIELD_JS)
852         {
853           // FIXME: This next line does nothing useable, right?
854           // *item[active_item].int_p = key;
855           menuaction = MENU_ACTION_DOWN;
856         }
857       menuaction = MENU_ACTION_HIT;
858       break;
859
860     case SDL_MOUSEBUTTONDOWN:
861       {
862         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
863         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
864
865         if(x > pos_x - get_width()/2 &&
866             x < pos_x + get_width()/2 &&
867             y > pos_y - get_height()/2 &&
868             y < pos_y + get_height()/2)
869           {
870             menuaction = MENU_ACTION_HIT;
871           }
872       }
873       break;
874
875     case SDL_MOUSEMOTION:
876       {
877         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
878         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
879
880         if(x > pos_x - get_width()/2 &&
881             x < pos_x + get_width()/2 &&
882             y > pos_y - get_height()/2 &&
883             y < pos_y + get_height()/2)
884           {
885             int new_active_item = (y - (pos_y - get_height()/2)) / 24;
886           
887             /* only change the mouse focus to a selectable item */
888                 if ((item[new_active_item].kind != MN_HL)
889                       && (item[new_active_item].kind != MN_LABEL)
890                       && (item[new_active_item].kind != MN_DEACTIVE))                
891               active_item = new_active_item;
892             
893                  if(MouseCursor::current())
894               MouseCursor::current()->set_state(MC_LINK);
895           }
896         else
897           {
898                  if(MouseCursor::current())
899               MouseCursor::current()->set_state(MC_NORMAL);
900           }
901       }
902       break;
903
904     default:
905       break;
906     }
907 }
908