capncoolio

GamePadWatcher

Jun 10th, 2021
531
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using XInputDotNetPure;
  6.  
  7. namespace XInputToVJoy
  8. {
  9.     public delegate void OnGamePadButtonPressed(object sender, GamePadButtonEventArgs args);
  10.     public delegate void OnGamePadButtonReleased(object sender, GamePadButtonEventArgs args);
  11.  
  12.     public delegate void OnGamePadDpadPressed(object sender, GamePadDpadEventArgs args);
  13.     public delegate void OnGamePadDpadReleased(object sender, GamePadDpadEventArgs args);
  14.  
  15.     public delegate void OnGamePadTriggerStateChanged(object sender, GamePadTriggerEventArgs args);
  16.     public delegate void OnGamePadThumbStickStateChanged(object sender, GamePadThumbStickEventArgs args);
  17.  
  18.     public class GamePadWatcher
  19.     {
  20.         private GamePadState _state;
  21.         private Dictionary<Button, DateTime> _buttonHolds = new Dictionary<Button, DateTime>();
  22.         private Dictionary<Dpad, DateTime> _dpadHolds = new Dictionary<Dpad, DateTime>();
  23.  
  24.         public GamePadWatcher()
  25.         {
  26.             _state = GamePad.GetState(PlayerIndex.One);
  27.             _ = Task.Run(PollState);
  28.         }
  29.  
  30.         public event OnGamePadButtonPressed GamePadButtonPressed;
  31.  
  32.         public event OnGamePadButtonReleased GamePadButtonReleased;
  33.  
  34.         public event OnGamePadThumbStickStateChanged GamePadThumbStickStateChanged;
  35.  
  36.         public event OnGamePadTriggerStateChanged GamePadTriggerStateChanged;
  37.  
  38.         public event OnGamePadDpadPressed GamePadDpadPressed;
  39.  
  40.         public event OnGamePadDpadReleased GamePadDpadReleased;
  41.  
  42.         private void PollState()
  43.         {
  44.             while (true)
  45.             {
  46.                 var currentState = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.Circular);
  47.  
  48.                 foreach (var stateChange in CheckPressedButtons(_state.Buttons, currentState.Buttons))
  49.                 {
  50.                     _buttonHolds.Add(stateChange.button, DateTime.Now);
  51.                     GamePadButtonPressed(this, new GamePadButtonEventArgs(stateChange.button, stateChange.state));
  52.                 }
  53.  
  54.                 foreach (var stateChange in CheckReleasedButtons(_state.Buttons, currentState.Buttons))
  55.                 {
  56.                     var buttonHold = _buttonHolds[stateChange.button];
  57.                     _buttonHolds.Remove(stateChange.button);
  58.                     GamePadButtonReleased(this, new GamePadButtonEventArgs(stateChange.button, stateChange.state, buttonHold));
  59.                 }
  60.  
  61.                 foreach(var stateChange in CheckThumbSticks(_state.ThumbSticks, currentState.ThumbSticks))
  62.                 {
  63.                     GamePadThumbStickStateChanged(this, new GamePadThumbStickEventArgs(stateChange.stick, stateChange.value));
  64.                 }
  65.  
  66.                 foreach (var stateChange in CheckTriggers(_state.Triggers, currentState.Triggers))
  67.                 {
  68.                     GamePadTriggerStateChanged(this, new GamePadTriggerEventArgs(stateChange.trigger, stateChange.value));
  69.                 }
  70.  
  71.                 foreach (var stateChange in CheckPressedDpad(_state.DPad, currentState.DPad))
  72.                 {
  73.                     _dpadHolds.Add(stateChange.dpad, DateTime.Now);
  74.                     GamePadDpadPressed(this, new GamePadDpadEventArgs(stateChange.dpad, stateChange.value));
  75.                 }
  76.  
  77.                 foreach (var stateChange in CheckReleasedDpad(_state.DPad, currentState.DPad))
  78.                 {
  79.                     var dpadHold = _dpadHolds[stateChange.dpad];
  80.                     _dpadHolds.Remove(stateChange.dpad);
  81.                     GamePadDpadReleased(this, new GamePadDpadEventArgs(stateChange.dpad, stateChange.value, dpadHold));
  82.                 }
  83.  
  84.                 _state = currentState;
  85.                 Thread.Sleep(10);
  86.             }
  87.         }
  88.  
  89.         private (Button button, ButtonState state)[] CheckPressedButtons(GamePadButtons oldVal, GamePadButtons newVal)
  90.         {
  91.             var buttonsPressed = new List<(Button button, ButtonState state)>();
  92.  
  93.             if (oldVal.A != newVal.A && newVal.A != ButtonState.Released)
  94.                 buttonsPressed.Add((Button.A, newVal.A));
  95.  
  96.             if (oldVal.B != newVal.B && newVal.B != ButtonState.Released)
  97.                 buttonsPressed.Add((Button.B, newVal.B));
  98.  
  99.             if (oldVal.X != newVal.X && newVal.X != ButtonState.Released)
  100.                 buttonsPressed.Add((Button.X, newVal.X));
  101.  
  102.             if (oldVal.Y != newVal.Y && newVal.Y != ButtonState.Released)
  103.                 buttonsPressed.Add((Button.Y, newVal.Y));
  104.  
  105.             if (oldVal.Back != newVal.Back && newVal.Back != ButtonState.Released)
  106.                 buttonsPressed.Add((Button.Back, newVal.Back));
  107.  
  108.             if (oldVal.Start != newVal.Start && newVal.Start != ButtonState.Released)
  109.                 buttonsPressed.Add((Button.Start, newVal.Start));
  110.  
  111.             if (oldVal.LeftShoulder != newVal.LeftShoulder && newVal.LeftShoulder != ButtonState.Released)
  112.                 buttonsPressed.Add((Button.LeftShoulder, newVal.LeftShoulder));
  113.  
  114.             if (oldVal.LeftStick != newVal.LeftStick && newVal.LeftStick != ButtonState.Released)
  115.                 buttonsPressed.Add((Button.LeftStick, newVal.LeftStick));
  116.  
  117.             if (oldVal.RightShoulder != newVal.RightShoulder && newVal.RightShoulder != ButtonState.Released)
  118.                 buttonsPressed.Add((Button.RightShoulder, newVal.RightShoulder));
  119.  
  120.             if (oldVal.RightStick != newVal.RightStick && newVal.RightStick != ButtonState.Released)
  121.                 buttonsPressed.Add((Button.RightStick, newVal.RightStick));
  122.  
  123.             return buttonsPressed.ToArray();
  124.         }
  125.  
  126.         private (Button button, ButtonState state)[] CheckReleasedButtons(GamePadButtons oldVal, GamePadButtons newVal)
  127.         {
  128.             var buttonsPressed = new List<(Button button, ButtonState state)>();
  129.  
  130.             if (oldVal.A != newVal.A && newVal.A != ButtonState.Pressed)
  131.                 buttonsPressed.Add((Button.A, newVal.A));
  132.  
  133.             if (oldVal.B != newVal.B && newVal.B != ButtonState.Pressed)
  134.                 buttonsPressed.Add((Button.B, newVal.B));
  135.  
  136.             if (oldVal.X != newVal.X && newVal.X != ButtonState.Pressed)
  137.                 buttonsPressed.Add((Button.X, newVal.X));
  138.  
  139.             if (oldVal.Y != newVal.Y && newVal.Y != ButtonState.Pressed)
  140.                 buttonsPressed.Add((Button.Y, newVal.Y));
  141.  
  142.             if (oldVal.Back != newVal.Back && newVal.Back != ButtonState.Pressed)
  143.                 buttonsPressed.Add((Button.Back, newVal.Back));
  144.  
  145.             if (oldVal.Start != newVal.Start && newVal.Start != ButtonState.Pressed)
  146.                 buttonsPressed.Add((Button.Start, newVal.Start));
  147.  
  148.             if (oldVal.LeftShoulder != newVal.LeftShoulder && newVal.LeftShoulder != ButtonState.Pressed)
  149.                 buttonsPressed.Add((Button.LeftShoulder, newVal.LeftShoulder));
  150.  
  151.             if (oldVal.LeftStick != newVal.LeftStick && newVal.LeftStick != ButtonState.Pressed)
  152.                 buttonsPressed.Add((Button.LeftStick, newVal.LeftStick));
  153.  
  154.             if (oldVal.RightShoulder != newVal.RightShoulder && newVal.RightShoulder != ButtonState.Pressed)
  155.                 buttonsPressed.Add((Button.RightShoulder, newVal.RightShoulder));
  156.  
  157.             if (oldVal.RightStick != newVal.RightStick && newVal.RightStick != ButtonState.Pressed)
  158.                 buttonsPressed.Add((Button.RightStick, newVal.RightStick));
  159.  
  160.             return buttonsPressed.ToArray();
  161.         }
  162.  
  163.         private (Stick stick, GamePadThumbSticks value)[] CheckThumbSticks(GamePadThumbSticks oldVal, GamePadThumbSticks newVal)
  164.         {
  165.             var stickStates = new List<(Stick stick, GamePadThumbSticks value)>();
  166.  
  167.             if (oldVal.Left.X != newVal.Left.X || oldVal.Left.Y != newVal.Left.Y)
  168.                 stickStates.Add((Stick.Left, newVal));
  169.  
  170.             if (oldVal.Right.X != newVal.Right.X || oldVal.Right.Y != newVal.Right.Y)
  171.                 stickStates.Add((Stick.Right, newVal));
  172.  
  173.             return stickStates.ToArray();
  174.         }
  175.  
  176.         private (Trigger trigger, GamePadTriggers value)[] CheckTriggers(GamePadTriggers oldVal, GamePadTriggers newVal)
  177.         {
  178.             var triggerStates = new List<(Trigger trigger, GamePadTriggers value)>();
  179.  
  180.             if (oldVal.Left != newVal.Left)
  181.                 triggerStates.Add((Trigger.Left, newVal));
  182.  
  183.             if (oldVal.Right != newVal.Right)
  184.                 triggerStates.Add((Trigger.Right, newVal));
  185.  
  186.             return triggerStates.ToArray();
  187.         }
  188.  
  189.         private (Dpad dpad, ButtonState value)[] CheckPressedDpad(GamePadDPad oldVal, GamePadDPad newVal)
  190.         {
  191.             var dpadStates = new List<(Dpad dpad, ButtonState value)>();
  192.  
  193.             if (oldVal.Up != newVal.Up && newVal.Up != ButtonState.Released)
  194.                 dpadStates.Add((Dpad.Up, newVal.Up));
  195.  
  196.             if(oldVal.Left != newVal.Left && newVal.Left != ButtonState.Released)
  197.                 dpadStates.Add((Dpad.Left, newVal.Left));
  198.  
  199.             if (oldVal.Down != newVal.Down && newVal.Down != ButtonState.Released)
  200.                 dpadStates.Add((Dpad.Down, newVal.Down));
  201.  
  202.             if (oldVal.Right != newVal.Right && newVal.Right != ButtonState.Released)
  203.                 dpadStates.Add((Dpad.Right, newVal.Right));
  204.  
  205.             return dpadStates.ToArray();
  206.         }
  207.  
  208.         private (Dpad dpad, ButtonState value)[] CheckReleasedDpad(GamePadDPad oldVal, GamePadDPad newVal)
  209.         {
  210.             var dpadStates = new List<(Dpad dpad, ButtonState value)>();
  211.  
  212.             if (oldVal.Up != newVal.Up && newVal.Up != ButtonState.Pressed)
  213.                 dpadStates.Add((Dpad.Up, newVal.Up));
  214.  
  215.             if (oldVal.Left != newVal.Left && newVal.Left != ButtonState.Pressed)
  216.                 dpadStates.Add((Dpad.Left, newVal.Left));
  217.  
  218.             if (oldVal.Down != newVal.Down && newVal.Down != ButtonState.Pressed)
  219.                 dpadStates.Add((Dpad.Down, newVal.Down));
  220.  
  221.             if (oldVal.Right != newVal.Right && newVal.Right != ButtonState.Pressed)
  222.                 dpadStates.Add((Dpad.Right, newVal.Right));
  223.  
  224.             return dpadStates.ToArray();
  225.         }
  226.     }
  227.  
  228.     public enum Button
  229.     {
  230.         A = 1,
  231.         B,
  232.         X,
  233.         Y,
  234.         Back,
  235.         Start,
  236.         LeftShoulder,
  237.         LeftStick,
  238.         RightShoulder,
  239.         RightStick
  240.     }
  241.  
  242.     public enum Stick
  243.     {
  244.         Left = 1,
  245.         Right
  246.     }
  247.  
  248.     public enum Trigger
  249.     {
  250.         Left = 1,
  251.         Right
  252.     }
  253.  
  254.     public enum Dpad
  255.     {
  256.         Up = 0,
  257.         Right,
  258.         Down,
  259.         Left,
  260.         Centre
  261.     }
  262.  
  263.     public class GamePadButtonEventArgs : EventArgs
  264.     {
  265.         public GamePadButtonEventArgs(Button button, ButtonState state)
  266.         {
  267.             Button = button;
  268.             State = state;
  269.             HoldLength = TimeSpan.Zero;
  270.         }
  271.  
  272.         public GamePadButtonEventArgs(Button button, ButtonState state, DateTime pressedTime)
  273.         {
  274.             Button = button;
  275.             State = state;
  276.             HoldLength = DateTime.Now - pressedTime;
  277.         }
  278.  
  279.         public Button Button;
  280.         public ButtonState State;
  281.         public TimeSpan HoldLength;
  282.     }
  283.  
  284.     public class GamePadTriggerEventArgs : EventArgs
  285.     {
  286.         public GamePadTriggerEventArgs (Trigger trigger, GamePadTriggers state)
  287.         {
  288.             ShoulderTrigger = trigger;
  289.             State = state;
  290.         }
  291.  
  292.         public Trigger ShoulderTrigger;
  293.         public GamePadTriggers State;
  294.     }
  295.  
  296.     public class GamePadDpadEventArgs : EventArgs
  297.     {
  298.         public GamePadDpadEventArgs(Dpad dpad, ButtonState state)
  299.         {
  300.             Direction = dpad;
  301.             State = state;
  302.             HoldLength = TimeSpan.Zero;
  303.         }
  304.  
  305.         public GamePadDpadEventArgs(Dpad dpad, ButtonState state, DateTime pressedTime)
  306.         {
  307.             Direction = dpad;
  308.             State = state;
  309.             HoldLength = DateTime.Now - pressedTime;
  310.         }
  311.  
  312.         public Dpad Direction;
  313.         public ButtonState State;
  314.         public TimeSpan HoldLength;
  315.     }
  316.  
  317.     public class GamePadThumbStickEventArgs : EventArgs
  318.     {
  319.         public GamePadThumbStickEventArgs(Stick stick, GamePadThumbSticks state)
  320.         {
  321.             ThumbStick = stick;
  322.             State = state;
  323.         }
  324.  
  325.         public Stick ThumbStick;
  326.         public GamePadThumbSticks State;
  327.     }
  328. }
  329.  
RAW Paste Data