1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-15 05:38:46 +02: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
Additions
- Added TokenizedString.Realign
- Added EnumHelper.GetFlags
- **Added the ability to find paths to one of multiple goals using AStar**
Improvements
@ -59,6 +60,7 @@ Fixes
Additions
- Added data, from, and copy instructions to DataTextureAtlas
- Added the ability to add additional regions to a RuntimeTexturePacker after packing
- Added DynamicEnum.GetFlags
Improvements
- 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;
}
/// <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>
/// Returns the bitwise OR (|) combination of the two dynamic enum values
/// </summary>

View file

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