When Enter is pressed in an input field, instead of hidding the menu, go to the next...
[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                 Menu::set_current(0); 
393                 item[active_item].toggled = true;
394                 break;
395               case MN_TEXTFIELD:
396               case MN_NUMFIELD:
397                 menuaction = MENU_ACTION_DOWN;
398                 action();
399                 break;
400
401               case MN_BACK:
402                 Menu::pop_current();
403                 break;
404               default:
405                 break;
406               }
407           }
408           break;
409
410         case MENU_ACTION_REMOVE:
411           if(item[active_item].kind == MN_TEXTFIELD
412               || item[active_item].kind == MN_NUMFIELD)
413             {
414               if(item[active_item].input != NULL)
415                 {
416                   int i = strlen(item[active_item].input);
417
418                   while(delete_character > 0)   /* remove charactes */
419                     {
420                       item[active_item].input[i-1] = '\0';
421                       delete_character--;
422                     }
423                 }
424             }
425           break;
426
427         case MENU_ACTION_INPUT:
428           if(item[active_item].kind == MN_TEXTFIELD
429               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
430             {
431               if(item[active_item].input != NULL)
432                 {
433                   int i = strlen(item[active_item].input);
434                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
435                   item[active_item].input[i] = mn_input_char;
436                   item[active_item].input[i+1] = '\0';
437                 }
438               else
439                 {
440                   item[active_item].input = (char*) malloc(2*sizeof(char));
441                   item[active_item].input[0] = mn_input_char;
442                   item[active_item].input[1] = '\0';
443                 }
444             }
445
446         case MENU_ACTION_NONE:
447           break;
448         }
449     }
450
451   MenuItem& new_item = item[active_item];
452   if(new_item.kind == MN_DEACTIVE
453       || new_item.kind == MN_LABEL
454       || new_item.kind == MN_HL)
455     {
456       // Skip the horzontal line item
457       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
458         menuaction = MENU_ACTION_DOWN;
459
460       if (item.size() > 1)
461         action();
462     }
463
464   menuaction = MENU_ACTION_NONE;
465 }
466
467 int
468 Menu::check()
469 {
470   if (hit_item != -1)
471     return item[hit_item].id;
472   else
473     return -1;
474 }
475
476 void
477 Menu::draw_item(int index, // Position of the current item in the menu
478                 int menu_width,
479                 int menu_height)
480 {
481   MenuItem& pitem = item[index];
482
483   int font_width  = 16;
484   int effect_offset = 0;
485   {
486     int effect_time = 0;
487
488     if(effect.check())
489       effect_time = effect.get_left() / 4;
490
491     effect_offset = (index % 2) ? effect_time : -effect_time;
492   }
493
494   int x_pos       = pos_x;
495   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
496   int shadow_size = 2;
497   int text_width  = strlen(pitem.text) * font_width;
498   int input_width = (strlen(pitem.input)+ 1) * font_width;
499   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
500   Text* text_font = white_text;
501
502   if (arrange_left)
503     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
504
505   if(index == active_item)
506     {
507       shadow_size = 3;
508       text_font = blue_text;
509     }
510
511   switch (pitem.kind)
512     {
513     case MN_DEACTIVE:
514       {
515         black_text->draw_align(pitem.text,
516                                x_pos, y_pos,
517                                A_HMIDDLE, A_VMIDDLE, 2);
518         break;
519       }
520
521     case MN_HL:
522       {
523         int x = pos_x - menu_width/2;
524         int y = y_pos - 12 - effect_offset;
525         /* Draw a horizontal line with a little 3d effect */
526         fillrect(x, y + 6,
527                  menu_width, 4,
528                  150,200,255,225);
529         fillrect(x, y + 6,
530                  menu_width, 2,
531                  255,255,255,255);
532         break;
533       }
534     case MN_LABEL:
535       {
536         white_big_text->draw_align(pitem.text,
537                                    x_pos, y_pos,
538                                    A_HMIDDLE, A_VMIDDLE, 2);
539         break;
540       }
541     case MN_TEXTFIELD:
542     case MN_NUMFIELD:
543     case MN_CONTROLFIELD:
544       {
545         int input_pos = input_width/2;
546         int text_pos  = (text_width + font_width)/2;
547
548         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
549                  input_width + font_width + 2, 20,
550                  255,255,255,255);
551         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
552                  input_width + font_width, 18,
553                  0,0,0,128);
554
555         if(pitem.kind == MN_CONTROLFIELD)
556           get_controlfield_key_into_input(&pitem);
557
558         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
559           {
560           if(active_item == index)
561             gold_text->draw_align(pitem.get_input_with_symbol(true), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
562           else
563             gold_text->draw_align(pitem.get_input_with_symbol(false), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
564           }
565         else
566           gold_text->draw_align(pitem.input,
567                               x_pos + text_pos, y_pos,
568                               A_HMIDDLE, A_VMIDDLE, 2);
569
570         text_font->draw_align(pitem.text,
571                               x_pos - (input_width + font_width)/2, y_pos,
572                               A_HMIDDLE, A_VMIDDLE, shadow_size);
573         break;
574       }
575     case MN_STRINGSELECT:
576       {
577         int list_pos_2 = list_width + font_width;
578         int list_pos   = list_width/2;
579         int text_pos   = (text_width + font_width)/2;
580
581         /* Draw arrows */
582         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
583         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
584
585         /* Draw input background */
586         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
587                  list_pos_2 + 2, 20,
588                  255,255,255,255);
589         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
590                  list_pos_2, 18,
591                  0,0,0,128);
592
593         gold_text->draw_align(string_list_active(pitem.list),
594                         x_pos + text_pos, y_pos,
595                         A_HMIDDLE, A_VMIDDLE,2);
596
597         text_font->draw_align(pitem.text,
598                         x_pos - list_pos_2/2, y_pos,
599                         A_HMIDDLE, A_VMIDDLE, shadow_size);
600         break;
601       }
602     case MN_BACK:
603       {
604         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
605         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
606         break;
607       }
608
609     case MN_TOGGLE:
610       {
611         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
612
613         if(pitem.toggled)
614           checkbox_checked->draw(
615                        x_pos + (text_width+font_width)/2,
616                        y_pos - 8);
617         else
618           checkbox->draw(
619                        x_pos + (text_width+font_width)/2,
620                        y_pos - 8);
621         break;
622       }
623     case MN_ACTION:
624       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
625       break;
626
627     case MN_GOTO:
628       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
629       break;
630     }
631 }
632
633 int Menu::get_width() const
634 {
635   /* The width of the menu has to be more than the width of the text
636      with the most characters */
637   int menu_width = 0;
638   for(unsigned int i = 0; i < item.size(); ++i)
639     {
640       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
641       if( w > menu_width )
642         {
643           menu_width = w;
644           if( item[i].kind == MN_TOGGLE)
645             menu_width += 2;
646         }
647     }
648
649   return (menu_width * 16 + 24);
650 }
651
652 int Menu::get_height() const
653 {
654   return item.size() * 24;
655 }
656
657 /* Draw the current menu. */
658 void
659 Menu::draw()
660 {
661   int menu_height = get_height();
662   int menu_width  = get_width();
663
664   /* Draw a transparent background */
665   fillrect(pos_x - menu_width/2,
666            pos_y - 24*item.size()/2 - 10,
667            menu_width,menu_height + 20,
668            150,180,200,125);
669
670   for(unsigned int i = 0; i < item.size(); ++i)
671     {
672       draw_item(i, menu_width, menu_height);
673     }
674 }
675
676 MenuItem&
677 Menu::get_item_by_id(int id)
678 {
679   for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
680     if(i->id == id)
681       return *i;
682   }
683
684   assert(false);
685   static MenuItem dummyitem;
686   return dummyitem;
687 }
688
689 int Menu::get_active_item_id()
690 {
691 return item[active_item].id;
692 }
693
694 bool
695 Menu::isToggled(int id)
696 {
697   return get_item_by_id(id).toggled;
698 }
699
700 /* Check for menu event */
701 void
702 Menu::event(SDL_Event& event)
703 {
704   SDLKey key;
705   switch(event.type)
706     {
707     case SDL_KEYDOWN:
708       key = event.key.keysym.sym;
709       SDLMod keymod;
710       char ch[2];
711       keymod = SDL_GetModState();
712       int x,y;
713
714       /* If the current unicode character is an ASCII character,
715          assign it to ch. */
716       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
717         {
718           ch[0] = event.key.keysym.unicode & 0x7F;
719           ch[1] = '\0';
720         }
721       else
722         {
723           /* An International Character. */
724         }
725
726       if(item[active_item].kind == MN_CONTROLFIELD)
727         {
728         if(key == SDLK_ESCAPE)
729           {
730           Menu::pop_current();
731           return;
732           }
733         *item[active_item].int_p = key;
734         menuaction = MENU_ACTION_DOWN;
735         return;
736         }
737
738
739       switch(key)
740         {
741         case SDLK_UP:           /* Menu Up */
742           menuaction = MENU_ACTION_UP;
743           break;
744         case SDLK_DOWN:         /* Menu Down */
745           menuaction = MENU_ACTION_DOWN;
746           break;
747         case SDLK_LEFT:         /* Menu Up */
748           menuaction = MENU_ACTION_LEFT;
749           break;
750         case SDLK_RIGHT:                /* Menu Down */
751           menuaction = MENU_ACTION_RIGHT;
752           break;
753         case SDLK_SPACE:
754           if(item[active_item].kind == MN_TEXTFIELD)
755             {
756               menuaction = MENU_ACTION_INPUT;
757               mn_input_char = ' ';
758               break;
759             }
760         case SDLK_RETURN: /* Menu Hit */
761           menuaction = MENU_ACTION_HIT;
762           break;
763         case SDLK_DELETE:
764         case SDLK_BACKSPACE:
765           menuaction = MENU_ACTION_REMOVE;
766           delete_character++;
767           break;
768         case SDLK_ESCAPE:
769           Menu::pop_current();
770           break;
771         default:
772           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
773             {
774               menuaction = MENU_ACTION_INPUT;
775               mn_input_char = *ch;
776             }
777           else
778             {
779               mn_input_char = '\0';
780             }
781           break;
782         }
783       break;
784     case  SDL_JOYAXISMOTION:
785       if(event.jaxis.axis == joystick_keymap.y_axis)
786         {
787           if (event.jaxis.value > 1024)
788             menuaction = MENU_ACTION_DOWN;
789           else if (event.jaxis.value < -1024)
790             menuaction = MENU_ACTION_UP;
791         }
792       break;
793     case  SDL_JOYBUTTONDOWN:
794       menuaction = MENU_ACTION_HIT;
795       break;
796     case SDL_MOUSEBUTTONDOWN:
797       x = event.motion.x;
798       y = event.motion.y;
799       if(x > pos_x - get_width()/2 &&
800          x < pos_x + get_width()/2 &&
801          y > pos_y - get_height()/2 &&
802          y < pos_y + get_height()/2)
803         {
804           menuaction = MENU_ACTION_HIT;
805         }
806       break;
807     case SDL_MOUSEMOTION:
808       x = event.motion.x;
809       y = event.motion.y;
810       if(x > pos_x - get_width()/2 &&
811          x < pos_x + get_width()/2 &&
812          y > pos_y - get_height()/2 &&
813          y < pos_y + get_height()/2)
814         {
815           active_item = (y - (pos_y - get_height()/2)) / 24;
816           mouse_cursor->set_state(MC_LINK);
817         }
818       else
819         {
820           mouse_cursor->set_state(MC_NORMAL);
821         }
822       break;
823     default:
824       break;
825     }
826 }
827
828
829 // EOF //