We have our own mouse-cursor now! (graphics by Settra Gaia)
[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 int Menu::width()
460 {
461   /* The width of the menu has to be more than the width of the text
462      with the most characters */
463   int menu_width = 0;
464   for(int i = 0; i < num_items; ++i)
465     {
466       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
467       if( w > menu_width )
468         {
469           menu_width = w;
470           if( item[i].kind == MN_TOGGLE)
471             menu_width += 2;
472         }
473     }
474
475   return (menu_width * 16 + 48);
476 }
477
478 int Menu::height()
479 {
480   return ((num_items) * 24);
481 }
482
483 /* Draw the current menu. */
484 void
485 Menu::draw()
486 {
487   int menu_height = height();
488   int menu_width = width();
489
490   /* Draw a transparent background */
491   fillrect(pos_x - menu_width/2,
492            pos_y - 24*num_items/2,
493            menu_width,menu_height,150,150,150,100);
494
495   for(int i = 0; i < num_items; ++i)
496     {
497       draw_item(i, menu_width, menu_height);
498     }
499 }
500
501 /* Reset/Set global defaults */
502 void menu_reset(void)
503 {
504   menu_change  = false;
505   show_menu    = false;
506   menuaction   = MENU_ACTION_NONE;
507   current_menu = NULL;
508
509   delete_character = 0;
510   mn_input_char    = '\0';
511 }
512
513 /* --- MENU --- */
514 /* Draw the current menu and execute the (menu)events */
515 void menu_process_current(void)
516 {
517   menu_change = false;
518
519   if(current_menu != NULL)
520     {
521       current_menu->action();
522       current_menu->draw();
523     }
524
525   menuaction = MENU_ACTION_NONE;
526 }
527
528 /* Check for menu event */
529 void menu_event(SDL_Event& event)
530 {
531   SDLKey key;
532   switch(event.type)
533     {
534     case SDL_KEYDOWN:
535       key = event.key.keysym.sym;
536       SDLMod keymod;
537       char ch[2];
538       keymod = SDL_GetModState();
539       int x,y;
540
541       /* If the current unicode character is an ASCII character,
542          assign it to ch. */
543       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
544         {
545           ch[0] = event.key.keysym.unicode & 0x7F;
546           ch[1] = '\0';
547         }
548       else
549         {
550           /* An International Character. */
551         }
552
553       switch(key)
554         {
555         case SDLK_UP:           /* Menu Up */
556           menuaction = MENU_ACTION_UP;
557           menu_change = true;
558           break;
559         case SDLK_DOWN:         /* Menu Down */
560           menuaction = MENU_ACTION_DOWN;
561           menu_change = true;
562           break;
563         case SDLK_LEFT:         /* Menu Up */
564           menuaction = MENU_ACTION_LEFT;
565           menu_change = true;
566           break;
567         case SDLK_RIGHT:                /* Menu Down */
568           menuaction = MENU_ACTION_RIGHT;
569           menu_change = true;
570           break;
571         case SDLK_SPACE:
572           if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
573             {
574               menuaction = MENU_ACTION_INPUT;
575               menu_change = true;
576               mn_input_char = ' ';
577               break;
578             }
579         case SDLK_RETURN: /* Menu Hit */
580           menuaction = MENU_ACTION_HIT;
581           menu_change = true;
582           break;
583         case SDLK_DELETE:
584         case SDLK_BACKSPACE:
585           menuaction = MENU_ACTION_REMOVE;
586           menu_change = true;
587           delete_character++;
588           break;
589         default:
590           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
591             {
592               menuaction = MENU_ACTION_INPUT;
593               menu_change = true;
594               mn_input_char = *ch;
595             }
596           else
597             {
598               mn_input_char = '\0';
599             }
600           break;
601         }
602       break;
603     case  SDL_JOYAXISMOTION:
604       if(event.jaxis.axis == JOY_Y)
605         {
606           if (event.jaxis.value > 1024)
607             menuaction = MENU_ACTION_DOWN;
608           else if (event.jaxis.value < -1024)
609             menuaction = MENU_ACTION_UP;
610         }
611       break;
612     case  SDL_JOYBUTTONDOWN:
613       menuaction = MENU_ACTION_HIT;
614       break;
615     case SDL_MOUSEBUTTONDOWN:
616       x = event.motion.x;
617       y = event.motion.y;
618       if(x > current_menu->pos_x - current_menu->width()/2 &&
619           x < current_menu->pos_x + current_menu->width()/2 &&
620           y > current_menu->pos_y - current_menu->height()/2 &&
621           y < current_menu->pos_y + current_menu->height()/2)
622         {
623           menuaction = MENU_ACTION_HIT;
624         }
625       break;
626     case SDL_MOUSEMOTION:
627       x = event.motion.x;
628       y = event.motion.y;
629       if(x > current_menu->pos_x - current_menu->width()/2 &&
630           x < current_menu->pos_x + current_menu->width()/2 &&
631           y > current_menu->pos_y - current_menu->height()/2 &&
632           y < current_menu->pos_y + current_menu->height()/2)
633         {
634           current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24;
635           menu_change = true;
636           mouse_cursor->set_state(MC_LINK);
637         }
638         else
639         {
640           mouse_cursor->set_state(MC_NORMAL);
641         }
642       break;
643     default:
644       break;
645     }
646 }
647
648
649 // EOF //