using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; namespace MLEM.Input { /// /// A keybind represents a generic way to trigger input. /// A keybind is made up of multiple key combinations, one of which has to be pressed for the keybind to be triggered. /// Note that this type is serializable using . /// [DataContract] public class Keybind { [DataMember] private readonly List combinations = new List(); /// /// Adds a new key combination to this keybind that can optionally be pressed for the keybind to trigger. /// /// The key to be pressed. /// The modifier keys that have to be held down. /// This keybind, for chaining public Keybind Add(GenericInput key, params GenericInput[] modifiers) { this.combinations.Add(new Combination(key, modifiers)); return this; } /// public Keybind Add(GenericInput key, ModifierKey modifier) { return this.Add(key, modifier.GetKeys().Select(m => (GenericInput) m).ToArray()); } /// /// Clears this keybind, removing all active combinations. /// /// This keybind, for chaining public Keybind Clear() { this.combinations.Clear(); return this; } /// /// Copies all of the combinations from the given keybind into this keybind. /// Note that this doesn't this keybind, so combinations will be merged rather than replaced. /// /// The keybind to copy from /// This keybind, for chaining public Keybind CopyFrom(Keybind other) { this.combinations.AddRange(other.combinations); return this; } /// /// Returns whether this keybind is considered to be down. /// See for more information. /// /// The input handler to query the keys with /// The index of the gamepad to query, or -1 to query all gamepads /// Whether this keybind is considered to be down public bool IsDown(InputHandler handler, int gamepadIndex = -1) { return this.combinations.Any(c => c.IsDown(handler, gamepadIndex)); } /// /// Returns whether this keybind is considered to be pressed. /// See for more information. /// /// The input handler to query the keys with /// The index of the gamepad to query, or -1 to query all gamepads /// Whether this keybind is considered to be pressed public bool IsPressed(InputHandler handler, int gamepadIndex = -1) { return this.combinations.Any(c => c.IsPressed(handler, gamepadIndex)); } /// /// Returns an enumerable of all of the combinations that this keybind currently contains /// /// This keybind's combinations public IEnumerable GetCombinations() { foreach (var combination in this.combinations) yield return combination; } /// /// A key combination is a combination of a set of modifier keys and a key. /// All of the keys are instances, so they can be keyboard-, mouse- or gamepad-based. /// [DataContract] public class Combination { /// /// The inputs that have to be held down for this combination to be valid. /// If this collection is empty, there are no required modifier keys. /// [DataMember] public readonly GenericInput[] Modifiers; /// /// The input that has to be down (or pressed) for this combination to be considered down (or pressed). /// Note that needs to be empty, or all of its values need to be down, as well. /// [DataMember] public readonly GenericInput Key; internal Combination(GenericInput key, GenericInput[] modifiers) { this.Modifiers = modifiers; this.Key = key; } internal bool IsDown(InputHandler handler, int gamepadIndex = -1) { return this.IsModifierDown(handler, gamepadIndex) && handler.IsDown(this.Key, gamepadIndex); } internal bool IsPressed(InputHandler handler, int gamepadIndex = -1) { return this.IsModifierDown(handler, gamepadIndex) && handler.IsPressed(this.Key, gamepadIndex); } private bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) { return this.Modifiers.Length <= 0 || this.Modifiers.Any(m => handler.IsDown(m, gamepadIndex)); } } } }