diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-22 21:05:04 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-01-22 21:05:04 -0500 |
commit | d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6 (patch) | |
tree | 02896d970c7650d8f7c8b84f54e53eddab88b4ca /src/SMAPI/Events | |
parent | 5953fc3bd083ae0a579d2da1ad833e6163848086 (diff) | |
parent | 733750fdc4f5d16069d95880144619c0e31e8a89 (diff) | |
download | SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.tar.gz SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.tar.bz2 SMAPI-d0dc3ea6f6d03e6aabdab5f5f10a60177f0e53b6.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Events')
-rw-r--r-- | src/SMAPI/Events/ButtonsChangedEventArgs.cs | 67 | ||||
-rw-r--r-- | src/SMAPI/Events/IInputEvents.cs | 3 |
2 files changed, 70 insertions, 0 deletions
diff --git a/src/SMAPI/Events/ButtonsChangedEventArgs.cs b/src/SMAPI/Events/ButtonsChangedEventArgs.cs new file mode 100644 index 00000000..dda41692 --- /dev/null +++ b/src/SMAPI/Events/ButtonsChangedEventArgs.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI.Framework.Input; + +namespace StardewModdingAPI.Events +{ + /// <summary>Event arguments when any buttons were pressed or released.</summary> + public class ButtonsChangedEventArgs : EventArgs + { + /********* + ** Fields + *********/ + /// <summary>The buttons that were pressed, held, or released since the previous tick.</summary> + private readonly Lazy<Dictionary<SButtonState, SButton[]>> ButtonsByState; + + + /********* + ** Accessors + *********/ + /// <summary>The current cursor position.</summary> + public ICursorPosition Cursor { get; } + + /// <summary>The buttons which were pressed since the previous tick.</summary> + public IEnumerable<SButton> Pressed => this.ButtonsByState.Value[SButtonState.Pressed]; + + /// <summary>The buttons which were held since the previous tick.</summary> + public IEnumerable<SButton> Held => this.ButtonsByState.Value[SButtonState.Held]; + + /// <summary>The buttons which were released since the previous tick.</summary> + public IEnumerable<SButton> Released => this.ButtonsByState.Value[SButtonState.Released]; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="cursor">The cursor position.</param> + /// <param name="inputState">The game's current input state.</param> + internal ButtonsChangedEventArgs(ICursorPosition cursor, SInputState inputState) + { + this.Cursor = cursor; + this.ButtonsByState = new Lazy<Dictionary<SButtonState, SButton[]>>(() => this.GetButtonsByState(inputState)); + } + + + /********* + ** Private methods + *********/ + /// <summary>Get the buttons that were pressed, held, or released since the previous tick.</summary> + /// <param name="inputState">The game's current input state.</param> + private Dictionary<SButtonState, SButton[]> GetButtonsByState(SInputState inputState) + { + Dictionary<SButtonState, SButton[]> lookup = inputState.ButtonStates + .GroupBy(p => p.Value) + .ToDictionary(p => p.Key, p => p.Select(p => p.Key).ToArray()); + + foreach (var state in new[] { SButtonState.Pressed, SButtonState.Held, SButtonState.Released }) + { + if (!lookup.ContainsKey(state)) + lookup[state] = new SButton[0]; + } + + return lookup; + } + } +} diff --git a/src/SMAPI/Events/IInputEvents.cs b/src/SMAPI/Events/IInputEvents.cs index 5c40a438..081c40c0 100644 --- a/src/SMAPI/Events/IInputEvents.cs +++ b/src/SMAPI/Events/IInputEvents.cs @@ -5,6 +5,9 @@ namespace StardewModdingAPI.Events /// <summary>Events raised when the player provides input using a controller, keyboard, or mouse.</summary> public interface IInputEvents { + /// <summary>Raised after the player presses or releases any buttons on the keyboard, controller, or mouse.</summary> + event EventHandler<ButtonsChangedEventArgs> ButtonsChanged; + /// <summary>Raised after the player presses a button on the keyboard, controller, or mouse.</summary> event EventHandler<ButtonPressedEventArgs> ButtonPressed; |