fixed little type error
[supertux.git] / src / menu.cpp
1 /*
2   menu.c
3   
4   Super Tux - Menu
5   
6   by Tobias Glaesser
7   tobi.web@gmx.de
8   http://www.newbreedsoftware.com/supertux/
9   
10   December 20, 2003 - March 15, 2004
11 */
12
13 #ifndef WIN32
14 #include <sys/types.h>
15 #include <ctype.h>
16 #endif
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include "defines.h"
23 #include "globals.h"
24 #include "menu.h"
25 #include "screen.h"
26 #include "setup.h"
27 #include "sound.h"
28 #include "scene.h"
29 #include "leveleditor.h"
30 #include "timer.h"
31 #include "high_scores.h"
32
33 /* (global) menu variables */
34 MenuAction menuaction;
35 bool show_menu;
36 bool menu_change;
37 texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right;
38
39 Menu* main_menu      = 0;
40 Menu* game_menu      = 0;
41 Menu* options_menu   = 0;
42 Menu* highscore_menu = 0;
43 Menu* load_game_menu = 0;
44 Menu* save_game_menu = 0;
45
46 Menu* current_menu = 0;
47 Menu* last_menu = 0;
48
49 /* input implementation variables */
50 int delete_character;
51 char mn_input_char;
52
53 /* Set the current menu */
54 void
55 Menu::set_current(Menu* pmenu)
56 {
57   if(pmenu != current_menu)
58     {
59       menu_change  = true;
60       last_menu    = current_menu;
61       current_menu = pmenu;
62       timer_start(&pmenu->effect, 500);
63     }
64 }
65
66 /* Return a pointer to a new menu item */
67 menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu)
68 {
69   menu_item_type *pnew_item = (menu_item_type*) malloc(sizeof(menu_item_type));
70   pnew_item->kind = kind;
71   pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text) + 1));
72   strcpy(pnew_item->text,text);
73   if(kind == MN_TOGGLE)
74     pnew_item->toggled = init_toggle;
75   else
76     pnew_item->toggled = false;
77   pnew_item->target_menu = target_menu;
78   pnew_item->input = (char*) malloc(sizeof(char));
79   pnew_item->input[0] = '\0';
80   if(kind == MN_STRINGSELECT)
81     {
82       pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
83       string_list_init(pnew_item->list);
84     }
85   else
86     pnew_item->list = NULL;
87   return pnew_item;
88 }
89
90 void menu_item_change_text(menu_item_type* pmenu_item,const  char *text)
91 {
92   if(text)
93     {
94       free(pmenu_item->text);
95       pmenu_item->text = (char*) malloc(sizeof(char )*(strlen(text)+1));
96       strcpy(pmenu_item->text,text);
97     }
98 }
99 void menu_item_change_input(menu_item_type* pmenu_item,const  char *text)
100 {
101   if(text)
102     {
103       free(pmenu_item->input);
104       pmenu_item->input = (char*) malloc(sizeof(char )*(strlen(text)+1));
105       strcpy(pmenu_item->input,text);
106     }
107 }
108
109 /* Free a menu and all its items */
110 Menu::~Menu()
111 {
112   if(num_items != 0 && item != NULL)
113     {
114       for(int i = 0; i < num_items; ++i)
115         {
116           free(item[i].text);
117           free(item[i].input);
118           string_list_free(item[i].list);
119         }
120       free(item);
121     }
122 }
123
124 Menu::Menu()
125 {
126   pos_x        = screen->w/2;
127   pos_y        = screen->h/2;
128   arrange_left = 0;
129   num_items    = 0;
130   active_item  = 0;
131   item         = NULL;
132   timer_init(&effect,false);
133 }
134
135 void
136 Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
137 {
138   additem(menu_item_create(kind, text, toggle, menu));
139 }
140
141 /* Add an item to a menu */
142 void 
143 Menu::additem(menu_item_type* pmenu_item)
144 {
145   ++num_items;
146   item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items);
147   memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type));
148   free(pmenu_item);
149 }
150
151 /* Process actions done on the menu */
152 void
153 Menu::action()
154 {
155   if(num_items != 0 && item != NULL)
156     {
157       switch(menuaction)
158         {
159         case MENU_ACTION_UP:
160           if (active_item > 0)
161             --active_item;
162           else
163             active_item = num_items-1;
164           break;
165
166         case MENU_ACTION_DOWN:
167           if(active_item < num_items-1)
168             ++active_item;
169           else
170             active_item = 0;
171           break;
172
173         case MENU_ACTION_LEFT:
174           if(item[active_item].kind == MN_STRINGSELECT
175              && item[active_item].list->num_items != 0)
176             {
177               if(item[active_item].list->active_item > 0)
178                 --item[active_item].list->active_item;
179               else
180                 item[active_item].list->active_item = item[active_item].list->num_items-1;
181             }
182           break;
183         case MENU_ACTION_RIGHT:
184           if(item[active_item].kind == MN_STRINGSELECT 
185              && item[active_item].list->num_items != 0)
186             {
187               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
188                 ++item[active_item].list->active_item;
189               else
190                 item[active_item].list->active_item = 0;
191             }
192           break;
193
194         case MENU_ACTION_HIT:
195           {
196             switch (item[active_item].kind)
197               {
198               case MN_GOTO: 
199                 if (item[active_item].target_menu != NULL)
200                   Menu::set_current(item[active_item].target_menu);
201                 else
202                   puts("NULLL");
203                 break;
204                 
205               case MN_TOGGLE:
206                 item[active_item].toggled = !item[active_item].toggled;
207                 menu_change = true;
208                 break;
209                 
210               case MN_ACTION:
211               case MN_TEXTFIELD:
212               case MN_NUMFIELD:
213                 item[active_item].toggled = true;
214                 break;
215
216               case MN_BACK:
217                 if(last_menu != NULL)
218                   Menu::set_current(last_menu);
219                 break;
220               default:
221                 break;
222               }
223           }
224           break;
225
226         case MENU_ACTION_REMOVE:
227           if(item[active_item].kind == MN_TEXTFIELD
228              || item[active_item].kind == MN_NUMFIELD)
229             {
230               if(item[active_item].input != NULL)
231                 {
232                   int i = strlen(item[active_item].input);
233
234                   while(delete_character > 0)   /* remove charactes */
235                     {
236                       item[active_item].input[i-1] = '\0';
237                       delete_character--;
238                     }
239                 }
240             }
241           break;
242
243         case MENU_ACTION_INPUT:
244           if(item[active_item].kind == MN_TEXTFIELD
245              || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
246             {
247               if(item[active_item].input != NULL)
248                 {
249                   int i = strlen(item[active_item].input);
250                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
251                   item[active_item].input[i] = mn_input_char;
252                   item[active_item].input[i+1] = '\0';
253                 }
254               else
255                 {
256                   item[active_item].input = (char*) malloc(2*sizeof(char));
257                   item[active_item].input[0] = mn_input_char;
258                   item[active_item].input[1] = '\0';
259                 }
260             }
261           break;
262
263         case MENU_ACTION_NONE:
264           break;
265         }
266     }
267
268   menu_item_type& new_item = item[active_item];
269   if(new_item.kind == MN_DEACTIVE
270      || new_item.kind == MN_LABEL 
271      || new_item.kind == MN_HL)
272     {
273       // Skip the horzontal line item
274       if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
275         menuaction = MENU_ACTION_DOWN;
276
277       if(num_items > 1)
278         action();
279     }
280 }
281
282 /* Check, if the value of the active menu item has changed. */
283 int
284 Menu::check()
285 {
286   if(num_items != 0 && item != NULL)
287     {
288       if((item[active_item].kind == MN_ACTION || item[active_item].kind == MN_TEXTFIELD || item[active_item].kind == MN_NUMFIELD) && item[active_item].toggled == true)
289         {
290           item[active_item].toggled = false;
291           show_menu = 0;
292           return active_item;
293         }
294       else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
295         {
296           return active_item;
297         }
298       else
299         return -1;
300     }
301   else
302     return -1;
303 }
304
305 void
306 Menu::draw_item(int index, // Position of the current item in the menu
307                 int menu_width, 
308                 int menu_height)
309 {
310   int font_width  = 16;
311
312   const menu_item_type& pitem =  item[index];
313
314   int effect_offset = 0;
315   {
316     int effect_time = 0;
317     if(timer_check(&effect))
318       effect_time = timer_get_left(&effect) / 4;
319
320     effect_offset = (index % 2) ? effect_time : -effect_time;
321   }
322
323   int x_pos       = pos_x;
324   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
325   int shadow_size = 2;
326   int text_width  = strlen(pitem.text) * font_width;
327   int input_width = strlen(pitem.input) * font_width;
328   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
329   text_type* text_font = &white_text;
330
331   if(arrange_left == true)
332     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
333   
334   if(index == active_item)
335     {
336       shadow_size = 3;
337       text_font = &blue_text;
338     }
339
340   switch (pitem.kind)
341     {
342     case MN_DEACTIVE:
343       {
344         text_draw_align(&black_text, pitem.text, 
345                         x_pos, y_pos,
346                         A_HMIDDLE, A_VMIDDLE, 2);
347         break;
348       }
349
350     case MN_HL:
351       {
352         int x = pos_x - menu_width/2; 
353         int y = y_pos - 12 - effect_offset;
354         /* Draw a horizontal line with a little 3d effect */
355         fillrect(x, y + 6,
356                  menu_width, 4,
357                  210,50,50,225);
358         fillrect(x, y + 6, 
359                  menu_width, 2,
360                  0,0,0,255);
361         break;
362       }
363     case MN_LABEL:
364       {
365         text_draw_align(&white_big_text, pitem.text, 
366                         x_pos, y_pos,
367                         A_HMIDDLE, A_VMIDDLE, 2);
368         break;
369       }
370     case MN_TEXTFIELD:
371     case MN_NUMFIELD:
372       {
373         int input_pos = input_width/2;
374         int text_pos  = (text_width + font_width)/2;
375
376         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
377                  input_width + font_width + 2, 20,
378                  255,255,255,255);
379         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
380                  input_width + font_width, 18,
381                  0,0,0,128);
382
383         text_draw_align(&gold_text, pitem.input,
384                         x_pos + text_pos, y_pos, 
385                         A_HMIDDLE, A_VMIDDLE, 2);
386
387         text_draw_align(text_font, pitem.text, 
388                         x_pos - (input_width + font_width)/2, y_pos,
389                         A_HMIDDLE, A_VMIDDLE, shadow_size);
390         break;
391       }
392     case MN_STRINGSELECT:
393       {
394         int list_pos_2 = list_width + font_width;
395         int list_pos   = list_width/2;
396         int text_pos   = (text_width + font_width)/2;
397
398         /* Draw arrows */
399         texture_draw(&arrow_left,  x_pos - list_pos + text_pos - 17, y_pos - 8);
400         texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
401
402         /* Draw input background */
403         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
404                  list_pos_2 + 2, 20,
405                  255,255,255,255);
406         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
407                  list_pos_2, 18,
408                  0,0,0,128);
409
410         text_draw_align(&gold_text, string_list_active(pitem.list), 
411                         x_pos + text_pos, y_pos,
412                         A_HMIDDLE, A_VMIDDLE,2);
413
414         text_draw_align(text_font, pitem.text,
415                         x_pos - list_pos_2/2, y_pos,
416                         A_HMIDDLE, A_VMIDDLE, shadow_size);
417         break;
418       }
419     case MN_BACK:
420       {
421         text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
422         texture_draw(&back, x_pos + text_width/2  + font_width, y_pos - 8);
423         break;
424       }
425
426     case MN_TOGGLE:
427       {
428         text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
429         
430         if(pitem.toggled == true)
431           texture_draw(&checkbox_checked, 
432                        x_pos + (text_width+font_width)/2,
433                        y_pos - 8);
434         else
435           texture_draw(&checkbox, 
436                        x_pos + (text_width+font_width)/2,
437                        y_pos - 8);
438         break;
439       }
440     case MN_ACTION:
441       text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
442       break;
443
444     case MN_GOTO:
445       text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
446       break;
447     }
448 }
449
450 /* Draw the current menu. */
451 void
452 Menu::draw()
453 {
454   int menu_height;
455   int menu_width;
456   
457   /* The width of the menu has to be more than the width of the text
458      with the most characters */
459   menu_width = 0;
460   for(int i = 0; i < num_items; ++i)
461     {
462       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
463       if( w > menu_width )
464         {
465           menu_width = w;
466           if( item[i].kind == MN_TOGGLE)
467             menu_width += 2;
468         }
469     }
470
471   menu_width  = menu_width * 16 + 48;
472   menu_height = (num_items) * 24;
473
474   /* Draw a transparent background */
475   fillrect(pos_x - menu_width/2,
476            pos_y - 24*num_items/2,
477            menu_width,menu_height,150,150,150,100);
478
479   for(int i = 0; i < num_items; ++i)
480     {
481       draw_item(i, menu_width, menu_height);
482     }
483 }
484
485 /* Reset/Set global defaults */
486 void menu_reset(void)
487 {
488   menu_change  = false;
489   show_menu    = false;
490   menuaction   = MENU_ACTION_NONE;
491   current_menu = NULL;
492   last_menu    = NULL;
493
494   delete_character = 0;
495   mn_input_char    = '\0';
496 }
497
498 /* --- MENU --- */
499 /* Draw the current menu and execute the (menu)events */
500 void menu_process_current(void)
501 {
502   menu_change = false;
503
504   if(current_menu != NULL)
505     {
506       current_menu->action();
507       current_menu->draw();
508     }
509
510   menuaction = MENU_ACTION_NONE;
511 }
512
513 /* Check for menu event */
514 void menu_event(SDL_keysym* keysym)
515 {
516   SDLKey key = keysym->sym;
517   SDLMod keymod;
518   char ch[2];
519   keymod = SDL_GetModState();
520
521   /* If the current unicode character is an ASCII character,
522      assign it to ch. */
523   if ( (keysym->unicode & 0xFF80) == 0 )
524     {
525       ch[0] = keysym->unicode & 0x7F;
526       ch[1] = '\0';
527     }
528   else
529     {
530       /* An International Character. */
531     }
532
533   switch(key)
534     {
535     case SDLK_UP:               /* Menu Up */
536       menuaction = MENU_ACTION_UP;
537       menu_change = true;
538       break;
539     case SDLK_DOWN:             /* Menu Down */
540       menuaction = MENU_ACTION_DOWN;
541       menu_change = true;
542       break;
543     case SDLK_LEFT:             /* Menu Up */
544       menuaction = MENU_ACTION_LEFT;
545       menu_change = true;
546       break;
547     case SDLK_RIGHT:            /* Menu Down */
548       menuaction = MENU_ACTION_RIGHT;
549       menu_change = true;
550       break;
551     case SDLK_SPACE:
552       if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
553         {
554           menuaction = MENU_ACTION_INPUT;
555           menu_change = true;
556           mn_input_char = ' ';
557           break;
558         }
559     case SDLK_RETURN: /* Menu Hit */
560       menuaction = MENU_ACTION_HIT;
561       menu_change = true;
562       break;
563     case SDLK_DELETE:
564     case SDLK_BACKSPACE:
565       menuaction = MENU_ACTION_REMOVE;
566       menu_change = true;
567       delete_character++;
568       break;
569     default:
570       if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
571         {
572           menuaction = MENU_ACTION_INPUT;
573           menu_change = true;
574           mn_input_char = *ch;
575         }
576       else
577         {
578           mn_input_char = '\0';
579         }
580       break;
581     }
582
583
584   /* FIXME: NO JOYSTICK SUPPORT */
585   /*#ifdef JOY_YES
586     else if (event.type == SDL_JOYBUTTONDOWN)
587     {
588     Joystick button: Continue:
589
590     done = 1;
591     }
592     #endif*/
593 }
594
595
596 // EOF //