We have our own mouse-cursor now! (graphics by Settra Gaia)
[supertux.git] / src / menu.cpp
index 5f6d561..61ebe59 100644 (file)
@@ -214,6 +214,7 @@ Menu::action()
               case MN_ACTION:
               case MN_TEXTFIELD:
               case MN_NUMFIELD:
+              case MN_CONTROLFIELD:
                 item[active_item].toggled = true;
                 break;
 
@@ -376,6 +377,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
       }
     case MN_TEXTFIELD:
     case MN_NUMFIELD:
+    case MN_CONTROLFIELD:
       {
         int input_pos = input_width/2;
         int text_pos  = (text_width + font_width)/2;
@@ -454,16 +456,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));
@@ -475,8 +472,20 @@ Menu::draw()
         }
     }
 
-  menu_width  = menu_width * 16 + 48;
-  menu_height = (num_items) * 24;
+  return (menu_width * 16 + 48);
+}
+
+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,
@@ -517,85 +526,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 )
-    {
-      ch[0] = keysym->unicode & 0x7F;
-      ch[1] = '\0';
-    }
-  else
+  SDLKey key;
+  switch(event.type)
     {
-      /* 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;
-          mn_input_char = ' ';
+          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)
+            {
+              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;
-    default:
-      if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
+    case  SDL_JOYBUTTONDOWN:
+      menuaction = MENU_ACTION_HIT;
+      break;
+    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*/
 }