MN_BACK works for multiple layers of menus 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                 item[active_item].toggled = true;
218                 break;
219
220               case MN_BACK:
221                 if(last_menu != NULL)
222                   Menu::set_current(last_menu);
223                 break;
224               default:
225                 break;
226               }
227           }
228           break;
229
230         case MENU_ACTION_REMOVE:
231           if(item[active_item].kind == MN_TEXTFIELD
232               || item[active_item].kind == MN_NUMFIELD)
233             {
234               if(item[active_item].input != NULL)
235                 {
236                   int i = strlen(item[active_item].input);
237
238                   while(delete_character > 0)   /* remove charactes */
239                     {
240                       item[active_item].input[i-1] = '\0';
241                       delete_character--;
242                     }
243                 }
244             }
245           break;
246
247         case MENU_ACTION_INPUT:
248           if(item[active_item].kind == MN_TEXTFIELD
249               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
250             {
251               if(item[active_item].input != NULL)
252                 {
253                   int i = strlen(item[active_item].input);
254                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
255                   item[active_item].input[i] = mn_input_char;
256                   item[active_item].input[i+1] = '\0';
257                 }
258               else
259                 {
260                   item[active_item].input = (char*) malloc(2*sizeof(char));
261                   item[active_item].input[0] = mn_input_char;
262                   item[active_item].input[1] = '\0';
263                 }
264             }
265           break;
266
267         case MENU_ACTION_NONE:
268           break;
269         }
270     }
271
272   menu_item_type& new_item = item[active_item];
273   if(new_item.kind == MN_DEACTIVE
274       || new_item.kind == MN_LABEL
275       || new_item.kind == MN_HL)
276     {
277       // Skip the horzontal line item
278       if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
279         menuaction = MENU_ACTION_DOWN;
280
281       if(num_items > 1)
282         action();
283     }
284 }
285
286 /* Check, if the value of the active menu item has changed. */
287 int
288 Menu::check()
289 {
290   if(num_items != 0 && item != NULL)
291     {
292       if((item[active_item].kind == MN_ACTION
293           || item[active_item].kind == MN_TEXTFIELD
294           || item[active_item].kind == MN_NUMFIELD)
295           && item[active_item].toggled)
296         {
297           item[active_item].toggled = false;
298           show_menu = 0;
299           return active_item;
300         }
301       else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
302         {
303           return active_item;
304         }
305       else
306         return -1;
307     }
308   else
309     return -1;
310 }
311
312 void
313 Menu::draw_item(int index, // Position of the current item in the menu
314                 int menu_width,
315                 int menu_height)
316 {
317   int font_width  = 16;
318
319   const menu_item_type& pitem =  item[index];
320
321   int effect_offset = 0;
322   {
323     int effect_time = 0;
324     if(timer_check(&effect))
325       effect_time = timer_get_left(&effect) / 4;
326
327     effect_offset = (index % 2) ? effect_time : -effect_time;
328   }
329
330   int x_pos       = pos_x;
331   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
332   int shadow_size = 2;
333   int text_width  = strlen(pitem.text) * font_width;
334   int input_width = strlen(pitem.input) * font_width;
335   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
336   text_type* text_font = &white_text;
337
338   if (arrange_left)
339     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
340
341   if(index == active_item)
342     {
343       shadow_size = 3;
344       text_font = &blue_text;
345     }
346
347   switch (pitem.kind)
348     {
349     case MN_DEACTIVE:
350       {
351         text_draw_align(&black_text, pitem.text,
352                         x_pos, y_pos,
353                         A_HMIDDLE, A_VMIDDLE, 2);
354         break;
355       }
356
357     case MN_HL:
358       {
359         int x = pos_x - menu_width/2;
360         int y = y_pos - 12 - effect_offset;
361         /* Draw a horizontal line with a little 3d effect */
362         fillrect(x, y + 6,
363                  menu_width, 4,
364                  210,50,50,225);
365         fillrect(x, y + 6,
366                  menu_width, 2,
367                  0,0,0,255);
368         break;
369       }
370     case MN_LABEL:
371       {
372         text_draw_align(&white_big_text, pitem.text,
373                         x_pos, y_pos,
374                         A_HMIDDLE, A_VMIDDLE, 2);
375         break;
376       }
377     case MN_TEXTFIELD:
378     case MN_NUMFIELD:
379       {
380         int input_pos = input_width/2;
381         int text_pos  = (text_width + font_width)/2;
382
383         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
384                  input_width + font_width + 2, 20,
385                  255,255,255,255);
386         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
387                  input_width + font_width, 18,
388                  0,0,0,128);
389
390         text_draw_align(&gold_text, pitem.input,
391                         x_pos + text_pos, y_pos,
392                         A_HMIDDLE, A_VMIDDLE, 2);
393
394         text_draw_align(text_font, pitem.text,
395                         x_pos - (input_width + font_width)/2, y_pos,
396                         A_HMIDDLE, A_VMIDDLE, shadow_size);
397         break;
398       }
399     case MN_STRINGSELECT:
400       {
401         int list_pos_2 = list_width + font_width;
402         int list_pos   = list_width/2;
403         int text_pos   = (text_width + font_width)/2;
404
405         /* Draw arrows */
406         texture_draw(&arrow_left,  x_pos - list_pos + text_pos - 17, y_pos - 8);
407         texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
408
409         /* Draw input background */
410         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
411                  list_pos_2 + 2, 20,
412                  255,255,255,255);
413         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
414                  list_pos_2, 18,
415                  0,0,0,128);
416
417         text_draw_align(&gold_text, string_list_active(pitem.list),
418                         x_pos + text_pos, y_pos,
419                         A_HMIDDLE, A_VMIDDLE,2);
420
421         text_draw_align(text_font, pitem.text,
422                         x_pos - list_pos_2/2, y_pos,
423                         A_HMIDDLE, A_VMIDDLE, shadow_size);
424         break;
425       }
426     case MN_BACK:
427       {
428         text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
429         texture_draw(&back, x_pos + text_width/2  + font_width, y_pos - 8);
430         break;
431       }
432
433     case MN_TOGGLE:
434       {
435         text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
436
437         if(pitem.toggled)
438           texture_draw(&checkbox_checked,
439                        x_pos + (text_width+font_width)/2,
440                        y_pos - 8);
441         else
442           texture_draw(&checkbox,
443                        x_pos + (text_width+font_width)/2,
444                        y_pos - 8);
445         break;
446       }
447     case MN_ACTION:
448       text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
449       break;
450
451     case MN_GOTO:
452       text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
453       break;
454     }
455 }
456
457 /* Draw the current menu. */
458 void
459 Menu::draw()
460 {
461   int menu_height;
462   int menu_width;
463
464   /* The width of the menu has to be more than the width of the text
465      with the most characters */
466   menu_width = 0;
467   for(int i = 0; i < num_items; ++i)
468     {
469       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
470       if( w > menu_width )
471         {
472           menu_width = w;
473           if( item[i].kind == MN_TOGGLE)
474             menu_width += 2;
475         }
476     }
477
478   menu_width  = menu_width * 16 + 48;
479   menu_height = (num_items) * 24;
480
481   /* Draw a transparent background */
482   fillrect(pos_x - menu_width/2,
483            pos_y - 24*num_items/2,
484            menu_width,menu_height,150,150,150,100);
485
486   for(int i = 0; i < num_items; ++i)
487     {
488       draw_item(i, menu_width, menu_height);
489     }
490 }
491
492 /* Reset/Set global defaults */
493 void menu_reset(void)
494 {
495   menu_change  = false;
496   show_menu    = false;
497   menuaction   = MENU_ACTION_NONE;
498   current_menu = NULL;
499
500   delete_character = 0;
501   mn_input_char    = '\0';
502 }
503
504 /* --- MENU --- */
505 /* Draw the current menu and execute the (menu)events */
506 void menu_process_current(void)
507 {
508   menu_change = false;
509
510   if(current_menu != NULL)
511     {
512       current_menu->action();
513       current_menu->draw();
514     }
515
516   menuaction = MENU_ACTION_NONE;
517 }
518
519 /* Check for menu event */
520 void menu_event(SDL_keysym* keysym)
521 {
522   SDLKey key = keysym->sym;
523   SDLMod keymod;
524   char ch[2];
525   keymod = SDL_GetModState();
526
527   /* If the current unicode character is an ASCII character,
528      assign it to ch. */
529   if ( (keysym->unicode & 0xFF80) == 0 )
530     {
531       ch[0] = keysym->unicode & 0x7F;
532       ch[1] = '\0';
533     }
534   else
535     {
536       /* An International Character. */
537     }
538
539   switch(key)
540     {
541     case SDLK_UP:               /* Menu Up */
542       menuaction = MENU_ACTION_UP;
543       menu_change = true;
544       break;
545     case SDLK_DOWN:             /* Menu Down */
546       menuaction = MENU_ACTION_DOWN;
547       menu_change = true;
548       break;
549     case SDLK_LEFT:             /* Menu Up */
550       menuaction = MENU_ACTION_LEFT;
551       menu_change = true;
552       break;
553     case SDLK_RIGHT:            /* Menu Down */
554       menuaction = MENU_ACTION_RIGHT;
555       menu_change = true;
556       break;
557     case SDLK_SPACE:
558       if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
559         {
560           menuaction = MENU_ACTION_INPUT;
561           menu_change = true;
562           mn_input_char = ' ';
563           break;
564         }
565     case SDLK_RETURN: /* Menu Hit */
566       menuaction = MENU_ACTION_HIT;
567       menu_change = true;
568       break;
569     case SDLK_DELETE:
570     case SDLK_BACKSPACE:
571       menuaction = MENU_ACTION_REMOVE;
572       menu_change = true;
573       delete_character++;
574       break;
575     default:
576       if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
577         {
578           menuaction = MENU_ACTION_INPUT;
579           menu_change = true;
580           mn_input_char = *ch;
581         }
582       else
583         {
584           mn_input_char = '\0';
585         }
586       break;
587     }
588
589
590   /* FIXME: NO JOYSTICK SUPPORT */
591   /*#ifdef JOY_YES
592     else if (event.type == SDL_JOYBUTTONDOWN)
593     {
594     Joystick button: Continue:
595
596     done = 1;
597     }
598     #endif*/
599 }
600
601
602 // EOF //