From 7e280a066db92c74e957e2a694c922d4c3eae017 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 Jan 2021 21:47:05 -0500 Subject: add Input.ButtonsChanged event (#744) --- src/SMAPI/Events/ButtonsChangedEventArgs.cs | 67 +++++++++++++++++++++++++++++ src/SMAPI/Events/IInputEvents.cs | 3 ++ 2 files changed, 70 insertions(+) create mode 100644 src/SMAPI/Events/ButtonsChangedEventArgs.cs (limited to 'src/SMAPI/Events') 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 +{ + /// Event arguments when any buttons were pressed or released. + public class ButtonsChangedEventArgs : EventArgs + { + /********* + ** Fields + *********/ + /// The buttons that were pressed, held, or released since the previous tick. + private readonly Lazy> ButtonsByState; + + + /********* + ** Accessors + *********/ + /// The current cursor position. + public ICursorPosition Cursor { get; } + + /// The buttons which were pressed since the previous tick. + public IEnumerable Pressed => this.ButtonsByState.Value[SButtonState.Pressed]; + + /// The buttons which were held since the previous tick. + public IEnumerable Held => this.ButtonsByState.Value[SButtonState.Held]; + + /// The buttons which were released since the previous tick. + public IEnumerable Released => this.ButtonsByState.Value[SButtonState.Released]; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The cursor position. + /// The game's current input state. + internal ButtonsChangedEventArgs(ICursorPosition cursor, SInputState inputState) + { + this.Cursor = cursor; + this.ButtonsByState = new Lazy>(() => this.GetButtonsByState(inputState)); + } + + + /********* + ** Private methods + *********/ + /// Get the buttons that were pressed, held, or released since the previous tick. + /// The game's current input state. + private Dictionary GetButtonsByState(SInputState inputState) + { + Dictionary 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 /// Events raised when the player provides input using a controller, keyboard, or mouse. public interface IInputEvents { + /// Raised after the player presses or releases any buttons on the keyboard, controller, or mouse. + event EventHandler ButtonsChanged; + /// Raised after the player presses a button on the keyboard, controller, or mouse. event EventHandler ButtonPressed; -- cgit