summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/InputHelper.cs
blob: 6c158258e3fbba5300c2a765c221ccaa4e960e5f (plain)
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
71
72
using System;
using StardewModdingAPI.Framework.Input;
using StardewModdingAPI.Utilities;

namespace StardewModdingAPI.Framework.ModHelpers
{
    /// <summary>Provides an API for checking and changing input state.</summary>
    internal class InputHelper : BaseHelper, IInputHelper
    {
        /*********
        ** Accessors
        *********/
        /// <summary>Manages the game's input state for the current player instance. That may not be the main player in split-screen mode.</summary>
        private readonly Func<SInputState> CurrentInputState;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="mod">The mod using this instance.</param>
        /// <param name="currentInputState">Manages the game's input state for the current player instance. That may not be the main player in split-screen mode.</param>
        public InputHelper(IModMetadata mod, Func<SInputState> currentInputState)
            : base(mod)
        {
            this.CurrentInputState = currentInputState;
        }

        /// <inheritdoc />
        public ICursorPosition GetCursorPosition()
        {
            return this.CurrentInputState().CursorPosition;
        }

        /// <inheritdoc />
        public bool IsDown(SButton button)
        {
            return this.CurrentInputState().IsDown(button);
        }

        /// <inheritdoc />
        public bool IsSuppressed(SButton button)
        {
            return this.CurrentInputState().IsSuppressed(button);
        }

        /// <inheritdoc />
        public void Suppress(SButton button)
        {
            this.CurrentInputState().OverrideButton(button, setDown: false);
        }

        /// <inheritdoc />
        public void SuppressActiveKeybinds(KeybindList keybindList)
        {
            foreach (Keybind keybind in keybindList.Keybinds)
            {
                if (!keybind.GetState().IsDown())
                    continue;

                foreach (SButton button in keybind.Buttons)
                    this.Suppress(button);
            }
        }

        /// <inheritdoc />
        public SButtonState GetState(SButton button)
        {
            return this.CurrentInputState().GetState(button);
        }
    }
}