//returns the custom names of the buttons
 public string GetButtonName(JoyCode j)
 {
     string name = _buttons.GetButtonName((JoyCode)j);
     return (name == "")? j.ToString() : name;
 }
 public bool GetButtonUp(JoyCode jb)
 {
     UpdateButtons();
     int val = Mathf.Abs((int)jb);
     return !_pressed[val] && _prevPressed[val];
 }
 public int GetKeyCodeOffset(JoyCode j)
 {
     return GetKeyCodeOffset(-(int)j);
 }
        /*** Get Buttons States ***/
        // returns if a button is held, released, or pressed that frame
        public bool GetButton(JoyCode jb)
        {
            UpdateButtons();
            int val = Mathf.Abs((int)jb);
            return _pressed[val];

            //if(_buttons.GetKeyCodeOffset(val) < 0) return false;
            //return Input.GetKey( KeyCode.JoystickButton0 + _buttons.GetKeyCodeOffset(val) + _player * 20);
        }
 //returns true if the joycode button is down on the controller
 public bool GetKeyUp(int player, JoyCode jc)
 {
     return GetKeyUp(player, (int)jc);
 }
 public string GetButtonName(JoyCode j)
 {
     return GetButtonName( -(int) j );
 }
    bool RemoveKey(Hashtable cfg, JoyCode jc, int jplayer, KeyConfig except)
    {
        ArrayList al = null;
        if( FindKey(cfg, jc, jplayer, out al) ){
            int ijc = (int)jc;

            foreach(KeyConfig e in al){
                if( e == except ) continue;

                e._joyKey = (ijc == e._joyKey && jplayer == e._joyKeyPlayer)? 0 : e._joyKey;
                e._negJoyKey = (ijc == e._negJoyKey && jplayer == e._negJoyKeyPlayer)? 0 : e._negJoyKey;
            }
        }
        return al.Count >= 1 && !(al.Count == 1 && al.Contains(except));
    }
 bool HasKey(KeyConfig config, JoyCode jc, int jplayer)
 {
     if( config == null ) return false;
     return (config._joyKey == (int)jc && config._joyKeyPlayer == jplayer) || (config._negJoyKey == (int)jc && config._negJoyKeyPlayer == jplayer);
 }
    //returns the appropriate name for the given string
    string GetControllerKeyName(JoyCode jc, int jplayer)
    {
        ControllerManager.JoyInterface ji = CMRef.GetController(jplayer);

        if(ji != null)
        {
            return "P" + jplayer + " " + ji.GetButtonName(jc);
        }
        else
        {
            return "P" + jplayer + " " + jc.ToString();
        }
    }
    bool FindKey(Hashtable cfg, JoyCode jc, int jplayer, out ArrayList keyConfigList)
    {
        bool found = false;
        keyConfigList = new ArrayList();

        foreach(IDictionaryEnumerator d in cfg){
            KeyConfig config = d.Value as KeyConfig;

            if( HasKey(config, jc, jplayer) ) keyConfigList.Add(config);
        }

        return found;
    }
    // edit the current key if a joystick key was pressed
    bool EditKey( KeyConfig config, EditStates es, JoyCode jc, int jplayer)
    {
        if( ( es == EditStates.VertNegKey || es == EditStates.VertPosKey ) && config._biDirLink == null) return false;

        KeyConfig HorizConfig;
        KeyConfig VertConfig;

        if( config._biDirLink == null || config.Direction == BiDir.Horizontal ){
            HorizConfig = config;
            VertConfig = config._biDirLink;
        }else{
            VertConfig = config;
            HorizConfig = config._biDirLink;
        }

        int ijc = (int) jc;

        //check for dupes in this joyconfig alone
        if(config.isAxis && config._biDirLink != null)
        {
            switch(es){
            case EditStates.VertNegKey:
                if(ijc == HorizConfig._joyKey && jplayer == HorizConfig._joyKeyPlayer) return false;
                goto case EditStates.PosKey;
            case EditStates.PosKey:
                if(ijc == VertConfig._joyKey && jplayer == VertConfig._joyKeyPlayer) return false;
                goto case EditStates.VertPosKey;
            case EditStates.VertPosKey:
                if(ijc == HorizConfig._negJoyKey && jplayer == HorizConfig._negJoyKeyPlayer) return false;
                goto case EditStates.NegKey;
            case EditStates.NegKey:
                break;
            }
        }
        else if(config.isAxis)
        {
            switch(es){
            case EditStates.PosKey:
                if(ijc == HorizConfig._negJoyKey && jplayer == HorizConfig._negJoyKeyPlayer) return false;
                goto case EditStates.NegKey;
            case EditStates.NegKey:
                break;
            }
        }

        // SET the key

        switch(es){
        case EditStates.PosKey:
            ClearKeys(HorizConfig, es, true);
            HorizConfig._joyKeyPlayer = jplayer;
            HorizConfig._joyKey = ijc;
            break;
        case EditStates.NegKey:
            ClearKeys(HorizConfig, es, true);
            HorizConfig._negJoyKeyPlayer = jplayer;
            HorizConfig._negJoyKey = ijc;
            break;
        case EditStates.VertPosKey:
            ClearKeys(VertConfig, es, true);
            VertConfig._joyKeyPlayer = jplayer;
            VertConfig._joyKey = ijc;
            break;
        case EditStates.VertNegKey:
            ClearKeys(VertConfig, es, true);
            VertConfig._negJoyKeyPlayer = jplayer;
            VertConfig._negJoyKey = ijc;
            break;
        }

        return true;
    }