Merge pull request #54 from dcronqvist/from-enum-storage-type

Add storage type parameter to CustomEnumDefinition.FromEnum
This commit is contained in:
dcronqvist 2024-11-16 19:16:44 +01:00 committed by GitHub
commit 23d218bac7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 22 deletions

View file

@ -138,7 +138,7 @@ The equivalent definition in DotTiled would look like the following:
var entityTypeDefinition = new CustomEnumDefinition
{
Name = "EntityType",
StorageType = CustomEnumStorageType.Int,
StorageType = CustomEnumStorageType.String,
ValueAsFlags = false,
Values = [
"Bomb",
@ -149,7 +149,7 @@ var entityTypeDefinition = new CustomEnumDefinition
};
```
Similarly to custom class definitions, you can also automatically generate custom enum definitions from C# enums. This is done by using the <xref:DotTiled.CustomEnumDefinition.FromEnum``1> method, or one of its overloads. This method will generate a <xref:DotTiled.CustomEnumDefinition> from a given C# enum, and you can then use this definition when loading your maps.
Similarly to custom class definitions, you can also automatically generate custom enum definitions from C# enums. This is done by using the <xref:DotTiled.CustomEnumDefinition.FromEnum``1(DotTiled.CustomEnumStorageType)> method, or one of its overloads. This method will generate a <xref:DotTiled.CustomEnumDefinition> from a given C# enum, and you can then use this definition when loading your maps.
```csharp
enum EntityType
@ -171,6 +171,9 @@ The generated custom enum definition will be identical to the one defined manual
For enum definitions, the <xref:System.FlagsAttribute> can be used to indicate that the enum should be treated as a flags enum. This will make it so the enum definition will have `ValueAsFlags = true` and the enum values will be treated as flags when working with them in DotTiled.
> [!NOTE]
> Tiled supports enums which can store their values as either strings or integers, and depending on the storage type you have specified in Tiled, you must make sure to have the same storage type in your <xref:DotTiled.CustomEnumDefinition>. This can be done by setting the `StorageType` property to either `CustomEnumStorageType.String` or `CustomEnumStorageType.Int` when creating the definition, or by passing the storage type as an argument to the <xref:DotTiled.CustomEnumDefinition.FromEnum``1(DotTiled.CustomEnumStorageType)> method. To be consistent with Tiled, <xref:DotTiled.CustomEnumDefinition.FromEnum``1(DotTiled.CustomEnumStorageType)> will default to `CustomEnumStorageType.String` for the storage type parameter.
## Mapping properties to C# classes or enums
So far, we have only discussed how to define custom property types in DotTiled, and why they are needed. However, the most important part is how you can map properties inside your maps to their corresponding C# classes or enums.

View file

@ -14,8 +14,10 @@ public class CustomEnumDefinitionTests
private enum TestEnum1 { Value1, Value2, Value3 }
[Fact]
public void FromEnum_Type_WhenTypeIsEnum_ReturnsCustomEnumDefinition()
[Theory]
[InlineData(CustomEnumStorageType.String)]
[InlineData(CustomEnumStorageType.Int)]
public void FromEnum_Type_WhenTypeIsEnum_ReturnsCustomEnumDefinition(CustomEnumStorageType storageType)
{
// Arrange
var type = typeof(TestEnum1);
@ -23,13 +25,13 @@ public class CustomEnumDefinitionTests
{
ID = 0,
Name = "TestEnum1",
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = false
};
// Act
var result = CustomEnumDefinition.FromEnum(type);
var result = CustomEnumDefinition.FromEnum(type, storageType);
// Assert
DotTiledAssert.AssertCustomEnumDefinitionEqual(expected, result);
@ -38,8 +40,10 @@ public class CustomEnumDefinitionTests
[Flags]
private enum TestEnum2 { Value1, Value2, Value3 }
[Fact]
public void FromEnum_Type_WhenEnumIsFlags_ReturnsCustomEnumDefinition()
[Theory]
[InlineData(CustomEnumStorageType.String)]
[InlineData(CustomEnumStorageType.Int)]
public void FromEnum_Type_WhenEnumIsFlags_ReturnsCustomEnumDefinition(CustomEnumStorageType storageType)
{
// Arrange
var type = typeof(TestEnum2);
@ -47,53 +51,57 @@ public class CustomEnumDefinitionTests
{
ID = 0,
Name = "TestEnum2",
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = true
};
// Act
var result = CustomEnumDefinition.FromEnum(type);
var result = CustomEnumDefinition.FromEnum(type, storageType);
// Assert
DotTiledAssert.AssertCustomEnumDefinitionEqual(expected, result);
}
[Fact]
public void FromEnum_T_WhenTypeIsEnum_ReturnsCustomEnumDefinition()
[Theory]
[InlineData(CustomEnumStorageType.String)]
[InlineData(CustomEnumStorageType.Int)]
public void FromEnum_T_WhenTypeIsEnum_ReturnsCustomEnumDefinition(CustomEnumStorageType storageType)
{
// Arrange
var expected = new CustomEnumDefinition
{
ID = 0,
Name = "TestEnum1",
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = false
};
// Act
var result = CustomEnumDefinition.FromEnum<TestEnum1>();
var result = CustomEnumDefinition.FromEnum<TestEnum1>(storageType);
// Assert
DotTiledAssert.AssertCustomEnumDefinitionEqual(expected, result);
}
[Fact]
public void FromEnum_T_WhenEnumIsFlags_ReturnsCustomEnumDefinition()
[Theory]
[InlineData(CustomEnumStorageType.String)]
[InlineData(CustomEnumStorageType.Int)]
public void FromEnum_T_WhenEnumIsFlags_ReturnsCustomEnumDefinition(CustomEnumStorageType storageType)
{
// Arrange
var expected = new CustomEnumDefinition
{
ID = 0,
Name = "TestEnum2",
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = true
};
// Act
var result = CustomEnumDefinition.FromEnum<TestEnum2>();
var result = CustomEnumDefinition.FromEnum<TestEnum2>(storageType);
// Assert
DotTiledAssert.AssertCustomEnumDefinitionEqual(expected, result);

View file

@ -51,8 +51,9 @@ public class CustomEnumDefinition : ICustomTypeDefinition
/// Creates a custom enum definition from the specified enum type.
/// </summary>
/// <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>
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 isFlags = type.GetCustomAttributes(typeof(FlagsAttribute), false).Length != 0;
@ -60,7 +61,7 @@ public class CustomEnumDefinition : ICustomTypeDefinition
return new CustomEnumDefinition
{
Name = type.Name,
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = Enum.GetNames(type).ToList(),
ValueAsFlags = isFlags
};
@ -69,8 +70,10 @@ public class CustomEnumDefinition : ICustomTypeDefinition
/// <summary>
/// Creates a custom enum definition from the specified enum type.
/// </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>
public static CustomEnumDefinition FromEnum(Type type)
public static CustomEnumDefinition FromEnum(Type type, CustomEnumStorageType storageType = CustomEnumStorageType.String)
{
if (!type.IsEnum)
throw new ArgumentException("Type must be an enum.", nameof(type));
@ -80,7 +83,7 @@ public class CustomEnumDefinition : ICustomTypeDefinition
return new CustomEnumDefinition
{
Name = type.Name,
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = Enum.GetNames(type).ToList(),
ValueAsFlags = isFlags
};