Ejemplo n.º 1
0
        private void Process()
        {
            while (!_closed)
            {
                _permit.Wait();

                if (!_gamepad.IsConnected())
                {
                    Thread.Sleep(500);
                    continue;
                }
                // update battery info
                GamePadBatteryLevel currentBatteryLevel = _gamepad.GetBatteryInfo();
                if (_batteryLevel != currentBatteryLevel)
                {
                    _batteryLevel = currentBatteryLevel;
                    OnBatteryLevelChanged?.Invoke(_batteryLevel);
                }

                int dwResult = XInputWrapper.XInputGetState(_gamepad.GetNumber(), ref _state);
                if (dwResult == XInputWrapper.ERROR_SUCCESS)
                {
                    // send input
                    SetInput(_state, InputType.Clicked, Controller.GetProfile().RepeatMode);
                }
                Thread.Sleep(_delay);
            }
        }
Ejemplo n.º 2
0
        public bool IsConnected()
        {
            int dwResult;

            for (int i = 0; i < XInputWrapper.XUSER_MAX_COUNT; i++)
            {
                dwResult = XInputWrapper.XInputGetState(i, ref _state);

                if (dwResult == XInputWrapper.ERROR_SUCCESS)
                {
                    _gamepadNumber = i;

                    return(true);
                }
            }
            _gamepadNumber = -1;

            return(false);
        }
Ejemplo n.º 3
0
        private void SetInput(XInputState st, InputType itype, RepeatType rmode)
        {
            bool             isOutDeadzone = false;
            GamePadEventType currentStick  = GamePadEventType.LeftThumbStick;

            //Left Thumb Stick
            float LX = st.Gamepad.SThumbLX;
            float LY = st.Gamepad.SThumbLY;

            float magnitudeL = (float)Math.Sqrt(LX * LX + LY * LY);

            float normalizedLX = LX / magnitudeL;
            float normalizedLY = LY / magnitudeL;

            if (magnitudeL > (float)XInputWrapper.GP.XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
            {
                isOutDeadzone = true;
                currentStick  = GamePadEventType.LeftThumbStick;
            }

            //Right Thumb Stick
            float RX = st.Gamepad.SThumbRX;
            float RY = st.Gamepad.SThumbRY;

            float magnitudeR = (float)Math.Sqrt(RX * RX + RY * RY);

            float normalizedRX = RX / magnitudeR;
            float normalizedRY = RY / magnitudeR;

            if (magnitudeR > (float)XInputWrapper.GP.XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
            {
                isOutDeadzone = true;
                currentStick  = GamePadEventType.RightThumbStick;
            }

            //lock
            if ((st.Gamepad.WButtons == 48) && isOutDeadzone == false)
            {
                _isStandby = !_isStandby;
                Thread.Sleep(500);
                _currentPacketNumber = st.DwPacketNumber;
            }
            if (_isStandby)
            {
                return;
            }

            if (isOutDeadzone)
            {
                ExecuteAction(st, currentStick, itype);
            }

            //EMULATE BUTTONS
            if (rmode == RepeatType.NoRepeats && _currentPacketNumber == st.DwPacketNumber)
            {
                return;
            }

            //Buttons
            if (st.Gamepad.WButtons != 0)
            {
                ExecuteAction(st, GamePadEventType.Button, itype);
            }

            //Left trigger
            if (st.Gamepad.BLeftTrigger > 254)
            {
                ExecuteAction(st, GamePadEventType.LeftTrigger, itype);
            }

            //Right trigger
            if (st.Gamepad.BRightTrigger > 254)
            {
                ExecuteAction(st, GamePadEventType.RightTrigger, itype);
            }

            st.ZeroMemory();
            XInputWrapper.XInputGetState(_gamepad.GetNumber(), ref st);
            _currentPacketNumber = st.DwPacketNumber;
        }