2019-08-09 22:04:26 +02:00
using System ;
2022-10-26 15:02:33 +02:00
using System.Collections.Generic ;
2019-09-09 20:56:10 +02:00
using Microsoft.Xna.Framework.Input ;
2019-08-09 22:04:26 +02:00
namespace MLEM.Misc {
2020-05-21 17:21:34 +02:00
/// <summary>
/// A helper class that allows easier usage of <see cref="Enum"/> values.
/// </summary>
2022-10-31 13:20:26 +01:00
[Obsolete("EnumHelper has been moved into the DynamicEnums library: https://www.nuget.org/packages/DynamicEnums")]
2019-08-09 22:04:26 +02:00
public static class EnumHelper {
2020-05-21 17:21:34 +02:00
/// <summary>
/// All values of the <see cref="Buttons"/> enum.
/// </summary>
2022-10-31 13:20:26 +01:00
[Obsolete("This field has been moved to InputHandler.AllButtons")]
2022-07-29 19:52:01 +02:00
public static readonly Buttons [ ] Buttons = EnumHelper . GetValues < Buttons > ( ) ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// All values of the <see cref="Keys"/> enum.
/// </summary>
2022-10-31 13:20:26 +01:00
[Obsolete("This field has been moved to InputHandler.AllKeys")]
2022-07-29 19:52:01 +02:00
public static readonly Keys [ ] Keys = EnumHelper . GetValues < Keys > ( ) ;
2019-09-09 20:56:10 +02:00
2020-05-21 17:21:34 +02:00
/// <summary>
2022-07-29 19:52:01 +02:00
/// 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 <c>Enum.GetValues<TEnum></c>.
2020-05-21 17:21:34 +02:00
/// </summary>
/// <typeparam name="T">The type whose enum to get</typeparam>
/// <returns>An enumerable of the values of the enum, in declaration order.</returns>
2022-09-14 19:24:00 +02:00
public static T [ ] GetValues < T > ( ) where T : struct , Enum {
2022-12-13 13:11:36 +01:00
#if NET6_0_OR_GREATER
2022-09-14 19:24:00 +02:00
return Enum . GetValues < T > ( ) ;
2022-12-13 13:11:36 +01:00
#else
2022-07-29 19:52:01 +02:00
return ( T [ ] ) Enum . GetValues ( typeof ( T ) ) ;
2022-12-13 13:11:36 +01:00
#endif
2019-08-09 22:04:26 +02:00
}
2022-10-26 15:02:33 +02:00
/// <summary>
/// Returns all of the defined values from the given enum type <typeparamref name="T"/> which are contained in <paramref name="combinedFlag"/>.
/// Note that, if combined flags are defined in <typeparamref name="T"/>, and <paramref name="combinedFlag"/> contains them, they will also be returned.
/// </summary>
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
/// <param name="includeZero">Whether the enum value 0 should also be returned, if <typeparamref name="T"/> contains one.</param>
/// <typeparam name="T">The type of enum.</typeparam>
/// <returns>All of the flags that make up <paramref name="combinedFlag"/>.</returns>
public static IEnumerable < T > GetFlags < T > ( T combinedFlag , bool includeZero = true ) where T : struct , Enum {
foreach ( var flag in EnumHelper . GetValues < T > ( ) ) {
if ( combinedFlag . HasFlag ( flag ) & & ( includeZero | | Convert . ToInt64 ( flag ) ! = 0 ) )
yield return flag ;
}
}
2022-10-26 23:34:30 +02:00
/// <summary>
/// Returns all of the defined unique flags from the given enum type <typeparamref name="T"/> which are contained in <paramref name="combinedFlag"/>.
/// Any combined flags (flags that aren't powers of two) which are defined in <typeparamref name="T"/> will not be returned.
/// </summary>
/// <param name="combinedFlag">The combined flags whose individual flags to return.</param>
/// <typeparam name="T">The type of enum.</typeparam>
/// <returns>All of the unique flags that make up <paramref name="combinedFlag"/>.</returns>
public static IEnumerable < T > GetUniqueFlags < T > ( T combinedFlag ) where T : struct , Enum {
var uniqueFlag = 1 ;
foreach ( var flag in EnumHelper . GetValues < T > ( ) ) {
var flagValue = Convert . ToInt64 ( flag ) ;
// GetValues is always ordered by binary value, so we can be sure that the next flag is bigger than the last
while ( uniqueFlag < flagValue )
uniqueFlag < < = 1 ;
if ( flagValue = = uniqueFlag & & combinedFlag . HasFlag ( flag ) )
yield return flag ;
}
}
2019-08-09 22:04:26 +02:00
}
2022-06-17 18:23:47 +02:00
}