1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

added Append and Prepend to the net452 version for better code compatibility

This commit is contained in:
Ell 2022-09-15 17:51:46 +02:00
parent fe89b28031
commit 02cf01fcb7
3 changed files with 39 additions and 7 deletions

View file

@ -9,6 +9,10 @@ using MLEM.Misc;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
using MLEM.Ui.Style; using MLEM.Ui.Style;
#if NET452
using MLEM.Extensions;
#endif
namespace MLEM.Ui { namespace MLEM.Ui {
/// <summary> /// <summary>
/// UiControls holds and manages all of the controls for a <see cref="UiSystem"/>. /// UiControls holds and manages all of the controls for a <see cref="UiSystem"/>.
@ -367,7 +371,7 @@ namespace MLEM.Ui {
protected virtual Element GetTabNextElement(bool backward) { protected virtual Element GetTabNextElement(bool backward) {
if (this.ActiveRoot == null) if (this.ActiveRoot == null)
return 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 // we can't add these checks to GetChildren because it ignores false grandchildren
.Where(c => c.CanBeSelected && c.AutoNavGroup == this.SelectedElement?.AutoNavGroup); .Where(c => c.CanBeSelected && c.AutoNavGroup == this.SelectedElement?.AutoNavGroup);
if (this.SelectedElement?.Root != this.ActiveRoot) { if (this.SelectedElement?.Root != this.ActiveRoot) {
@ -402,7 +406,7 @@ namespace MLEM.Ui {
protected virtual Element GetGamepadNextElement(Direction2 direction) { protected virtual Element GetGamepadNextElement(Direction2 direction) {
if (this.ActiveRoot == null) if (this.ActiveRoot == null)
return 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 // we can't add these checks to GetChildren because it ignores false grandchildren
.Where(c => c.CanBeSelected && c.AutoNavGroup == this.SelectedElement?.AutoNavGroup); .Where(c => c.CanBeSelected && c.AutoNavGroup == this.SelectedElement?.AutoNavGroup);
if (this.SelectedElement?.Root != this.ActiveRoot) { if (this.SelectedElement?.Root != this.ActiveRoot) {

View file

@ -25,7 +25,7 @@ namespace MLEM.Extensions {
public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<IEnumerable<T>> things) { public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<IEnumerable<T>> things) {
var combos = Enumerable.Repeat(Enumerable.Empty<T>(), 1); var combos = Enumerable.Repeat(Enumerable.Empty<T>(), 1);
foreach (var t in things) 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; return combos;
} }
@ -47,9 +47,33 @@ namespace MLEM.Extensions {
public static IEnumerable<IEnumerable<int>> IndexCombinations<T>(this IEnumerable<IEnumerable<T>> things) { public static IEnumerable<IEnumerable<int>> IndexCombinations<T>(this IEnumerable<IEnumerable<T>> things) {
var combos = Enumerable.Repeat(Enumerable.Empty<int>(), 1); var combos = Enumerable.Repeat(Enumerable.Empty<int>(), 1);
foreach (var t in things) 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; return combos;
} }
#if NET452
/// <summary>Appends a value to the end of the sequence.</summary>
/// <param name="source">A sequence of values.</param>
/// <param name="element">The value to append to <paramref name="source"/>.</param>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <returns>A new sequence that ends with <paramref name="element"/>.</returns>
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T element) {
foreach (var src in source)
yield return src;
yield return element;
}
/// <summary>Prepends a value to the beginning of the sequence.</summary>
/// <param name="source">A sequence of values.</param>
/// <param name="element">The value to prepend to <paramref name="source"/>.</param>
/// <typeparam name="T">The type of the elements of <paramref name="source"/>.</typeparam>
/// <returns>A new sequence that begins with <paramref name="element"/>.</returns>
public static IEnumerable<T> Prepend<T>(this IEnumerable<T> source, T element) {
yield return element;
foreach (var src in source)
yield return src;
}
#endif
} }
} }

View file

@ -3,6 +3,10 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
#if NET452
using MLEM.Extensions;
#endif
namespace MLEM.Input { namespace MLEM.Input {
/// <summary> /// <summary>
/// A keybind represents a generic way to trigger input. /// A keybind represents a generic way to trigger input.
@ -44,7 +48,7 @@ namespace MLEM.Input {
/// <param name="modifiers">The modifier keys that have to be held down.</param> /// <param name="modifiers">The modifier keys that have to be held down.</param>
/// <returns>This keybind, for chaining</returns> /// <returns>This keybind, for chaining</returns>
public Keybind Add(GenericInput key, params GenericInput[] modifiers) { 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; return this;
} }
@ -63,7 +67,7 @@ namespace MLEM.Input {
/// <param name="modifiers">The modifier keys that have to be held down.</param> /// <param name="modifiers">The modifier keys that have to be held down.</param>
/// <returns>This keybind, for chaining.</returns> /// <returns>This keybind, for chaining.</returns>
public Keybind Insert(int index, GenericInput key, params GenericInput[] modifiers) { 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; return this;
} }
@ -351,7 +355,7 @@ namespace MLEM.Input {
/// <param name="inputName">The function to use for determining the display name of a <see cref="GenericInput"/>. If this is null, the generic input's default <see cref="GenericInput.ToString"/> method is used.</param> /// <param name="inputName">The function to use for determining the display name of a <see cref="GenericInput"/>. If this is null, the generic input's default <see cref="GenericInput.ToString"/> method is used.</param>
/// <returns>A human-readable string representing this combination</returns> /// <returns>A human-readable string representing this combination</returns>
public string ToString(string joiner, Func<GenericInput, string> inputName = null) { public string ToString(string joiner, Func<GenericInput, string> 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()));
} }
/// <summary>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.</summary> /// <summary>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.</summary>