/// <summary> /// Gets values of specified thumbstick like X and Y axis etc /// </summary> /// <param name="stick">Stick to get values</param> /// <param name="gamepadNumber">Gamepad number</param> /// <returns></returns> public static GamepadStickState GetStickValue(GamepadSticks stick, GamepadNumbers gamepadNumber) { if (GamepadInstance.IsDeviceConnected) { if (GamepadInstance.IsSupported) { XInput.State state = GetState(gamepadNumber); GamepadStickState gamepadStick = GamepadStickState.Empty; switch (stick) { case GamepadSticks.LeftStick: gamepadStick.IsDown = IsButtonDown(GamepadButtons.LeftStick); gamepadStick.X = state.Gamepad.LeftThumbX; gamepadStick.Y = state.Gamepad.LeftThumbY; break; case GamepadSticks.RightStick: gamepadStick.IsDown = IsButtonDown(GamepadButtons.LeftStick); gamepadStick.X = state.Gamepad.LeftThumbX; gamepadStick.Y = state.Gamepad.LeftThumbY; break; } return(gamepadStick); } else { return(GamepadStickState.Empty); } } else { return(GamepadStickState.Empty); } }
// constructor internal Controllers(AtomicBoolean usingController) : base(usingController) { dzPos = (short)(stickDeadzone * 32767.0d); dzNeg = (short)(stickDeadzone * -32768.0d); dzTrig = (byte)(triggerDeadzone * 255.0d); controllers[0] = new Controller(UserIndex.One); controllers[1] = new Controller(UserIndex.Two); controllers[2] = new Controller(UserIndex.Three); controllers[3] = new Controller(UserIndex.Four); for (int i = 0; i < controllers.Length; i++) { if (controllers[i].IsConnected) { Interlocked.Increment(ref connectedControllers); SharpDX.XInput.State state = controllers[i].GetState(); states[i] = state.Gamepad; Interlocked.Exchange(ref packetNumbers[i], state.PacketNumber); } else { states[i] = default(Gamepad); Interlocked.Exchange(ref packetNumbers[i], int.MinValue); } } }
public SharpDXGamePad(GamePadNumber number = GamePadNumber.Any) : base(number) { controller = new Controller(GetUserIndexFromNumber()); states = new State[GamePadButton.A.GetCount()]; for (int i = 0; i < states.Length; i++) states[i] = State.Released; }
public static unsafe int XInputGetState(int dwUserIndex, out State stateRef) { stateRef = new State(); int state; fixed (State* statePtr = &stateRef) state = XInput.XInputGetState_(dwUserIndex, (void*) statePtr); return state; }
private static XInput.State GetState(GamepadNumbers gamepadNumber) { try { if (Gamepad.GamepadInstance.IsSupported) { XInput.State state = GamepadInstance.EmptyState; switch (gamepadNumber) { case GamepadNumbers.One: state = GamepadInstance.Gamepad1.GetState(); break; case GamepadNumbers.Two: state = GamepadInstance.Gamepad2.GetState(); break; case GamepadNumbers.Three: state = GamepadInstance.Gamepad3.GetState(); break; case GamepadNumbers.Four: state = GamepadInstance.Gamepad4.GetState(); break; } GamepadInstance.IsDeviceConnected = true; return(state); } else { return(GamepadInstance.EmptyState); } } catch (Exception ex) { Debugging.LogException(LogEntryType.Warning, ex); GamepadInstance.IsDeviceConnected = false; return(GamepadInstance.EmptyState); } }
private Dictionary <Button, bool> determineSimpleButtonState(SharpDX.XInput.State controllerState) { Dictionary <Button, bool> result = new Dictionary <Button, bool>(); setButtonState(result, Button.LB, (controllerState.Gamepad.Buttons & GamepadButtonFlags.LeftShoulder) != 0); setButtonState(result, Button.RB, (controllerState.Gamepad.Buttons & GamepadButtonFlags.RightShoulder) != 0); setButtonState(result, Button.X, (controllerState.Gamepad.Buttons & GamepadButtonFlags.X) != 0); setButtonState(result, Button.Y, (controllerState.Gamepad.Buttons & GamepadButtonFlags.Y) != 0); setButtonState(result, Button.A, (controllerState.Gamepad.Buttons & GamepadButtonFlags.A) != 0); setButtonState(result, Button.B, (controllerState.Gamepad.Buttons & GamepadButtonFlags.B) != 0); setButtonState(result, Button.LS, (controllerState.Gamepad.Buttons & GamepadButtonFlags.LeftThumb) != 0); setButtonState(result, Button.RS, (controllerState.Gamepad.Buttons & GamepadButtonFlags.RightThumb) != 0); setButtonState(result, Button.DU, (controllerState.Gamepad.Buttons & GamepadButtonFlags.DPadUp) != 0); setButtonState(result, Button.DD, (controllerState.Gamepad.Buttons & GamepadButtonFlags.DPadDown) != 0); setButtonState(result, Button.DL, (controllerState.Gamepad.Buttons & GamepadButtonFlags.DPadLeft) != 0); setButtonState(result, Button.DR, (controllerState.Gamepad.Buttons & GamepadButtonFlags.DPadRight) != 0); setButtonState(result, Button.START, (controllerState.Gamepad.Buttons & GamepadButtonFlags.Start) != 0); setButtonState(result, Button.BACK, (controllerState.Gamepad.Buttons & GamepadButtonFlags.Back) != 0); setJoystickButtonState(result, "LS", "L", "R", (float)controllerState.Gamepad.LeftThumbX, LDEADZONEX); setJoystickButtonState(result, "LS", "D", "U", (float)controllerState.Gamepad.LeftThumbY, LDEADZONEY); setJoystickButtonState(result, "RS", "L", "R", (float)controllerState.Gamepad.RightThumbX, RDEADZONEX); setJoystickButtonState(result, "RS", "D", "U", (float)controllerState.Gamepad.RightThumbY, RDEADZONEY); setTriggerButtonState(result, Button.LT, (float)controllerState.Gamepad.LeftTrigger); setTriggerButtonState(result, Button.RT, (float)controllerState.Gamepad.RightTrigger); int stat = secret_get_gamepad(0, out xgs); // Console.WriteLine(xgs.wButtons); if (stat == 0) { bool value = ((xgs.wButtons & 0x0400) != 0); setButtonState(result, Button.GUIDE, value); } return(result); }
public void Update(InputManager manager) { if (!IsConnected) return; currentState = controller.GetState(); if (CheckButtonPressed(GamepadButtonFlags.A)) manager.SendClear(); if (CheckButtonPressed(GamepadButtonFlags.B)) manager.StopDrone(); if (CheckButtonPressed(GamepadButtonFlags.Y)) manager.ToogleArmStatus(); float deadZone = 0.075f; if (!manager.DeadZone) deadZone = 0; TargetData target = new TargetData(); target.Roll = DeadZone.Compute(currentState.Gamepad.LeftThumbX, short.MaxValue, deadZone); target.Pitch = -DeadZone.Compute(currentState.Gamepad.LeftThumbY, short.MaxValue, deadZone); target.RotationalSpeed = DeadZone.Compute(currentState.Gamepad.RightThumbX, short.MaxValue, deadZone); target.Thrust = DeadZone.Compute(currentState.Gamepad.RightThumbY + short.MaxValue, short.MaxValue * 2, deadZone); float x = GetButtonValue(GamepadButtonFlags.DPadRight) - GetButtonValue(GamepadButtonFlags.DPadLeft); float y = -GetButtonValue(GamepadButtonFlags.DPadDown) - GetButtonValue(GamepadButtonFlags.DPadUp); target.Roll += x * 0.1f; target.Pitch += y * 0.1f; manager.SendTargetData(target); lastState = currentState; firstUpdate = false; }
private async void timerElapsedHandler(ThreadPoolTimer timer) { float pitch = 0, roll = 0, yaw = 0, gaz = 0; if (_DroneClient == null) return; if (!_Controller.IsConnected || !_Controller.GetState(out _ControllerState)) { _FailCounter++; if (_FailCounter > _FailCounterMax) { DroneClient.InputState.Update(0, 0, 0, 0); //Avoid overflow _FailCounter = _FailCounterMax; } return; } if (_ControllerState.PacketNumber <= _ControllerPreviousState.PacketNumber) return; //Thumbs var leftThumb = InputHelper.NormalizeInput(_ControllerState.Gamepad.LeftThumbX, _ControllerState.Gamepad.LeftThumbY, Convert.ToInt16(SharpDX.XInput.Gamepad.LeftThumbDeadZone * 1.1), _JoystickRange); if (leftThumb.NormalizedMagnitude > 0) { roll = (float)_ControllerState.Gamepad.LeftThumbX * _RollThrottle / this._JoystickRange; pitch = (float)_ControllerState.Gamepad.LeftThumbY * _PitchThrottle / this._JoystickRange; } var rightThumb = InputHelper.NormalizeInput(_ControllerState.Gamepad.RightThumbX, _ControllerState.Gamepad.RightThumbY, Convert.ToInt16(SharpDX.XInput.Gamepad.RightThumbDeadZone * 1.1), _JoystickRange); if (rightThumb.NormalizedMagnitude > 0) { yaw = (float)_ControllerState.Gamepad.RightThumbX * _YawThrottle / this._JoystickRange; gaz = (float)_ControllerState.Gamepad.RightThumbY * _GazThrottle / this._JoystickRange; } _FailCounter = 0; DroneClient.InputState.Update(roll, -pitch, yaw, gaz); //Buttons var buttons = _ControllerState.Gamepad.Buttons; if (buttons.HasFlag(GamepadButtonFlags.Start)) { if (await DroneClient.ConnectAsync()) { if (DroneClient.NavigationData.State.HasFlag(NavigationState.Landed)) { DroneClient.TakeOff(); } else { DroneClient.Land(); } } } if (buttons.HasFlag(GamepadButtonFlags.Back)) { DroneClient.Emergency(); } if (buttons.HasFlag(GamepadButtonFlags.Y)) { DroneClient.Hover(); } if (buttons.HasFlag(GamepadButtonFlags.X)) { DroneClient.CycleProgressiveMode(); } _ControllerPreviousState = _ControllerState; }
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); }
public bool GetState(out State state) { return XInput.XInputGetState((int) this.userIndex, out state) == 0; }
public void SetPreviousState() { previousState = currentState; }
/// <summary> /// Function to poll the joystick for data. /// </summary> protected override void PollJoystick() { if (!_controller.IsConnected) { Gorgon.Log.Print("XInput Controller {0} disconnected.", LoggingLevel.Verbose, _controllerID); IsConnected = false; return; } // If we weren't connected before, then get the caps for the device. if (!IsConnected) { var previousDeadZone = DeadZone; Initialize(); IsConnected = true; // Restore the dead zone. DeadZone.Rudder = new GorgonRange(previousDeadZone.Rudder); DeadZone.Throttle = new GorgonRange(previousDeadZone.Throttle); DeadZone.X = new GorgonRange(previousDeadZone.X); DeadZone.Y = new GorgonRange(previousDeadZone.Y); DeadZone.SecondaryX = new GorgonRange(previousDeadZone.SecondaryX); DeadZone.SecondaryY = new GorgonRange(previousDeadZone.SecondaryY); #if DEBUG XI.Capabilities caps = _controller.GetCapabilities(XI.DeviceQueryType.Any); Gorgon.Log.Print("XInput Controller {0} (ID:{1}) re-connected.", LoggingLevel.Verbose, caps.SubType.ToString(), _controllerID); #endif } XI.State state = _controller.GetState(); // Do nothing if the data has not changed since the last poll. if (LastPacket == state.PacketNumber) { return; } // Get axis data. X = state.Gamepad.LeftThumbX; Y = state.Gamepad.LeftThumbY; SecondaryX = state.Gamepad.RightThumbX; SecondaryY = state.Gamepad.RightThumbY; Throttle = state.Gamepad.RightTrigger; Rudder = state.Gamepad.LeftTrigger; // Get button info. if (state.Gamepad.Buttons != XI.GamepadButtonFlags.None) { // ReSharper disable once ForCanBeConvertedToForeach for (int i = 0; i < _button.Length; i++) { _buttonList.SetButtonState(_button[i], (_button[i] != XI.GamepadButtonFlags.None) && ((state.Gamepad.Buttons & _button[i]) == _button[i])); } } // Get POV values. GetPOVData(state.Gamepad.Buttons); }
private State ProcessState(State state) { var eventArgs = new ModifyStateEventArgs(state); OnModifyState(this, eventArgs); state = eventArgs.State; var rtXorig = state.Gamepad.RightThumbX; var rtYorig = state.Gamepad.RightThumbY; int rtX = rtXorig + (int)(DeltaX * 32676); int rtY = rtYorig + (int)(-DeltaY * 32676); rtX = Math.Max(Math.Min(rtX, short.MaxValue), short.MinValue); rtY = Math.Max(Math.Min(rtY, short.MaxValue), short.MinValue); //var deadzone = 10; //if ((Math.Abs(state.Gamepad.RightThumbX) < deadzone) // && (Math.Abs(state.Gamepad.RightThumbY) < deadzone)) { state.Gamepad.RightThumbX = (short)rtX; state.Gamepad.RightThumbY = (short)rtY; } return state; }
public ModifyStateEventArgs(State state) { State = state; }
public int XInputGetState(int dwUserIndex, out State stateRef) { return Native.XInputGetState(dwUserIndex, out stateRef); }
/// <summary> /// Collect the current data from the device /// </summary> public override void GetCmdData( String cmd, out int data ) { // Make sure there is a valid device. if ( m_device == null ) { data = 0; return; } // Get the state of the device - retaining the previous state to find the lates change m_prevState = m_state; // Poll the device for info. try { m_state = m_device.GetState( ); switch ( cmd ) { case "xi_thumblx": data = (int) ((float)m_state.Gamepad.LeftThumbX/32.767f); break; // data should be -1000..1000 case "xi_thumbly": data = (int) ((float)m_state.Gamepad.LeftThumbY/32.767f); break; case "xi_thumbrx": data = (int) ((float)m_state.Gamepad.RightThumbX/32.767f); break; case "xi_thumbry": data = ( int )( ( float )m_state.Gamepad.RightThumbY / 32.767f ); break; default: data = 0; break; } } catch ( SharpDXException e ) { log.Error( "Gamepad-GetData: Unexpected Poll Exception", e ); data = 0; return; // EXIT see ex code } }
// Main XBox processing private void PollGamepad() { if ((m_xbox == null) || !m_xbox.IsConnected) { return; } m_xboxStateLast = m_xboxState; m_xboxState = m_xbox.GetState(); if (ButtonPushed(GamepadButtonFlags.A)) { OnXBoxGamepadButtonPressA.Invoke(this, null); } if (ButtonOneShot(GamepadButtonFlags.A)) { OnXBoxGamepadButtonPressAOneShot.Invoke(this, null); } if (ButtonPushed(GamepadButtonFlags.B)) { OnXBoxGamepadButtonPressB.Invoke(this, null); } if (ButtonOneShot(GamepadButtonFlags.B)) { OnXBoxGamepadButtonPressBOneShot.Invoke(this, null); } var LX = m_xboxState.Gamepad.LeftThumbX; var LY = m_xboxState.Gamepad.LeftThumbY; //determine how far the controller is pushed var magnitude = Math.Sqrt(LX * LX + LY * LY); //determine the direction the controller is pushed var normalizedLX = LX / magnitude; var normalizedLY = LY / magnitude; var normalizedMagnitude = 0.0; //check if the controller is outside a circular dead zone if (magnitude > Gamepad.LeftThumbDeadZone) { //clip the magnitude at its expected maximum value if (magnitude > 32767) { magnitude = 32767; } //adjust magnitude relative to the end of the dead zone magnitude -= Gamepad.LeftThumbDeadZone; //optionally normalize the magnitude with respect to its expected range //giving a magnitude value of 0.0 to 1.0 normalizedMagnitude = magnitude / (32767 - Gamepad.LeftThumbDeadZone); } else //if the controller is in the deadzone zero out the magnitude { magnitude = 0.0; normalizedMagnitude = 0.0; // m_deviceX.Stop(6000); } m_deviceX.MoveContinuous(MotorDirection.Forward); }
// public static void GetVidPid(string str, ref int vid, ref int pid) // { // var matches = Regex.Matches(str, @"VID_(\w{4})&PID_(\w{4})"); // if (matches.Count <= 0 || matches[0].Groups.Count <= 1) return; // vid = Convert.ToInt32(matches[0].Groups[1].Value, 16); // pid = Convert.ToInt32(matches[0].Groups[2].Value, 16); // } public void start() { // var stroke = new ManagedWrapper.Stroke(); // stroke.key.code = (ushort)Keys.J; // stroke.key.state = (ushort)ManagedWrapper.KeyState.Down; // int devId = 1; // ManagedWrapper.Send(_deviceContext, devId, ref stroke, 1); //use this code to determine keys // ManagedWrapper.SetFilter(deviceContext, ManagedWrapper.IsKeyboard, ManagedWrapper.Filter.All); // var stroke = new ManagedWrapper.Stroke(); // int device = 0; // while (ManagedWrapper.Receive(deviceContext, device = ManagedWrapper.Wait(deviceContext), ref stroke, 1) > 0) // { // if(ManagedWrapper.IsKeyboard(device) > 0) { // Console.WriteLine(stroke.key.code); // if(stroke.key.code == 1) // break; // } // } // ManagedWrapper.SetFilter(deviceContext, ManagedWrapper.IsKeyboard, ManagedWrapper.Filter.None); // var ret = new List<DeviceInfo>(); for (var i = 1; i < 21; i++) { var handle = ManagedWrapper.GetHardwareStr(deviceContext, i, 1000); if (handle == "") { continue; } // int foundVid = 0, foundPid = 0; // GetVidPid(handle, ref foundVid, ref foundPid); //if (foundVid == 0 || foundPid == 0) continue; // Console.WriteLine(i + " " + handle); // ret.Add(new DeviceInfo {Id = i, IsMouse = i > 10, Handle = handle}); } // foreach (var device in ret) // { // Console.WriteLine(device); // } var controllers = new[] { new Controller(), new Controller(UserIndex.One), new Controller(UserIndex.Two), new Controller(UserIndex.Three), new Controller(UserIndex.Four) }; // Get 1st controller available Controller controller = controllers[0]; foreach (var selectControler in controllers) { // Console.WriteLine(selectControler); if (selectControler.IsConnected) { controller = selectControler; break; } } if (controller == null) { Console.WriteLine("No XInput controller installed"); } else { Console.WriteLine("KeyMapper loaded"); // Console.WriteLine("Found a XInput controller available"); // Console.WriteLine("Press buttons on the controller to display events or escape key to exit... "); // Poll events from joystick SharpDX.XInput.State previousControllerState = controller.GetState(); Dictionary <Button, bool> lastSimpleGamepadState = determineSimpleButtonState(previousControllerState); StateTransition lastState = null; while (!ModuleData.getInstance().cancellationToken.IsCancellationRequested) { if (controller.IsConnected) { SharpDX.XInput.State controllerState = controller.GetState(); if (previousControllerState.PacketNumber != controllerState.PacketNumber) { // Console.WriteLine(controllerState.Gamepad); Dictionary <Button, bool> simpleGamepadState = determineSimpleButtonState(controllerState); Dictionary <Button, Settings.Action> changedState = determineStateDifferences(lastSimpleGamepadState, simpleGamepadState); printStateChanges(changedState); // Console.WriteLine(GetActiveWindowTitle()); //determine if we are transitioning to a new 'state' // this is based on the current state, the game state and the keys pressed/not pressed // NOTE: the first state wins, for speed StateTransition newState = getNewState(simpleGamepadState, ModuleData.getInstance().companionSettings.stateTransitions); if (newState == null && lastState == null) { //nothing to do } else if (newState != null && lastState == null) { activateState(newState); } else if (newState == null && lastState != null) { deactivateState(lastState); } else if (newState != null && lastState != null && !newState.transitionName.Equals(lastState.transitionName)) { deactivateState(lastState); activateState(newState); } //now that we have the state name determined, load the correct mappings StateControllerMapping stateControllerMappings = findStateControllerMappings(newState, ModuleData.getInstance().companionSettings.stateMappings); //apply button presses and such foreach (Button key in changedState.Keys) { Settings.Action action = changedState[key]; foreach (ControllerMapping controllerMapping in stateControllerMappings.controllerMappings) { if (controllerMapping.button.button == key && action == controllerMapping.button.action) { handleCommand(controllerMapping); } } } lastState = newState; lastSimpleGamepadState = simpleGamepadState; } Thread.Sleep(10); previousControllerState = controllerState; } } } }
/// <summary> /// Collect the current data from the device /// </summary> public override void GetData( ) { // Make sure there is a valid device. if ( m_device == null ) return; // Get the state of the device - retaining the previous state to find the lates change m_prevState = m_state; // Poll the device for info. try { m_state = m_device.GetState( ); } catch ( SharpDXException e ) { log.Error( "Gamepad-GetData: Unexpected Poll Exception", e ); return; // EXIT see ex code } UpdateUI( ); // and update the GUI }
public async void Update() { float pitch = 0, roll = 0, yaw = 0, gaz = 0; if (_DroneClient == null || _Controller == null) return; if (!_Controller.IsConnected || !_Controller.GetState(out _ControllerState)) { _FailCounter++; if (_FailCounter > _FailCounterMax) { DroneClient.InputState.Update(0, 0, 0, 0); //Avoid overflow _FailCounter = _FailCounterMax; } return; } if (_ControllerState.PacketNumber <= _ControllerPreviousState.PacketNumber) return; //Thumbs var leftThumb = NormalizeInput(_ControllerState.Gamepad.LeftThumbX, _ControllerState.Gamepad.LeftThumbY, Convert.ToInt16(SharpDX.XInput.Gamepad.LeftThumbDeadZone * 1.1), _JoystickRange); if (leftThumb.NormalizedMagnitude > 0) { roll = (float)_ControllerState.Gamepad.LeftThumbX * _RollThrottle / _JoystickRange; pitch = (float)_ControllerState.Gamepad.LeftThumbY * _PitchThrottle / _JoystickRange; } var rightThumb = NormalizeInput(_ControllerState.Gamepad.RightThumbX, _ControllerState.Gamepad.RightThumbY, Convert.ToInt16(SharpDX.XInput.Gamepad.RightThumbDeadZone * 1.1), _JoystickRange); if (rightThumb.NormalizedMagnitude > 0) { yaw = (float)_ControllerState.Gamepad.RightThumbX * _YawThrottle / _JoystickRange; gaz = (float)_ControllerState.Gamepad.RightThumbY * _GazThrottle / _JoystickRange; } _FailCounter = 0; DroneClient.InputState.Update(roll, -pitch, yaw, gaz); //Debug.WriteLine("InputState=" + DroneClient.InputState.ToString()); //Buttons var buttons = _ControllerState.Gamepad.Buttons; if (buttons.HasFlag(GamepadButtonFlags.Start)) { if (await DroneClient.ConnectAsync()) { if (DroneClient.NavigationData.State.HasFlag(NavigationState.Landed)) { DroneClient.TakeOff(); } else { DroneClient.Land(); } } } if (buttons.HasFlag(GamepadButtonFlags.Back)) { DroneClient.Emergency(); } if (buttons.HasFlag(GamepadButtonFlags.Y)) { DroneClient.CycleProgressiveMode(); } if (buttons.HasFlag(GamepadButtonFlags.X)) { DroneClient.TakePicture(); } if (buttons.HasFlag(GamepadButtonFlags.A)) { DroneClient.StartRecordingVideo(); } if (buttons.HasFlag(GamepadButtonFlags.B)) { DroneClient.StopRecordingVideo(); } if (buttons.HasFlag(GamepadButtonFlags.DPadLeft)) { DroneClient.PlayAnimation(ARDRONE_ANIMATION.ARDRONE_ANIMATION_FLIP_LEFT); } if (buttons.HasFlag(GamepadButtonFlags.DPadUp)) { DroneClient.PlayAnimation(ARDRONE_ANIMATION.ARDRONE_ANIMATION_FLIP_AHEAD); } if (buttons.HasFlag(GamepadButtonFlags.DPadRight)) { DroneClient.PlayAnimation(ARDRONE_ANIMATION.ARDRONE_ANIMATION_FLIP_RIGHT); } if (buttons.HasFlag(GamepadButtonFlags.DPadDown)) { DroneClient.PlayAnimation(ARDRONE_ANIMATION.ARDRONE_ANIMATION_FLIP_BEHIND); } _ControllerPreviousState = _ControllerState; }
// Update the Packet Number and the state public GamepadButtonFlags UpdateState() { UpdatePacketNumber(); if (_newUpdate) { _lastState = _Controller.GetState(); return _lastState.Gamepad.Buttons; } return new GamepadButtonFlags(); }
public static extern int XInputGetState(int dwUserIndex, out State stateRef);
private State ProcessState(State state) { try { var eventArgs = new ModifyStateEventArgs(state); OnModifyState(this, eventArgs); state = eventArgs.State; int rtXorig = state.Gamepad.RightThumbX; int rtYorig = state.Gamepad.RightThumbY; int deadzone = 8689; if (Math.Abs(rtYorig) < deadzone) //deadzone for xbox controller { rtYorig = 0; } int rtX = rtXorig + (int)(DeltaX * 32676); int rtY = rtYorig + (int)(-DeltaY * 32676); rtX = Math.Max(Math.Min(rtX, short.MaxValue), short.MinValue); rtY = Math.Max(Math.Min(rtY, short.MaxValue), short.MinValue); state.Gamepad.RightThumbX = (short)rtX; state.Gamepad.RightThumbY = (short)rtY; } catch (Exception e) { Debug.Log("ProcessState: " + e.Message); } return state; }
uint XInputGetState_Hooked(int dwUserIndex, out State state) { state = new State(); try { var controller1 = new Controller(UserIndex.One); var controller2 = new Controller(UserIndex.Two); var controller3 = new Controller(UserIndex.Three); var controller4 = new Controller(UserIndex.Four); var controller = new Controller((UserIndex)dwUserIndex); if (!controller1.IsConnected && !controller2.IsConnected && !controller3.IsConnected && !controller4.IsConnected && ((UserIndex)dwUserIndex == UserIndex.One || (UserIndex)dwUserIndex == UserIndex.Any)) { ControllerConnected = false; _fakeState.PacketNumber = _fakeState.PacketNumber++; state = _fakeState; } else if (controller.IsConnected) { ControllerConnected = controller.IsConnected; state = controller.GetState(); } else { return ERROR_DEVICE_NOT_CONNECTED; } ControllerState = state; if (Enabled) { state = ProcessState(state); } return ERROR_SUCCESS; } catch { return ERROR_DEVICE_NOT_CONNECTED; } }
public void UpdateFromXInput(State state, bool IsConnected) { // If nothing changed then return. if (state.Equals(oldState)) return; oldState = state; var wasConnected = gamePadStateIsConnected; var nowConnected = IsConnected; gamePadStateIsConnected = IsConnected; gamePadState = state; // If form was disabled and no data is comming then just return. if (!wasConnected && !nowConnected) return; // If device connection changed then... if (wasConnected != nowConnected) { if (nowConnected) { // Enable form. this.FrontPictureBox.Image = frontImage; this.TopPictureBox.Image = topImage; } else { // Disable form. this.FrontPictureBox.Image = frontDisabledImage; this.TopPictureBox.Image = topDisabledImage; } } if (nowConnected) { this.leftX = state.Gamepad.LeftThumbX; this.leftY = state.Gamepad.LeftThumbY; this.rightX = state.Gamepad.RightThumbX; this.rightY = state.Gamepad.RightThumbY; } else { this.leftX = 0; this.leftY = 0; this.rightX = 0; this.rightY = 0; } UpdateControl(LeftThumbTextBox, string.Format("{0};{1}", this.leftX, this.leftY)); UpdateControl(RightThumbTextBox, string.Format("{0};{1}", this.rightX, this.rightY)); this.TopPictureBox.Refresh(); this.FrontPictureBox.Refresh(); }
public ControllerEmulation() { ControllerState = new State(); _fakeState = new State(); CreateHooks(); }
public void Refresh(State gamePadState, Bitmap markB) { _gamepadState = gamePadState; _markB = markB; ButtonImagePictureBox.Refresh(); }
public void SetCurrentState() { if (controller.IsConnected) currentState = controller.GetState(); }
/// <summary> /// Querries all current input states. /// </summary> public IEnumerable <InputStateBase> GetInputStates() { // Update connected states first for (int loop = 0; loop < m_controllers.Length; loop++) { bool isConnected = m_controllers[loop].IsConnected; if (!isConnected) { m_states[loop].NotifyConnected(false); continue; } m_states[loop].NotifyConnected(true); // Query all state structures XI.State xiState = m_controllers[loop].GetState(); XI.Gamepad xiGamepad = xiState.Gamepad; // Convert float values GamepadReportedState repState = new GamepadReportedState() { LeftThumbstickX = EngineMath.Clamp((float)xiGamepad.LeftThumbX / (float)short.MaxValue, -1f, 1f), LeftThumbstickY = EngineMath.Clamp((float)xiGamepad.LeftThumbY / (float)short.MaxValue, -1f, 1f), LeftTrigger = EngineMath.Clamp((float)xiGamepad.LeftTrigger / 255f, 0f, 1f), RightThumbstickX = EngineMath.Clamp((float)xiGamepad.RightThumbX / (float)short.MaxValue, -1f, 1f), RightThumbstickY = EngineMath.Clamp((float)xiGamepad.RightThumbY / (float)short.MaxValue, -1f, 1f), RightTrigger = EngineMath.Clamp((float)xiGamepad.RightTrigger / 255f, 0, 1f) }; // Convert button states GamepadButton pressedButtons = GamepadButton.None; if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.A)) { pressedButtons |= GamepadButton.A; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.B)) { pressedButtons |= GamepadButton.B; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.Back)) { pressedButtons |= GamepadButton.View; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadDown)) { pressedButtons |= GamepadButton.DPadDown; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadLeft)) { pressedButtons |= GamepadButton.DPadLeft; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadRight)) { pressedButtons |= GamepadButton.DPadRight; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.DPadUp)) { pressedButtons |= GamepadButton.DPadUp; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.LeftShoulder)) { pressedButtons |= GamepadButton.LeftShoulder; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.LeftThumb)) { pressedButtons |= GamepadButton.LeftThumbstick; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.RightShoulder)) { pressedButtons |= GamepadButton.RightShoulder; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.RightThumb)) { pressedButtons |= GamepadButton.RightThumbstick; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.Start)) { pressedButtons |= GamepadButton.Menu; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.X)) { pressedButtons |= GamepadButton.X; } if (xiGamepad.Buttons.HasFlag(XI.GamepadButtonFlags.Y)) { pressedButtons |= GamepadButton.Y; } repState.Buttons = pressedButtons; // Report controller state to the system m_states[loop].NotifyState(repState); } // Now return all input states for (int loop = 0; loop < m_states.Length; loop++) { yield return(m_states[loop]); } }
void UpdateStates() { ProfilerShort.Begin("MyXInputInput::UpdateStates"); m_previousMouseState = m_actualMouseState; if (IsJoystickConnected()) { m_previousJoystickState = m_actualJoystickState; m_actualJoystickState = m_joystick.GetState(); // Always emulating mouse (only controllers here!) { //var xPos = GetJoystickAxisStateForGameplay(MyJoystickAxesEnum.Xpos); //var xNeg = -GetJoystickAxisStateForGameplay(MyJoystickAxesEnum.Xneg); //var yPos = GetJoystickAxisStateForGameplay(MyJoystickAxesEnum.Ypos); //var yNeg = -GetJoystickAxisStateForGameplay(MyJoystickAxesEnum.Yneg); //m_absoluteMousePosition.X += (xPos + xNeg) * MyJoystickConstants.JOYSTICK_AS_MOUSE_MULTIPLIER; //m_absoluteMousePosition.Y += (yPos + yNeg) * MyJoystickConstants.JOYSTICK_AS_MOUSE_MULTIPLIER; // MyWindowsMouse.SetPosition((int)m_absoluteMousePosition.X, (int)m_absoluteMousePosition.Y); float lx = m_actualJoystickState.Gamepad.LeftThumbX; float ly = m_actualJoystickState.Gamepad.LeftThumbY; float magnitude = (float)Math.Sqrt(lx*lx + ly*ly); // determine the direction the controller is pushed float normalizedLX = lx / magnitude; float normalizedLY = ly / magnitude; float normalizedMagnitude = 0.0f; // check if the controller is outside a circular dead zone if (magnitude > (m_joystickDeadzone)*32767.0f) { if (magnitude > 32767.0f) magnitude = 32767.0f; magnitude -= m_joystickDeadzone; normalizedMagnitude = magnitude / (32767.0f - m_joystickDeadzone); // Use cubic magnitude to achieve better precision near the dead zone float mult = normalizedMagnitude * normalizedMagnitude * normalizedMagnitude * MyJoystickConstants.JOYSTICK_AS_MOUSE_MULTIPLIER * 2.0f; m_absoluteMousePosition.X += mult * normalizedLX; m_absoluteMousePosition.Y -= mult * normalizedLY; } m_absoluteMousePosition = Vector2.Clamp(m_absoluteMousePosition, m_mouseMinsBound, m_mouseMaxsBound); } } if (IsJoystickLastUsed) { /* if (IsAnyMousePressed() || IsAnyKeyPress()) IsJoystickLastUsed = false; * */ } else { if (IsAnyJoystickButtonPressed() || IsAnyJoystickAxisPressed()) IsJoystickLastUsed = true; } ProfilerShort.End(); }
private static void FireEventsForStateChange(UserIndex inActiveController, State? inLastState, State inState) { var keysUp = new List<GamepadButtonFlags>(); var keysDown = new List<GamepadButtonFlags>(); var keysRepeated = new List<GamepadButtonFlags>(); // Check each known key in turn. foreach (GamepadButtonFlags keyEnum in Enum.GetValues(typeof(GamepadButtonFlags))) { if (keyEnum == GamepadButtonFlags.None) continue; var key = (Int32)keyEnum; // First state? Fire a key down. if (inLastState == null && ((((Int32)inState.Gamepad.Buttons) & key) == key)) { keysDown.Add((GamepadButtonFlags)key); continue; } if (inLastState == null) continue; // Key newly pressed, fire a key down. if ((((Int32)inLastState.Value.Gamepad.Buttons) & key) == 0 && ((((Int32)inState.Gamepad.Buttons) & key) == key)) { keysDown.Add((GamepadButtonFlags)key); continue; } // Key no longer pressed, fire a key up. if ((((Int32)inLastState.Value.Gamepad.Buttons) & key) == key && ((((Int32)inState.Gamepad.Buttons) & key) == 0)) { keysUp.Add((GamepadButtonFlags)key); continue; } // Key continues to be pressed, fire a key repeat. if ((((Int32)inLastState.Value.Gamepad.Buttons) & key) == key && ((((Int32)inState.Gamepad.Buttons) & key) == key)) { keysRepeated.Add((GamepadButtonFlags)key); continue; } } // Fire changes (via dispatcher.) if (Changed != null) { if (!_lastDown.ContainsKey(inActiveController)) _lastDown.Add(inActiveController, new Dictionary<GamepadButtonFlags, DateTime>()); // Fire key up events. foreach (var keyUp in keysUp) { Changed(null, new GamepadEventArgs(keyUp, false)); if (_lastDown[inActiveController].ContainsKey(keyUp)) _lastDown[inActiveController].Remove(keyUp); } // Fire key down events. foreach (var keyDown in keysDown) { Changed(null, new GamepadEventArgs(keyDown, true)); _lastDown[inActiveController][keyDown] = DateTime.UtcNow; } // Fire another key down for repeated keys (as per key repeat rate.) foreach (var keyRepeated in keysRepeated) if (!_lastDown.ContainsKey(inActiveController) || !_lastDown[inActiveController].ContainsKey(keyRepeated) || _lastDown[inActiveController][keyRepeated] < DateTime.UtcNow.Subtract(TimeSpan.FromMilliseconds(REPEAT_RATE))) { Changed(null, new GamepadEventArgs(keyRepeated, true)); _lastDown[inActiveController][keyRepeated] = DateTime.UtcNow; } } }
public void UpdateFromXInput(State state, bool IsConnected) { // If nothing changed then return. if (state.Equals(oldState)) return; oldState = state; var wasConnected = gamePadStateIsConnected; var nowConnected = IsConnected; gamePadStateIsConnected = IsConnected; gamePadState = state; // If form was disabled and no data is comming then just return. if (!wasConnected && !nowConnected) return; // If device connection changed then... if (wasConnected != nowConnected) { if (nowConnected) { // Enable form. this.FrontPictureBox.Image = frontImage; this.TopPictureBox.Image = topImage; } else { // Disable form. this.FrontPictureBox.Image = frontDisabledImage; this.TopPictureBox.Image = topDisabledImage; } } if (nowConnected) { _leftX = state.Gamepad.LeftThumbX; _leftY = state.Gamepad.LeftThumbY; _rightX = state.Gamepad.RightThumbX; _rightY = state.Gamepad.RightThumbY; } else { _leftX = 0; _leftY = 0; _rightX = 0; _rightY = 0; } UpdateControl(LeftThumbTextBox, string.Format("{0};{1}", _leftX, _leftY)); UpdateControl(RightThumbTextBox, string.Format("{0};{1}", _rightX, _rightY)); var axis = diControl.Axis; bool success; int index; SettingType type; success = SettingsConverter.TryParseIndexAndType(LeftThumbAxisXComboBox.Text, out index, out type); if (success) LeftThumbXUserControl.DrawPoint(axis[index - 1], _leftX, type == SettingType.IAxis); success = SettingsConverter.TryParseIndexAndType(LeftThumbAxisYComboBox.Text, out index, out type); if (success) LeftThumbYUserControl.DrawPoint(axis[index - 1], _leftY, type == SettingType.IAxis); success = SettingsConverter.TryParseIndexAndType(RightThumbAxisXComboBox.Text, out index, out type); if (success) RightThumbXUserControl.DrawPoint(axis[index - 1], _rightX, type == SettingType.IAxis); success = SettingsConverter.TryParseIndexAndType(RightThumbAxisYComboBox.Text, out index, out type); if (success) RightThumbYUserControl.DrawPoint(axis[index - 1], _rightY, type == SettingType.IAxis); this.TopPictureBox.Refresh(); this.FrontPictureBox.Refresh(); }
/// <summary> /// Updates controller state /// </summary> protected void UpdateState() { if (!controller.IsConnected) return; state = controller.GetState(); }