Skip to content

Commit

Permalink
Merge pull request #54 from dcronqvist/from-enum-storage-type
Browse files Browse the repository at this point in the history
Add storage type parameter to CustomEnumDefinition.FromEnum
  • Loading branch information
dcronqvist authored Nov 16, 2024
2 parents cbd03bc + 666a343 commit 23d218b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
7 changes: 5 additions & 2 deletions docs/docs/essentials/custom-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ public void FromEnum_Type_WhenTypeIsNotEnum_ThrowsArgumentException()

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);
var expected = new CustomEnumDefinition
{
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);
Expand All @@ -38,62 +40,68 @@ public void FromEnum_Type_WhenTypeIsEnum_ReturnsCustomEnumDefinition()
[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);
var expected = new CustomEnumDefinition
{
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);
Expand Down
11 changes: 7 additions & 4 deletions src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ 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;

return new CustomEnumDefinition
{
Name = type.Name,
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = Enum.GetNames(type).ToList(),
ValueAsFlags = isFlags
};
Expand All @@ -69,8 +70,10 @@ public static CustomEnumDefinition FromEnum<T>() where T : Enum
/// <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));
Expand All @@ -80,7 +83,7 @@ public static CustomEnumDefinition FromEnum(Type type)
return new CustomEnumDefinition
{
Name = type.Name,
StorageType = CustomEnumStorageType.Int,
StorageType = storageType,
Values = Enum.GetNames(type).ToList(),
ValueAsFlags = isFlags
};
Expand Down

0 comments on commit 23d218b

Please sign in to comment.