Esempio n. 1
0
        public void Update()
        {
            _controllers.NumControllers = 0;
            if (!_controllers.Supported)
            {
                return;
            }

            for (int i = 0; i < _controllers.controllers.Length; i++)
            {
                if (!_controllers.controllers[i].IsConnected)
                {
                    _controllers.states[i] = default(Gamepad);
                    continue;
                }

                _controllers.NumControllers++;

                State s = _controllers.controllers[i].GetState();
                if (s.PacketNumber == _controllers.stateNumbers[i])
                {
                    continue;
                }
                _controllers.stateNumbers[i] = s.PacketNumber;

                Gamepad tempState = s.Gamepad;

                if ((tempState.Buttons & GamepadButtonFlags.A) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.A) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.A));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.A) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.A) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.A));
                }
                if ((tempState.Buttons & GamepadButtonFlags.B) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.B) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.B));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.B) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.B) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.B));
                }
                if ((tempState.Buttons & GamepadButtonFlags.Y) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.Y) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Y));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.Y) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.Y) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Y));
                }
                if ((tempState.Buttons & GamepadButtonFlags.X) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.X) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.X));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.X) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.X) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.X));
                }

                if ((tempState.Buttons & GamepadButtonFlags.LeftShoulder) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.LeftShoulder) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.LeftBumper));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.LeftShoulder) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.LeftShoulder) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.LeftBumper));
                }
                if ((tempState.Buttons & GamepadButtonFlags.RightShoulder) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.RightShoulder) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.RightBumper));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.RightShoulder) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.RightShoulder) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.RightBumper));
                }

                if ((tempState.Buttons & GamepadButtonFlags.LeftThumb) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.LeftThumb) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(this, new ButtonEventArgs(XboxButtonCode.LeftStick));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.LeftThumb) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.LeftThumb) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.LeftStick));
                }
                if ((tempState.Buttons & GamepadButtonFlags.RightThumb) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.RightThumb) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.RightStick));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.RightThumb) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.RightThumb) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.RightStick));
                }

                if ((tempState.Buttons & GamepadButtonFlags.Start) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.Start) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Start));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.Start) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.Start) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Start));
                }
                if ((tempState.Buttons & GamepadButtonFlags.Back) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.Back) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Back));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.Back) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.Back) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Back));
                }

                if ((tempState.Buttons & GamepadButtonFlags.DPadUp) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadUp) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Up));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.DPadUp) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadUp) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Up));
                }
                if ((tempState.Buttons & GamepadButtonFlags.DPadDown) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadDown) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Down));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.DPadDown) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadDown) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Down));
                }
                if ((tempState.Buttons & GamepadButtonFlags.DPadLeft) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadLeft) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Left));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.DPadLeft) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadLeft) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Left));
                }
                if ((tempState.Buttons & GamepadButtonFlags.DPadRight) != GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadRight) == GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonDown?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Right));
                }
                else if ((tempState.Buttons & GamepadButtonFlags.DPadRight) == GamepadButtonFlags.None && (_controllers.states[i].Buttons & GamepadButtonFlags.DPadRight) != GamepadButtonFlags.None)
                {
                    _controllers.CurrentlyUsingController = true;
                    ButtonUp?.Invoke(_focusedWindow, new ButtonEventArgs(XboxButtonCode.Right));
                }

                short dzPos = (short)(_controllers.StickDeadZone * 32767.0d);
                short dzNeg = (short)(_controllers.StickDeadZone * -32768.0d);

                if (tempState.LeftThumbY >= dzPos && tempState.LeftThumbX < maxAnglePos && tempState.LeftThumbX > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftN, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbX >= dzPos && tempState.LeftThumbY < maxAnglePos && tempState.LeftThumbY > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftE, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbY <= dzNeg && tempState.LeftThumbX < maxAnglePos && tempState.LeftThumbX > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftS, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbX <= dzNeg && tempState.LeftThumbY < maxAnglePos && tempState.LeftThumbY > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftW, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbY >= dzPos && tempState.LeftThumbX >= maxAnglePos)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftNE, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbY >= dzPos && tempState.LeftThumbX <= maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftNW, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbY <= dzNeg && tempState.LeftThumbX >= maxAnglePos)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftSE, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                else if (tempState.LeftThumbY <= dzNeg && tempState.LeftThumbX <= maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.LeftSW, new PrecisePoint((tempState.LeftThumbX < 0) ? tempState.LeftThumbX / 32768.0d : tempState.LeftThumbX / 32767.0d, (tempState.LeftThumbY < 0) ? tempState.LeftThumbY / 32768.0d : tempState.LeftThumbY / 32767.0d)));
                }
                if (tempState.RightThumbY >= dzPos && tempState.RightThumbX < maxAnglePos && tempState.RightThumbX > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightN, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbX >= dzPos && tempState.RightThumbY < maxAnglePos && tempState.RightThumbY > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightE, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbY <= dzNeg && tempState.RightThumbX < maxAnglePos && tempState.RightThumbX > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightS, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbX <= dzNeg && tempState.RightThumbY < maxAnglePos && tempState.RightThumbY > maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightW, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbY >= dzPos && tempState.RightThumbX >= maxAnglePos)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightNE, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbY >= dzPos && tempState.RightThumbX <= maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightNW, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbY <= dzNeg && tempState.RightThumbX >= maxAnglePos)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightSE, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }
                else if (tempState.RightThumbY <= dzNeg && tempState.RightThumbX <= maxAngleNeg)
                {
                    _controllers.CurrentlyUsingController = true;
                    StickMoved?.Invoke(_focusedWindow, new StickEventArgs(XboxStickCode.RightSW, new PrecisePoint((tempState.RightThumbX < 0) ? tempState.RightThumbX / 32768.0d : tempState.RightThumbX / 32767.0d, (tempState.RightThumbY < 0) ? tempState.RightThumbY / 32768.0d : tempState.RightThumbY / 32767.0d)));
                }

                byte dzTrig = (byte)(_controllers.TriggerDeadZone * 255.0d);

                if (tempState.LeftTrigger >= dzTrig)
                {
                    _controllers.CurrentlyUsingController = true;
                    TriggerPressed?.Invoke(_focusedWindow, new TriggerEventArgs(XboxTriggerCode.Left, tempState.LeftTrigger / 255.0d));
                }
                if (tempState.RightTrigger >= dzTrig)
                {
                    _controllers.CurrentlyUsingController = true;
                    TriggerPressed?.Invoke(_focusedWindow, new TriggerEventArgs(XboxTriggerCode.Right, tempState.RightTrigger / 255.0d));
                }

                _controllers.states[i] = tempState;
            }
        }
Esempio n. 2
0
        private void UpdateInternal(Window window)
        {
            int numControllers = 0;

            for (int i = 0; i < controllers.Length; i++)
            {
                if (!controllers[i].IsConnected)
                {
                    states[i] = default(Gamepad);
                    continue;
                }
                numControllers++;

                SharpDX.XInput.State state = controllers[i].GetState();
                if (state.PacketNumber == packetNumbers[i])
                {
                    return;
                }
                Interlocked.Exchange(ref packetNumbers[i], state.PacketNumber);

                Gamepad pad = state.Gamepad;

                if ((pad.Buttons & GamepadButtonFlags.A) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.A) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.A));
                }
                else if ((pad.Buttons & GamepadButtonFlags.A) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.A) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.A));
                }
                if ((pad.Buttons & GamepadButtonFlags.B) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.B) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.B));
                }
                else if ((pad.Buttons & GamepadButtonFlags.B) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.B) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.B));
                }
                if ((pad.Buttons & GamepadButtonFlags.Y) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.Y) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Y));
                }
                else if ((pad.Buttons & GamepadButtonFlags.Y) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.Y) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Y));
                }
                if ((pad.Buttons & GamepadButtonFlags.X) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.X) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.X));
                }
                else if ((pad.Buttons & GamepadButtonFlags.X) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.X) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.X));
                }

                if ((pad.Buttons & GamepadButtonFlags.LeftShoulder) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.LeftShoulder) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.LeftBumper));
                }
                else if ((pad.Buttons & GamepadButtonFlags.LeftShoulder) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.LeftShoulder) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.LeftBumper));
                }
                if ((pad.Buttons & GamepadButtonFlags.RightShoulder) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.RightShoulder) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.RightBumper));
                }
                else if ((pad.Buttons & GamepadButtonFlags.RightShoulder) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.RightShoulder) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.RightBumper));
                }

                if ((pad.Buttons & GamepadButtonFlags.LeftThumb) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.LeftThumb) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(this, new ButtonEventArgs(i, XboxButtonCode.LeftStick));
                }
                else if ((pad.Buttons & GamepadButtonFlags.LeftThumb) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.LeftThumb) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.LeftStick));
                }
                if ((pad.Buttons & GamepadButtonFlags.RightThumb) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.RightThumb) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.RightStick));
                }
                else if ((pad.Buttons & GamepadButtonFlags.RightThumb) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.RightThumb) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.RightStick));
                }

                if ((pad.Buttons & GamepadButtonFlags.Start) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.Start) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Start));
                }
                else if ((pad.Buttons & GamepadButtonFlags.Start) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.Start) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Start));
                }
                if ((pad.Buttons & GamepadButtonFlags.Back) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.Back) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Back));
                }
                else if ((pad.Buttons & GamepadButtonFlags.Back) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.Back) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Back));
                }

                if ((pad.Buttons & GamepadButtonFlags.DPadUp) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadUp) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Up));
                }
                else if ((pad.Buttons & GamepadButtonFlags.DPadUp) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadUp) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Up));
                }
                if ((pad.Buttons & GamepadButtonFlags.DPadDown) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadDown) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Down));
                }
                else if ((pad.Buttons & GamepadButtonFlags.DPadDown) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadDown) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Down));
                }
                if ((pad.Buttons & GamepadButtonFlags.DPadLeft) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadLeft) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Left));
                }
                else if ((pad.Buttons & GamepadButtonFlags.DPadLeft) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadLeft) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Left));
                }
                if ((pad.Buttons & GamepadButtonFlags.DPadRight) != GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadRight) == GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonDown?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Right));
                }
                else if ((pad.Buttons & GamepadButtonFlags.DPadRight) == GamepadButtonFlags.None && (states[i].Buttons & GamepadButtonFlags.DPadRight) != GamepadButtonFlags.None)
                {
                    usingController.Value = true;
                    ButtonUp?.Invoke(window, new ButtonEventArgs(i, XboxButtonCode.Right));
                }

                if (pad.LeftThumbY >= dzPos && pad.LeftThumbX < maxAnglePos && pad.LeftThumbX > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.North, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbX >= dzPos && pad.LeftThumbY < maxAnglePos && pad.LeftThumbY > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.East, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbY <= dzNeg && pad.LeftThumbX < maxAnglePos && pad.LeftThumbX > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.South, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbX <= dzNeg && pad.LeftThumbY < maxAnglePos && pad.LeftThumbY > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.West, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbY >= dzPos && pad.LeftThumbX >= maxAnglePos)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.NorthEast, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbY >= dzPos && pad.LeftThumbX <= maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.NorthWest, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbY <= dzNeg && pad.LeftThumbX >= maxAnglePos)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.SouthEast, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                else if (pad.LeftThumbY <= dzNeg && pad.LeftThumbX <= maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Left, XboxStickDirection.SouthWest, (pad.LeftThumbX < 0) ? pad.LeftThumbX / 32768.0d : pad.LeftThumbX / 32767.0d, (pad.LeftThumbY < 0) ? pad.LeftThumbY / 32768.0d : pad.LeftThumbY / 32767.0d));
                }
                if (pad.RightThumbY >= dzPos && pad.RightThumbX < maxAnglePos && pad.RightThumbX > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.North, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbX >= dzPos && pad.RightThumbY < maxAnglePos && pad.RightThumbY > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.East, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbY <= dzNeg && pad.RightThumbX < maxAnglePos && pad.RightThumbX > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.South, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbX <= dzNeg && pad.RightThumbY < maxAnglePos && pad.RightThumbY > maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.West, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbY >= dzPos && pad.RightThumbX >= maxAnglePos)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.NorthEast, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbY >= dzPos && pad.RightThumbX <= maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.NorthWest, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbY <= dzNeg && pad.RightThumbX >= maxAnglePos)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.SouthEast, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }
                else if (pad.RightThumbY <= dzNeg && pad.RightThumbX <= maxAngleNeg)
                {
                    usingController.Value = true;
                    StickMoved?.Invoke(window, new StickEventArgs(i, XboxSide.Right, XboxStickDirection.SouthWest, (pad.RightThumbX < 0) ? pad.RightThumbX / 32768.0d : pad.RightThumbX / 32767.0d, (pad.RightThumbY < 0) ? pad.RightThumbY / 32768.0d : pad.RightThumbY / 32767.0d));
                }

                if (pad.LeftTrigger >= dzTrig)
                {
                    usingController.Value = true;
                    TriggerPressed?.Invoke(window, new TriggerEventArgs(i, XboxSide.Left, pad.LeftTrigger / 255.0d));
                }
                if (pad.RightTrigger >= dzTrig)
                {
                    usingController.Value = true;
                    TriggerPressed?.Invoke(window, new TriggerEventArgs(i, XboxSide.Right, pad.RightTrigger / 255.0d));
                }

                states[i] = pad;
            }
            Interlocked.Exchange(ref connectedControllers, numControllers);
        }