aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Input/GamepadStateSnapshot.cs
blob: 3de08f574ad8086681f1d5486d8d75afcfd1c299 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Ryujinx.Common.Memory;
using System.Runtime.CompilerServices;

namespace Ryujinx.Input
{
    /// <summary>
    /// A snapshot of a <see cref="IGamepad"/>.
    /// </summary>
    public struct GamepadStateSnapshot
    {
        // NOTE: Update Array size if JoystickInputId is changed.
        private Array3<Array2<float>> _joysticksState;
        // NOTE: Update Array size if GamepadInputId is changed.
        private Array28<bool> _buttonsState;

        /// <summary>
        /// Create a new instance of <see cref="GamepadStateSnapshot"/>.
        /// </summary>
        /// <param name="joysticksState">The joysticks state</param>
        /// <param name="buttonsState">The buttons state</param>
        public GamepadStateSnapshot(Array3<Array2<float>> joysticksState, Array28<bool> buttonsState)
        {
            _joysticksState = joysticksState;
            _buttonsState = buttonsState;
        }

        /// <summary>
        /// Check if a given input button is pressed.
        /// </summary>
        /// <param name="inputId">The button id</param>
        /// <returns>True if the given button is pressed</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public bool IsPressed(GamepadButtonInputId inputId) => _buttonsState[(int)inputId];


        /// <summary>
        /// Set the state of a given button.
        /// </summary>
        /// <param name="inputId">The button id</param>
        /// <param name="value">The state to assign for the given button.</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void SetPressed(GamepadButtonInputId inputId, bool value) => _buttonsState[(int)inputId] = value;

        /// <summary>
        /// Get the values of a given input joystick.
        /// </summary>
        /// <param name="inputId">The stick id</param>
        /// <returns>The values of the given input joystick</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public (float, float) GetStick(StickInputId inputId)
        {
            var result = _joysticksState[(int)inputId];

            return (result[0], result[1]);
        }

        /// <summary>
        /// Set the values of a given input joystick.
        /// </summary>
        /// <param name="inputId">The stick id</param>
        /// <param name="x">The x axis value</param>
        /// <param name="y">The y axis value</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void SetStick(StickInputId inputId, float x, float y)
        {
            _joysticksState[(int)inputId][0] = x;
            _joysticksState[(int)inputId][1] = y;
        }
    }
}