diff --git a/CHANGELOG.md b/CHANGELOG.md index 8380f02..6f136ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,8 +20,10 @@ Additions - **Added the ability for formatted (tokenized) strings to be drawn with custom rotation, origin and flipping** - Added a RectangleF.FromCorners overload that accepts points - Added indexers and Count to SpriteAnimation and SpriteAnimationGroup +- Added a Keybind constructor that accepts a set of combinations Improvements +- **Made the Keybind.Combinations collection a public property** - Allow NumberExtensions.GetPoints to include bottom and right coordinates - Allow AutoTiling overlayTextures to return null texture regions diff --git a/MLEM/Input/Keybind.cs b/MLEM/Input/Keybind.cs index 2d58fd1..d39a726 100644 --- a/MLEM/Input/Keybind.cs +++ b/MLEM/Input/Keybind.cs @@ -20,8 +20,12 @@ namespace MLEM.Input { private static readonly Combination[] EmptyCombinations = new Combination[0]; private static readonly GenericInput[] EmptyInputs = new GenericInput[0]; + /// + /// The combinations that make up this keybind. + /// This collection can be modified using , , , , etc. + /// [DataMember] - private Combination[] combinations = Keybind.EmptyCombinations; + public Combination[] Combinations { get; private set; } = Keybind.EmptyCombinations; /// /// Creates a new keybind and adds the given key and modifiers using @@ -37,6 +41,14 @@ namespace MLEM.Input { this.Add(key, modifier); } + /// + /// Creates a new keybind with the given . + /// + /// The combinations to initialize this keybind with. + public Keybind(params Combination[] combinations) { + this.Combinations = combinations.ToArray(); + } + /// /// Creates a new keybind with no default combinations /// @@ -58,7 +70,7 @@ namespace MLEM.Input { /// The combination to add. /// This keybind, for chaining public Keybind Add(Combination combination) { - this.combinations = this.combinations.Append(combination).ToArray(); + this.Combinations = this.Combinations.Append(combination).ToArray(); return this; } @@ -87,7 +99,7 @@ namespace MLEM.Input { /// The combination to insert. /// This keybind, for chaining. public Keybind Insert(int index, Combination combination) { - this.combinations = this.combinations.Take(index).Append(combination).Concat(this.combinations.Skip(index)).ToArray(); + this.Combinations = this.Combinations.Take(index).Append(combination).Concat(this.Combinations.Skip(index)).ToArray(); return this; } @@ -103,7 +115,7 @@ namespace MLEM.Input { /// /// This keybind, for chaining public Keybind Clear() { - this.combinations = Keybind.EmptyCombinations; + this.Combinations = Keybind.EmptyCombinations; return this; } @@ -113,7 +125,7 @@ namespace MLEM.Input { /// The predicate to match against /// This keybind, for chaining public Keybind Remove(Func predicate) { - this.combinations = this.combinations.Where((c, i) => !predicate(c, i)).ToArray(); + this.Combinations = this.Combinations.Where((c, i) => !predicate(c, i)).ToArray(); return this; } @@ -124,7 +136,7 @@ namespace MLEM.Input { /// The keybind to copy from /// This keybind, for chaining public Keybind CopyFrom(Keybind other) { - this.combinations = this.combinations.Concat(other.combinations).ToArray(); + this.Combinations = this.Combinations.Concat(other.Combinations).ToArray(); return this; } @@ -136,7 +148,7 @@ namespace MLEM.Input { /// 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) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.IsDown(handler, gamepadIndex)) return true; } @@ -151,7 +163,7 @@ namespace MLEM.Input { /// The index of the gamepad to query, or -1 to query all gamepads /// Whether this keybind was considered to be down public bool WasDown(InputHandler handler, int gamepadIndex = -1) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.WasDown(handler, gamepadIndex)) return true; } @@ -166,7 +178,7 @@ namespace MLEM.Input { /// 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) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.IsPressed(handler, gamepadIndex)) return true; } @@ -181,7 +193,7 @@ namespace MLEM.Input { /// The index of the gamepad to query, or -1 to query all gamepads /// Whether this keybind is considered to be pressed public bool IsPressedAvailable(InputHandler handler, int gamepadIndex = -1) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.IsPressedAvailable(handler, gamepadIndex)) return true; } @@ -196,7 +208,7 @@ namespace MLEM.Input { /// The index of the gamepad to query, or -1 to query all gamepads /// Whether this keybind is considered to be pressed public bool TryConsumePressed(InputHandler handler, int gamepadIndex = -1) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.TryConsumePressed(handler, gamepadIndex)) return true; } @@ -211,7 +223,7 @@ namespace MLEM.Input { /// The index of the gamepad to query, or -1 to query all gamepads /// Whether any of this keyboard's modifier keys are down public bool IsModifierDown(InputHandler handler, int gamepadIndex = -1) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.IsModifierDown(handler, gamepadIndex)) return true; } @@ -226,7 +238,7 @@ namespace MLEM.Input { /// The index of the gamepad to query, or -1 to query all gamepads /// Whether any of this keyboard's modifier keys were down public bool WasModifierDown(InputHandler handler, int gamepadIndex = -1) { - foreach (var combination in this.combinations) { + foreach (var combination in this.Combinations) { if (combination.WasModifierDown(handler, gamepadIndex)) return true; } @@ -242,7 +254,7 @@ namespace MLEM.Input { /// The resulting down time, or if the input is not being held. [Obsolete("This method is deprecated, as it does not query Modifiers or InverseModifiers. Use InputHandler.GetDownTime or custom handling instead.")] public TimeSpan GetDownTime(InputHandler handler, int gamepadIndex = -1) { - return this.combinations.Max(c => c.GetDownTime(handler, gamepadIndex)); + return this.Combinations.Max(c => c.GetDownTime(handler, gamepadIndex)); } /// @@ -254,7 +266,7 @@ namespace MLEM.Input { /// The resulting up time, or if the input is being held. [Obsolete("This method is deprecated, as it does not query Modifiers or InverseModifiers. Use InputHandler.GetUpTime or custom handling instead.")] public TimeSpan GetUpTime(InputHandler handler, int gamepadIndex = -1) { - return this.combinations.Min(c => c.GetUpTime(handler, gamepadIndex)); + return this.Combinations.Min(c => c.GetUpTime(handler, gamepadIndex)); } /// @@ -266,7 +278,7 @@ namespace MLEM.Input { /// The resulting up time, or if the input has never been pressed, or is currently pressed. [Obsolete("This method is deprecated, as it does not query Modifiers or InverseModifiers. Use InputHandler.GetTimeSincePress or custom handling instead.")] public TimeSpan GetTimeSincePress(InputHandler handler, int gamepadIndex = -1) { - return this.combinations.Min(c => c.GetTimeSincePress(handler, gamepadIndex)); + return this.Combinations.Min(c => c.GetTimeSincePress(handler, gamepadIndex)); } /// @@ -274,7 +286,7 @@ namespace MLEM.Input { /// /// This keybind's combinations public IEnumerable GetCombinations() { - foreach (var combination in this.combinations) + foreach (var combination in this.Combinations) yield return combination; } @@ -285,8 +297,8 @@ namespace MLEM.Input { /// The combination, or default if this method returns false. /// Whether the combination could be successfully retrieved or the index was out of bounds of this keybind's combination collection. public bool TryGetCombination(int index, out Combination combination) { - if (index >= 0 && index < this.combinations.Length) { - combination = this.combinations[index]; + if (index >= 0 && index < this.Combinations.Length) { + combination = this.Combinations[index]; return true; } else { combination = default; @@ -303,7 +315,7 @@ namespace MLEM.Input { /// The function to use for determining the display name of generic inputs, see /// A human-readable string representing this keybind public string ToString(string joiner, string combinationJoiner = " + ", Func inputName = null) { - return string.Join(joiner, this.combinations.Select(c => c.ToString(combinationJoiner, inputName))); + return string.Join(joiner, this.Combinations.Select(c => c.ToString(combinationJoiner, inputName))); } /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. @@ -312,7 +324,7 @@ namespace MLEM.Input { /// /// Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order. public int CompareTo(Keybind other) { - return this.combinations.Sum(c => other.combinations.Sum(c.CompareTo)); + return this.Combinations.Sum(c => other.Combinations.Sum(c.CompareTo)); } /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.