Controls menu wasn't showing the keys until they were changed (fixed).
[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
30 #include "defines.h"
31 #include "globals.h"
32 #include "menu.h"
33 #include "screen.h"
34 #include "setup.h"
35 #include "sound.h"
36 #include "scene.h"
37 #include "leveleditor.h"
38 #include "timer.h"
39 #include "high_scores.h"
40
41 Surface* checkbox;
42 Surface* checkbox_checked;
43 Surface* back;
44 Surface* arrow_left;
45 Surface* arrow_right;
46
47 Menu* main_menu      = 0;
48 Menu* game_menu      = 0;
49 Menu* worldmap_menu  = 0;
50 Menu* options_menu   = 0;
51 Menu* options_controls_menu   = 0;
52 Menu* highscore_menu = 0;
53 Menu* load_game_menu = 0;
54 Menu* save_game_menu = 0;
55 Menu* contrib_menu   = 0;
56 Menu* contrib_subset_menu   = 0;
57
58 std::vector<Menu*> Menu::last_menus;
59 Menu* Menu::current_ = 0;
60
61 void
62 Menu::push_current(Menu* pmenu)
63 {
64   if (current_)
65     last_menus.push_back(current_);
66   
67   current_ = pmenu;
68   current_->effect.start(500);
69 }
70
71 void
72 Menu::pop_current()
73 {
74   if (!last_menus.empty())
75     {
76       current_ = last_menus.back();
77       current_->effect.start(500);
78
79       last_menus.pop_back();
80     }
81   else
82     {
83       current_ = 0;
84     }
85 }
86
87 void
88 Menu::set_current(Menu* menu)
89 {
90   last_menus.clear();
91
92   if (menu)
93     menu->effect.start(500);
94   
95   current_ = menu;
96 }
97
98 /* Return a pointer to a new menu item */
99 MenuItem*
100 MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
101 {
102   MenuItem *pnew_item = new MenuItem;
103   
104   pnew_item->kind = kind_;
105   pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
106   strcpy(pnew_item->text, text_);
107
108   if(kind_ == MN_TOGGLE)
109     pnew_item->toggled = init_toggle_;
110   else
111     pnew_item->toggled = false;
112
113   pnew_item->target_menu = target_menu_;
114   pnew_item->input = (char*) malloc(sizeof(char));
115   pnew_item->input[0] = '\0';
116
117   if(kind_ == MN_STRINGSELECT)
118     {
119       pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
120       string_list_init(pnew_item->list);
121     }
122   else
123     pnew_item->list = NULL;
124
125   pnew_item->int_p = int_p_;
126
127   return pnew_item;
128 }
129
130 void
131 MenuItem::change_text(const  char *text_)
132 {
133   if (text_)
134     {
135       free(text);
136       text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
137       strcpy(text, text_);
138     }
139 }
140
141 void
142 MenuItem::change_input(const  char *text_)
143 {
144   if(text)
145     {
146       free(input);
147       input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
148       strcpy(input, text_);
149     }
150 }
151
152 /* Set ControlField a key */
153 void Menu::get_controlfield_key_into_input(MenuItem *item)
154 {
155 switch(*item->int_p)
156   {
157   case SDLK_UP:
158     strcpy(item->input, "Up cursor");
159     break;
160   case SDLK_DOWN:
161     strcpy(item->input, "Down cursor");
162     break;
163   case SDLK_LEFT:
164     strcpy(item->input, "Left cursor");
165     break;
166   case SDLK_RIGHT:
167     strcpy(item->input, "Right cursor");
168     break;
169   case SDLK_RETURN:
170     strcpy(item->input, "Return");
171     break;
172   case SDLK_SPACE:
173     strcpy(item->input, "Space");
174     break;
175   case SDLK_RSHIFT:
176     strcpy(item->input, "Right Shift");
177     break;
178   case SDLK_LSHIFT:
179     strcpy(item->input, "Left Shift");
180     break;
181   case SDLK_RCTRL:
182     strcpy(item->input, "Right Control");
183     break;
184   case SDLK_LCTRL:
185     strcpy(item->input, "Left Control");
186     break;
187   case SDLK_RALT:
188     strcpy(item->input, "Right Alt");
189     break;
190   case SDLK_LALT:
191     strcpy(item->input, "Left Alt");
192     break;
193   default:
194     strcpy(item->input, (char*)item->int_p);
195     break;
196   }
197 }
198
199 /* Free a menu and all its items */
200 Menu::~Menu()
201 {
202   if(item.size() != 0)
203     {
204       for(unsigned int i = 0; i < item.size(); ++i)
205         {
206           free(item[i].text);
207           free(item[i].input);
208           string_list_free(item[i].list);
209         }
210     }
211 }
212
213
214 Menu::Menu()
215 {
216   hit_item = -1;
217   menuaction = MENU_ACTION_NONE;
218   delete_character = 0;
219   mn_input_char = '\0';
220   
221   pos_x        = screen->w/2;
222   pos_y        = screen->h/2;
223   has_backitem = false;
224   arrange_left = 0;
225   active_item  = 0;
226   effect.init(false);
227 }
228
229 void Menu::set_pos(int x, int y, float rw, float rh)
230 {
231   pos_x = x + (int)((float)get_width() * rw);
232   pos_y = y + (int)((float)get_height() * rh);
233 }
234
235 void
236 Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int* int_p)
237 {
238   if(kind_ == MN_BACK)
239     has_backitem = true;
240
241   additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, int_p));
242 }
243
244 /* Add an item to a menu */
245 void
246 Menu::additem(MenuItem* pmenu_item)
247 {
248   if(pmenu_item->kind == MN_BACK)
249     has_backitem = true;
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   return hit_item;
399   /*
400   if (item.size() != 0)
401     {
402       if((item[active_item].kind == MN_ACTION
403           || item[active_item].kind == MN_TEXTFIELD
404           || item[active_item].kind == MN_NUMFIELD)
405           && item[active_item].toggled)
406         { 
407           item[active_item].toggled = false;
408           Menu::set_current(0);
409           return active_item;
410         }
411       else if(item[active_item].kind == MN_TOGGLE 
412               || item[active_item].kind == MN_GOTO)
413         {
414           return active_item;
415         }
416       else
417         return -1;
418     }
419   else
420     return -1;
421   */
422 }
423
424 void
425 Menu::draw_item(int index, // Position of the current item in the menu
426                 int menu_width,
427                 int menu_height)
428 {
429   MenuItem& pitem = item[index];
430
431   int font_width  = 16;
432   int effect_offset = 0;
433   {
434     int effect_time = 0;
435
436     if(effect.check())
437       effect_time = effect.get_left() / 4;
438
439     effect_offset = (index % 2) ? effect_time : -effect_time;
440   }
441
442   int x_pos       = pos_x;
443   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
444   int shadow_size = 2;
445   int text_width  = strlen(pitem.text) * font_width;
446   int input_width = strlen(pitem.input) * font_width;
447   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
448   Text* text_font = white_text;
449
450   if (arrange_left)
451     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
452
453   if(index == active_item)
454     {
455       shadow_size = 3;
456       text_font = blue_text;
457     }
458
459   switch (pitem.kind)
460     {
461     case MN_DEACTIVE:
462       {
463         black_text->draw_align(pitem.text,
464                                x_pos, y_pos,
465                                A_HMIDDLE, A_VMIDDLE, 2);
466         break;
467       }
468
469     case MN_HL:
470       {
471         int x = pos_x - menu_width/2;
472         int y = y_pos - 12 - effect_offset;
473         /* Draw a horizontal line with a little 3d effect */
474         fillrect(x, y + 6,
475                  menu_width, 4,
476                  150,200,255,225);
477         fillrect(x, y + 6,
478                  menu_width, 2,
479                  255,255,255,255);
480         break;
481       }
482     case MN_LABEL:
483       {
484         white_big_text->draw_align(pitem.text,
485                                    x_pos, y_pos,
486                                    A_HMIDDLE, A_VMIDDLE, 2);
487         break;
488       }
489     case MN_TEXTFIELD:
490     case MN_NUMFIELD:
491     case MN_CONTROLFIELD:
492       {
493         int input_pos = input_width/2;
494         int text_pos  = (text_width + font_width)/2;
495
496         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
497                  input_width + font_width + 2, 20,
498                  255,255,255,255);
499         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
500                  input_width + font_width, 18,
501                  0,0,0,128);
502
503         if(pitem.kind == MN_CONTROLFIELD)
504           get_controlfield_key_into_input(&pitem);
505
506         gold_text->draw_align(pitem.input,
507                               x_pos + text_pos, y_pos,
508                               A_HMIDDLE, A_VMIDDLE, 2);
509
510         text_font->draw_align(pitem.text,
511                               x_pos - (input_width + font_width)/2, y_pos,
512                               A_HMIDDLE, A_VMIDDLE, shadow_size);
513         break;
514       }
515     case MN_STRINGSELECT:
516       {
517         int list_pos_2 = list_width + font_width;
518         int list_pos   = list_width/2;
519         int text_pos   = (text_width + font_width)/2;
520
521         /* Draw arrows */
522         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
523         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
524
525         /* Draw input background */
526         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
527                  list_pos_2 + 2, 20,
528                  255,255,255,255);
529         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
530                  list_pos_2, 18,
531                  0,0,0,128);
532
533         gold_text->draw_align(string_list_active(pitem.list),
534                         x_pos + text_pos, y_pos,
535                         A_HMIDDLE, A_VMIDDLE,2);
536
537         text_font->draw_align(pitem.text,
538                         x_pos - list_pos_2/2, y_pos,
539                         A_HMIDDLE, A_VMIDDLE, shadow_size);
540         break;
541       }
542     case MN_BACK:
543       {
544         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
545         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
546         break;
547       }
548
549     case MN_TOGGLE:
550       {
551         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
552
553         if(pitem.toggled)
554           checkbox_checked->draw(
555                        x_pos + (text_width+font_width)/2,
556                        y_pos - 8);
557         else
558           checkbox->draw(
559                        x_pos + (text_width+font_width)/2,
560                        y_pos - 8);
561         break;
562       }
563     case MN_ACTION:
564       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
565       break;
566
567     case MN_GOTO:
568       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
569       break;
570     }
571 }
572
573 int Menu::get_width() const
574 {
575   /* The width of the menu has to be more than the width of the text
576      with the most characters */
577   int menu_width = 0;
578   for(unsigned int i = 0; i < item.size(); ++i)
579     {
580       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
581       if( w > menu_width )
582         {
583           menu_width = w;
584           if( item[i].kind == MN_TOGGLE)
585             menu_width += 2;
586         }
587     }
588
589   return (menu_width * 16 + 24);
590 }
591
592 int Menu::get_height() const
593 {
594   return item.size() * 24;
595 }
596
597 /* Draw the current menu. */
598 void
599 Menu::draw()
600 {
601   int menu_height = get_height();
602   int menu_width  = get_width();
603
604   /* Draw a transparent background */
605   fillrect(pos_x - menu_width/2,
606            pos_y - 24*item.size()/2 - 10,
607            menu_width,menu_height + 20,
608            150,180,200,125);
609
610   for(unsigned int i = 0; i < item.size(); ++i)
611     {
612       draw_item(i, menu_width, menu_height);
613     }
614 }
615
616 /* Check for menu event */
617 void
618 Menu::event(SDL_Event& event)
619 {
620   SDLKey key;
621   switch(event.type)
622     {
623     case SDL_KEYDOWN:
624       key = event.key.keysym.sym;
625       SDLMod keymod;
626       char ch[2];
627       keymod = SDL_GetModState();
628       int x,y;
629
630       /* If the current unicode character is an ASCII character,
631          assign it to ch. */
632       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
633         {
634           ch[0] = event.key.keysym.unicode & 0x7F;
635           ch[1] = '\0';
636         }
637       else
638         {
639           /* An International Character. */
640         }
641
642       if(item[active_item].kind == MN_CONTROLFIELD)
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 //