At least my compiler (g++ (GCC) 3.3.2) needs this include.
[supertux.git] / src / menu.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef WIN32
21 #include <sys/types.h>
22 #include <ctype.h>
23 #endif
24
25 #include <iostream>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <assert.h>
30
31 #include "defines.h"
32 #include "globals.h"
33 #include "menu.h"
34 #include "screen.h"
35 #include "setup.h"
36 #include "sound.h"
37 #include "scene.h"
38 #include "leveleditor.h"
39 #include "timer.h"
40 #include "high_scores.h"
41
42 Surface* checkbox;
43 Surface* checkbox_checked;
44 Surface* back;
45 Surface* arrow_left;
46 Surface* arrow_right;
47
48 Menu* main_menu      = 0;
49 Menu* game_menu      = 0;
50 Menu* worldmap_menu  = 0;
51 Menu* options_menu   = 0;
52 Menu* options_keys_menu     = 0;
53 Menu* options_joystick_menu = 0;
54 Menu* highscore_menu = 0;
55 Menu* load_game_menu = 0;
56 Menu* save_game_menu = 0;
57 Menu* contrib_menu   = 0;
58 Menu* contrib_subset_menu   = 0;
59
60 std::vector<Menu*> Menu::last_menus;
61 Menu* Menu::current_ = 0;
62
63 void
64 Menu::push_current(Menu* pmenu)
65 {
66   if (current_)
67     last_menus.push_back(current_);
68   
69   current_ = pmenu;
70   current_->effect.start(500);
71 }
72
73 void
74 Menu::pop_current()
75 {
76   if (!last_menus.empty())
77     {
78       current_ = last_menus.back();
79       current_->effect.start(500);
80
81       last_menus.pop_back();
82     }
83   else
84     {
85       current_ = 0;
86     }
87 }
88
89 void
90 Menu::set_current(Menu* menu)
91 {
92   last_menus.clear();
93
94   if (menu)
95     menu->effect.start(500);
96   
97   current_ = menu;
98 }
99
100 /* Return a pointer to a new menu item */
101 MenuItem*
102 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
103 {
104   MenuItem *pnew_item = new MenuItem;
105   
106   pnew_item->kind = kind_;
107   pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
108   strcpy(pnew_item->text, text_);
109
110   if(kind_ == MN_TOGGLE)
111     pnew_item->toggled = init_toggle_;
112   else
113     pnew_item->toggled = false;
114
115   pnew_item->target_menu = target_menu_;
116   pnew_item->input = (char*) malloc(sizeof(char));
117   pnew_item->input[0] = '\0';
118
119   if(kind_ == MN_STRINGSELECT)
120     {
121       pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
122       string_list_init(pnew_item->list);
123     }
124   else
125     pnew_item->list = NULL;
126
127   pnew_item->id = id;
128   pnew_item->int_p = int_p_;
129
130   return pnew_item;
131 }
132
133 void
134 MenuItem::change_text(const  char *text_)
135 {
136   if (text_)
137     {
138       free(text);
139       text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
140       strcpy(text, text_);
141     }
142 }
143
144 void
145 MenuItem::change_input(const  char *text_)
146 {
147   if(text)
148     {
149       free(input);
150       input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
151       strcpy(input, text_);
152     }
153 }
154
155 /* Set ControlField a key */
156 void Menu::get_controlfield_key_into_input(MenuItem *item)
157 {
158   switch(*item->int_p)
159   {
160   case SDLK_UP:
161     strcpy(item->input, "Up cursor");
162     break;
163   case SDLK_DOWN:
164     strcpy(item->input, "Down cursor");
165     break;
166   case SDLK_LEFT:
167     strcpy(item->input, "Left cursor");
168     break;
169   case SDLK_RIGHT:
170     strcpy(item->input, "Right cursor");
171     break;
172   case SDLK_RETURN:
173     strcpy(item->input, "Return");
174     break;
175   case SDLK_SPACE:
176     strcpy(item->input, "Space");
177     break;
178   case SDLK_RSHIFT:
179     strcpy(item->input, "Right Shift");
180     break;
181   case SDLK_LSHIFT:
182     strcpy(item->input, "Left Shift");
183     break;
184   case SDLK_RCTRL:
185     strcpy(item->input, "Right Control");
186     break;
187   case SDLK_LCTRL:
188     strcpy(item->input, "Left Control");
189     break;
190   case SDLK_RALT:
191     strcpy(item->input, "Right Alt");
192     break;
193   case SDLK_LALT:
194     strcpy(item->input, "Left Alt");
195     break;
196   default:
197     strcpy(item->input, (char*)item->int_p);
198     break;
199   }
200 }
201
202 /* Free a menu and all its items */
203 Menu::~Menu()
204 {
205   if(item.size() != 0)
206     {
207       for(unsigned int i = 0; i < item.size(); ++i)
208         {
209           free(item[i].text);
210           free(item[i].input);
211           string_list_free(item[i].list);
212         }
213     }
214 }
215
216
217 Menu::Menu()
218 {
219   hit_item = -1;
220   menuaction = MENU_ACTION_NONE;
221   delete_character = 0;
222   mn_input_char = '\0';
223   
224   pos_x        = screen->w/2;
225   pos_y        = screen->h/2;
226   has_backitem = false;
227   last_id = 0;
228   arrange_left = 0;
229   active_item  = 0;
230   effect.init(false);
231 }
232
233 void Menu::set_pos(int x, int y, float rw, float rh)
234 {
235   pos_x = x + (int)((float)get_width() * rw);
236   pos_y = y + (int)((float)get_height() * rh);
237 }
238
239 void
240 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
241 {
242   if(kind_ == MN_BACK)
243     has_backitem = true;
244   
245   if(id == -1 && item.size() == (unsigned)last_id)
246     {
247     id = last_id;
248     last_id++;
249     }
250
251   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
252 }
253
254 /* Add an item to a menu */
255 void
256 Menu::additem(MenuItem* pmenu_item)
257 {
258   if(pmenu_item->kind == MN_BACK)
259     has_backitem = true;
260
261   item.push_back(*pmenu_item);
262   delete pmenu_item;
263 }
264
265 void
266 Menu::clear()
267 {
268   item.clear();
269 }
270
271 /* Process actions done on the menu */
272 void
273 Menu::action()
274 {
275   hit_item = -1;
276   if(item.size() != 0)
277     {
278       switch(menuaction)
279         {
280         case MENU_ACTION_UP:
281           if (active_item > 0)
282             --active_item;
283           else
284             active_item = int(item.size())-1;
285           break;
286
287         case MENU_ACTION_DOWN:
288           if(active_item < int(item.size())-1)
289             ++active_item;
290           else
291             active_item = 0;
292           break;
293
294         case MENU_ACTION_LEFT:
295           if(item[active_item].kind == MN_STRINGSELECT
296               && item[active_item].list->num_items != 0)
297             {
298               if(item[active_item].list->active_item > 0)
299                 --item[active_item].list->active_item;
300               else
301                 item[active_item].list->active_item = item[active_item].list->num_items-1;
302             }
303           break;
304
305         case MENU_ACTION_RIGHT:
306           if(item[active_item].kind == MN_STRINGSELECT
307               && item[active_item].list->num_items != 0)
308             {
309               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
310                 ++item[active_item].list->active_item;
311               else
312                 item[active_item].list->active_item = 0;
313             }
314           break;
315
316         case MENU_ACTION_HIT:
317           {
318             hit_item = active_item;
319             switch (item[active_item].kind)
320               {
321               case MN_GOTO:
322                 if (item[active_item].target_menu != NULL)
323                   Menu::push_current(item[active_item].target_menu);
324                 else
325                   puts("NULLL");
326                 break;
327
328               case MN_TOGGLE:
329                 item[active_item].toggled = !item[active_item].toggled;
330                 break;
331
332               case MN_ACTION:
333               case MN_TEXTFIELD:
334               case MN_NUMFIELD:
335                 Menu::set_current(0); 
336                 item[active_item].toggled = true;
337                 break;
338
339               case MN_BACK:
340                 Menu::pop_current();
341                 break;
342               default:
343                 break;
344               }
345           }
346           break;
347
348         case MENU_ACTION_REMOVE:
349           if(item[active_item].kind == MN_TEXTFIELD
350               || item[active_item].kind == MN_NUMFIELD)
351             {
352               if(item[active_item].input != NULL)
353                 {
354                   int i = strlen(item[active_item].input);
355
356                   while(delete_character > 0)   /* remove charactes */
357                     {
358                       item[active_item].input[i-1] = '\0';
359                       delete_character--;
360                     }
361                 }
362             }
363           break;
364
365         case MENU_ACTION_INPUT:
366           if(item[active_item].kind == MN_TEXTFIELD
367               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
368             {
369               if(item[active_item].input != NULL)
370                 {
371                   int i = strlen(item[active_item].input);
372                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
373                   item[active_item].input[i] = mn_input_char;
374                   item[active_item].input[i+1] = '\0';
375                 }
376               else
377                 {
378                   item[active_item].input = (char*) malloc(2*sizeof(char));
379                   item[active_item].input[0] = mn_input_char;
380                   item[active_item].input[1] = '\0';
381                 }
382             }
383
384         case MENU_ACTION_NONE:
385           break;
386         }
387     }
388
389   MenuItem& new_item = item[active_item];
390   if(new_item.kind == MN_DEACTIVE
391       || new_item.kind == MN_LABEL
392       || new_item.kind == MN_HL)
393     {
394       // Skip the horzontal line item
395       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
396         menuaction = MENU_ACTION_DOWN;
397
398       if (item.size() > 1)
399         action();
400     }
401
402   menuaction = MENU_ACTION_NONE;
403 }
404
405 int
406 Menu::check()
407 {
408   if (hit_item != -1)
409     return item[hit_item].id;
410   else
411     return -1;
412 }
413
414 void
415 Menu::draw_item(int index, // Position of the current item in the menu
416                 int menu_width,
417                 int menu_height)
418 {
419   MenuItem& pitem = item[index];
420
421   int font_width  = 16;
422   int effect_offset = 0;
423   {
424     int effect_time = 0;
425
426     if(effect.check())
427       effect_time = effect.get_left() / 4;
428
429     effect_offset = (index % 2) ? effect_time : -effect_time;
430   }
431
432   int x_pos       = pos_x;
433   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
434   int shadow_size = 2;
435   int text_width  = strlen(pitem.text) * font_width;
436   int input_width = strlen(pitem.input) * font_width;
437   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
438   Text* text_font = white_text;
439
440   if (arrange_left)
441     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
442
443   if(index == active_item)
444     {
445       shadow_size = 3;
446       text_font = blue_text;
447     }
448
449   switch (pitem.kind)
450     {
451     case MN_DEACTIVE:
452       {
453         black_text->draw_align(pitem.text,
454                                x_pos, y_pos,
455                                A_HMIDDLE, A_VMIDDLE, 2);
456         break;
457       }
458
459     case MN_HL:
460       {
461         int x = pos_x - menu_width/2;
462         int y = y_pos - 12 - effect_offset;
463         /* Draw a horizontal line with a little 3d effect */
464         fillrect(x, y + 6,
465                  menu_width, 4,
466                  150,200,255,225);
467         fillrect(x, y + 6,
468                  menu_width, 2,
469                  255,255,255,255);
470         break;
471       }
472     case MN_LABEL:
473       {
474         white_big_text->draw_align(pitem.text,
475                                    x_pos, y_pos,
476                                    A_HMIDDLE, A_VMIDDLE, 2);
477         break;
478       }
479     case MN_TEXTFIELD:
480     case MN_NUMFIELD:
481     case MN_CONTROLFIELD:
482       {
483         int input_pos = input_width/2;
484         int text_pos  = (text_width + font_width)/2;
485
486         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
487                  input_width + font_width + 2, 20,
488                  255,255,255,255);
489         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
490                  input_width + font_width, 18,
491                  0,0,0,128);
492
493         if(pitem.kind == MN_CONTROLFIELD)
494           get_controlfield_key_into_input(&pitem);
495
496         gold_text->draw_align(pitem.input,
497                               x_pos + text_pos, y_pos,
498                               A_HMIDDLE, A_VMIDDLE, 2);
499
500         text_font->draw_align(pitem.text,
501                               x_pos - (input_width + font_width)/2, y_pos,
502                               A_HMIDDLE, A_VMIDDLE, shadow_size);
503         break;
504       }
505     case MN_STRINGSELECT:
506       {
507         int list_pos_2 = list_width + font_width;
508         int list_pos   = list_width/2;
509         int text_pos   = (text_width + font_width)/2;
510
511         /* Draw arrows */
512         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
513         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
514
515         /* Draw input background */
516         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
517                  list_pos_2 + 2, 20,
518                  255,255,255,255);
519         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
520                  list_pos_2, 18,
521                  0,0,0,128);
522
523         gold_text->draw_align(string_list_active(pitem.list),
524                         x_pos + text_pos, y_pos,
525                         A_HMIDDLE, A_VMIDDLE,2);
526
527         text_font->draw_align(pitem.text,
528                         x_pos - list_pos_2/2, y_pos,
529                         A_HMIDDLE, A_VMIDDLE, shadow_size);
530         break;
531       }
532     case MN_BACK:
533       {
534         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
535         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
536         break;
537       }
538
539     case MN_TOGGLE:
540       {
541         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
542
543         if(pitem.toggled)
544           checkbox_checked->draw(
545                        x_pos + (text_width+font_width)/2,
546                        y_pos - 8);
547         else
548           checkbox->draw(
549                        x_pos + (text_width+font_width)/2,
550                        y_pos - 8);
551         break;
552       }
553     case MN_ACTION:
554       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
555       break;
556
557     case MN_GOTO:
558       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
559       break;
560     }
561 }
562
563 int Menu::get_width() const
564 {
565   /* The width of the menu has to be more than the width of the text
566      with the most characters */
567   int menu_width = 0;
568   for(unsigned int i = 0; i < item.size(); ++i)
569     {
570       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
571       if( w > menu_width )
572         {
573           menu_width = w;
574           if( item[i].kind == MN_TOGGLE)
575             menu_width += 2;
576         }
577     }
578
579   return (menu_width * 16 + 24);
580 }
581
582 int Menu::get_height() const
583 {
584   return item.size() * 24;
585 }
586
587 /* Draw the current menu. */
588 void
589 Menu::draw()
590 {
591   int menu_height = get_height();
592   int menu_width  = get_width();
593
594   /* Draw a transparent background */
595   fillrect(pos_x - menu_width/2,
596            pos_y - 24*item.size()/2 - 10,
597            menu_width,menu_height + 20,
598            150,180,200,125);
599
600   for(unsigned int i = 0; i < item.size(); ++i)
601     {
602       draw_item(i, menu_width, menu_height);
603     }
604 }
605
606 MenuItem&
607 Menu::get_item_by_id(int id)
608 {
609   for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
610     if(i->id == id)
611       return *i;
612   }
613
614   assert(false);
615   static MenuItem dummyitem;
616   return dummyitem;
617 }
618
619 bool
620 Menu::isToggled(int id)
621 {
622   return get_item_by_id(id).toggled;
623 }
624
625 /* Check for menu event */
626 void
627 Menu::event(SDL_Event& event)
628 {
629   SDLKey key;
630   switch(event.type)
631     {
632     case SDL_KEYDOWN:
633       key = event.key.keysym.sym;
634       SDLMod keymod;
635       char ch[2];
636       keymod = SDL_GetModState();
637       int x,y;
638
639       /* If the current unicode character is an ASCII character,
640          assign it to ch. */
641       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
642         {
643           ch[0] = event.key.keysym.unicode & 0x7F;
644           ch[1] = '\0';
645         }
646       else
647         {
648           /* An International Character. */
649         }
650
651       if(item[active_item].kind == MN_CONTROLFIELD)
652         {
653         if(key == SDLK_ESCAPE)
654           {
655           Menu::pop_current();
656           return;
657           }
658         *item[active_item].int_p = key;
659         menuaction = MENU_ACTION_DOWN;
660         return;
661         }
662
663
664       switch(key)
665         {
666         case SDLK_UP:           /* Menu Up */
667           menuaction = MENU_ACTION_UP;
668           break;
669         case SDLK_DOWN:         /* Menu Down */
670           menuaction = MENU_ACTION_DOWN;
671           break;
672         case SDLK_LEFT:         /* Menu Up */
673           menuaction = MENU_ACTION_LEFT;
674           break;
675         case SDLK_RIGHT:                /* Menu Down */
676           menuaction = MENU_ACTION_RIGHT;
677           break;
678         case SDLK_SPACE:
679           if(item[active_item].kind == MN_TEXTFIELD)
680             {
681               menuaction = MENU_ACTION_INPUT;
682               mn_input_char = ' ';
683               break;
684             }
685         case SDLK_RETURN: /* Menu Hit */
686           menuaction = MENU_ACTION_HIT;
687           break;
688         case SDLK_DELETE:
689         case SDLK_BACKSPACE:
690           menuaction = MENU_ACTION_REMOVE;
691           delete_character++;
692           break;
693         case SDLK_ESCAPE:
694           Menu::pop_current();
695           break;
696         default:
697           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
698             {
699               menuaction = MENU_ACTION_INPUT;
700               mn_input_char = *ch;
701             }
702           else
703             {
704               mn_input_char = '\0';
705             }
706           break;
707         }
708       break;
709     case  SDL_JOYAXISMOTION:
710       if(event.jaxis.axis == joystick_keymap.y_axis)
711         {
712           if (event.jaxis.value > 1024)
713             menuaction = MENU_ACTION_DOWN;
714           else if (event.jaxis.value < -1024)
715             menuaction = MENU_ACTION_UP;
716         }
717       break;
718     case  SDL_JOYBUTTONDOWN:
719       menuaction = MENU_ACTION_HIT;
720       break;
721     case SDL_MOUSEBUTTONDOWN:
722       x = event.motion.x;
723       y = event.motion.y;
724       if(x > pos_x - get_width()/2 &&
725          x < pos_x + get_width()/2 &&
726          y > pos_y - get_height()/2 &&
727          y < pos_y + get_height()/2)
728         {
729           menuaction = MENU_ACTION_HIT;
730         }
731       break;
732     case SDL_MOUSEMOTION:
733       x = event.motion.x;
734       y = event.motion.y;
735       if(x > pos_x - get_width()/2 &&
736          x < pos_x + get_width()/2 &&
737          y > pos_y - get_height()/2 &&
738          y < pos_y + get_height()/2)
739         {
740           active_item = (y - (pos_y - get_height()/2)) / 24;
741           mouse_cursor->set_state(MC_LINK);
742         }
743       else
744         {
745           mouse_cursor->set_state(MC_NORMAL);
746         }
747       break;
748     default:
749       break;
750     }
751 }
752
753
754 // EOF //