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