/**
         * This updates the state of the device and triggers events
         * to any registered listeners.
         **/
        public void Update()
        {
            try
            {
                XInputState gpState;
                XInputMethods.GetState(deviceID, out gpState);
                XInputGamepad gp = gpState.Gamepad;
               // If previously disconnected, notify of connect
                if (!isConnected && OnConnect != null)
                {
                    isConnected = true;
                    OnConnect(this, new GamepadEventArgs(deviceID, GamepadState.Connected));
                }
 
                if (isConnected)
                {
                    // A Button
                    // Check to see if anything changed
                    if (!(lastAButtonState == gp.IsAButtonDown))
                    {
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.A, gp.IsAButtonDown);

                        if (gp.IsAButtonDown) // If button changed to press state
                        {
                            if (OnAButtonPress != null) OnAButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnAButtonRelease != null) OnAButtonRelease(this, btnEvt);
                        }

                        lastAButtonState = gp.IsAButtonDown;
                    }

                    // B Button
                    // Check to see if anything changed
                    if (!(lastBButtonState == gp.IsBButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.B, gp.IsBButtonDown);

                        if (gp.IsBButtonDown) // If button changed to press state
                        {
                            if (OnBButtonPress != null) OnBButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnBButtonRelease != null) OnBButtonRelease(this, btnEvt);
                        }

                        lastBButtonState = gp.IsBButtonDown;
                    }

                    // X Button
                    // Check to see if anything changed
                    if (!(lastXButtonState == gp.IsXButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.X, gp.IsXButtonDown);

                        if (gp.IsXButtonDown) // If button changed to press state
                        {
                            if (OnXButtonPress != null) OnXButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnXButtonRelease != null) OnXButtonRelease(this, btnEvt);
                        }

                        lastXButtonState = gp.IsXButtonDown;
                    }

                    // Y Button
                    // Check to see if anything changed
                    if (!(lastYButtonState == gp.IsYButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Y, gp.IsYButtonDown);

                        if (gp.IsYButtonDown) // If button changed to press state
                        {
                            if (OnYButtonPress != null) OnYButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnYButtonRelease != null) OnYButtonRelease(this, btnEvt);
                        }

                        lastYButtonState = gp.IsYButtonDown;
                    }

                    // Back Button
                    // Check to see if anything changed
                    if (!(lastBackButtonState == gp.IsBackButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Back, gp.IsBackButtonDown);

                        if (gp.IsBackButtonDown) // If button changed to press state
                        {
                            if (OnBackButtonPress != null) OnBackButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnBackButtonRelease != null) OnBackButtonRelease(this, btnEvt);
                        }

                        lastBackButtonState = gp.IsBackButtonDown;
                    }

                    // Start Button
                    // Check to see if anything changed
                    if (!(lastStartButtonState == gp.IsStartButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Start, gp.IsStartButtonDown);

                        if (gp.IsStartButtonDown) // If button changed to press state
                        {
                            if (OnStartButtonPress != null) OnStartButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnStartButtonRelease != null) OnStartButtonRelease(this, btnEvt);
                        }

                        lastStartButtonState = gp.IsStartButtonDown;
                    }

                    // Left Thumb Button
                    // Check to see if anything changed
                    if (!(lastLeftThumbButtonState == gp.IsLeftThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftThumb, gp.IsLeftThumbButtonDown);

                        if (gp.IsLeftThumbButtonDown) // If button changed to press state
                        {
                            if (OnLeftThumbButtonPress != null) OnLeftThumbButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftThumbButtonRelease != null) OnLeftThumbButtonRelease(this, btnEvt);
                        }

                        lastLeftThumbButtonState = gp.IsLeftThumbButtonDown;
                    }

                    // Right Thumb Button
                    // Check to see if anything changed
                    if (!(lastRightThumbButtonState == gp.IsRightThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightThumb, gp.IsRightThumbButtonDown);

                        if (gp.IsRightThumbButtonDown) // If button changed to press state
                        {
                            if (OnRightThumbButtonPress != null) OnRightThumbButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnRightThumbButtonRelease != null) OnRightThumbButtonRelease(this, btnEvt);
                        }

                        lastRightThumbButtonState = gp.IsRightThumbButtonDown;
                    }

                    // Left Shoulder Button
                    // Check to see if anything changed
                    if (!(lastLeftShoulderButtonState == gp.IsLeftShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftShoulder, gp.IsLeftShoulderButtonDown);

                        if (gp.IsLeftShoulderButtonDown) // If button changed to press state
                        {
                            if (OnLeftShoulderButtonPress != null) OnLeftShoulderButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftShoulderButtonRelease != null) OnLeftShoulderButtonRelease(this, btnEvt);
                        }

                        lastLeftShoulderButtonState = gp.IsLeftShoulderButtonDown;
                    }

                    // Right Shoulder Button
                    // Check to see if anything changed
                    if (!(lastRightShoulderButtonState == gp.IsRightShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightShoulder, gp.IsRightShoulderButtonDown);

                        if (gp.IsRightShoulderButtonDown) // If button changed to press state
                        {
                            if (OnRightShoulderButtonPress != null) OnRightShoulderButtonPress(this, btnEvt);
                        }
                        else // If button changed to release state
                        {
                            if (OnRightShoulderButtonRelease != null) OnRightShoulderButtonRelease(this, btnEvt);
                        }

                        lastRightShoulderButtonState = gp.IsRightShoulderButtonDown;
                    }

                    // DPad
                    // Check to see if DPad state has changed.
                    GamepadButtons tmpButtons = (GamepadButtons)gp.Buttons;
                    GamepadButtons dpNewState = (GamepadButtons)((int)tmpButtons % 0x10); // Remove other button states
                    if (dpNewState != _lastDPadState) // A change has occured
                    {
                        GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);

                        if (dpNewState == 0) // DPad has been released
                        {
                            if (OnDPadRelease != null) 
                                if(!(_RightThumbMoving || _LeftThumbMoving))
                            {
                                OnDPadRelease(this, padEvt);
                            }
                        }
                        else if (_lastDPadState == 0) // DPad has been pressed
                        {
                            if (OnDPadPress != null)
                            {
                                if (!(_RightThumbMoving || _LeftThumbMoving))
                                {
                                    OnDPadPress(this, padEvt);
                                }
                            }
                        }

                        if (OnDPadChange != null) OnDPadChange(this, padEvt);

                        _lastDPadState = dpNewState;
                    }
                    else if ((_lastDPadState == GamepadButtons.DPadUp) || (_lastDPadState == GamepadButtons.DPadDown)) // If the button is pressed
                    {
                        if (!(_RightThumbMoving || _LeftThumbMoving))
                        {
                            GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);
                            OnDPadPress(this, padEvt);
                        }

                    }

                    // Left Trigger
                    // Check to see if left trigger has changed.
                    if (lastLeftTrigger != gp.LeftTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Left, gp.LeftTrigger);

                        if (!leftTriggerPressed) // Trigger was pressed
                        {
                            leftTriggerPressed = true;
                            if (OnLeftTriggerPress != null) OnLeftTriggerPress(this, trigEvt);
                        }
                        else if (gp.LeftTrigger == 0) // Trigger was released
                        {
                            if (OnLeftTriggerRelease != null) OnLeftTriggerRelease(this, trigEvt);
                            leftTriggerPressed = false;
                        }

                        if (OnLeftTriggerChange != null) OnLeftTriggerChange(this, trigEvt);

                        lastLeftTrigger = gp.LeftTrigger;
                    }

                    // Right Trigger
                    // Check to see if right trigger has changed.
                    if (lastRightTrigger != gp.RightTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Right, gp.RightTrigger);

                        if (!rightTriggerPressed) // Trigger was pressed
                        {
                            rightTriggerPressed = true;
                            if (OnRightTriggerPress != null) OnRightTriggerPress(this, trigEvt);
                        }
                        else if (gp.RightTrigger == 0) // Trigger was released
                        {
                            if (OnRightTriggerRelease != null) OnRightTriggerRelease(this, trigEvt);
                            rightTriggerPressed = false;
                        }

                        if (OnRightTriggerChange != null) OnRightTriggerChange(this, trigEvt);

                        lastRightTrigger = gp.RightTrigger;
                    }

                    // Left Thumb Joystick
                    GamepadThumbEventArgs joyEvt = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Left, gp.LeftThumbX, gp.LeftThumbY, gp.IsLeftThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.LeftThumb, gp.LeftThumbX, gp.LeftThumbY, 8000.0))
                    {
                        if (OnLeftThumbUpdate != null)
                        {
                            if (_RightThumbMoving)
                            {
                                _Move3Axis = true;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;
                            }

                            else
                            {
                                _Move3Axis = false;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;

                            }
                        
                        }

                    }
                    else
                    {
                        if (IsNoButtonHeldDown(gp))
                        {
                            if (OnThumbBackInsideRadius != null) OnThumbBackInsideRadius(this, joyEvt);
                            _LeftThumbMoving = false;
                        }
                    }

                    // Right Thumb Joystick
                    GamepadThumbEventArgs joyEvt1 = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Right, gp.RightThumbX, gp.RightThumbY, gp.IsRightThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.RightThumb, gp.RightThumbX, gp.RightThumbY, 8000.0))
                    {
                        if (OnRightThumbUpdate != null)
                        {
                            if (!gp.IsDPadUpButtonDown && !gp.IsDPadDownButtonDown && !_LeftThumbMoving)
                            {
                                _Move3Axis = false;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;
                            }
                            else
                            {

                                _Move3Axis = true;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;

                            }
                        }
                    }
                    else
                    {
                        if ( (IsNoButtonHeldDown(gp) && !_LeftThumbMoving) )
                        {
                            if (OnThumbBackInsideRadius != null) OnThumbBackInsideRadius(this, joyEvt1);
                            _RightThumbMoving = false;
                        }
                    }
                    // Update complete
                }
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == 1167) // This is a disconnect error
                {
                    if (OnDisconnect != null) OnDisconnect(this, new GamepadEventArgs(deviceID, GamepadState.Disconnected));
                    isConnected = false;
                }
                else
                {
                    throw e; // Unknown error, rethrow
                }
            }
        }
예제 #2
0
        /**
         * This updates the state of the device and triggers events
         * to any registered listeners.
         **/
        public void Update()
        {
            try
            {
                XInputState gpState;
                XInputMethods.GetState(deviceID, out gpState);
                XInputGamepad gp = gpState.Gamepad;
                // If previously disconnected, notify of connect
                if (!isConnected && OnConnect != null)
                {
                    isConnected = true;
                    OnConnect(this, new GamepadEventArgs(deviceID, GamepadState.Connected));
                }

                if (isConnected)
                {
                    // A Button
                    // Check to see if anything changed
                    if (!(lastAButtonState == gp.IsAButtonDown))
                    {
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.A, gp.IsAButtonDown);

                        if (gp.IsAButtonDown) // If button changed to press state
                        {
                            if (OnAButtonPress != null)
                            {
                                OnAButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnAButtonRelease != null)
                            {
                                OnAButtonRelease(this, btnEvt);
                            }
                        }

                        lastAButtonState = gp.IsAButtonDown;
                    }

                    // B Button
                    // Check to see if anything changed
                    if (!(lastBButtonState == gp.IsBButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.B, gp.IsBButtonDown);

                        if (gp.IsBButtonDown) // If button changed to press state
                        {
                            if (OnBButtonPress != null)
                            {
                                OnBButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnBButtonRelease != null)
                            {
                                OnBButtonRelease(this, btnEvt);
                            }
                        }

                        lastBButtonState = gp.IsBButtonDown;
                    }

                    // X Button
                    // Check to see if anything changed
                    if (!(lastXButtonState == gp.IsXButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.X, gp.IsXButtonDown);

                        if (gp.IsXButtonDown) // If button changed to press state
                        {
                            if (OnXButtonPress != null)
                            {
                                OnXButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnXButtonRelease != null)
                            {
                                OnXButtonRelease(this, btnEvt);
                            }
                        }

                        lastXButtonState = gp.IsXButtonDown;
                    }

                    // Y Button
                    // Check to see if anything changed
                    if (!(lastYButtonState == gp.IsYButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Y, gp.IsYButtonDown);

                        if (gp.IsYButtonDown) // If button changed to press state
                        {
                            if (OnYButtonPress != null)
                            {
                                OnYButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnYButtonRelease != null)
                            {
                                OnYButtonRelease(this, btnEvt);
                            }
                        }

                        lastYButtonState = gp.IsYButtonDown;
                    }

                    // Back Button
                    // Check to see if anything changed
                    if (!(lastBackButtonState == gp.IsBackButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Back, gp.IsBackButtonDown);

                        if (gp.IsBackButtonDown) // If button changed to press state
                        {
                            if (OnBackButtonPress != null)
                            {
                                OnBackButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnBackButtonRelease != null)
                            {
                                OnBackButtonRelease(this, btnEvt);
                            }
                        }

                        lastBackButtonState = gp.IsBackButtonDown;
                    }

                    // Start Button
                    // Check to see if anything changed
                    if (!(lastStartButtonState == gp.IsStartButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.Start, gp.IsStartButtonDown);

                        if (gp.IsStartButtonDown) // If button changed to press state
                        {
                            if (OnStartButtonPress != null)
                            {
                                OnStartButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnStartButtonRelease != null)
                            {
                                OnStartButtonRelease(this, btnEvt);
                            }
                        }

                        lastStartButtonState = gp.IsStartButtonDown;
                    }

                    // Left Thumb Button
                    // Check to see if anything changed
                    if (!(lastLeftThumbButtonState == gp.IsLeftThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftThumb, gp.IsLeftThumbButtonDown);

                        if (gp.IsLeftThumbButtonDown) // If button changed to press state
                        {
                            if (OnLeftThumbButtonPress != null)
                            {
                                OnLeftThumbButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftThumbButtonRelease != null)
                            {
                                OnLeftThumbButtonRelease(this, btnEvt);
                            }
                        }

                        lastLeftThumbButtonState = gp.IsLeftThumbButtonDown;
                    }

                    // Right Thumb Button
                    // Check to see if anything changed
                    if (!(lastRightThumbButtonState == gp.IsRightThumbButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightThumb, gp.IsRightThumbButtonDown);

                        if (gp.IsRightThumbButtonDown) // If button changed to press state
                        {
                            if (OnRightThumbButtonPress != null)
                            {
                                OnRightThumbButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnRightThumbButtonRelease != null)
                            {
                                OnRightThumbButtonRelease(this, btnEvt);
                            }
                        }

                        lastRightThumbButtonState = gp.IsRightThumbButtonDown;
                    }

                    // Left Shoulder Button
                    // Check to see if anything changed
                    if (!(lastLeftShoulderButtonState == gp.IsLeftShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.LeftShoulder, gp.IsLeftShoulderButtonDown);

                        if (gp.IsLeftShoulderButtonDown) // If button changed to press state
                        {
                            if (OnLeftShoulderButtonPress != null)
                            {
                                OnLeftShoulderButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnLeftShoulderButtonRelease != null)
                            {
                                OnLeftShoulderButtonRelease(this, btnEvt);
                            }
                        }

                        lastLeftShoulderButtonState = gp.IsLeftShoulderButtonDown;
                    }

                    // Right Shoulder Button
                    // Check to see if anything changed
                    if (!(lastRightShoulderButtonState == gp.IsRightShoulderButtonDown))
                    {
                        // Something has changed, lets dispatch events
                        GamepadButtonEventArgs btnEvt = new GamepadButtonEventArgs(deviceID, GamepadButtons.RightShoulder, gp.IsRightShoulderButtonDown);

                        if (gp.IsRightShoulderButtonDown) // If button changed to press state
                        {
                            if (OnRightShoulderButtonPress != null)
                            {
                                OnRightShoulderButtonPress(this, btnEvt);
                            }
                        }
                        else // If button changed to release state
                        {
                            if (OnRightShoulderButtonRelease != null)
                            {
                                OnRightShoulderButtonRelease(this, btnEvt);
                            }
                        }

                        lastRightShoulderButtonState = gp.IsRightShoulderButtonDown;
                    }

                    // DPad
                    // Check to see if DPad state has changed.
                    GamepadButtons tmpButtons = (GamepadButtons)gp.Buttons;
                    GamepadButtons dpNewState = (GamepadButtons)((int)tmpButtons % 0x10); // Remove other button states
                    if (dpNewState != _lastDPadState)                                     // A change has occured
                    {
                        GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);

                        if (dpNewState == 0) // DPad has been released
                        {
                            if (OnDPadRelease != null)
                            {
                                if (!(_RightThumbMoving || _LeftThumbMoving))
                                {
                                    OnDPadRelease(this, padEvt);
                                }
                            }
                        }
                        else if (_lastDPadState == 0) // DPad has been pressed
                        {
                            if (OnDPadPress != null)
                            {
                                if (!(_RightThumbMoving || _LeftThumbMoving))
                                {
                                    OnDPadPress(this, padEvt);
                                }
                            }
                        }

                        if (OnDPadChange != null)
                        {
                            OnDPadChange(this, padEvt);
                        }

                        _lastDPadState = dpNewState;
                    }
                    else if ((_lastDPadState == GamepadButtons.DPadUp) || (_lastDPadState == GamepadButtons.DPadDown)) // If the button is pressed
                    {
                        if (!(_RightThumbMoving || _LeftThumbMoving))
                        {
                            GamepadDPadEventArgs padEvt = new GamepadDPadEventArgs(deviceID, dpNewState);
                            OnDPadPress(this, padEvt);
                        }
                    }

                    // Left Trigger
                    // Check to see if left trigger has changed.
                    if (lastLeftTrigger != gp.LeftTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Left, gp.LeftTrigger);

                        if (!leftTriggerPressed) // Trigger was pressed
                        {
                            leftTriggerPressed = true;
                            if (OnLeftTriggerPress != null)
                            {
                                OnLeftTriggerPress(this, trigEvt);
                            }
                        }
                        else if (gp.LeftTrigger == 0) // Trigger was released
                        {
                            if (OnLeftTriggerRelease != null)
                            {
                                OnLeftTriggerRelease(this, trigEvt);
                            }
                            leftTriggerPressed = false;
                        }

                        if (OnLeftTriggerChange != null)
                        {
                            OnLeftTriggerChange(this, trigEvt);
                        }

                        lastLeftTrigger = gp.LeftTrigger;
                    }

                    // Right Trigger
                    // Check to see if right trigger has changed.
                    if (lastRightTrigger != gp.RightTrigger) // A change has occured
                    {
                        GamepadTriggerEventArgs trigEvt = new GamepadTriggerEventArgs(deviceID, GamepadTriggers.Right, gp.RightTrigger);

                        if (!rightTriggerPressed) // Trigger was pressed
                        {
                            rightTriggerPressed = true;
                            if (OnRightTriggerPress != null)
                            {
                                OnRightTriggerPress(this, trigEvt);
                            }
                        }
                        else if (gp.RightTrigger == 0) // Trigger was released
                        {
                            if (OnRightTriggerRelease != null)
                            {
                                OnRightTriggerRelease(this, trigEvt);
                            }
                            rightTriggerPressed = false;
                        }

                        if (OnRightTriggerChange != null)
                        {
                            OnRightTriggerChange(this, trigEvt);
                        }

                        lastRightTrigger = gp.RightTrigger;
                    }

                    // Left Thumb Joystick
                    GamepadThumbEventArgs joyEvt = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Left, gp.LeftThumbX, gp.LeftThumbY, gp.IsLeftThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.LeftThumb, gp.LeftThumbX, gp.LeftThumbY, 8000.0))
                    {
                        if (OnLeftThumbUpdate != null)
                        {
                            if (_RightThumbMoving)
                            {
                                _Move3Axis = true;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;
                            }

                            else
                            {
                                _Move3Axis = false;
                                OnLeftThumbUpdate(this, joyEvt);
                                _LeftThumbMoving = true;
                            }
                        }
                    }
                    else
                    {
                        if (IsNoButtonHeldDown(gp))
                        {
                            if (OnThumbBackInsideRadius != null)
                            {
                                OnThumbBackInsideRadius(this, joyEvt);
                            }
                            _LeftThumbMoving = false;
                        }
                    }

                    // Right Thumb Joystick
                    GamepadThumbEventArgs joyEvt1 = new GamepadThumbEventArgs(deviceID, GamepadThumbs.Right, gp.RightThumbX, gp.RightThumbY, gp.IsRightThumbButtonDown);
                    if (!isThumbPostionInsideRadius(GamepadButtons.RightThumb, gp.RightThumbX, gp.RightThumbY, 8000.0))
                    {
                        if (OnRightThumbUpdate != null)
                        {
                            if (!gp.IsDPadUpButtonDown && !gp.IsDPadDownButtonDown && !_LeftThumbMoving)
                            {
                                _Move3Axis = false;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;
                            }
                            else
                            {
                                _Move3Axis = true;
                                OnRightThumbUpdate(this, joyEvt1);
                                _RightThumbMoving = true;
                            }
                        }
                    }
                    else
                    {
                        if ((IsNoButtonHeldDown(gp) && !_LeftThumbMoving))
                        {
                            if (OnThumbBackInsideRadius != null)
                            {
                                OnThumbBackInsideRadius(this, joyEvt1);
                            }
                            _RightThumbMoving = false;
                        }
                    }
                    // Update complete
                }
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == 1167) // This is a disconnect error
                {
                    if (OnDisconnect != null)
                    {
                        OnDisconnect(this, new GamepadEventArgs(deviceID, GamepadState.Disconnected));
                    }
                    isConnected = false;
                }
                else
                {
                    throw e; // Unknown error, rethrow
                }
            }
        }
        private void OnThumbBackInsideRadius(object sender, GamepadThumbEventArgs evt)
        {
            if (evt.Thumb == GamepadThumbs.Right)
            {
                myStage.Stop();
                Text = "(" + evt.XPosition + "," + evt.YPosition + ")";

            }
        }
        private void OnRightThumbUpdate(object sender, GamepadThumbEventArgs evt)
        {
            if (evt.Thumb == GamepadThumbs.Right)
            {
                double x, y, z;
                double rightThumbCurrentRadius = 0.0;
                double rightThumbRadius = 0.0;
                double rightThumbX = 0.0;
                double rightThumbY = 0.0;
                double deltaRadius = 0.0, deltax = 0.0, deltay = 0.0, deltaz=0.05;;
                double magnitude = 0.0;
                double deltapos = 0.05;
                gamepad.GetThumbRadius(GamepadButtons.RightThumb, ref rightThumbRadius);
                gamepad.GetThumbXY(GamepadButtons.RightThumb, ref rightThumbX, ref rightThumbY);
                Gamepad gp = (Gamepad)sender;
                if (gp._ZdirectionDown)
                {
                    deltaz = -deltaz;
                }
                rightThumbCurrentRadius = gamepad.CalculateMagnitude((double)evt.XPosition, (double)evt.YPosition);

                deltaRadius = gamepad.CalculatePosDifference(rightThumbRadius, rightThumbCurrentRadius, 4000.0);

                if (deltaRadius <= 0.0)
                {
                    return;
                }

                deltax = gamepad.CalculatePosDifference((double)evt.XPosition, rightThumbX, 4000);
                deltay = gamepad.CalculatePosDifference((double)evt.YPosition, rightThumbX, 4000);


                magnitude = gamepad.CalculateMagnitude(deltax, deltay);

                deltax = deltax / magnitude;
                deltay = deltay / magnitude;

                if (Math.Abs(deltax) >= 0.9)
                {
                    deltay = 0.0;
                }

                if (Math.Abs(deltay) >= 0.9)
                {
                    deltax = 0.0;
                }

                deltax = deltax * deltapos;
                deltay = deltay * deltapos;
                myStage.GetPos(out x, out y, out z);
                if (gp._Move3Axis)
                {
                    myStage.MoveTo(x + deltax, y + deltay, z + deltaz, false);
                }
                else
                {
                    myStage.MoveTo(x + deltax, y + deltay, z, false);
                }
                Text = "(" + evt.XPosition + "," + evt.YPosition + ")";
            }
        }
        private void OnLeftThumbUpdate(object sender, GamepadThumbEventArgs evt)
        {
            if (evt.Thumb == GamepadThumbs.Left)
            {
                double x, y, z;
                double leftThumbCurrentRadius = 0.0;
                double leftThumbRadius = 0.0;
                double leftThumbX = 0.0;
                double leftThumbY = 0.0;
                double deltaRadius = 0.0, deltax = 0.0, deltay = 0.0, deltaz=0.02;
                double magnitude = 0.0;
                double deltapos = 0.2;
                Gamepad gp = (Gamepad)sender;

                gamepad.GetThumbRadius(GamepadButtons.LeftThumb, ref leftThumbRadius);
                gamepad.GetThumbXY(GamepadButtons.LeftThumb, ref leftThumbX, ref leftThumbY);

                leftThumbCurrentRadius = gamepad.CalculateMagnitude((double)evt.XPosition, (double)evt.YPosition);

                deltaRadius = gamepad.CalculatePosDifference(leftThumbRadius, leftThumbCurrentRadius, 5000);

                if (deltaRadius <= 0.0)
                {
                    return;
                }

                deltax = gamepad.CalculatePosDifference((double)evt.XPosition, leftThumbX, 5000);
                deltay = gamepad.CalculatePosDifference((double)evt.YPosition, leftThumbX, 5000);
                magnitude = gamepad.CalculateMagnitude(deltax, deltay);

                deltax = deltax / magnitude;
                deltay = deltay / magnitude;

                if (Math.Abs(deltax) > 0.0)
                {
                    return;
                }

                deltax = deltax * deltapos;
                deltay = deltay * deltapos;
                
                if (Math.Abs(deltax) >= 0.5)
                {
                    deltay = 0.0;
                }

                if (Math.Abs(deltay) >= 0.5)
                {
                    deltax = 0.0;
                }
                
                if (deltay > 0)
                {
                    deltaz = -0.05;
                    gp._ZdirectionDown = true;
                }

                if (deltay < 0)
                {
                    deltaz = 0.05;
                    gp._ZdirectionDown = false;
                }

                myStage.GetPos(out x, out y, out z);
                if (!gp._Move3Axis)
                {
                    myStage.MoveTo(x, y, z + deltaz, false);
                }
                Text = "(" + evt.XPosition + "," + evt.YPosition + ")";

            }
        }