Add storageType parameter to FromEnum, default value is consistent with Tiled

This commit is contained in:
Daniel Cronqvist 2024-11-16 18:35:21 +01:00
parent 88352cfa45
commit 35c6ba5002

View file

@ -51,8 +51,9 @@ public class CustomEnumDefinition : ICustomTypeDefinition
/// Creates a custom enum definition from the specified enum type. /// Creates a custom enum definition from the specified enum type.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="storageType">The storage type of the custom enum. Defaults to <see cref="CustomEnumStorageType.String"/> to be consistent with Tiled.</param>
/// <returns></returns> /// <returns></returns>
public static CustomEnumDefinition FromEnum<T>() where T : Enum public static CustomEnumDefinition FromEnum<T>(CustomEnumStorageType storageType = CustomEnumStorageType.String) where T : Enum
{ {
var type = typeof(T); var type = typeof(T);
var isFlags = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0; var isFlags = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0;
@ -60,7 +61,7 @@ public class CustomEnumDefinition : ICustomTypeDefinition
return new CustomEnumDefinition return new CustomEnumDefinition
{ {
Name = type.Name, Name = type.Name,
StorageType = CustomEnumStorageType.Int, StorageType = storageType,
Values = Enum.GetNames(type).ToList(), Values = Enum.GetNames(type).ToList(),
ValueAsFlags = isFlags ValueAsFlags = isFlags
}; };
@ -69,8 +70,10 @@ public class CustomEnumDefinition : ICustomTypeDefinition
/// <summary> /// <summary>
/// Creates a custom enum definition from the specified enum type. /// Creates a custom enum definition from the specified enum type.
/// </summary> /// </summary>
/// <param name="type">The enum type to create a custom enum definition from.</param>
/// <param name="storageType">The storage type of the custom enum. Defaults to <see cref="CustomEnumStorageType.String"/> to be consistent with Tiled.</param>
/// <returns></returns> /// <returns></returns>
public static CustomEnumDefinition FromEnum(Type type) public static CustomEnumDefinition FromEnum(Type type, CustomEnumStorageType storageType = CustomEnumStorageType.String)
{ {
if (!type.IsEnum) if (!type.IsEnum)
throw new ArgumentException("Type must be an enum.", nameof(type)); throw new ArgumentException("Type must be an enum.", nameof(type));
@ -80,7 +83,7 @@ public class CustomEnumDefinition : ICustomTypeDefinition
return new CustomEnumDefinition return new CustomEnumDefinition
{ {
Name = type.Name, Name = type.Name,
StorageType = CustomEnumStorageType.Int, StorageType = storageType,
Values = Enum.GetNames(type).ToList(), Values = Enum.GetNames(type).ToList(),
ValueAsFlags = isFlags ValueAsFlags = isFlags
}; };