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