Example #1
0
        public Controller(int index)
        {
            inputStatesCurrent    = inputStatesPrev = new XInputState();
            timedVibrationEnabled = false;
            vibrationStopTime     = DateTime.Now;

            IsConnected = false;
            UserIndex   = index;
        }
Example #2
0
        public void Update()
        {
            XInputState newInputState = new XInputState();
            Errors      result        = (Errors)NativeMethods.GetState(UserIndex, ref newInputState);

            if (result == Errors.Success)
            {
                IsConnected        = true;
                inputStatesPrev    = inputStatesCurrent;
                inputStatesCurrent = newInputState;

                if ((inputStatesCurrent.Gamepad.sThumbLX < XInputGamepad.LeftThumbDeadzone && inputStatesCurrent.Gamepad.sThumbLX > -XInputGamepad.LeftThumbDeadzone) &&
                    (inputStatesCurrent.Gamepad.sThumbLY < XInputGamepad.LeftThumbDeadzone && inputStatesCurrent.Gamepad.sThumbLY > -XInputGamepad.LeftThumbDeadzone))
                {
                    inputStatesCurrent.Gamepad.sThumbLX = inputStatesCurrent.Gamepad.sThumbLY = 0;
                }

                if ((inputStatesCurrent.Gamepad.sThumbRX < XInputGamepad.RightThumbDeadzone && inputStatesCurrent.Gamepad.sThumbRX > -XInputGamepad.RightThumbDeadzone) &&
                    (inputStatesCurrent.Gamepad.sThumbRY < XInputGamepad.RightThumbDeadzone && inputStatesCurrent.Gamepad.sThumbRY > -XInputGamepad.RightThumbDeadzone))
                {
                    inputStatesCurrent.Gamepad.sThumbRX = inputStatesCurrent.Gamepad.sThumbRY = 0;
                }

                if (inputStatesCurrent.Gamepad.bLeftTrigger < XInputGamepad.TriggerThreshold)
                {
                    inputStatesCurrent.Gamepad.bLeftTrigger = 0;
                }
                if (inputStatesCurrent.Gamepad.bRightTrigger < XInputGamepad.TriggerThreshold)
                {
                    inputStatesCurrent.Gamepad.bRightTrigger = 0;
                }

                if (timedVibrationEnabled && DateTime.Now >= vibrationStopTime)
                {
                    timedVibrationEnabled = false;
                    Vibrate(0.0f, 0.0f);
                }
            }
            else if (result == Errors.DeviceNotConnected)
            {
                IsConnected = false;
            }
            else
            {
                throw new Exception(string.Format("Error code {0}", (int)result));
            }
        }
Example #3
0
 public static extern int GetState(int dwUserIndex, ref XInputState pState);