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