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