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