34dd8b8edbbd5ded3978759e70e2329edbbc2d65
[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 char* 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 return (char*)str;
222 }
223
224 /* Set ControlField a key */
225 void Menu::get_controlfield_key_into_input(MenuItem *item)
226 {
227   switch(*item->int_p)
228   {
229   case SDLK_UP:
230     item->change_input("Up cursor");
231     break;
232   case SDLK_DOWN:
233     item->change_input("Down cursor");
234     break;
235   case SDLK_LEFT:
236     item->change_input("Left cursor");
237     break;
238   case SDLK_RIGHT:
239     item->change_input("Right cursor");
240     break;
241   case SDLK_RETURN:
242     item->change_input("Return");
243     break;
244   case SDLK_SPACE:
245     item->change_input("Space");
246     break;
247   case SDLK_RSHIFT:
248     item->change_input("Right Shift");
249     break;
250   case SDLK_LSHIFT:
251     item->change_input("Left Shift");
252     break;
253   case SDLK_RCTRL:
254     item->change_input("Right Control");
255     break;
256   case SDLK_LCTRL:
257     item->change_input("Left Control");
258     break;
259   case SDLK_RALT:
260     item->change_input("Right Alt");
261     break;
262   case SDLK_LALT:
263     item->change_input("Left Alt");
264     break;
265   default:
266     {
267       char tmp[64];
268       snprintf(tmp, 64, "%d", *item->int_p);
269       item->change_input(tmp);
270     }
271     break;
272   }
273 }
274
275 /* Free a menu and all its items */
276 Menu::~Menu()
277 {
278   if(item.size() != 0)
279     {
280       for(unsigned int i = 0; i < item.size(); ++i)
281         {
282           free(item[i].text);
283           free(item[i].input);
284           string_list_free(item[i].list);
285         }
286     }
287 }
288
289
290 Menu::Menu()
291 {
292   hit_item = -1;
293   menuaction = MENU_ACTION_NONE;
294   delete_character = 0;
295   mn_input_char = '\0';
296   
297   pos_x        = screen->w/2;
298   pos_y        = screen->h/2;
299   arrange_left = 0;
300   active_item  = 0;
301   effect.init(false);
302 }
303
304 void Menu::set_pos(int x, int y, float rw, float rh)
305 {
306   pos_x = x + (int)((float)get_width() * rw);
307   pos_y = y + (int)((float)get_height() * rh);
308 }
309
310 void
311 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
312 {
313   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
314 }
315
316 /* Add an item to a menu */
317 void
318 Menu::additem(MenuItem* pmenu_item)
319 {
320   item.push_back(*pmenu_item);
321   delete pmenu_item;
322 }
323
324 void
325 Menu::clear()
326 {
327   item.clear();
328 }
329
330 /* Process actions done on the menu */
331 void
332 Menu::action()
333 {
334   hit_item = -1;
335   if(item.size() != 0)
336     {
337       switch(menuaction)
338         {
339         case MENU_ACTION_UP:
340           if (active_item > 0)
341             --active_item;
342           else
343             active_item = int(item.size())-1;
344           break;
345
346         case MENU_ACTION_DOWN:
347           if(active_item < int(item.size())-1)
348             ++active_item;
349           else
350             active_item = 0;
351           break;
352
353         case MENU_ACTION_LEFT:
354           if(item[active_item].kind == MN_STRINGSELECT
355               && item[active_item].list->num_items != 0)
356             {
357               if(item[active_item].list->active_item > 0)
358                 --item[active_item].list->active_item;
359               else
360                 item[active_item].list->active_item = item[active_item].list->num_items-1;
361             }
362           break;
363
364         case MENU_ACTION_RIGHT:
365           if(item[active_item].kind == MN_STRINGSELECT
366               && item[active_item].list->num_items != 0)
367             {
368               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
369                 ++item[active_item].list->active_item;
370               else
371                 item[active_item].list->active_item = 0;
372             }
373           break;
374
375         case MENU_ACTION_HIT:
376           {
377             hit_item = active_item;
378             switch (item[active_item].kind)
379               {
380               case MN_GOTO:
381                 if (item[active_item].target_menu != NULL)
382                   Menu::push_current(item[active_item].target_menu);
383                 else
384                   puts("NULLL");
385                 break;
386
387               case MN_TOGGLE:
388                 item[active_item].toggled = !item[active_item].toggled;
389                 break;
390
391               case MN_ACTION:
392               case MN_TEXTFIELD:
393               case MN_NUMFIELD:
394                 Menu::set_current(0); 
395                 item[active_item].toggled = true;
396                 break;
397
398               case MN_BACK:
399                 Menu::pop_current();
400                 break;
401               default:
402                 break;
403               }
404           }
405           break;
406
407         case MENU_ACTION_REMOVE:
408           if(item[active_item].kind == MN_TEXTFIELD
409               || item[active_item].kind == MN_NUMFIELD)
410             {
411               if(item[active_item].input != NULL)
412                 {
413                   int i = strlen(item[active_item].input);
414
415                   while(delete_character > 0)   /* remove charactes */
416                     {
417                       item[active_item].input[i-1] = '\0';
418                       delete_character--;
419                     }
420                 }
421             }
422           break;
423
424         case MENU_ACTION_INPUT:
425           if(item[active_item].kind == MN_TEXTFIELD
426               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
427             {
428               if(item[active_item].input != NULL)
429                 {
430                   int i = strlen(item[active_item].input);
431                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
432                   item[active_item].input[i] = mn_input_char;
433                   item[active_item].input[i+1] = '\0';
434                 }
435               else
436                 {
437                   item[active_item].input = (char*) malloc(2*sizeof(char));
438                   item[active_item].input[0] = mn_input_char;
439                   item[active_item].input[1] = '\0';
440                 }
441             }
442
443         case MENU_ACTION_NONE:
444           break;
445         }
446     }
447
448   MenuItem& new_item = item[active_item];
449   if(new_item.kind == MN_DEACTIVE
450       || new_item.kind == MN_LABEL
451       || new_item.kind == MN_HL)
452     {
453       // Skip the horzontal line item
454       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
455         menuaction = MENU_ACTION_DOWN;
456
457       if (item.size() > 1)
458         action();
459     }
460
461   menuaction = MENU_ACTION_NONE;
462 }
463
464 int
465 Menu::check()
466 {
467   if (hit_item != -1)
468     return item[hit_item].id;
469   else
470     return -1;
471 }
472
473 void
474 Menu::draw_item(int index, // Position of the current item in the menu
475                 int menu_width,
476                 int menu_height)
477 {
478   MenuItem& pitem = item[index];
479
480   int font_width  = 16;
481   int effect_offset = 0;
482   {
483     int effect_time = 0;
484
485     if(effect.check())
486       effect_time = effect.get_left() / 4;
487
488     effect_offset = (index % 2) ? effect_time : -effect_time;
489   }
490
491   int x_pos       = pos_x;
492   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
493   int shadow_size = 2;
494   int text_width  = strlen(pitem.text) * font_width;
495   int input_width = (strlen(pitem.input)+ 1) * font_width;
496   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
497   Text* text_font = white_text;
498
499   if (arrange_left)
500     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
501
502   if(index == active_item)
503     {
504       shadow_size = 3;
505       text_font = blue_text;
506     }
507
508   switch (pitem.kind)
509     {
510     case MN_DEACTIVE:
511       {
512         black_text->draw_align(pitem.text,
513                                x_pos, y_pos,
514                                A_HMIDDLE, A_VMIDDLE, 2);
515         break;
516       }
517
518     case MN_HL:
519       {
520         int x = pos_x - menu_width/2;
521         int y = y_pos - 12 - effect_offset;
522         /* Draw a horizontal line with a little 3d effect */
523         fillrect(x, y + 6,
524                  menu_width, 4,
525                  150,200,255,225);
526         fillrect(x, y + 6,
527                  menu_width, 2,
528                  255,255,255,255);
529         break;
530       }
531     case MN_LABEL:
532       {
533         white_big_text->draw_align(pitem.text,
534                                    x_pos, y_pos,
535                                    A_HMIDDLE, A_VMIDDLE, 2);
536         break;
537       }
538     case MN_TEXTFIELD:
539     case MN_NUMFIELD:
540     case MN_CONTROLFIELD:
541       {
542         int input_pos = input_width/2;
543         int text_pos  = (text_width + font_width)/2;
544
545         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
546                  input_width + font_width + 2, 20,
547                  255,255,255,255);
548         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
549                  input_width + font_width, 18,
550                  0,0,0,128);
551
552         if(pitem.kind == MN_CONTROLFIELD)
553           get_controlfield_key_into_input(&pitem);
554
555         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
556           {
557           if(active_item == index)
558             gold_text->draw_align(pitem.get_input_with_symbol(true), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
559           else
560             gold_text->draw_align(pitem.get_input_with_symbol(false), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
561           }
562         else
563           gold_text->draw_align(pitem.input,
564                               x_pos + text_pos, y_pos,
565                               A_HMIDDLE, A_VMIDDLE, 2);
566
567         text_font->draw_align(pitem.text,
568                               x_pos - (input_width + font_width)/2, y_pos,
569                               A_HMIDDLE, A_VMIDDLE, shadow_size);
570         break;
571       }
572     case MN_STRINGSELECT:
573       {
574         int list_pos_2 = list_width + font_width;
575         int list_pos   = list_width/2;
576         int text_pos   = (text_width + font_width)/2;
577
578         /* Draw arrows */
579         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
580         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
581
582         /* Draw input background */
583         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
584                  list_pos_2 + 2, 20,
585                  255,255,255,255);
586         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
587                  list_pos_2, 18,
588                  0,0,0,128);
589
590         gold_text->draw_align(string_list_active(pitem.list),
591                         x_pos + text_pos, y_pos,
592                         A_HMIDDLE, A_VMIDDLE,2);
593
594         text_font->draw_align(pitem.text,
595                         x_pos - list_pos_2/2, y_pos,
596                         A_HMIDDLE, A_VMIDDLE, shadow_size);
597         break;
598       }
599     case MN_BACK:
600       {
601         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
602         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
603         break;
604       }
605
606     case MN_TOGGLE:
607       {
608         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
609
610         if(pitem.toggled)
611           checkbox_checked->draw(
612                        x_pos + (text_width+font_width)/2,
613                        y_pos - 8);
614         else
615           checkbox->draw(
616                        x_pos + (text_width+font_width)/2,
617                        y_pos - 8);
618         break;
619       }
620     case MN_ACTION:
621       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
622       break;
623
624     case MN_GOTO:
625       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
626       break;
627     }
628 }
629
630 int Menu::get_width() const
631 {
632   /* The width of the menu has to be more than the width of the text
633      with the most characters */
634   int menu_width = 0;
635   for(unsigned int i = 0; i < item.size(); ++i)
636     {
637       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
638       if( w > menu_width )
639         {
640           menu_width = w;
641           if( item[i].kind == MN_TOGGLE)
642             menu_width += 2;
643         }
644     }
645
646   return (menu_width * 16 + 24);
647 }
648
649 int Menu::get_height() const
650 {
651   return item.size() * 24;
652 }
653
654 /* Draw the current menu. */
655 void
656 Menu::draw()
657 {
658   int menu_height = get_height();
659   int menu_width  = get_width();
660
661   /* Draw a transparent background */
662   fillrect(pos_x - menu_width/2,
663            pos_y - 24*item.size()/2 - 10,
664            menu_width,menu_height + 20,
665            150,180,200,125);
666
667   for(unsigned int i = 0; i < item.size(); ++i)
668     {
669       draw_item(i, menu_width, menu_height);
670     }
671 }
672
673 MenuItem&
674 Menu::get_item_by_id(int id)
675 {
676   for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
677     if(i->id == id)
678       return *i;
679   }
680
681   assert(false);
682   static MenuItem dummyitem;
683   return dummyitem;
684 }
685
686 int Menu::get_active_item_id()
687 {
688 return item[active_item].id;
689 }
690
691 bool
692 Menu::isToggled(int id)
693 {
694   return get_item_by_id(id).toggled;
695 }
696
697 /* Check for menu event */
698 void
699 Menu::event(SDL_Event& event)
700 {
701   SDLKey key;
702   switch(event.type)
703     {
704     case SDL_KEYDOWN:
705       key = event.key.keysym.sym;
706       SDLMod keymod;
707       char ch[2];
708       keymod = SDL_GetModState();
709       int x,y;
710
711       /* If the current unicode character is an ASCII character,
712          assign it to ch. */
713       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
714         {
715           ch[0] = event.key.keysym.unicode & 0x7F;
716           ch[1] = '\0';
717         }
718       else
719         {
720           /* An International Character. */
721         }
722
723       if(item[active_item].kind == MN_CONTROLFIELD)
724         {
725         if(key == SDLK_ESCAPE)
726           {
727           Menu::pop_current();
728           return;
729           }
730         *item[active_item].int_p = key;
731         menuaction = MENU_ACTION_DOWN;
732         return;
733         }
734
735
736       switch(key)
737         {
738         case SDLK_UP:           /* Menu Up */
739           menuaction = MENU_ACTION_UP;
740           break;
741         case SDLK_DOWN:         /* Menu Down */
742           menuaction = MENU_ACTION_DOWN;
743           break;
744         case SDLK_LEFT:         /* Menu Up */
745           menuaction = MENU_ACTION_LEFT;
746           break;
747         case SDLK_RIGHT:                /* Menu Down */
748           menuaction = MENU_ACTION_RIGHT;
749           break;
750         case SDLK_SPACE:
751           if(item[active_item].kind == MN_TEXTFIELD)
752             {
753               menuaction = MENU_ACTION_INPUT;
754               mn_input_char = ' ';
755               break;
756             }
757         case SDLK_RETURN: /* Menu Hit */
758           menuaction = MENU_ACTION_HIT;
759           break;
760         case SDLK_DELETE:
761         case SDLK_BACKSPACE:
762           menuaction = MENU_ACTION_REMOVE;
763           delete_character++;
764           break;
765         case SDLK_ESCAPE:
766           Menu::pop_current();
767           break;
768         default:
769           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
770             {
771               menuaction = MENU_ACTION_INPUT;
772               mn_input_char = *ch;
773             }
774           else
775             {
776               mn_input_char = '\0';
777             }
778           break;
779         }
780       break;
781     case  SDL_JOYAXISMOTION:
782       if(event.jaxis.axis == joystick_keymap.y_axis)
783         {
784           if (event.jaxis.value > 1024)
785             menuaction = MENU_ACTION_DOWN;
786           else if (event.jaxis.value < -1024)
787             menuaction = MENU_ACTION_UP;
788         }
789       break;
790     case  SDL_JOYBUTTONDOWN:
791       menuaction = MENU_ACTION_HIT;
792       break;
793     case SDL_MOUSEBUTTONDOWN:
794       x = event.motion.x;
795       y = event.motion.y;
796       if(x > pos_x - get_width()/2 &&
797          x < pos_x + get_width()/2 &&
798          y > pos_y - get_height()/2 &&
799          y < pos_y + get_height()/2)
800         {
801           menuaction = MENU_ACTION_HIT;
802         }
803       break;
804     case SDL_MOUSEMOTION:
805       x = event.motion.x;
806       y = event.motion.y;
807       if(x > pos_x - get_width()/2 &&
808          x < pos_x + get_width()/2 &&
809          y > pos_y - get_height()/2 &&
810          y < pos_y + get_height()/2)
811         {
812           active_item = (y - (pos_y - get_height()/2)) / 24;
813           mouse_cursor->set_state(MC_LINK);
814         }
815       else
816         {
817           mouse_cursor->set_state(MC_NORMAL);
818         }
819       break;
820     default:
821       break;
822     }
823 }
824
825
826 // EOF //