/*----------------------------------\*
||             input.js             ||
||==================================||
||            Game input.           ||
||         By DaVince, 2010         ||
\*----------------------------------*/



/** Control handler that abstracts user controls. Works with both the keyboard and joystick. */
function Controls(type, joynum)
{
  this.type = (type != undefined) ? type : "keyboard";
  this.joy_num = joynum ? joynum : 0;
  
  this.changeType = function(p_type)
  {
    this.type = p_type;
    if (p_type == "keyboard")
    {
      this.action = KEY_ENTER;
      this.cancel = KEY_SPACE;
      this.menu = KEY_ESCAPE;
      
      this.jump = KEY_CTRL;  //Special for this platformer.
      this.fly = KEY_UP;
      
      this.left = KEY_LEFT;
      this.right = KEY_RIGHT;
      this.up = KEY_UP;
      this.down = KEY_DOWN;
    }
    
    else if (p_type == "joystick")
    {
      this.action = 0;
      this.cancel = 1;
      this.menu = 3;
      this.jump = 0;  //Special for this platformer.
      this.fly = 1;
      //Directions are not assigned in joystick mode (Sphere uses special functions for these).
    }
  }
  
  //Invoke it immediately (like a constructor).
  this.changeType(this.type);
  
  
  this.pressed = function(key)    //key can be "action", "cancel", "menu".
  {
    if (this.type == "keyboard")
    {
      switch(key)
      {
        case "action": return IsKeyPressed(this.action);
        case "confirm": return IsKeyPressed(this.action);
        case "cancel": return IsKeyPressed(this.cancel);
        case "menu":   return IsKeyPressed(this.menu);
        case "jump": return IsKeyPressed(this.jump);
        case "fly": return IsKeyPressed(this.fly);
        
        case "up": return IsKeyPressed(this.up);
        case "down": return IsKeyPressed(this.down);
        case "left": return IsKeyPressed(this.left);
        case "right": return IsKeyPressed(this.right);
        
        //Special cases
        case "any":  //Any button in the scheme at all?
          return this.pressed("action") || this.pressed("cancel") || this.pressed("menu") ||
                 this.pressed("up") || this.pressed("down") || this.pressed("left") || this.pressed("right");
        
        case "dir":  //Any directional button?
          return this.pressed("up") || this.pressed("down") || this.pressed("left") || this.pressed("right");
        
        default: Abort("LOL DOES NOT EXIST: " + key);
      }
    }
    
    else if (this.type == "joystick")
    {
      switch(key)
      {
        case "action": return IsJoystickButtonPressed(this.joy_num, this.action);
        case "confirm": return IsJoystickButtonPressed(this.joy_num, this.action);
        case "cancel": return IsJoystickButtonPressed(this.joy_num, this.cancel);
        case "menu":   return IsJoystickButtonPressed(this.joy_num, this.menu);
        case "jump": return IsJoystickButtonPressed(this.joy_num, this.jump);
        case "fly": return IsJoystickButtonPressed(this.joy_num, this.fly);
        
        case "up": return (GetJoystickAxis(this.joy_num, JOYSTICK_AXIS_Y) < -0.5);  //0.5 to prevent automatic movement from analog input.
        case "down": return (GetJoystickAxis(this.joy_num, JOYSTICK_AXIS_Y) > 0.5); //Now you're required to move the stick at least halfway.
        case "left": return (GetJoystickAxis(this.joy_num, JOYSTICK_AXIS_X) < -0.5);
        case "right": return (GetJoystickAxis(this.joy_num, JOYSTICK_AXIS_X) > 0.5);
        
        //Special cases
        case "any":  //Any button in the scheme at all?
          return this.pressed("action") || this.pressed("cancel") || this.pressed("menu") ||
                 this.pressed("up") || this.pressed("down") || this.pressed("left") || this.pressed("right");
        
        case "dir":  //Any directional button?
          return this.pressed("up") || this.pressed("down") || this.pressed("left") || this.pressed("right");
        
        default: Abort("ERR: Button does not exist: " + key);
      }
    }
  }
  
  
  /** Retreive what key was pressed, if it was within the spectrum of this input method. */
  this.getKey = function()
  {
    if (this.type == "keyboard")
    {
      if (AreKeysLeft())
      {
        var k = GetKey();
        switch(k)
        {
          case this.up: return "up";
          case this.down: return "down";
          case this.left: return "left";
          case this.right: return "right";
          case this.action: return "action";
          case this.cancel: return "cancel";
          case this.jump: return "jump";
          case this.menu: return "menu";
        }
      }
    }
    
    else if (this.type == "joystick")
    {
      //TODO.
    }
  }
}
