GamepadButtonAssigner.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Ryujinx.Input.Assigner
  6. {
  7. /// <summary>
  8. /// <see cref="IButtonAssigner"/> implementation for regular <see cref="IGamepad"/>.
  9. /// </summary>
  10. public class GamepadButtonAssigner : IButtonAssigner
  11. {
  12. private readonly IGamepad _gamepad;
  13. private GamepadStateSnapshot _currState;
  14. private GamepadStateSnapshot _prevState;
  15. private readonly JoystickButtonDetector _detector;
  16. private readonly bool _forStick;
  17. public GamepadButtonAssigner(IGamepad gamepad, float triggerThreshold, bool forStick)
  18. {
  19. _gamepad = gamepad;
  20. _detector = new JoystickButtonDetector();
  21. _forStick = forStick;
  22. _gamepad?.SetTriggerThreshold(triggerThreshold);
  23. }
  24. public void Initialize()
  25. {
  26. if (_gamepad != null)
  27. {
  28. _currState = _gamepad.GetStateSnapshot();
  29. _prevState = _currState;
  30. }
  31. }
  32. public void ReadInput()
  33. {
  34. if (_gamepad != null)
  35. {
  36. _prevState = _currState;
  37. _currState = _gamepad.GetStateSnapshot();
  38. }
  39. CollectButtonStats();
  40. }
  41. public bool IsAnyButtonPressed()
  42. {
  43. return _detector.IsAnyButtonPressed();
  44. }
  45. public bool ShouldCancel()
  46. {
  47. return _gamepad == null || !_gamepad.IsConnected;
  48. }
  49. public Button? GetPressedButton()
  50. {
  51. IEnumerable<GamepadButtonInputId> pressedButtons = _detector.GetPressedButtons();
  52. return !_forStick ? new(pressedButtons.FirstOrDefault()) : new((StickInputId)pressedButtons.FirstOrDefault());
  53. }
  54. private void CollectButtonStats()
  55. {
  56. if (_forStick)
  57. {
  58. for (StickInputId inputId = StickInputId.Left; inputId < StickInputId.Count; inputId++)
  59. {
  60. (float x, float y) = _currState.GetStick(inputId);
  61. float value;
  62. if (x != 0.0f)
  63. {
  64. value = x;
  65. }
  66. else if (y != 0.0f)
  67. {
  68. value = y;
  69. }
  70. else
  71. {
  72. continue;
  73. }
  74. _detector.AddInput((GamepadButtonInputId)inputId, value);
  75. }
  76. }
  77. else
  78. {
  79. for (GamepadButtonInputId inputId = GamepadButtonInputId.A; inputId < GamepadButtonInputId.Count; inputId++)
  80. {
  81. if (_currState.IsPressed(inputId) && !_prevState.IsPressed(inputId))
  82. {
  83. _detector.AddInput(inputId, 1);
  84. }
  85. if (!_currState.IsPressed(inputId) && _prevState.IsPressed(inputId))
  86. {
  87. _detector.AddInput(inputId, -1);
  88. }
  89. }
  90. }
  91. }
  92. private class JoystickButtonDetector
  93. {
  94. private readonly Dictionary<GamepadButtonInputId, InputSummary> _stats;
  95. public JoystickButtonDetector()
  96. {
  97. _stats = new Dictionary<GamepadButtonInputId, InputSummary>();
  98. }
  99. public bool IsAnyButtonPressed()
  100. {
  101. return _stats.Values.Any(CheckButtonPressed);
  102. }
  103. public IEnumerable<GamepadButtonInputId> GetPressedButtons()
  104. {
  105. return _stats.Where(kvp => CheckButtonPressed(kvp.Value)).Select(kvp => kvp.Key);
  106. }
  107. public void AddInput(GamepadButtonInputId button, float value)
  108. {
  109. if (!_stats.TryGetValue(button, out InputSummary inputSummary))
  110. {
  111. inputSummary = new InputSummary();
  112. _stats.Add(button, inputSummary);
  113. }
  114. inputSummary.AddInput(value);
  115. }
  116. public override string ToString()
  117. {
  118. StringWriter writer = new();
  119. foreach (var kvp in _stats)
  120. {
  121. writer.WriteLine($"Button {kvp.Key} -> {kvp.Value}");
  122. }
  123. return writer.ToString();
  124. }
  125. private bool CheckButtonPressed(InputSummary sequence)
  126. {
  127. float distance = Math.Abs(sequence.Min - sequence.Avg) + Math.Abs(sequence.Max - sequence.Avg);
  128. return distance > 1.5; // distance range [0, 2]
  129. }
  130. }
  131. private class InputSummary
  132. {
  133. public float Min, Max, Sum, Avg;
  134. public int NumSamples;
  135. public InputSummary()
  136. {
  137. Min = float.MaxValue;
  138. Max = float.MinValue;
  139. Sum = 0;
  140. NumSamples = 0;
  141. Avg = 0;
  142. }
  143. public void AddInput(float value)
  144. {
  145. Min = Math.Min(Min, value);
  146. Max = Math.Max(Max, value);
  147. Sum += value;
  148. NumSamples += 1;
  149. Avg = Sum / NumSamples;
  150. }
  151. public override string ToString()
  152. {
  153. return $"Avg: {Avg} Min: {Min} Max: {Max} Sum: {Sum} NumSamples: {NumSamples}";
  154. }
  155. }
  156. }
  157. }