Ingo, that code is not buggy!
[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   if (hit_item != -1)
408     return item[hit_item].id;
409   else
410     return -1;
411 }
412
413 void
414 Menu::draw_item(int index, // Position of the current item in the menu
415                 int menu_width,
416                 int menu_height)
417 {
418   MenuItem& pitem = item[index];
419
420   int font_width  = 16;
421   int effect_offset = 0;
422   {
423     int effect_time = 0;
424
425     if(effect.check())
426       effect_time = effect.get_left() / 4;
427
428     effect_offset = (index % 2) ? effect_time : -effect_time;
429   }
430
431   int x_pos       = pos_x;
432   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
433   int shadow_size = 2;
434   int text_width  = strlen(pitem.text) * font_width;
435   int input_width = strlen(pitem.input) * font_width;
436   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
437   Text* text_font = white_text;
438
439   if (arrange_left)
440     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
441
442   if(index == active_item)
443     {
444       shadow_size = 3;
445       text_font = blue_text;
446     }
447
448   switch (pitem.kind)
449     {
450     case MN_DEACTIVE:
451       {
452         black_text->draw_align(pitem.text,
453                                x_pos, y_pos,
454                                A_HMIDDLE, A_VMIDDLE, 2);
455         break;
456       }
457
458     case MN_HL:
459       {
460         int x = pos_x - menu_width/2;
461         int y = y_pos - 12 - effect_offset;
462         /* Draw a horizontal line with a little 3d effect */
463         fillrect(x, y + 6,
464                  menu_width, 4,
465                  150,200,255,225);
466         fillrect(x, y + 6,
467                  menu_width, 2,
468                  255,255,255,255);
469         break;
470       }
471     case MN_LABEL:
472       {
473         white_big_text->draw_align(pitem.text,
474                                    x_pos, y_pos,
475                                    A_HMIDDLE, A_VMIDDLE, 2);
476         break;
477       }
478     case MN_TEXTFIELD:
479     case MN_NUMFIELD:
480     case MN_CONTROLFIELD:
481       {
482         int input_pos = input_width/2;
483         int text_pos  = (text_width + font_width)/2;
484
485         fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
486                  input_width + font_width + 2, 20,
487                  255,255,255,255);
488         fillrect(x_pos - input_pos + text_pos, y_pos - 9,
489                  input_width + font_width, 18,
490                  0,0,0,128);
491
492         if(pitem.kind == MN_CONTROLFIELD)
493           get_controlfield_key_into_input(&pitem);
494
495         gold_text->draw_align(pitem.input,
496                               x_pos + text_pos, y_pos,
497                               A_HMIDDLE, A_VMIDDLE, 2);
498
499         text_font->draw_align(pitem.text,
500                               x_pos - (input_width + font_width)/2, y_pos,
501                               A_HMIDDLE, A_VMIDDLE, shadow_size);
502         break;
503       }
504     case MN_STRINGSELECT:
505       {
506         int list_pos_2 = list_width + font_width;
507         int list_pos   = list_width/2;
508         int text_pos   = (text_width + font_width)/2;
509
510         /* Draw arrows */
511         arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
512         arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
513
514         /* Draw input background */
515         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
516                  list_pos_2 + 2, 20,
517                  255,255,255,255);
518         fillrect(x_pos - list_pos + text_pos, y_pos - 9,
519                  list_pos_2, 18,
520                  0,0,0,128);
521
522         gold_text->draw_align(string_list_active(pitem.list),
523                         x_pos + text_pos, y_pos,
524                         A_HMIDDLE, A_VMIDDLE,2);
525
526         text_font->draw_align(pitem.text,
527                         x_pos - list_pos_2/2, y_pos,
528                         A_HMIDDLE, A_VMIDDLE, shadow_size);
529         break;
530       }
531     case MN_BACK:
532       {
533         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
534         back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
535         break;
536       }
537
538     case MN_TOGGLE:
539       {
540         text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
541
542         if(pitem.toggled)
543           checkbox_checked->draw(
544                        x_pos + (text_width+font_width)/2,
545                        y_pos - 8);
546         else
547           checkbox->draw(
548                        x_pos + (text_width+font_width)/2,
549                        y_pos - 8);
550         break;
551       }
552     case MN_ACTION:
553       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
554       break;
555
556     case MN_GOTO:
557       text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
558       break;
559     }
560 }
561
562 int Menu::get_width() const
563 {
564   /* The width of the menu has to be more than the width of the text
565      with the most characters */
566   int menu_width = 0;
567   for(unsigned int i = 0; i < item.size(); ++i)
568     {
569       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
570       if( w > menu_width )
571         {
572           menu_width = w;
573           if( item[i].kind == MN_TOGGLE)
574             menu_width += 2;
575         }
576     }
577
578   return (menu_width * 16 + 24);
579 }
580
581 int Menu::get_height() const
582 {
583   return item.size() * 24;
584 }
585
586 /* Draw the current menu. */
587 void
588 Menu::draw()
589 {
590   int menu_height = get_height();
591   int menu_width  = get_width();
592
593   /* Draw a transparent background */
594   fillrect(pos_x - menu_width/2,
595            pos_y - 24*item.size()/2 - 10,
596            menu_width,menu_height + 20,
597            150,180,200,125);
598
599   for(unsigned int i = 0; i < item.size(); ++i)
600     {
601       draw_item(i, menu_width, menu_height);
602     }
603 }
604
605 /* Check for menu event */
606 void
607 Menu::event(SDL_Event& event)
608 {
609   SDLKey key;
610   switch(event.type)
611     {
612     case SDL_KEYDOWN:
613       key = event.key.keysym.sym;
614       SDLMod keymod;
615       char ch[2];
616       keymod = SDL_GetModState();
617       int x,y;
618
619       /* If the current unicode character is an ASCII character,
620          assign it to ch. */
621       if ( (event.key.keysym.unicode & 0xFF80) == 0 )
622         {
623           ch[0] = event.key.keysym.unicode & 0x7F;
624           ch[1] = '\0';
625         }
626       else
627         {
628           /* An International Character. */
629         }
630
631       if(item[active_item].kind == MN_CONTROLFIELD)
632         {
633         if(key == SDLK_ESCAPE)
634           {
635           Menu::pop_current();
636           return;
637           }
638         *item[active_item].int_p = key;
639         menuaction = MENU_ACTION_DOWN;
640         return;
641         }
642
643
644       switch(key)
645         {
646         case SDLK_UP:           /* Menu Up */
647           menuaction = MENU_ACTION_UP;
648           break;
649         case SDLK_DOWN:         /* Menu Down */
650           menuaction = MENU_ACTION_DOWN;
651           break;
652         case SDLK_LEFT:         /* Menu Up */
653           menuaction = MENU_ACTION_LEFT;
654           break;
655         case SDLK_RIGHT:                /* Menu Down */
656           menuaction = MENU_ACTION_RIGHT;
657           break;
658         case SDLK_SPACE:
659           if(item[active_item].kind == MN_TEXTFIELD)
660             {
661               menuaction = MENU_ACTION_INPUT;
662               mn_input_char = ' ';
663               break;
664             }
665         case SDLK_RETURN: /* Menu Hit */
666           menuaction = MENU_ACTION_HIT;
667           break;
668         case SDLK_DELETE:
669         case SDLK_BACKSPACE:
670           menuaction = MENU_ACTION_REMOVE;
671           delete_character++;
672           break;
673         case SDLK_ESCAPE:
674           Menu::pop_current();
675           break;
676         default:
677           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
678             {
679               menuaction = MENU_ACTION_INPUT;
680               mn_input_char = *ch;
681             }
682           else
683             {
684               mn_input_char = '\0';
685             }
686           break;
687         }
688       break;
689     case  SDL_JOYAXISMOTION:
690       if(event.jaxis.axis == joystick_keymap.y_axis)
691         {
692           if (event.jaxis.value > 1024)
693             menuaction = MENU_ACTION_DOWN;
694           else if (event.jaxis.value < -1024)
695             menuaction = MENU_ACTION_UP;
696         }
697       break;
698     case  SDL_JOYBUTTONDOWN:
699       menuaction = MENU_ACTION_HIT;
700       break;
701     case SDL_MOUSEBUTTONDOWN:
702       x = event.motion.x;
703       y = event.motion.y;
704       if(x > pos_x - get_width()/2 &&
705          x < pos_x + get_width()/2 &&
706          y > pos_y - get_height()/2 &&
707          y < pos_y + get_height()/2)
708         {
709           menuaction = MENU_ACTION_HIT;
710         }
711       break;
712     case SDL_MOUSEMOTION:
713       x = event.motion.x;
714       y = event.motion.y;
715       if(x > pos_x - get_width()/2 &&
716          x < pos_x + get_width()/2 &&
717          y > pos_y - get_height()/2 &&
718          y < pos_y + get_height()/2)
719         {
720           active_item = (y - (pos_y - get_height()/2)) / 24;
721           mouse_cursor->set_state(MC_LINK);
722         }
723       else
724         {
725           mouse_cursor->set_state(MC_NORMAL);
726         }
727       break;
728     default:
729       break;
730     }
731 }
732
733
734 // EOF //