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