From c0ba24456ba741ad5b072c033f9b0537ee74ad04 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 2 Jun 2018 16:15:49 -0400 Subject: remove suppression from new events, add convenience methods (#310) Mods should use the new input API to suppress input instead. --- src/SMAPI/Events/InputButtonPressedEventArgs.cs | 36 +++++++++++++----------- src/SMAPI/Events/InputButtonReleasedEventArgs.cs | 36 +++++++++++++----------- src/SMAPI/Framework/SGame.cs | 4 +-- 3 files changed, 42 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/SMAPI/Events/InputButtonPressedEventArgs.cs b/src/SMAPI/Events/InputButtonPressedEventArgs.cs index c8d55cd4..002f7cf1 100644 --- a/src/SMAPI/Events/InputButtonPressedEventArgs.cs +++ b/src/SMAPI/Events/InputButtonPressedEventArgs.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using StardewModdingAPI.Framework.Input; namespace StardewModdingAPI.Events { @@ -9,8 +9,8 @@ namespace StardewModdingAPI.Events /********* ** Properties *********/ - /// The buttons to suppress. - private readonly HashSet SuppressButtons; + /// The game's current input state. + private readonly SInputState InputState; /********* @@ -22,9 +22,6 @@ namespace StardewModdingAPI.Events /// The current cursor position. public ICursorPosition Cursor { get; } - /// Whether a mod has indicated the key was already handled. - public bool IsSuppressed => this.SuppressButtons.Contains(this.Button); - /********* ** Public methods @@ -32,25 +29,32 @@ namespace StardewModdingAPI.Events /// Construct an instance. /// The button on the controller, keyboard, or mouse. /// The cursor position. - /// The buttons to suppress. - public InputButtonPressedArgsInput(SButton button, ICursorPosition cursor, HashSet suppressButtons) + /// The game's current input state. + internal InputButtonPressedArgsInput(SButton button, ICursorPosition cursor, SInputState inputState) { this.Button = button; this.Cursor = cursor; - this.SuppressButtons = suppressButtons; + this.InputState = inputState; + } + + /// Whether a mod has indicated the key was already handled, so the game should handle it. + public bool IsSuppressed() + { + return this.IsSuppressed(this.Button); } - /// Prevent the game from handling the current button press. This doesn't prevent other mods from receiving the event. - public void SuppressButton() + /// Whether a mod has indicated the key was already handled, so the game should handle it. + /// The button to check. + public bool IsSuppressed(SButton button) { - this.SuppressButton(this.Button); + return this.InputState.SuppressButtons.Contains(button); } - /// Prevent the game from handling a button press. This doesn't prevent other mods from receiving the event. - /// The button to suppress. - public void SuppressButton(SButton button) + /// Get whether a given button was pressed or held. + /// The button to check. + public bool IsDown(SButton button) { - this.SuppressButtons.Add(button); + return this.InputState.IsDown(button); } } } diff --git a/src/SMAPI/Events/InputButtonReleasedEventArgs.cs b/src/SMAPI/Events/InputButtonReleasedEventArgs.cs index 863fab6a..bc5e4a89 100644 --- a/src/SMAPI/Events/InputButtonReleasedEventArgs.cs +++ b/src/SMAPI/Events/InputButtonReleasedEventArgs.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using StardewModdingAPI.Framework.Input; namespace StardewModdingAPI.Events { @@ -9,8 +9,8 @@ namespace StardewModdingAPI.Events /********* ** Properties *********/ - /// The buttons to suppress. - private readonly HashSet SuppressButtons; + /// The game's current input state. + private readonly SInputState InputState; /********* @@ -22,9 +22,6 @@ namespace StardewModdingAPI.Events /// The current cursor position. public ICursorPosition Cursor { get; } - /// Whether a mod has indicated the key was already handled. - public bool IsSuppressed => this.SuppressButtons.Contains(this.Button); - /********* ** Public methods @@ -32,25 +29,32 @@ namespace StardewModdingAPI.Events /// Construct an instance. /// The button on the controller, keyboard, or mouse. /// The cursor position. - /// The buttons to suppress. - public InputButtonReleasedArgsInput(SButton button, ICursorPosition cursor, HashSet suppressButtons) + /// The game's current input state. + internal InputButtonReleasedArgsInput(SButton button, ICursorPosition cursor, SInputState inputState) { this.Button = button; this.Cursor = cursor; - this.SuppressButtons = suppressButtons; + this.InputState = inputState; + } + + /// Whether a mod has indicated the key was already handled, so the game should handle it. + public bool IsSuppressed() + { + return this.IsSuppressed(this.Button); } - /// Prevent the game from handling the current button press. This doesn't prevent other mods from receiving the event. - public void SuppressButton() + /// Whether a mod has indicated the key was already handled, so the game should handle it. + /// The button to check. + public bool IsSuppressed(SButton button) { - this.SuppressButton(this.Button); + return this.InputState.SuppressButtons.Contains(button); } - /// Prevent the game from handling a button press. This doesn't prevent other mods from receiving the event. - /// The button to suppress. - public void SuppressButton(SButton button) + /// Get whether a given button was pressed or held. + /// The button to check. + public bool IsDown(SButton button) { - this.SuppressButtons.Add(button); + return this.InputState.IsDown(button); } } } diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 560b54a4..ae80f680 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -480,7 +480,7 @@ namespace StardewModdingAPI.Framework if (status == InputStatus.Pressed) { - this.Events.Input_ButtonPressed.Raise(new InputButtonPressedArgsInput(button, cursor, inputState.SuppressButtons)); + this.Events.Input_ButtonPressed.Raise(new InputButtonPressedArgsInput(button, cursor, inputState)); this.Events.Legacy_Input_ButtonPressed.Raise(new EventArgsInput(button, cursor, inputState.SuppressButtons)); // legacy events @@ -499,7 +499,7 @@ namespace StardewModdingAPI.Framework } else if (status == InputStatus.Released) { - this.Events.Input_ButtonReleased.Raise(new InputButtonReleasedArgsInput(button, cursor, inputState.SuppressButtons)); + this.Events.Input_ButtonReleased.Raise(new InputButtonReleasedArgsInput(button, cursor, inputState)); this.Events.Legacy_Input_ButtonReleased.Raise(new EventArgsInput(button, cursor, inputState.SuppressButtons)); // legacy events -- cgit