private void GetThumbXY(GamepadButtons buttonType, int deviceID, ref double ThumbX, ref double ThumbY) { XInputState gpState; XInputMethods.GetState(deviceID, out gpState); XInputGamepad gp = gpState.Gamepad; if (buttonType == GamepadButtons.RightThumb) { ThumbX = (double)gp.RightThumbX; ThumbY = (double)gp.RightThumbY; } if (buttonType == GamepadButtons.LeftThumb) { ThumbX = (double)gp.LeftThumbX; ThumbY = (double)gp.LeftThumbY; } }
/** * 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 } } }