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