- reduced menu fadness a bit (ie. balanced the edges)
[supertux.git] / src / menu.cpp
index 9b9aec6..99073f5 100644 (file)
@@ -31,7 +31,7 @@
 #include "high_scores.h"
 
 /* (global) menu variables */
-MenuAction menuaction;
+MenuAction menuaction = MENU_ACTION_NONE;
 bool show_menu;
 bool menu_change;
 texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right;
@@ -39,12 +39,13 @@ texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right;
 Menu* main_menu      = 0;
 Menu* game_menu      = 0;
 Menu* options_menu   = 0;
+Menu* options_controls_menu   = 0;
 Menu* highscore_menu = 0;
 Menu* load_game_menu = 0;
 Menu* save_game_menu = 0;
+Menu* contrib_menu   = 0;
 
 Menu* current_menu = 0;
-Menu* last_menu = 0;
 
 /* input implementation variables */
 int delete_character;
@@ -57,8 +58,12 @@ Menu::set_current(Menu* pmenu)
   if(pmenu != current_menu)
     {
       menu_change  = true;
-      last_menu    = current_menu;
+      Menu* tmp = current_menu;
       current_menu = pmenu;
+      if(tmp)
+        if(tmp->last_menu != pmenu)
+          current_menu->last_menu = tmp;
+
       timer_start(&pmenu->effect, 500);
     }
 }
@@ -125,21 +130,29 @@ Menu::Menu()
 {
   pos_x        = screen->w/2;
   pos_y        = screen->h/2;
+  last_menu    = 0;
   arrange_left = 0;
   num_items    = 0;
   active_item  = 0;
+  last_menu    = 0;
   item         = NULL;
   timer_init(&effect,false);
 }
 
-menu_item_type*
+void Menu::set_pos(int x, int y, float rw, float rh)
+{
+ pos_x = x + (int)((float)width() * rw);
+ pos_y = y + (int)((float)height() * rh);
+}
+
+void
 Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
 {
   additem(menu_item_create(kind, text, toggle, menu));
 }
 
 /* Add an item to a menu */
-void 
+void
 Menu::additem(menu_item_type* pmenu_item)
 {
   ++num_items;
@@ -172,7 +185,7 @@ Menu::action()
 
         case MENU_ACTION_LEFT:
           if(item[active_item].kind == MN_STRINGSELECT
-             && item[active_item].list->num_items != 0)
+              && item[active_item].list->num_items != 0)
             {
               if(item[active_item].list->active_item > 0)
                 --item[active_item].list->active_item;
@@ -180,9 +193,10 @@ Menu::action()
                 item[active_item].list->active_item = item[active_item].list->num_items-1;
             }
           break;
+
         case MENU_ACTION_RIGHT:
-          if(item[active_item].kind == MN_STRINGSELECT 
-             && item[active_item].list->num_items != 0)
+          if(item[active_item].kind == MN_STRINGSELECT
+              && item[active_item].list->num_items != 0)
             {
               if(item[active_item].list->active_item < item[active_item].list->num_items-1)
                 ++item[active_item].list->active_item;
@@ -195,21 +209,22 @@ Menu::action()
           {
             switch (item[active_item].kind)
               {
-              case MN_GOTO: 
+              case MN_GOTO:
                 if (item[active_item].target_menu != NULL)
                   Menu::set_current(item[active_item].target_menu);
                 else
                   puts("NULLL");
                 break;
-                
+
               case MN_TOGGLE:
                 item[active_item].toggled = !item[active_item].toggled;
                 menu_change = true;
                 break;
-                
+
               case MN_ACTION:
               case MN_TEXTFIELD:
               case MN_NUMFIELD:
+              case MN_CONTROLFIELD:
                 item[active_item].toggled = true;
                 break;
 
@@ -225,7 +240,7 @@ Menu::action()
 
         case MENU_ACTION_REMOVE:
           if(item[active_item].kind == MN_TEXTFIELD
-             || item[active_item].kind == MN_NUMFIELD)
+              || item[active_item].kind == MN_NUMFIELD)
             {
               if(item[active_item].input != NULL)
                 {
@@ -242,7 +257,7 @@ Menu::action()
 
         case MENU_ACTION_INPUT:
           if(item[active_item].kind == MN_TEXTFIELD
-             || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
+              || (item[active_item].kind == MN_NUMFIELD && mn_input_char >= '0' && mn_input_char <= '9'))
             {
               if(item[active_item].input != NULL)
                 {
@@ -267,8 +282,8 @@ Menu::action()
 
   menu_item_type& new_item = item[active_item];
   if(new_item.kind == MN_DEACTIVE
-     || new_item.kind == MN_LABEL 
-     || new_item.kind == MN_HL)
+      || new_item.kind == MN_LABEL
+      || new_item.kind == MN_HL)
     {
       // Skip the horzontal line item
       if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
@@ -279,13 +294,15 @@ Menu::action()
     }
 }
 
-/* Check, if the value of the active menu item has changed. */
 int
 Menu::check()
 {
   if(num_items != 0 && item != NULL)
     {
-      if((item[active_item].kind == MN_ACTION || item[active_item].kind == MN_TEXTFIELD || item[active_item].kind == MN_NUMFIELD) && item[active_item].toggled == true)
+      if((item[active_item].kind == MN_ACTION
+          || item[active_item].kind == MN_TEXTFIELD
+          || item[active_item].kind == MN_NUMFIELD)
+          && item[active_item].toggled)
         {
           item[active_item].toggled = false;
           show_menu = 0;
@@ -304,7 +321,7 @@ Menu::check()
 
 void
 Menu::draw_item(int index, // Position of the current item in the menu
-                int menu_width, 
+                int menu_width,
                 int menu_height)
 {
   int font_width  = 16;
@@ -328,9 +345,9 @@ Menu::draw_item(int index, // Position of the current item in the menu
   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
   text_type* text_font = &white_text;
 
-  if(arrange_left == true)
+  if (arrange_left)
     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
-  
+
   if(index == active_item)
     {
       shadow_size = 3;
@@ -341,7 +358,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
     {
     case MN_DEACTIVE:
       {
-        text_draw_align(&black_text, pitem.text, 
+        text_draw_align(&black_text, pitem.text,
                         x_pos, y_pos,
                         A_HMIDDLE, A_VMIDDLE, 2);
         break;
@@ -349,26 +366,27 @@ Menu::draw_item(int index, // Position of the current item in the menu
 
     case MN_HL:
       {
-        int x = pos_x - menu_width/2; 
+        int x = pos_x - menu_width/2;
         int y = y_pos - 12 - effect_offset;
         /* Draw a horizontal line with a little 3d effect */
         fillrect(x, y + 6,
                  menu_width, 4,
-                 210,50,50,225);
-        fillrect(x, y + 6, 
+                 150,200,255,225);
+        fillrect(x, y + 6,
                  menu_width, 2,
-                 0,0,0,255);
+                 255,255,255,255);
         break;
       }
     case MN_LABEL:
       {
-        text_draw_align(&white_big_text, pitem.text, 
+        text_draw_align(&white_big_text, pitem.text,
                         x_pos, y_pos,
                         A_HMIDDLE, A_VMIDDLE, 2);
         break;
       }
     case MN_TEXTFIELD:
     case MN_NUMFIELD:
+    case MN_CONTROLFIELD:
       {
         int input_pos = input_width/2;
         int text_pos  = (text_width + font_width)/2;
@@ -381,10 +399,10 @@ Menu::draw_item(int index, // Position of the current item in the menu
                  0,0,0,128);
 
         text_draw_align(&gold_text, pitem.input,
-                        x_pos + text_pos, y_pos, 
+                        x_pos + text_pos, y_pos,
                         A_HMIDDLE, A_VMIDDLE, 2);
 
-        text_draw_align(text_font, pitem.text, 
+        text_draw_align(text_font, pitem.text,
                         x_pos - (input_width + font_width)/2, y_pos,
                         A_HMIDDLE, A_VMIDDLE, shadow_size);
         break;
@@ -407,7 +425,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
                  list_pos_2, 18,
                  0,0,0,128);
 
-        text_draw_align(&gold_text, string_list_active(pitem.list), 
+        text_draw_align(&gold_text, string_list_active(pitem.list),
                         x_pos + text_pos, y_pos,
                         A_HMIDDLE, A_VMIDDLE,2);
 
@@ -426,13 +444,13 @@ Menu::draw_item(int index, // Position of the current item in the menu
     case MN_TOGGLE:
       {
         text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
-        
-        if(pitem.toggled == true)
-          texture_draw(&checkbox_checked, 
+
+        if(pitem.toggled)
+          texture_draw(&checkbox_checked,
                        x_pos + (text_width+font_width)/2,
                        y_pos - 8);
         else
-          texture_draw(&checkbox, 
+          texture_draw(&checkbox,
                        x_pos + (text_width+font_width)/2,
                        y_pos - 8);
         break;
@@ -447,16 +465,11 @@ Menu::draw_item(int index, // Position of the current item in the menu
     }
 }
 
-/* Draw the current menu. */
-void
-Menu::draw()
+int Menu::width()
 {
-  int menu_height;
-  int menu_width;
-  
   /* The width of the menu has to be more than the width of the text
      with the most characters */
-  menu_width = 0;
+  int menu_width = 0;
   for(int i = 0; i < num_items; ++i)
     {
       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
@@ -468,13 +481,26 @@ Menu::draw()
         }
     }
 
-  menu_width  = menu_width * 16 + 48;
-  menu_height = (num_items) * 24;
+  return (menu_width * 16 + 24);
+}
+
+int Menu::height()
+{
+  return ((num_items) * 24);
+}
+
+/* Draw the current menu. */
+void
+Menu::draw()
+{
+  int menu_height = height();
+  int menu_width = width();
 
   /* Draw a transparent background */
   fillrect(pos_x - menu_width/2,
-           pos_y - 24*num_items/2,
-           menu_width,menu_height,150,150,150,100);
+           pos_y - 24*num_items/2 - 10,
+           menu_width,menu_height + 20,
+           150,180,200,125);
 
   for(int i = 0; i < num_items; ++i)
     {
@@ -489,7 +515,6 @@ void menu_reset(void)
   show_menu    = false;
   menuaction   = MENU_ACTION_NONE;
   current_menu = NULL;
-  last_menu    = NULL;
 
   delete_character = 0;
   mn_input_char    = '\0';
@@ -511,85 +536,123 @@ void menu_process_current(void)
 }
 
 /* Check for menu event */
-void menu_event(SDL_keysym* keysym)
+void menu_event(SDL_Event& event)
 {
-  SDLKey key = keysym->sym;
-  SDLMod keymod;
-  char ch[2];
-  keymod = SDL_GetModState();
-
-  /* If the current unicode character is an ASCII character,
-     assign it to ch. */
-  if ( (keysym->unicode & 0xFF80) == 0 )
+  SDLKey key;
+  switch(event.type)
     {
-      ch[0] = keysym->unicode & 0x7F;
-      ch[1] = '\0';
-    }
-  else
-    {
-      /* An International Character. */
-    }
+    case SDL_KEYDOWN:
+      key = event.key.keysym.sym;
+      SDLMod keymod;
+      char ch[2];
+      keymod = SDL_GetModState();
+      int x,y;
+
+      /* If the current unicode character is an ASCII character,
+         assign it to ch. */
+      if ( (event.key.keysym.unicode & 0xFF80) == 0 )
+        {
+          ch[0] = event.key.keysym.unicode & 0x7F;
+          ch[1] = '\0';
+        }
+      else
+        {
+          /* An International Character. */
+        }
 
-  switch(key)
-    {
-    case SDLK_UP:              /* Menu Up */
-      menuaction = MENU_ACTION_UP;
-      menu_change = true;
-      break;
-    case SDLK_DOWN:            /* Menu Down */
-      menuaction = MENU_ACTION_DOWN;
-      menu_change = true;
-      break;
-    case SDLK_LEFT:            /* Menu Up */
-      menuaction = MENU_ACTION_LEFT;
-      menu_change = true;
-      break;
-    case SDLK_RIGHT:           /* Menu Down */
-      menuaction = MENU_ACTION_RIGHT;
-      menu_change = true;
-      break;
-    case SDLK_SPACE:
-      if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
+      switch(key)
         {
-          menuaction = MENU_ACTION_INPUT;
+        case SDLK_UP:          /* Menu Up */
+          menuaction = MENU_ACTION_UP;
+          menu_change = true;
+          break;
+        case SDLK_DOWN:                /* Menu Down */
+          menuaction = MENU_ACTION_DOWN;
+          menu_change = true;
+          break;
+        case SDLK_LEFT:                /* Menu Up */
+          menuaction = MENU_ACTION_LEFT;
+          menu_change = true;
+          break;
+        case SDLK_RIGHT:               /* Menu Down */
+          menuaction = MENU_ACTION_RIGHT;
           menu_change = true;
-          mn_input_char = ' ';
+          break;
+        case SDLK_SPACE:
+          if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
+            {
+              menuaction = MENU_ACTION_INPUT;
+              menu_change = true;
+              mn_input_char = ' ';
+              break;
+            }
+        case SDLK_RETURN: /* Menu Hit */
+          menuaction = MENU_ACTION_HIT;
+          menu_change = true;
+          break;
+        case SDLK_DELETE:
+        case SDLK_BACKSPACE:
+          menuaction = MENU_ACTION_REMOVE;
+          menu_change = true;
+          delete_character++;
+          break;
+        default:
+          if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
+            {
+              menuaction = MENU_ACTION_INPUT;
+              menu_change = true;
+              mn_input_char = *ch;
+            }
+          else
+            {
+              mn_input_char = '\0';
+            }
           break;
         }
-    case SDLK_RETURN: /* Menu Hit */
-      menuaction = MENU_ACTION_HIT;
-      menu_change = true;
       break;
-    case SDLK_DELETE:
-    case SDLK_BACKSPACE:
-      menuaction = MENU_ACTION_REMOVE;
-      menu_change = true;
-      delete_character++;
+    case  SDL_JOYAXISMOTION:
+      if(event.jaxis.axis == JOY_Y)
+        {
+          if (event.jaxis.value > 1024)
+            menuaction = MENU_ACTION_DOWN;
+          else if (event.jaxis.value < -1024)
+            menuaction = MENU_ACTION_UP;
+        }
+      break;
+    case  SDL_JOYBUTTONDOWN:
+      menuaction = MENU_ACTION_HIT;
       break;
-    default:
-      if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
+    case SDL_MOUSEBUTTONDOWN:
+      x = event.motion.x;
+      y = event.motion.y;
+      if(x > current_menu->pos_x - current_menu->width()/2 &&
+          x < current_menu->pos_x + current_menu->width()/2 &&
+          y > current_menu->pos_y - current_menu->height()/2 &&
+          y < current_menu->pos_y + current_menu->height()/2)
         {
-          menuaction = MENU_ACTION_INPUT;
-          menu_change = true;
-          mn_input_char = *ch;
+          menuaction = MENU_ACTION_HIT;
         }
-      else
+      break;
+    case SDL_MOUSEMOTION:
+      x = event.motion.x;
+      y = event.motion.y;
+      if(x > current_menu->pos_x - current_menu->width()/2 &&
+          x < current_menu->pos_x + current_menu->width()/2 &&
+          y > current_menu->pos_y - current_menu->height()/2 &&
+          y < current_menu->pos_y + current_menu->height()/2)
         {
-          mn_input_char = '\0';
+          current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24;
+          menu_change = true;
+         mouse_cursor->set_state(MC_LINK);
         }
+       else
+       {
+         mouse_cursor->set_state(MC_NORMAL);
+       }
+      break;
+    default:
       break;
     }
-
-
-  /* FIXME: NO JOYSTICK SUPPORT */
-  /*#ifdef JOY_YES
-    else if (event.type == SDL_JOYBUTTONDOWN)
-    {
-    Joystick button: Continue:
-
-    done = 1;
-    }
-    #endif*/
 }