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