GamepadStateSnapshot.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Ryujinx.Common.Memory;
  2. using System.Runtime.CompilerServices;
  3. namespace Ryujinx.Input
  4. {
  5. /// <summary>
  6. /// A snapshot of a <see cref="IGamepad"/>.
  7. /// </summary>
  8. public struct GamepadStateSnapshot
  9. {
  10. // NOTE: Update Array size if JoystickInputId is changed.
  11. private Array3<Array2<float>> _joysticksState;
  12. // NOTE: Update Array size if GamepadInputId is changed.
  13. private Array28<bool> _buttonsState;
  14. /// <summary>
  15. /// Create a new instance of <see cref="GamepadStateSnapshot"/>.
  16. /// </summary>
  17. /// <param name="joysticksState">The joysticks state</param>
  18. /// <param name="buttonsState">The buttons state</param>
  19. public GamepadStateSnapshot(Array3<Array2<float>> joysticksState, Array28<bool> buttonsState)
  20. {
  21. _joysticksState = joysticksState;
  22. _buttonsState = buttonsState;
  23. }
  24. /// <summary>
  25. /// Check if a given input button is pressed.
  26. /// </summary>
  27. /// <param name="inputId">The button id</param>
  28. /// <returns>True if the given button is pressed</returns>
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public bool IsPressed(GamepadButtonInputId inputId) => _buttonsState[(int)inputId];
  31. /// <summary>
  32. /// Set the state of a given button.
  33. /// </summary>
  34. /// <param name="inputId">The button id</param>
  35. /// <param name="value">The state to assign for the given button.</param>
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public void SetPressed(GamepadButtonInputId inputId, bool value) => _buttonsState[(int)inputId] = value;
  38. /// <summary>
  39. /// Get the values of a given input joystick.
  40. /// </summary>
  41. /// <param name="inputId">The stick id</param>
  42. /// <returns>The values of the given input joystick</returns>
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public (float, float) GetStick(StickInputId inputId)
  45. {
  46. var result = _joysticksState[(int)inputId];
  47. return (result[0], result[1]);
  48. }
  49. /// <summary>
  50. /// Set the values of a given input joystick.
  51. /// </summary>
  52. /// <param name="inputId">The stick id</param>
  53. /// <param name="x">The x axis value</param>
  54. /// <param name="y">The y axis value</param>
  55. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  56. public void SetStick(StickInputId inputId, float x, float y)
  57. {
  58. _joysticksState[(int)inputId][0] = x;
  59. _joysticksState[(int)inputId][1] = y;
  60. }
  61. }
  62. }