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