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