Ejemplo n.º 1
0
        /// <summary>
        /// Observes the gamepad for status changes.
        /// </summary>
        private void ObserveGamepad()
        {
            uint lastPacketNumber = 0;

            XInput.State     state     = new XInput.State();
            XInput.KeyStroke keystroke = new XInput.KeyStroke();
            while (!this.StopObserving)
            {
                XInput.Error result = XInput.GetState((XInput.UserIndex) this.UserIndex, out state);
                if (result == XInput.Error.Success && state.packetNumber > lastPacketNumber)
                {
                    lastPacketNumber = state.packetNumber;
                    this.SetStateFromXInputState(state);
                    this.RaiseStateChanged();
                }

                result = XInput.GetKeystroke((XInput.UserIndex) this.UserIndex, out keystroke);
                if (result == XInput.Error.Success)
                {
                    if (keystroke.flags == XInput.KeyStrokeFlags.KeyUp)
                    {
                        this.RaiseKeyUp((KeyEventArgs.KeyCode)keystroke.virtualKey);
                    }

                    if (keystroke.flags == XInput.KeyStrokeFlags.KeyDown)
                    {
                        this.RaiseKeyDown((KeyEventArgs.KeyCode)keystroke.virtualKey);
                    }
                }

                Thread.Sleep(2);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new <see cref="Gamepad"/> instance from an xinput state structure.
        /// </summary>
        /// <param name="index">The controller number. Can be in the range of 0 to 3.</param>
        /// <param name="state">The current state of the gamepad.</param>
        private Gamepad(XInput.State state, XInput.UserIndex index)
        {
            this.LeftThumbDeadzone     = XInput.Gamepad.LeftThumbDeadzone / (double)thumbMaxValue;
            this.RightThumbDeadzone    = XInput.Gamepad.RightThumbDeadzone / (double)thumbMaxValue;
            this.LeftTriggerThreshold  = XInput.Gamepad.TriggerThreshold / (double)triggerMaxValue;
            this.RightTriggerThreshold = XInput.Gamepad.TriggerThreshold / (double)triggerMaxValue;

            this.UserIndex = (int)index;
            this.StartObserverThread();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Collects all the connected xinput gamepads.
        /// </summary>
        /// <returns>A list of all connected devices.</returns>
        public static List <Gamepad> GetConnectedDevices()
        {
            List <Gamepad> gamepads = new List <Gamepad>();

            for (XInput.UserIndex userIndex = 0; userIndex < XInput.UserIndex.MaxCount; userIndex++)
            {
                XInput.State state  = new XInput.State();
                XInput.Error result = XInput.GetState(userIndex, out state);
                if (result == XInput.Error.Success)
                {
                    gamepads.Add(new Gamepad(state, userIndex));
                }
            }

            return(gamepads);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Sets the state of the <see cref="Gamepad"/> instance from an XInput.State object.
 /// </summary>
 /// <param name="state">The raw input state.</param>
 private void SetStateFromXInputState(XInput.State state)
 {
     this.A                      = state.gamepad.isButtonPressed(XInput.GamepadButtons.A);
     this.B                      = state.gamepad.isButtonPressed(XInput.GamepadButtons.B);
     this.X                      = state.gamepad.isButtonPressed(XInput.GamepadButtons.X);
     this.Y                      = state.gamepad.isButtonPressed(XInput.GamepadButtons.Y);
     this.L                      = state.gamepad.isButtonPressed(XInput.GamepadButtons.LeftShoulder);
     this.R                      = state.gamepad.isButtonPressed(XInput.GamepadButtons.RightShoulder);
     this.Start                  = state.gamepad.isButtonPressed(XInput.GamepadButtons.Start);
     this.Back                   = state.gamepad.isButtonPressed(XInput.GamepadButtons.Back);
     this.LeftThumbPress         = state.gamepad.isButtonPressed(XInput.GamepadButtons.LeftThumb);
     this.RightThumbPress        = state.gamepad.isButtonPressed(XInput.GamepadButtons.RightThumb);
     this.DPadUp                 = state.gamepad.isButtonPressed(XInput.GamepadButtons.DPadUp);
     this.DPadDown               = state.gamepad.isButtonPressed(XInput.GamepadButtons.DPadDown);
     this.DPadLeft               = state.gamepad.isButtonPressed(XInput.GamepadButtons.DPadLeft);
     this.DPadRight              = state.gamepad.isButtonPressed(XInput.GamepadButtons.DPadRight);
     this.LeftThumbXUnfiltered   = state.gamepad.leftThumbX / (double)thumbMaxValue;
     this.LeftThumbYUnfiltered   = state.gamepad.leftThumbY / (double)thumbMaxValue;
     this.RightThumbXUnfiltered  = state.gamepad.rightThumbX / (double)thumbMaxValue;
     this.RightThumbYUnfiltered  = state.gamepad.rightThumbY / (double)thumbMaxValue;
     this.LeftTriggerUnfiltered  = state.gamepad.leftTrigger / (double)triggerMaxValue;
     this.RightTriggerUnfiltered = state.gamepad.rightTrigger / (double)triggerMaxValue;
 }