diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-03-08 14:53:41 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-03-08 14:53:41 -0400 |
commit | e80b7712f7e4c78a5b016289ca19efaf7d39cd8b (patch) | |
tree | 5ca5277b1755504a1e95b655b16ff696114d26b8 /src/SMAPI/Framework/Input/KeyboardStateBuilder.cs | |
parent | e39b9e0d699079edfbcf8595d7499aff894578b6 (diff) | |
download | SMAPI-e80b7712f7e4c78a5b016289ca19efaf7d39cd8b.tar.gz SMAPI-e80b7712f7e4c78a5b016289ca19efaf7d39cd8b.tar.bz2 SMAPI-e80b7712f7e4c78a5b016289ca19efaf7d39cd8b.zip |
encapsulate logic for each input type
Diffstat (limited to 'src/SMAPI/Framework/Input/KeyboardStateBuilder.cs')
-rw-r--r-- | src/SMAPI/Framework/Input/KeyboardStateBuilder.cs | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs b/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs index 12d780f6..f95a28bf 100644 --- a/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs +++ b/src/SMAPI/Framework/Input/KeyboardStateBuilder.cs @@ -4,24 +4,40 @@ using Microsoft.Xna.Framework.Input; namespace StardewModdingAPI.Framework.Input { - /// <summary>Manipulates keyboard state.</summary> - internal class KeyboardStateBuilder + /// <summary>Manages keyboard state.</summary> + internal class KeyboardStateBuilder : IInputStateBuilder<KeyboardStateBuilder, KeyboardState> { /********* ** Fields *********/ + /// <summary>The underlying keyboard state.</summary> + private KeyboardState? State; + /// <summary>The pressed buttons.</summary> - private readonly HashSet<Keys> PressedButtons; + private readonly HashSet<Keys> PressedButtons = new HashSet<Keys>(); /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> - /// <param name="state">The initial state.</param> - public KeyboardStateBuilder(KeyboardState state) + /// <param name="state">The initial state, or <c>null</c> to get the latest state.</param> + public KeyboardStateBuilder(KeyboardState? state = null) { - this.PressedButtons = new HashSet<Keys>(state.GetPressedKeys()); + this.Reset(state); + } + + /// <summary>Reset the tracked state.</summary> + /// <param name="state">The state from which to reset, or <c>null</c> to get the latest state.</param> + public KeyboardStateBuilder Reset(KeyboardState? state = null) + { + this.State = state ??= Keyboard.GetState(); + + this.PressedButtons.Clear(); + foreach (var button in state.Value.GetPressedKeys()) + this.PressedButtons.Add(button); + + return this; } /// <summary>Override the states for a set of buttons.</summary> @@ -32,6 +48,8 @@ namespace StardewModdingAPI.Framework.Input { if (pair.Key.TryGetKeyboard(out Keys key)) { + this.State = null; + if (pair.Value.IsDown()) this.PressedButtons.Add(key); else @@ -42,10 +60,19 @@ namespace StardewModdingAPI.Framework.Input return this; } - /// <summary>Build an equivalent state.</summary> - public KeyboardState ToState() + /// <summary>Get the currently pressed buttons.</summary> + public IEnumerable<SButton> GetPressedButtons() + { + foreach (Keys key in this.PressedButtons) + yield return key.ToSButton(); + } + + /// <summary>Get the equivalent state.</summary> + public KeyboardState GetState() { - return new KeyboardState(this.PressedButtons.ToArray()); + return + this.State + ?? (this.State = new KeyboardState(this.PressedButtons.ToArray())).Value; } } } |