1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

Added EnumHelper and DynamicEnum GetFlags

This commit is contained in:
Ell 2022-10-26 15:02:33 +02:00
parent d5e5d1d536
commit 7ab76d239d
3 changed files with 33 additions and 0 deletions

View file

@ -14,6 +14,7 @@ Jump to version:
### MLEM ### MLEM
Additions Additions
- Added TokenizedString.Realign - Added TokenizedString.Realign
- Added EnumHelper.GetFlags
- **Added the ability to find paths to one of multiple goals using AStar** - **Added the ability to find paths to one of multiple goals using AStar**
Improvements Improvements
@ -59,6 +60,7 @@ Fixes
Additions Additions
- Added data, from, and copy instructions to DataTextureAtlas - Added data, from, and copy instructions to DataTextureAtlas
- Added the ability to add additional regions to a RuntimeTexturePacker after packing - Added the ability to add additional regions to a RuntimeTexturePacker after packing
- Added DynamicEnum.GetFlags
Improvements Improvements
- Allow data texture atlas pivots and offsets to be negative - Allow data texture atlas pivots and offsets to be negative

View file

@ -174,6 +174,21 @@ namespace MLEM.Data {
return DynamicEnum.GetStorage(type).Values.Values; return DynamicEnum.GetStorage(type).Values.Values;
} }
/// <summary>
/// Returns all of the defined values from the given dynamic 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 : DynamicEnum {
foreach (var flag in DynamicEnum.GetValues<T>()) {
if (combinedFlag.HasFlag(flag) && (includeZero || DynamicEnum.GetValue(flag) != BigInteger.Zero))
yield return flag;
}
}
/// <summary> /// <summary>
/// Returns the bitwise OR (|) combination of the two dynamic enum values /// Returns the bitwise OR (|) combination of the two dynamic enum values
/// </summary> /// </summary>

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
namespace MLEM.Misc { namespace MLEM.Misc {
@ -30,5 +31,20 @@ namespace MLEM.Misc {
#endif #endif
} }
/// <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;
}
}
} }
} }