From 35c6ba5002afaeabf22351da52c5ae2d0dabf75f Mon Sep 17 00:00:00 2001 From: Daniel Cronqvist Date: Sat, 16 Nov 2024 18:35:21 +0100 Subject: [PATCH 1/3] Add storageType parameter to FromEnum, default value is consistent with Tiled --- .../Properties/CustomTypes/CustomEnumDefinition.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs b/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs index 72593d9..b0d6e88 100644 --- a/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs +++ b/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs @@ -51,8 +51,9 @@ public class CustomEnumDefinition : ICustomTypeDefinition /// Creates a custom enum definition from the specified enum type. /// /// + /// The storage type of the custom enum. Defaults to to be consistent with Tiled. /// - public static CustomEnumDefinition FromEnum() where T : Enum + public static CustomEnumDefinition FromEnum(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 /// /// Creates a custom enum definition from the specified enum type. /// + /// The enum type to create a custom enum definition from. + /// The storage type of the custom enum. Defaults to to be consistent with Tiled. /// - 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 }; From 2e8eaa5a729101c6d9681c7fecab9ad9a03754a3 Mon Sep 17 00:00:00 2001 From: Daniel Cronqvist Date: Sat, 16 Nov 2024 18:35:59 +0100 Subject: [PATCH 2/3] Update FromEnum tests with storage type parameter --- .../CustomTypes/CustomEnumDefinitionTests.cs | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/DotTiled.Tests/UnitTests/Properties/CustomTypes/CustomEnumDefinitionTests.cs b/src/DotTiled.Tests/UnitTests/Properties/CustomTypes/CustomEnumDefinitionTests.cs index c342f77..02b16d2 100644 --- a/src/DotTiled.Tests/UnitTests/Properties/CustomTypes/CustomEnumDefinitionTests.cs +++ b/src/DotTiled.Tests/UnitTests/Properties/CustomTypes/CustomEnumDefinitionTests.cs @@ -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(); + var result = CustomEnumDefinition.FromEnum(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(); + var result = CustomEnumDefinition.FromEnum(storageType); // Assert DotTiledAssert.AssertCustomEnumDefinitionEqual(expected, result); From 666a3433e315ba4d96f5aa986d017b83f8625215 Mon Sep 17 00:00:00 2001 From: Daniel Cronqvist Date: Sat, 16 Nov 2024 18:48:15 +0100 Subject: [PATCH 3/3] Update docs section on CustomEnumDefinitions with new storage type parameter info --- docs/docs/essentials/custom-properties.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/docs/essentials/custom-properties.md b/docs/docs/essentials/custom-properties.md index 9fc19aa..addffd3 100644 --- a/docs/docs/essentials/custom-properties.md +++ b/docs/docs/essentials/custom-properties.md @@ -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 method, or one of its overloads. This method will generate a 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 method, or one of its overloads. This method will generate a 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 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 . 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 method. To be consistent with Tiled, 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.