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