Added a joystick control dialog.
[supertux.git] / src / 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 <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "defines.h"
31 #include "globals.h"
32 #include "menu.h"
33 #include "screen.h"
34 #include "setup.h"
35 #include "sound.h"
36 #include "scene.h"
37 #include "leveleditor.h"
38 #include "timer.h"
39 #include "high_scores.h"
40
41 Surface* checkbox;
42 Surface* checkbox_checked;
43 Surface* back;
44 Surface* arrow_left;
45 Surface* arrow_right;
46
47 Menu* main_menu      = 0;
48 Menu* game_menu      = 0;
49 Menu* worldmap_menu  = 0;
50 Menu* options_menu   = 0;
51 Menu* options_keys_menu     = 0;
52 Menu* options_joystick_menu = 0;
53 Menu* highscore_menu = 0;
54 Menu* load_game_menu = 0;
55 Menu* save_game_menu = 0;
56 Menu* contrib_menu   = 0;
57 Menu* contrib_subset_menu   = 0;
58
59 std::vector<Menu*> Menu::last_menus;
60 Menu* Menu::current_ = 0;
61
62 void
63 Menu::push_current(Menu* pmenu)
64 {
65   if (current_)
66     last_menus.push_back(current_);
67   
68   current_ = pmenu;
69   current_->effect.start(500);
70 }
71
72 void
73 Menu::pop_current()
74 {
75   if (!last_menus.empty())
76     {
77       current_ = last_menus.back();
78       current_->effect.start(500);
79
80       last_menus.pop_back();
81     }
82   else
83     {
84       current_ = 0;
85     }
86 }
87
88 void
89 Menu::set_current(Menu* menu)
90 {
91   last_menus.clear();
92
93   if (menu)
94     menu->effect.start(500);
95   
96   current_ = menu;
97 }
98
99 /* Return a pointer to a new menu item */
100 MenuItem*
101 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
102 {
103   MenuItem *pnew_item = new MenuItem;
104   
105   pnew_item->kind = kind_;
106   pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
107   strcpy(pnew_item->text, text_);
108
109   if(kind_ == MN_TOGGLE)
110     pnew_item->toggled = init_toggle_;
111   else
112     pnew_item->toggled = false;
113
114   pnew_item->target_menu = target_menu_;
115   pnew_item->input = (char*) malloc(sizeof(char));
116   pnew_item->input[0] = '\0';
117
118   if(kind_ == MN_STRINGSELECT)
119     {
120       pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
121       string_list_init(pnew_item->list);
122     }
123   else
124     pnew_item->list = NULL;
125
126   pnew_item->int_p = int_p_;
127
128   return pnew_item;
129 }
130
131 void
132 MenuItem::change_text(const  char *text_)
133 {
134   if (text_)
135     {
136       free(text);
137       text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
138       strcpy(text, text_);
139     }
140 }
141
142 void
143 MenuItem::change_input(const  char *text_)
144 {
145   if(text)
146     {
147       free(input);
148       input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
149       strcpy(input, text_);
150     }
151 }
152
153 /* Set ControlField a key */
154 void Menu::get_controlfield_key_into_input(MenuItem *item)
155 {
156 switch(*item->int_p)
157   {
158   case SDLK_UP:
159     strcpy(item->input, "Up cursor");
160     break;
161   case SDLK_DOWN:
162     strcpy(item->input, "Down cursor");
163     break;
164   case SDLK_LEFT:
165     strcpy(item->input, "Left cursor");
166     break;
167   case SDLK_RIGHT:
168     strcpy(item->input, "Right cursor");
169     break;
170   case SDLK_RETURN:
171     strcpy(item->input, "Return");
172     break;
173   case SDLK_SPACE:
174     strcpy(item->input, "Space");
175     break;
176   case SDLK_RSHIFT:
177     strcpy(item->input, "Right Shift");
178     break;
179   case SDLK_LSHIFT:
180     strcpy(item->input, "Left Shift");
181     break;
182   case SDLK_RCTRL:
183     strcpy(item->input, "Right Control");
184     break;
185   case SDLK_LCTRL:
186     strcpy(item->input, "Left Control");
187     break;
188   case SDLK_RALT:
189     strcpy(item->input, "Right Alt");
190     break;
191   case SDLK_LALT:
192     strcpy(item->input, "Left Alt");
193     break;
194   default:
195     strcpy(item->input, (char*)item->int_p);
196     break;
197   }
198 }
199
200 /* Free a menu and all its items */
201 Menu::~Menu()
202 {
203   if(item.size() != 0)
204     {
205       for(unsigned int i = 0; i < item.size(); ++i)
206         {
207           free(item[i].text);
208           free(item[i].input);
209           string_list_free(item[i].list);
210         }
211     }
212 }
213
214
215 Menu::Menu()
216 {
217   hit_item = -1;
218   menuaction = MENU_ACTION_NONE;
219   delete_character = 0;
220   mn_input_char = '\0';
221   
222   pos_x        = screen->w/2;
223   pos_y        = screen->h/2;
224   has_backitem = false;
225   arrange_left = 0;
226   active_item  = 0;
227   effect.init(false);
228 }
229
230 void Menu::set_pos(int x, int y, float rw, float rh)
231 {
232   pos_x = x + (int)((float)get_width() * rw);
233   pos_y = y + (int)((float)get_height() * rh);
234 }
235
236 void
237 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int* int_p)
238 {
239   if(kind_ == MN_BACK)
240     has_backitem = true;
241
242   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, int_p));
243 }
244
245 /* Add an item to a menu */
246 void
247 Menu::additem(MenuItem* pmenu_item)
248 {
249   if(pmenu_item->kind == MN_BACK)
250     has_backitem = true;
251
252   item.push_back(*pmenu_item);
253   delete pmenu_item;
254 }
255
256 void
257 Menu::clear()
258 {
259   item.clear();
260 }
261
262 /* Process actions done on the menu */
263 void
264 Menu::action()
265 {
266   hit_item = -1;
267   if(item.size() != 0)
268     {
269       switch(menuaction)
270         {
271         case MENU_ACTION_UP:
272           if (active_item > 0)
273             --active_item;
274           else
275             active_item = int(item.size())-1;
276           break;
277
278         case MENU_ACTION_DOWN:
279           if(active_item < int(item.size())-1)
280             ++active_item;
281           else
282             active_item = 0;
283           break;
284
285         case MENU_ACTION_LEFT:
286           if(item[active_item].kind == MN_STRINGSELECT
287               && item[active_item].list->num_items != 0)
288             {
289               if(item[active_item].list->active_item > 0)
290                 --item[active_item].list->active_item;
291               else
292                 item[active_item].list->active_item = item[active_item].list->num_items-1;
293             }
294           break;
295
296         case MENU_ACTION_RIGHT:
297           if(item[active_item].kind == MN_STRINGSELECT
298               && item[active_item].list->num_items != 0)
299             {
300               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
301                 ++item[active_item].list->active_item;
302               else
303                 item[active_item].list->active_item = 0;
304             }
305           break;
306
307         case MENU_ACTION_HIT:
308           {
309             hit_item = active_item;
310             switch (item[active_item].kind)
311               {
312               case MN_GOTO:
313                 if (item[active_item].target_menu != NULL)
314                   Menu::push_current(item[active_item].target_menu);
315                 else
316                   puts("NULLL");
317                 break;
318
319               case MN_TOGGLE:
320                 item[active_item].toggled = !item[active_item].toggled;
321                 break;
322
323               case MN_ACTION:
324               case MN_TEXTFIELD:
325               case MN_NUMFIELD:
326                 Menu::set_current(0); 
327                 item[active_item].toggled = true;
328                 break;
329
330               case MN_BACK:
331                 Menu::pop_current();
332                 break;
333               default:
334                 break;
335               }
336           }
337           break;
338
339         case MENU_ACTION_REMOVE:
340           if(item[active_item].kind == MN_TEXTFIELD
341               || item[active_item].kind == MN_NUMFIELD)
342             {
343               if(item[active_item].input != NULL)
344                 {
345                   int i = strlen(item[active_item].input);
346
347                   while(delete_character > 0)   /* remove charactes */
348                     {
349                       item[active_item].input[i-1] = '\0';
350                       delete_character--;
351                     }
352                 }
353             }
354           break;
355
356         case MENU_ACTION_INPUT:
357           if(item[active_item].kind == MN_TEXTFIELD
358               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
359             {
360               if(item[active_item].input != NULL)
361                 {
362                   int i = strlen(item[active_item].input);
363                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
364                   item[active_item].input[i] = mn_input_char;
365                   item[active_item].input[i+1] = '\0';
366                 }
367               else
368                 {
369                   item[active_item].input = (char*) malloc(2*sizeof(char));
370                   item[active_item].input[0] = mn_input_char;
371                   item[active_item].input[1] = '\0';
372                 }
373             }
374
375         case MENU_ACTION_NONE:
376           break;
377         }
378     }
379
380   MenuItem& new_item = item[active_item];
381   if(new_item.kind == MN_DEACTIVE
382       || new_item.kind == MN_LABEL
383       || new_item.kind == MN_HL)
384     {
385       // Skip the horzontal line item
386       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
387         menuaction = MENU_ACTION_DOWN;
388
389       if (item.size() > 1)
390         action();
391     }
392
393   menuaction = MENU_ACTION_NONE;
394 }
395
396 int
397 Menu::check()
398 {
399   return hit_item;
400   /*
401   if (item.size() != 0)
402     {
403       if((item[active_item].kind == MN_ACTION
404           || item[active_item].kind == MN_TEXTFIELD
405           || item[active_item].kind == MN_NUMFIELD)
406           && item[active_item].toggled)
407         { 
408           item[active_item].toggled = false;
409           Menu::set_current(0);
410           return active_item;
411         }
412       else if(item[active_item].kind == MN_TOGGLE 
413               || item[active_item].kind == MN_GOTO)
414         {
415           return active_item;
416         }
417       else
418         return -1;
419     }
420   else
421     return -1;
422   */
423 }
424
425 void
426 Menu::draw_item(int index, // Position of the current item in the menu
427                 int menu_width,
428                 int menu_height)
429 {
430   MenuItem& pitem = item[index];
431
432   int font_width  = 16;
433   int effect_offset = 0;
434   {
435     int effect_time = 0;
436
437     if(effect.check())
438       effect_time = effect.get_left() / 4;
439
440     effect_offset = (index % 2) ? effect_time : -effect_time;
441   }
442
443   int x_pos       = pos_x;
444   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
445   int shadow_size = 2;
446   int text_width  = strlen(pitem.text) * font_width;
447   int input_width = strlen(pitem.input) * font_width;
448   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
449   Text* text_font = white_text;
450
451   if (arrange_left)
452     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
453
454   if(index == active_item)
455     {
456       shadow_size = 3;
457       text_font = blue_text;
458     }
459
460   switch (pitem.kind)
461     {
462     case MN_DEACTIVE:
463       {
464         black_text->draw_align(pitem.text,
465                                x_pos, y_pos,
466                                A_HMIDDLE, A_VMIDDLE, 2);
467         break;
468       }
469
470     case MN_HL:
471       {
472         int x = pos_x - menu_width/2;
473         int y = y_pos - 12 - effect_offset;
474         /* Draw a horizontal line with a little 3d effect */
475         fillrect(x, y + 6,
476                  menu_width, 4,
477                  150,200,255,225);
478         fillrect(x, y + 6,
479                  menu_width, 2,
480                  255,255,255,255);
481         break;
482       }
483     case MN_LABEL:
484       {
485         white_big_text->draw_align(pitem.text,
486                                    x_pos, y_pos,
487                                    A_HMIDDLE, A_VMIDDLE, 2);
488         break;
489       }
490     case MN_TEXTFIELD:
491     case MN_NUMFIELD:
492     case MN_CONTROLFIELD:
493       {
494         int input_pos = input_width/2;
495         int text_pos  = (text_width + font_width)/2;
496
497         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
498                  input_width + font_width + 2, 20,
499                  255,255,255,255);
500         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
501                  input_width + font_width, 18,
502                  0,0,0,128);
503
504         if(pitem.kind == MN_CONTROLFIELD)
505           get_controlfield_key_into_input(&pitem);
506
507         gold_text->draw_align(pitem.input,
508                               x_pos + text_pos, y_pos,
509                               A_HMIDDLE, A_VMIDDLE, 2);
510
511         text_font->draw_align(pitem.text,
512                               x_pos - (input_width + font_width)/2, y_pos,
513                               A_HMIDDLE, A_VMIDDLE, shadow_size);
514         break;
515       }
516     case MN_STRINGSELECT:
517       {
518         int list_pos_2 = list_width + font_width;
519         int list_pos   = list_width/2;
520         int text_pos   = (text_width + font_width)/2;
521
522         /* Draw arrows */
523         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
524         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
525
526         /* Draw input background */
527         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
528                  list_pos_2 + 2, 20,
529                  255,255,255,255);
530         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
531                  list_pos_2, 18,
532                  0,0,0,128);
533
534         gold_text->draw_align(string_list_active(pitem.list),
535                         x_pos + text_pos, y_pos,
536                         A_HMIDDLE, A_VMIDDLE,2);
537
538         text_font->draw_align(pitem.text,
539                         x_pos - list_pos_2/2, y_pos,
540                         A_HMIDDLE, A_VMIDDLE, shadow_size);
541         break;
542       }
543     case MN_BACK:
544       {
545         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
546         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
547         break;
548       }
549
550     case MN_TOGGLE:
551       {
552         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
553
554         if(pitem.toggled)
555           checkbox_checked->draw(
556                        x_pos + (text_width+font_width)/2,
557                        y_pos - 8);
558         else
559           checkbox->draw(
560                        x_pos + (text_width+font_width)/2,
561                        y_pos - 8);
562         break;
563       }
564     case MN_ACTION:
565       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
566       break;
567
568     case MN_GOTO:
569       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
570       break;
571     }
572 }
573
574 int Menu::get_width() const
575 {
576   /* The width of the menu has to be more than the width of the text
577      with the most characters */
578   int menu_width = 0;
579   for(unsigned int i = 0; i < item.size(); ++i)
580     {
581       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
582       if( w > menu_width )
583         {
584           menu_width = w;
585           if( item[i].kind == MN_TOGGLE)
586             menu_width += 2;
587         }
588     }
589
590   return (menu_width * 16 + 24);
591 }
592
593 int Menu::get_height() const
594 {
595   return item.size() * 24;
596 }
597
598 /* Draw the current menu. */
599 void
600 Menu::draw()
601 {
602   int menu_height = get_height();
603   int menu_width  = get_width();
604
605   /* Draw a transparent background */
606   fillrect(pos_x - menu_width/2,
607            pos_y - 24*item.size()/2 - 10,
608            menu_width,menu_height + 20,
609            150,180,200,125);
610
611   for(unsigned int i = 0; i < item.size(); ++i)
612     {
613       draw_item(i, menu_width, menu_height);
614     }
615 }
616
617 /* Check for menu event */
618 void
619 Menu::event(SDL_Event& event)
620 {
621   SDLKey key;
622   switch(event.type)
623     {
624     case SDL_KEYDOWN:
625       key = event.key.keysym.sym;
626       SDLMod keymod;
627       char ch[2];
628       keymod = SDL_GetModState();
629       int x,y;
630
631       /* If the current unicode character is an ASCII character,
632          assign it to ch. */
633       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
634         {
635           ch[0] = event.key.keysym.unicode & 0x7F;
636           ch[1] = '\0';
637         }
638       else
639         {
640           /* An International Character. */
641         }
642
643       if(item[active_item].kind == MN_CONTROLFIELD)
644         {
645         if(key == SDLK_ESCAPE)
646           {
647           Menu::pop_current();
648           return;
649           }
650         *item[active_item].int_p = key;
651         menuaction = MENU_ACTION_DOWN;
652         return;
653         }
654
655
656       switch(key)
657         {
658         case SDLK_UP:           /* Menu Up */
659           menuaction = MENU_ACTION_UP;
660           break;
661         case SDLK_DOWN:         /* Menu Down */
662           menuaction = MENU_ACTION_DOWN;
663           break;
664         case SDLK_LEFT:         /* Menu Up */
665           menuaction = MENU_ACTION_LEFT;
666           break;
667         case SDLK_RIGHT:                /* Menu Down */
668           menuaction = MENU_ACTION_RIGHT;
669           break;
670         case SDLK_SPACE:
671           if(item[active_item].kind == MN_TEXTFIELD)
672             {
673               menuaction = MENU_ACTION_INPUT;
674               mn_input_char = ' ';
675               break;
676             }
677         case SDLK_RETURN: /* Menu Hit */
678           menuaction = MENU_ACTION_HIT;
679           break;
680         case SDLK_DELETE:
681         case SDLK_BACKSPACE:
682           menuaction = MENU_ACTION_REMOVE;
683           delete_character++;
684           break;
685         case SDLK_ESCAPE:
686           Menu::pop_current();
687           break;
688         default:
689           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
690             {
691               menuaction = MENU_ACTION_INPUT;
692               mn_input_char = *ch;
693             }
694           else
695             {
696               mn_input_char = '\0';
697             }
698           break;
699         }
700       break;
701     case  SDL_JOYAXISMOTION:
702       if(event.jaxis.axis == joystick_keymap.y_axis)
703         {
704           if (event.jaxis.value > 1024)
705             menuaction = MENU_ACTION_DOWN;
706           else if (event.jaxis.value < -1024)
707             menuaction = MENU_ACTION_UP;
708         }
709       break;
710     case  SDL_JOYBUTTONDOWN:
711       menuaction = MENU_ACTION_HIT;
712       break;
713     case SDL_MOUSEBUTTONDOWN:
714       x = event.motion.x;
715       y = event.motion.y;
716       if(x > pos_x - get_width()/2 &&
717          x < pos_x + get_width()/2 &&
718          y > pos_y - get_height()/2 &&
719          y < pos_y + get_height()/2)
720         {
721           menuaction = MENU_ACTION_HIT;
722         }
723       break;
724     case SDL_MOUSEMOTION:
725       x = event.motion.x;
726       y = event.motion.y;
727       if(x > pos_x - get_width()/2 &&
728          x < pos_x + get_width()/2 &&
729          y > pos_y - get_height()/2 &&
730          y < pos_y + get_height()/2)
731         {
732           active_item = (y - (pos_y - get_height()/2)) / 24;
733           mouse_cursor->set_state(MC_LINK);
734         }
735       else
736         {
737           mouse_cursor->set_state(MC_NORMAL);
738         }
739       break;
740     default:
741       break;
742     }
743 }
744
745
746 // EOF //