using System; using System.Collections.Generic; using Microsoft.Xna.Framework.Input; namespace MLEM.Misc { /// /// A helper class that allows easier usage of values. /// public static class EnumHelper { /// /// All values of the enum. /// public static readonly Buttons[] Buttons = EnumHelper.GetValues(); /// /// All values of the enum. /// public static readonly Keys[] Keys = EnumHelper.GetValues(); /// /// Returns an array containing all of the values of the given enum type. /// Note that this method is a version-independent equivalent of .NET 5's Enum.GetValues<TEnum>. /// /// The type whose enum to get /// An enumerable of the values of the enum, in declaration order. public static T[] GetValues() where T : struct, Enum { #if NET6_0_OR_GREATER return Enum.GetValues(); #else return (T[]) Enum.GetValues(typeof(T)); #endif } /// /// Returns all of the defined values from the given enum type which are contained in . /// Note that, if combined flags are defined in , and contains them, they will also be returned. /// /// The combined flags whose individual flags to return. /// Whether the enum value 0 should also be returned, if contains one. /// The type of enum. /// All of the flags that make up . public static IEnumerable GetFlags(T combinedFlag, bool includeZero = true) where T : struct, Enum { foreach (var flag in EnumHelper.GetValues()) { if (combinedFlag.HasFlag(flag) && (includeZero || Convert.ToInt64(flag) != 0)) yield return flag; } } } }