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