- bullet tweaks
[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   arrange_left = 0;
227   active_item  = 0;
228   effect.init(false);
229 }
230
231 void Menu::set_pos(int x, int y, float rw, float rh)
232 {
233   pos_x = x + (int)((float)get_width() * rw);
234   pos_y = y + (int)((float)get_height() * rh);
235 }
236
237 void
238 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
239 {
240   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
241 }
242
243 /* Add an item to a menu */
244 void
245 Menu::additem(MenuItem* pmenu_item)
246 {
247   item.push_back(*pmenu_item);
248   delete pmenu_item;
249 }
250
251 void
252 Menu::clear()
253 {
254   item.clear();
255 }
256
257 /* Process actions done on the menu */
258 void
259 Menu::action()
260 {
261   hit_item = -1;
262   if(item.size() != 0)
263     {
264       switch(menuaction)
265         {
266         case MENU_ACTION_UP:
267           if (active_item > 0)
268             --active_item;
269           else
270             active_item = int(item.size())-1;
271           break;
272
273         case MENU_ACTION_DOWN:
274           if(active_item < int(item.size())-1)
275             ++active_item;
276           else
277             active_item = 0;
278           break;
279
280         case MENU_ACTION_LEFT:
281           if(item[active_item].kind == MN_STRINGSELECT
282               && item[active_item].list->num_items != 0)
283             {
284               if(item[active_item].list->active_item > 0)
285                 --item[active_item].list->active_item;
286               else
287                 item[active_item].list->active_item = item[active_item].list->num_items-1;
288             }
289           break;
290
291         case MENU_ACTION_RIGHT:
292           if(item[active_item].kind == MN_STRINGSELECT
293               && item[active_item].list->num_items != 0)
294             {
295               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
296                 ++item[active_item].list->active_item;
297               else
298                 item[active_item].list->active_item = 0;
299             }
300           break;
301
302         case MENU_ACTION_HIT:
303           {
304             hit_item = active_item;
305             switch (item[active_item].kind)
306               {
307               case MN_GOTO:
308                 if (item[active_item].target_menu != NULL)
309                   Menu::push_current(item[active_item].target_menu);
310                 else
311                   puts("NULLL");
312                 break;
313
314               case MN_TOGGLE:
315                 item[active_item].toggled = !item[active_item].toggled;
316                 break;
317
318               case MN_ACTION:
319               case MN_TEXTFIELD:
320               case MN_NUMFIELD:
321                 Menu::set_current(0); 
322                 item[active_item].toggled = true;
323                 break;
324
325               case MN_BACK:
326                 Menu::pop_current();
327                 break;
328               default:
329                 break;
330               }
331           }
332           break;
333
334         case MENU_ACTION_REMOVE:
335           if(item[active_item].kind == MN_TEXTFIELD
336               || item[active_item].kind == MN_NUMFIELD)
337             {
338               if(item[active_item].input != NULL)
339                 {
340                   int i = strlen(item[active_item].input);
341
342                   while(delete_character > 0)   /* remove charactes */
343                     {
344                       item[active_item].input[i-1] = '\0';
345                       delete_character--;
346                     }
347                 }
348             }
349           break;
350
351         case MENU_ACTION_INPUT:
352           if(item[active_item].kind == MN_TEXTFIELD
353               || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
354             {
355               if(item[active_item].input != NULL)
356                 {
357                   int i = strlen(item[active_item].input);
358                   item[active_item].input = (char*) realloc(item[active_item].input,sizeof(char)*(i + 2));
359                   item[active_item].input[i] = mn_input_char;
360                   item[active_item].input[i+1] = '\0';
361                 }
362               else
363                 {
364                   item[active_item].input = (char*) malloc(2*sizeof(char));
365                   item[active_item].input[0] = mn_input_char;
366                   item[active_item].input[1] = '\0';
367                 }
368             }
369
370         case MENU_ACTION_NONE:
371           break;
372         }
373     }
374
375   MenuItem& new_item = item[active_item];
376   if(new_item.kind == MN_DEACTIVE
377       || new_item.kind == MN_LABEL
378       || new_item.kind == MN_HL)
379     {
380       // Skip the horzontal line item
381       if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
382         menuaction = MENU_ACTION_DOWN;
383
384       if (item.size() > 1)
385         action();
386     }
387
388   menuaction = MENU_ACTION_NONE;
389 }
390
391 int
392 Menu::check()
393 {
394   if (hit_item != -1)
395     return item[hit_item].id;
396   else
397     return -1;
398 }
399
400 void
401 Menu::draw_item(int index, // Position of the current item in the menu
402                 int menu_width,
403                 int menu_height)
404 {
405   MenuItem& pitem = item[index];
406
407   int font_width  = 16;
408   int effect_offset = 0;
409   {
410     int effect_time = 0;
411
412     if(effect.check())
413       effect_time = effect.get_left() / 4;
414
415     effect_offset = (index % 2) ? effect_time : -effect_time;
416   }
417
418   int x_pos       = pos_x;
419   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
420   int shadow_size = 2;
421   int text_width  = strlen(pitem.text) * font_width;
422   int input_width = strlen(pitem.input) * font_width;
423   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
424   Text* text_font = white_text;
425
426   if (arrange_left)
427     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
428
429   if(index == active_item)
430     {
431       shadow_size = 3;
432       text_font = blue_text;
433     }
434
435   switch (pitem.kind)
436     {
437     case MN_DEACTIVE:
438       {
439         black_text->draw_align(pitem.text,
440                                x_pos, y_pos,
441                                A_HMIDDLE, A_VMIDDLE, 2);
442         break;
443       }
444
445     case MN_HL:
446       {
447         int x = pos_x - menu_width/2;
448         int y = y_pos - 12 - effect_offset;
449         /* Draw a horizontal line with a little 3d effect */
450         fillrect(x, y + 6,
451                  menu_width, 4,
452                  150,200,255,225);
453         fillrect(x, y + 6,
454                  menu_width, 2,
455                  255,255,255,255);
456         break;
457       }
458     case MN_LABEL:
459       {
460         white_big_text->draw_align(pitem.text,
461                                    x_pos, y_pos,
462                                    A_HMIDDLE, A_VMIDDLE, 2);
463         break;
464       }
465     case MN_TEXTFIELD:
466     case MN_NUMFIELD:
467     case MN_CONTROLFIELD:
468       {
469         int input_pos = input_width/2;
470         int text_pos  = (text_width + font_width)/2;
471
472         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
473                  input_width + font_width + 2, 20,
474                  255,255,255,255);
475         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
476                  input_width + font_width, 18,
477                  0,0,0,128);
478
479         if(pitem.kind == MN_CONTROLFIELD)
480           get_controlfield_key_into_input(&pitem);
481
482         gold_text->draw_align(pitem.input,
483                               x_pos + text_pos, y_pos,
484                               A_HMIDDLE, A_VMIDDLE, 2);
485
486         text_font->draw_align(pitem.text,
487                               x_pos - (input_width + font_width)/2, y_pos,
488                               A_HMIDDLE, A_VMIDDLE, shadow_size);
489         break;
490       }
491     case MN_STRINGSELECT:
492       {
493         int list_pos_2 = list_width + font_width;
494         int list_pos   = list_width/2;
495         int text_pos   = (text_width + font_width)/2;
496
497         /* Draw arrows */
498         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
499         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
500
501         /* Draw input background */
502         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
503                  list_pos_2 + 2, 20,
504                  255,255,255,255);
505         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
506                  list_pos_2, 18,
507                  0,0,0,128);
508
509         gold_text->draw_align(string_list_active(pitem.list),
510                         x_pos + text_pos, y_pos,
511                         A_HMIDDLE, A_VMIDDLE,2);
512
513         text_font->draw_align(pitem.text,
514                         x_pos - list_pos_2/2, y_pos,
515                         A_HMIDDLE, A_VMIDDLE, shadow_size);
516         break;
517       }
518     case MN_BACK:
519       {
520         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
521         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
522         break;
523       }
524
525     case MN_TOGGLE:
526       {
527         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
528
529         if(pitem.toggled)
530           checkbox_checked->draw(
531                        x_pos + (text_width+font_width)/2,
532                        y_pos - 8);
533         else
534           checkbox->draw(
535                        x_pos + (text_width+font_width)/2,
536                        y_pos - 8);
537         break;
538       }
539     case MN_ACTION:
540       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
541       break;
542
543     case MN_GOTO:
544       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
545       break;
546     }
547 }
548
549 int Menu::get_width() const
550 {
551   /* The width of the menu has to be more than the width of the text
552      with the most characters */
553   int menu_width = 0;
554   for(unsigned int i = 0; i < item.size(); ++i)
555     {
556       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
557       if( w > menu_width )
558         {
559           menu_width = w;
560           if( item[i].kind == MN_TOGGLE)
561             menu_width += 2;
562         }
563     }
564
565   return (menu_width * 16 + 24);
566 }
567
568 int Menu::get_height() const
569 {
570   return item.size() * 24;
571 }
572
573 /* Draw the current menu. */
574 void
575 Menu::draw()
576 {
577   int menu_height = get_height();
578   int menu_width  = get_width();
579
580   /* Draw a transparent background */
581   fillrect(pos_x - menu_width/2,
582            pos_y - 24*item.size()/2 - 10,
583            menu_width,menu_height + 20,
584            150,180,200,125);
585
586   for(unsigned int i = 0; i < item.size(); ++i)
587     {
588       draw_item(i, menu_width, menu_height);
589     }
590 }
591
592 MenuItem&
593 Menu::get_item_by_id(int id)
594 {
595   for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
596     if(i->id == id)
597       return *i;
598   }
599
600   assert(false);
601   static MenuItem dummyitem;
602   return dummyitem;
603 }
604
605 bool
606 Menu::isToggled(int id)
607 {
608   return get_item_by_id(id).toggled;
609 }
610
611 /* Check for menu event */
612 void
613 Menu::event(SDL_Event& event)
614 {
615   SDLKey key;
616   switch(event.type)
617     {
618     case SDL_KEYDOWN:
619       key = event.key.keysym.sym;
620       SDLMod keymod;
621       char ch[2];
622       keymod = SDL_GetModState();
623       int x,y;
624
625       /* If the current unicode character is an ASCII character,
626          assign it to ch. */
627       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
628         {
629           ch[0] = event.key.keysym.unicode & 0x7F;
630           ch[1] = '\0';
631         }
632       else
633         {
634           /* An International Character. */
635         }
636
637       if(item[active_item].kind == MN_CONTROLFIELD)
638         {
639         if(key == SDLK_ESCAPE)
640           {
641           Menu::pop_current();
642           return;
643           }
644         *item[active_item].int_p = key;
645         menuaction = MENU_ACTION_DOWN;
646         return;
647         }
648
649
650       switch(key)
651         {
652         case SDLK_UP:           /* Menu Up */
653           menuaction = MENU_ACTION_UP;
654           break;
655         case SDLK_DOWN:         /* Menu Down */
656           menuaction = MENU_ACTION_DOWN;
657           break;
658         case SDLK_LEFT:         /* Menu Up */
659           menuaction = MENU_ACTION_LEFT;
660           break;
661         case SDLK_RIGHT:                /* Menu Down */
662           menuaction = MENU_ACTION_RIGHT;
663           break;
664         case SDLK_SPACE:
665           if(item[active_item].kind == MN_TEXTFIELD)
666             {
667               menuaction = MENU_ACTION_INPUT;
668               mn_input_char = ' ';
669               break;
670             }
671         case SDLK_RETURN: /* Menu Hit */
672           menuaction = MENU_ACTION_HIT;
673           break;
674         case SDLK_DELETE:
675         case SDLK_BACKSPACE:
676           menuaction = MENU_ACTION_REMOVE;
677           delete_character++;
678           break;
679         case SDLK_ESCAPE:
680           Menu::pop_current();
681           break;
682         default:
683           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
684             {
685               menuaction = MENU_ACTION_INPUT;
686               mn_input_char = *ch;
687             }
688           else
689             {
690               mn_input_char = '\0';
691             }
692           break;
693         }
694       break;
695     case  SDL_JOYAXISMOTION:
696       if(event.jaxis.axis == joystick_keymap.y_axis)
697         {
698           if (event.jaxis.value > 1024)
699             menuaction = MENU_ACTION_DOWN;
700           else if (event.jaxis.value < -1024)
701             menuaction = MENU_ACTION_UP;
702         }
703       break;
704     case  SDL_JOYBUTTONDOWN:
705       menuaction = MENU_ACTION_HIT;
706       break;
707     case SDL_MOUSEBUTTONDOWN:
708       x = event.motion.x;
709       y = event.motion.y;
710       if(x > pos_x - get_width()/2 &&
711          x < pos_x + get_width()/2 &&
712          y > pos_y - get_height()/2 &&
713          y < pos_y + get_height()/2)
714         {
715           menuaction = MENU_ACTION_HIT;
716         }
717       break;
718     case SDL_MOUSEMOTION:
719       x = event.motion.x;
720       y = event.motion.y;
721       if(x > pos_x - get_width()/2 &&
722          x < pos_x + get_width()/2 &&
723          y > pos_y - get_height()/2 &&
724          y < pos_y + get_height()/2)
725         {
726           active_item = (y - (pos_y - get_height()/2)) / 24;
727           mouse_cursor->set_state(MC_LINK);
728         }
729       else
730         {
731           mouse_cursor->set_state(MC_NORMAL);
732         }
733       break;
734     default:
735       break;
736     }
737 }
738
739
740 // EOF //