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