private void CaptureXInput() { var controller = new SXI.Controller((SXI.UserIndex)_joyInfo.XInputDevice); var inputState = controller.GetState(); bool[] axisMoved = { false, false, false, false, false, false, false, false }; //AXIS axisMoved[0] = GetAxisMovement(JoystickState.Axis[0], -inputState.Gamepad.LeftThumbY); axisMoved[1] = GetAxisMovement(JoystickState.Axis[1], inputState.Gamepad.LeftThumbX); axisMoved[2] = GetAxisMovement(JoystickState.Axis[2], -inputState.Gamepad.RightThumbY); axisMoved[3] = GetAxisMovement(JoystickState.Axis[3], inputState.Gamepad.RightThumbX); axisMoved[4] = GetAxisMovement(JoystickState.Axis[4], inputState.Gamepad.LeftTrigger * 129 < Joystick.Max_Axis ? inputState.Gamepad.LeftTrigger * 129 : Joystick.Max_Axis); axisMoved[5] = GetAxisMovement(JoystickState.Axis[5], inputState.Gamepad.RightTrigger * 129 < Joystick.Max_Axis ? inputState.Gamepad.RightTrigger * 129 : Joystick.Max_Axis); //POV Pov.Position previousPov = JoystickState.Povs[0].Direction; Pov.Position pov = Pov.Position.Centered; if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadUp) != 0) { pov |= Pov.Position.North; } else if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadDown) != 0) { pov |= Pov.Position.South; } if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadLeft) != 0) { pov |= Pov.Position.West; } else if ((inputState.Gamepad.Buttons & SXI.GamepadButtonFlags.DPadRight) != 0) { pov |= Pov.Position.East; } JoystickState.Povs[0].Direction = pov; //BUTTONS // Skip the first 4 as they are the DPad. var previousButtons = JoystickState.Buttons; for (int i = 0; i < 12; i++) { if (((int)inputState.Gamepad.Buttons & (1 << (i + 4))) != 0) { JoystickState.Buttons |= 1 << (i + 4); } else { JoystickState.Buttons &= ~(1 << (i + 4)); } } //Send Events if (IsBuffered && EventListener != null) { var joystickEvent = new JoystickEventArgs(this, JoystickState); // Axes for (int index = 0; index < axisMoved.Length; index++) { if (axisMoved[index] == true && EventListener.AxisMoved(joystickEvent, index)) { return; } } //POV if (previousPov != pov && !EventListener.PovMoved(joystickEvent, 0)) { return; } //Buttons for (int i = 4; i < 16; i++) { if (((previousButtons & (1 << i)) == 0) && JoystickState.IsButtonDown(i)) { if (!EventListener.ButtonPressed(joystickEvent, i)) { return; } } else if (((previousButtons & (1 << i)) != 0) && !JoystickState.IsButtonDown(i)) { if (!EventListener.ButtonReleased(joystickEvent, i)) { return; } } } } }
public override void Capture() { if (_joyInfo.IsXInput) { CaptureXInput(); return; } MDI.JoystickUpdate[] bufferedData = null; bufferedData = this._joystick.GetBufferedData(); if (bufferedData == null) { this._joystick.Poll(); bufferedData = this._joystick.GetBufferedData(); if (bufferedData == null) { return; } } bool[] axisMoved = { false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false }; bool[] sliderMoved = { false, false, false, false }; foreach (var entry in bufferedData) { switch (entry.Offset) { /* sliders */ case MDI.JoystickOffset.Sliders1: sliderMoved[0] = true; JoystickState.Sliders[0].X = entry.Value; break; case MDI.JoystickOffset.Sliders0: sliderMoved[0] = true; JoystickState.Sliders[0].Y = entry.Value; break; case MDI.JoystickOffset.PointOfViewControllers0: if (ChangePOV(0, entry)) { return; } break; case MDI.JoystickOffset.PointOfViewControllers1: if (ChangePOV(1, entry)) { return; } break; case MDI.JoystickOffset.PointOfViewControllers2: if (ChangePOV(2, entry)) { return; } break; case MDI.JoystickOffset.PointOfViewControllers3: if (ChangePOV(3, entry)) { return; } break; case MDI.JoystickOffset.VelocitySliders0: sliderMoved[1] = true; JoystickState.Sliders[1].X = entry.Value; break; case MDI.JoystickOffset.VelocitySliders1: sliderMoved[1] = true; JoystickState.Sliders[1].X = entry.Value; break; case MDI.JoystickOffset.AccelerationSliders0: sliderMoved[0] = true; JoystickState.Sliders[2].X = entry.Value; break; case MDI.JoystickOffset.AccelerationSliders1: sliderMoved[0] = true; JoystickState.Sliders[2].X = entry.Value; break; case MDI.JoystickOffset.ForceSliders0: sliderMoved[0] = true; JoystickState.Sliders[3].X = entry.Value; break; case MDI.JoystickOffset.ForceSliders1: sliderMoved[0] = true; JoystickState.Sliders[3].X = entry.Value; break; default: if (entry.Offset >= MDI.JoystickOffset.Buttons0 && entry.Offset < MDI.JoystickOffset.Buttons127) { if (!DoButtonClick((int)entry.Offset - (int)MDI.JoystickOffset.Buttons0, entry)) { return; } } else { } break; } } //Check to see if any of the axes values have changed.. if so send events if ((IsBuffered == true) && (EventListener != null) && bufferedData.Length > 0) { JoystickEventArgs temp = new JoystickEventArgs(this, JoystickState); //Update axes for (int i = 0; i < axisMoved.Length; i++) { if (axisMoved[i]) { if (EventListener.AxisMoved(temp, i) == false) { return; } } } //Now update sliders for (int i = 0; i < 4; i++) { if (sliderMoved[i]) { if (EventListener.SliderMoved(temp, i) == false) { return; } } } } }