From 02cf01fcb74b7bf27fb3353dfe02fdd4bf79e107 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 15 Sep 2022 17:51:46 +0200 Subject: [PATCH] added Append and Prepend to the net452 version for better code compatibility --- MLEM.Ui/UiControls.cs | 8 +++++-- MLEM/Extensions/CollectionExtensions.cs | 28 +++++++++++++++++++++++-- MLEM/Input/Keybind.cs | 10 ++++++--- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index c9b69a2..d02a413 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -9,6 +9,10 @@ using MLEM.Misc; using MLEM.Ui.Elements; using MLEM.Ui.Style; +#if NET452 +using MLEM.Extensions; +#endif + namespace MLEM.Ui { /// /// UiControls holds and manages all of the controls for a . @@ -367,7 +371,7 @@ namespace MLEM.Ui { protected virtual Element GetTabNextElement(bool backward) { if (this.ActiveRoot == null) return null; - var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Concat(Enumerable.Repeat(this.ActiveRoot.Element, 1)) + var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Append(this.ActiveRoot.Element) // we can't add these checks to GetChildren because it ignores false grandchildren .Where(c => c.CanBeSelected && c.AutoNavGroup == this.SelectedElement?.AutoNavGroup); if (this.SelectedElement?.Root != this.ActiveRoot) { @@ -402,7 +406,7 @@ namespace MLEM.Ui { protected virtual Element GetGamepadNextElement(Direction2 direction) { if (this.ActiveRoot == null) return null; - var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Concat(Enumerable.Repeat(this.ActiveRoot.Element, 1)) + var children = this.ActiveRoot.Element.GetChildren(c => !c.IsHidden, true, true).Append(this.ActiveRoot.Element) // we can't add these checks to GetChildren because it ignores false grandchildren .Where(c => c.CanBeSelected && c.AutoNavGroup == this.SelectedElement?.AutoNavGroup); if (this.SelectedElement?.Root != this.ActiveRoot) { diff --git a/MLEM/Extensions/CollectionExtensions.cs b/MLEM/Extensions/CollectionExtensions.cs index 0f92f0c..f983751 100644 --- a/MLEM/Extensions/CollectionExtensions.cs +++ b/MLEM/Extensions/CollectionExtensions.cs @@ -25,7 +25,7 @@ namespace MLEM.Extensions { public static IEnumerable> Combinations(this IEnumerable> things) { var combos = Enumerable.Repeat(Enumerable.Empty(), 1); foreach (var t in things) - combos = combos.SelectMany(c => t.Select(o => c.Concat(Enumerable.Repeat(o, 1)))); + combos = combos.SelectMany(c => t.Select(c.Append)); return combos; } @@ -47,9 +47,33 @@ namespace MLEM.Extensions { public static IEnumerable> IndexCombinations(this IEnumerable> things) { var combos = Enumerable.Repeat(Enumerable.Empty(), 1); foreach (var t in things) - combos = combos.SelectMany(c => t.Select((o, i) => c.Concat(Enumerable.Repeat(i, 1)))); + combos = combos.SelectMany(c => t.Select((o, i) => c.Append(i))); return combos; } + #if NET452 + /// Appends a value to the end of the sequence. + /// A sequence of values. + /// The value to append to . + /// The type of the elements of . + /// A new sequence that ends with . + public static IEnumerable Append(this IEnumerable source, T element) { + foreach (var src in source) + yield return src; + yield return element; + } + + /// Prepends a value to the beginning of the sequence. + /// A sequence of values. + /// The value to prepend to . + /// The type of the elements of . + /// A new sequence that begins with . + public static IEnumerable Prepend(this IEnumerable source, T element) { + yield return element; + foreach (var src in source) + yield return src; + } + #endif + } } diff --git a/MLEM/Input/Keybind.cs b/MLEM/Input/Keybind.cs index b4c01e6..78ad1b4 100644 --- a/MLEM/Input/Keybind.cs +++ b/MLEM/Input/Keybind.cs @@ -3,6 +3,10 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; +#if NET452 +using MLEM.Extensions; +#endif + namespace MLEM.Input { /// /// A keybind represents a generic way to trigger input. @@ -44,7 +48,7 @@ namespace MLEM.Input { /// The modifier keys that have to be held down. /// This keybind, for chaining public Keybind Add(GenericInput key, params GenericInput[] modifiers) { - this.combinations = this.combinations.Concat(Enumerable.Repeat(new Combination(key, modifiers), 1)).ToArray(); + this.combinations = this.combinations.Append(new Combination(key, modifiers)).ToArray(); return this; } @@ -63,7 +67,7 @@ namespace MLEM.Input { /// The modifier keys that have to be held down. /// This keybind, for chaining. public Keybind Insert(int index, GenericInput key, params GenericInput[] modifiers) { - this.combinations = this.combinations.Take(index).Concat(Enumerable.Repeat(new Combination(key, modifiers), 1)).Concat(this.combinations.Skip(index)).ToArray(); + this.combinations = this.combinations.Take(index).Append(new Combination(key, modifiers)).Concat(this.combinations.Skip(index)).ToArray(); return this; } @@ -351,7 +355,7 @@ namespace MLEM.Input { /// The function to use for determining the display name of a . If this is null, the generic input's default method is used. /// A human-readable string representing this combination public string ToString(string joiner, Func inputName = null) { - return string.Join(joiner, this.Modifiers.Concat(Enumerable.Repeat(this.Key, 1)).Select(i => inputName?.Invoke(i) ?? i.ToString())); + return string.Join(joiner, this.Modifiers.Append(this.Key).Select(i => inputName?.Invoke(i) ?? i.ToString())); } /// 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.