Finally, made keys configurable via the menu!! ;)
[supertux.git] / src / menu.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef WIN32
21 #include <sys/types.h>
22 #include <ctype.h>
23 #endif
24
25 #include <iostream>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #include "defines.h"
31 #include "globals.h"
32 #include "menu.h"
33 #include "screen.h"
34 #include "setup.h"
35 #include "sound.h"
36 #include "scene.h"
37 #include "leveleditor.h"
38 #include "timer.h"
39 #include "high_scores.h"
40
41 Surface* checkbox;
42 Surface* checkbox_checked;
43 Surface* back;
44 Surface* arrow_left;
45 Surface* arrow_right;
46
47 Menu* main_menu      = 0;
48 Menu* game_menu      = 0;
49 Menu* worldmap_menu  = 0;
50 Menu* options_menu   = 0;
51 Menu* options_controls_menu   = 0;
52 Menu* highscore_menu = 0;
53 Menu* load_game_menu = 0;
54 Menu* save_game_menu = 0;
55 Menu* contrib_menu   = 0;
56 Menu* contrib_subset_menu   = 0;
57
58 std::vector<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
60
61 void
62 Menu::push_current(Menu* pmenu)
63 {
64   if (current_)
65     last_menus.push_back(current_);
66   
67   current_ = pmenu;
68   current_->effect.start(500);
69 }
70
71 void
72 Menu::pop_current()
73 {
74   if (!last_menus.empty())
75     {
76       current_ = last_menus.back();
77       current_->effect.start(500);
78
79       last_menus.pop_back();
80     }
81   else
82     {
83       current_ = 0;
84     }
85 }
86
87 void
88 Menu::set_current(Menu* menu)
89 {
90   last_menus.clear();
91
92   if (menu)
93     menu->effect.start(500);
94   
95   current_ = menu;
96 }
97
98 /* Return a pointer to a new menu item */
99 MenuItem*
100 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
101 {
102   MenuItem *pnew_item = new MenuItem;
103   
104   pnew_item->kind = kind_;
105   pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
106   strcpy(pnew_item->text, text_);
107
108   if(kind_ == MN_TOGGLE)
109     pnew_item->toggled = init_toggle_;
110   else
111     pnew_item->toggled = false;
112
113   pnew_item->target_menu = target_menu_;
114   pnew_item->input = (char*) malloc(sizeof(char));
115   pnew_item->input[0] = '\0';
116
117   if(kind_ == MN_STRINGSELECT)
118     {
119       pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
120       string_list_init(pnew_item->list);
121     }
122   else
123     pnew_item->list = NULL;
124
125   pnew_item->int_p = int_p_;
126
127   return pnew_item;
128 }
129
130 void
131 MenuItem::change_text(const  char *text_)
132 {
133   if (text_)
134     {
135       free(text);
136       text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
137       strcpy(text, text_);
138     }
139 }
140
141 void
142 MenuItem::change_input(const  char *text_)
143 {
144   if(text)
145     {
146       free(input);
147       input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
148       strcpy(input, text_);
149     }
150 }
151
152 /* Free a menu and all its items */
153 Menu::~Menu()
154 {
155   if(item.size() != 0)
156     {
157       for(unsigned int i = 0; i < item.size(); ++i)
158         {
159           free(item[i].text);
160           free(item[i].input);
161           string_list_free(item[i].list);
162         }
163     }
164 }
165
166
167 Menu::Menu()
168 {
169   hit_item = -1;
170   menuaction = MENU_ACTION_NONE;
171   delete_character = 0;
172   mn_input_char = '\0';
173   
174   pos_x        = screen->w/2;
175   pos_y        = screen->h/2;
176   has_backitem = false;
177   arrange_left = 0;
178   active_item  = 0;
179   effect.init(false);
180 }
181
182 void Menu::set_pos(int x, int y, float rw, float rh)
183 {
184   pos_x = x + (int)((float)get_width() * rw);
185   pos_y = y + (int)((float)get_height() * rh);
186 }
187
188 void
189 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int* int_p)
190 {
191   if(kind_ == MN_BACK)
192     has_backitem = true;
193
194   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, int_p));
195 }
196
197 /* Add an item to a menu */
198 void
199 Menu::additem(MenuItem* pmenu_item)
200 {
201   if(pmenu_item->kind == MN_BACK)
202     has_backitem = true;
203
204   item.push_back(*pmenu_item);
205   delete pmenu_item;
206 }
207
208 void
209 Menu::clear()
210 {
211   item.clear();
212 }
213
214 /* Process actions done on the menu */
215 void
216 Menu::action()
217 {
218   hit_item = -1;
219   if(item.size() != 0)
220     {
221       switch(menuaction)
222         {
223         case MENU_ACTION_UP:
224           if (active_item > 0)
225             --active_item;
226           else
227             active_item = int(item.size())-1;
228           break;
229
230         case MENU_ACTION_DOWN:
231           if(active_item < int(item.size())-1)
232             ++active_item;
233           else
234             active_item = 0;
235           break;
236
237         case MENU_ACTION_LEFT:
238           if(item[active_item].kind == MN_STRINGSELECT
239               && item[active_item].list->num_items != 0)
240             {
241               if(item[active_item].list->active_item > 0)
242                 --item[active_item].list->active_item;
243               else
244                 item[active_item].list->active_item = item[active_item].list->num_items-1;
245             }
246           break;
247
248         case MENU_ACTION_RIGHT:
249           if(item[active_item].kind == MN_STRINGSELECT
250               && item[active_item].list->num_items != 0)
251             {
252               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
253                 ++item[active_item].list->active_item;
254               else
255                 item[active_item].list->active_item = 0;
256             }
257           break;
258
259         case MENU_ACTION_HIT:
260           {
261             hit_item = active_item;
262             switch (item[active_item].kind)
263               {
264               case MN_GOTO:
265                 if (item[active_item].target_menu != NULL)
266                   Menu::push_current(item[active_item].target_menu);
267                 else
268                   puts("NULLL");
269                 break;
270
271               case MN_TOGGLE:
272                 item[active_item].toggled = !item[active_item].toggled;
273                 break;
274
275               case MN_ACTION:
276               case MN_TEXTFIELD:
277               case MN_NUMFIELD:
278                 Menu::set_current(0); 
279                 item[active_item].toggled = true;
280                 break;
281               case MN_CONTROLFIELD:
282                 break;
283
284               case MN_BACK:
285                 Menu::pop_current();
286                 break;
287               default:
288                 break;
289               }
290           }
291           break;
292
293         case MENU_ACTION_REMOVE:
294           if(item[active_item].kind == MN_TEXTFIELD
295               || item[active_item].kind == MN_NUMFIELD)
296             {
297               if(item[active_item].input != NULL)
298                 {
299                   int i = strlen(item[active_item].input);
300
301                   while(delete_character > 0)   /* remove charactes */
302                     {
303                       item[active_item].input[i-1] = '\0';
304                       delete_character--;
305                     }
306                 }
307             }
308           break;
309
310         case MENU_ACTION_INPUT:
311           if(item[active_item].kind == MN_TEXTFIELD
312               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
313             {
314               if(item[active_item].input != NULL)
315                 {
316                   int i = strlen(item[active_item].input);
317                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
318                   item[active_item].input[i] = mn_input_char;
319                   item[active_item].input[i+1] = '\0';
320                 }
321               else
322                 {
323                   item[active_item].input = (char*) malloc(2*sizeof(char));
324                   item[active_item].input[0] = mn_input_char;
325                   item[active_item].input[1] = '\0';
326                 }
327             }
328
329         case MENU_ACTION_NONE:
330           break;
331         }
332     }
333
334   MenuItem& new_item = item[active_item];
335   if(new_item.kind == MN_DEACTIVE
336       || new_item.kind == MN_LABEL
337       || new_item.kind == MN_HL)
338     {
339       // Skip the horzontal line item
340       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
341         menuaction = MENU_ACTION_DOWN;
342
343       if (item.size() > 1)
344         action();
345     }
346
347   menuaction = MENU_ACTION_NONE;
348 }
349
350 int
351 Menu::check()
352 {
353   return hit_item;
354   /*
355   if (item.size() != 0)
356     {
357       if((item[active_item].kind == MN_ACTION
358           || item[active_item].kind == MN_TEXTFIELD
359           || item[active_item].kind == MN_NUMFIELD)
360           && item[active_item].toggled)
361         { 
362           item[active_item].toggled = false;
363           Menu::set_current(0);
364           return active_item;
365         }
366       else if(item[active_item].kind == MN_TOGGLE 
367               || item[active_item].kind == MN_GOTO)
368         {
369           return active_item;
370         }
371       else
372         return -1;
373     }
374   else
375     return -1;
376   */
377 }
378
379 void
380 Menu::draw_item(int index, // Position of the current item in the menu
381                 int menu_width,
382                 int menu_height)
383 {
384   const MenuItem& pitem = item[index];
385
386   int font_width  = 16;
387   int effect_offset = 0;
388   {
389     int effect_time = 0;
390
391     if(effect.check())
392       effect_time = effect.get_left() / 4;
393
394     effect_offset = (index % 2) ? effect_time : -effect_time;
395   }
396
397   int x_pos       = pos_x;
398   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
399   int shadow_size = 2;
400   int text_width  = strlen(pitem.text) * font_width;
401   int input_width = strlen(pitem.input) * font_width;
402   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
403   Text* text_font = white_text;
404
405   if (arrange_left)
406     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
407
408   if(index == active_item)
409     {
410       shadow_size = 3;
411       text_font = blue_text;
412     }
413
414   switch (pitem.kind)
415     {
416     case MN_DEACTIVE:
417       {
418         black_text->draw_align(pitem.text,
419                                x_pos, y_pos,
420                                A_HMIDDLE, A_VMIDDLE, 2);
421         break;
422       }
423
424     case MN_HL:
425       {
426         int x = pos_x - menu_width/2;
427         int y = y_pos - 12 - effect_offset;
428         /* Draw a horizontal line with a little 3d effect */
429         fillrect(x, y + 6,
430                  menu_width, 4,
431                  150,200,255,225);
432         fillrect(x, y + 6,
433                  menu_width, 2,
434                  255,255,255,255);
435         break;
436       }
437     case MN_LABEL:
438       {
439         white_big_text->draw_align(pitem.text,
440                                    x_pos, y_pos,
441                                    A_HMIDDLE, A_VMIDDLE, 2);
442         break;
443       }
444     case MN_TEXTFIELD:
445     case MN_NUMFIELD:
446     case MN_CONTROLFIELD:
447       {
448         int input_pos = input_width/2;
449         int text_pos  = (text_width + font_width)/2;
450
451         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
452                  input_width + font_width + 2, 20,
453                  255,255,255,255);
454         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
455                  input_width + font_width, 18,
456                  0,0,0,128);
457
458         gold_text->draw_align(pitem.input,
459                               x_pos + text_pos, y_pos,
460                               A_HMIDDLE, A_VMIDDLE, 2);
461
462         text_font->draw_align(pitem.text,
463                               x_pos - (input_width + font_width)/2, y_pos,
464                               A_HMIDDLE, A_VMIDDLE, shadow_size);
465         break;
466       }
467     case MN_STRINGSELECT:
468       {
469         int list_pos_2 = list_width + font_width;
470         int list_pos   = list_width/2;
471         int text_pos   = (text_width + font_width)/2;
472
473         /* Draw arrows */
474         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
475         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
476
477         /* Draw input background */
478         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
479                  list_pos_2 + 2, 20,
480                  255,255,255,255);
481         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
482                  list_pos_2, 18,
483                  0,0,0,128);
484
485         gold_text->draw_align(string_list_active(pitem.list),
486                         x_pos + text_pos, y_pos,
487                         A_HMIDDLE, A_VMIDDLE,2);
488
489         text_font->draw_align(pitem.text,
490                         x_pos - list_pos_2/2, y_pos,
491                         A_HMIDDLE, A_VMIDDLE, shadow_size);
492         break;
493       }
494     case MN_BACK:
495       {
496         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
497         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
498         break;
499       }
500
501     case MN_TOGGLE:
502       {
503         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
504
505         if(pitem.toggled)
506           checkbox_checked->draw(
507                        x_pos + (text_width+font_width)/2,
508                        y_pos - 8);
509         else
510           checkbox->draw(
511                        x_pos + (text_width+font_width)/2,
512                        y_pos - 8);
513         break;
514       }
515     case MN_ACTION:
516       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
517       break;
518
519     case MN_GOTO:
520       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
521       break;
522     }
523 }
524
525 int Menu::get_width() const
526 {
527   /* The width of the menu has to be more than the width of the text
528      with the most characters */
529   int menu_width = 0;
530   for(unsigned int i = 0; i < item.size(); ++i)
531     {
532       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
533       if( w > menu_width )
534         {
535           menu_width = w;
536           if( item[i].kind == MN_TOGGLE)
537             menu_width += 2;
538         }
539     }
540
541   return (menu_width * 16 + 24);
542 }
543
544 int Menu::get_height() const
545 {
546   return item.size() * 24;
547 }
548
549 /* Draw the current menu. */
550 void
551 Menu::draw()
552 {
553   int menu_height = get_height();
554   int menu_width  = get_width();
555
556   /* Draw a transparent background */
557   fillrect(pos_x - menu_width/2,
558            pos_y - 24*item.size()/2 - 10,
559            menu_width,menu_height + 20,
560            150,180,200,125);
561
562   for(unsigned int i = 0; i < item.size(); ++i)
563     {
564       draw_item(i, menu_width, menu_height);
565     }
566 }
567
568 /* Check for menu event */
569 void
570 Menu::event(SDL_Event& event)
571 {
572   SDLKey key;
573   switch(event.type)
574     {
575     case SDL_KEYDOWN:
576       key = event.key.keysym.sym;
577       SDLMod keymod;
578       char ch[2];
579       keymod = SDL_GetModState();
580       int x,y;
581
582       /* If the current unicode character is an ASCII character,
583          assign it to ch. */
584       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
585         {
586           ch[0] = event.key.keysym.unicode & 0x7F;
587           ch[1] = '\0';
588         }
589       else
590         {
591           /* An International Character. */
592         }
593
594       if(item[active_item].kind == MN_CONTROLFIELD)
595         {
596         *item[active_item].int_p = event.key.keysym.unicode;
597         menuaction = MENU_ACTION_DOWN;
598         return;
599         }
600
601
602       switch(key)
603         {
604         case SDLK_UP:           /* Menu Up */
605           menuaction = MENU_ACTION_UP;
606           break;
607         case SDLK_DOWN:         /* Menu Down */
608           menuaction = MENU_ACTION_DOWN;
609           break;
610         case SDLK_LEFT:         /* Menu Up */
611           menuaction = MENU_ACTION_LEFT;
612           break;
613         case SDLK_RIGHT:                /* Menu Down */
614           menuaction = MENU_ACTION_RIGHT;
615           break;
616         case SDLK_SPACE:
617           if(item[active_item].kind == MN_TEXTFIELD)
618             {
619               menuaction = MENU_ACTION_INPUT;
620               mn_input_char = ' ';
621               break;
622             }
623         case SDLK_RETURN: /* Menu Hit */
624           menuaction = MENU_ACTION_HIT;
625           break;
626         case SDLK_DELETE:
627         case SDLK_BACKSPACE:
628           menuaction = MENU_ACTION_REMOVE;
629           delete_character++;
630           break;
631         case SDLK_ESCAPE:
632           Menu::pop_current();
633           break;
634         default:
635           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
636             {
637               menuaction = MENU_ACTION_INPUT;
638               mn_input_char = *ch;
639             }
640           else
641             {
642               mn_input_char = '\0';
643             }
644           break;
645         }
646       break;
647     case  SDL_JOYAXISMOTION:
648       if(event.jaxis.axis == joystick_keymap.y_axis)
649         {
650           if (event.jaxis.value > 1024)
651             menuaction = MENU_ACTION_DOWN;
652           else if (event.jaxis.value < -1024)
653             menuaction = MENU_ACTION_UP;
654         }
655       break;
656     case  SDL_JOYBUTTONDOWN:
657       menuaction = MENU_ACTION_HIT;
658       break;
659     case SDL_MOUSEBUTTONDOWN:
660       x = event.motion.x;
661       y = event.motion.y;
662       if(x > pos_x - get_width()/2 &&
663          x < pos_x + get_width()/2 &&
664          y > pos_y - get_height()/2 &&
665          y < pos_y + get_height()/2)
666         {
667           menuaction = MENU_ACTION_HIT;
668         }
669       break;
670     case SDL_MOUSEMOTION:
671       x = event.motion.x;
672       y = event.motion.y;
673       if(x > pos_x - get_width()/2 &&
674          x < pos_x + get_width()/2 &&
675          y > pos_y - get_height()/2 &&
676          y < pos_y + get_height()/2)
677         {
678           active_item = (y - (pos_y - get_height()/2)) / 24;
679           mouse_cursor->set_state(MC_LINK);
680         }
681       else
682         {
683           mouse_cursor->set_state(MC_NORMAL);
684         }
685       break;
686     default:
687       break;
688     }
689 }
690
691
692 // EOF //