using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework.Input; namespace StardewModdingAPI.Framework.Input { /// Manipulates keyboard state. internal class KeyboardStateBuilder { /********* ** Fields *********/ /// The pressed buttons. private readonly HashSet PressedButtons; /********* ** Public methods *********/ /// Construct an instance. /// The initial state. public KeyboardStateBuilder(KeyboardState state) { this.PressedButtons = new HashSet(state.GetPressedKeys()); } /// Override the states for a set of buttons. /// The button state overrides. public KeyboardStateBuilder OverrideButtons(IDictionary overrides) { foreach (var pair in overrides) { if (pair.Key.TryGetKeyboard(out Keys key)) { if (pair.Value.IsDown()) this.PressedButtons.Add(key); else this.PressedButtons.Remove(key); } } return this; } /// Build an equivalent state. public KeyboardState ToState() { return new KeyboardState(this.PressedButtons.ToArray()); } } }