Exemple #1
0
    private void Update()
    {
        if (KeyboardMapping == null)
        {
            return;
        }

        if (!_inMapping)
        {
            float yAxis = Input.GetAxis("Vertical");

            if (_axisThresold > 0 && yAxis < _axisThresold)
            {
                _axisBackToZero = true;
            }
            else if (_axisThresold < 0 && yAxis > _axisThresold)
            {
                _axisBackToZero = true;
            }

            if (yAxis > 0.2f && _axisBackToZero)
            {
                _currentActive  = (_currentActive - 1) % KeyboardMapping.buttons.Length;
                _axisBackToZero = false;
                _axisThresold   = 0.2f;
            }
            else if (yAxis < -0.2f)
            {
                _currentActive  = (_currentActive + 1) % KeyboardMapping.buttons.Length;
                _axisBackToZero = false;
                _axisThresold   = -0.2f;
            }

            if (Input.GetKeyDown(keyboardActivatingKey))
            {
                _inMapping = true;
                _mapperEntries[_currentActive].keyboardKey.text = "Press Any";
                if (PadMapping != null)
                {
                    _mapperEntries[_currentActive].padKey.text = "Press Any";
                }
            }

            if (Input.GetKeyDown(keyboardCancelKey))
            {
                SceneManager.UnloadSceneAsync("inputMapper");
                Time.timeScale = 1.0f;
            }
        }
        else
        {
            if (Input.anyKeyDown)
            {
                bool keyValid = false;
                foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
                {
                    if (Input.GetKeyDown(key))
                    {
                        //TODO: handle multiple joystick by finding the number of the joystick
                        string name = key.ToString();
                        if (name.Contains("Joystick"))
                        {
                            if (PadMapping != null)
                            {
                                keyValid = true;
                                PadMapping.ChangeKeycode(PadMapping.buttons[_currentActive].name, key);
                                break;
                            }
                        }
                        else
                        {
                            keyValid = true;
                            KeyboardMapping.ChangeKeycode(KeyboardMapping.buttons[_currentActive].name, key);
                            break;
                        }
                    }
                }

                if (keyValid)
                {
                    _inMapping = false;
                    _mapperEntries[_currentActive].keyboardKey.text = KeyboardMapping.GetKeyCode(KeyboardMapping.buttons[_currentActive].name).ToString();
                    if (PadMapping != null)
                    {
                        _mapperEntries[_currentActive].padKey.text = PadMapping.GetKeyCode(PadMapping.buttons[_currentActive].name).ToString().Replace("Joystick", "");
                    }
                }
            }
        }
    }