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