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