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