diff --git a/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.cs b/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.cs index b2dbc7a..2437d0b 100644 --- a/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.cs +++ b/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.cs @@ -57,7 +57,8 @@ public partial class TestData new FloatProperty { Name = "floatprop", Value = 4.2f }, new IntProperty { Name = "intprop", Value = 8 }, new ObjectProperty { Name = "objectprop", Value = 5 }, - new StringProperty { Name = "stringprop", Value = "This is a string, hello world!" } + new StringProperty { Name = "stringprop", Value = "This is a string, hello world!" }, + new ColorProperty { Name = "unsetcolorprop", Value = Optional.Empty } ] }; } diff --git a/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmj b/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmj index c7182ef..4fbc980 100644 --- a/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmj +++ b/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmj @@ -58,6 +58,11 @@ "name":"stringprop", "type":"string", "value":"This is a string, hello world!" + }, + { + "name":"unsetcolorprop", + "type":"color", + "value":"" }], "renderorder":"right-down", "tiledversion":"1.11.0", diff --git a/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmx b/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmx index b4b36cd..1aeacd7 100644 --- a/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmx +++ b/src/DotTiled.Tests/TestData/Maps/map-with-common-props/map-with-common-props.tmx @@ -8,6 +8,7 @@ + diff --git a/src/DotTiled/Properties/ColorProperty.cs b/src/DotTiled/Properties/ColorProperty.cs index 0fff029..295096e 100644 --- a/src/DotTiled/Properties/ColorProperty.cs +++ b/src/DotTiled/Properties/ColorProperty.cs @@ -3,7 +3,7 @@ namespace DotTiled; /// /// Represents a color property. /// -public class ColorProperty : IProperty +public class ColorProperty : IProperty> { /// public required string Name { get; set; } @@ -14,7 +14,7 @@ public class ColorProperty : IProperty /// /// The color value of the property. /// - public required Color Value { get; set; } + public required Optional Value { get; set; } /// public IProperty Clone() => new ColorProperty diff --git a/src/DotTiled/Properties/IHasProperties.cs b/src/DotTiled/Properties/IHasProperties.cs index 03e610a..6dd1798 100644 --- a/src/DotTiled/Properties/IHasProperties.cs +++ b/src/DotTiled/Properties/IHasProperties.cs @@ -105,7 +105,9 @@ public abstract class HasPropertiesBase : IHasProperties type.GetProperty(prop.Name)?.SetValue(instance, boolProp.Value); break; case ColorProperty colorProp: - type.GetProperty(prop.Name)?.SetValue(instance, colorProp.Value); + if (!colorProp.Value.HasValue) + break; + type.GetProperty(prop.Name)?.SetValue(instance, colorProp.Value.Value); break; case FloatProperty floatProp: type.GetProperty(prop.Name)?.SetValue(instance, floatProp.Value); diff --git a/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs b/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs index 1ffff1c..acb63be 100644 --- a/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs +++ b/src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs @@ -36,7 +36,7 @@ public abstract partial class TmjReaderBase PropertyType.Int => new IntProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Float => new FloatProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Bool => new BoolProperty { Name = name, Value = e.GetRequiredProperty("value") }, - PropertyType.Color => new ColorProperty { Name = name, Value = e.GetRequiredPropertyParseable("value") }, + PropertyType.Color => new ColorProperty { Name = name, Value = e.GetRequiredPropertyParseable("value", s => s == "" ? default : Color.Parse(s, CultureInfo.InvariantCulture)) }, PropertyType.File => new FileProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Object => new ObjectProperty { Name = name, Value = e.GetRequiredProperty("value") }, PropertyType.Class => throw new JsonException("Class property must have a property type"), diff --git a/src/DotTiled/Serialization/Tmx/ExtensionsXmlReader.cs b/src/DotTiled/Serialization/Tmx/ExtensionsXmlReader.cs index 0d06ade..f1c2a5d 100644 --- a/src/DotTiled/Serialization/Tmx/ExtensionsXmlReader.cs +++ b/src/DotTiled/Serialization/Tmx/ExtensionsXmlReader.cs @@ -45,7 +45,7 @@ internal static class ExtensionsXmlReader return T.Parse(value, CultureInfo.InvariantCulture); } - internal static Optional GetOptionalAttributeParseable(this XmlReader reader, string attribute, Func parser) where T : struct + internal static Optional GetOptionalAttributeParseable(this XmlReader reader, string attribute, Func parser) { var value = reader.GetAttribute(attribute); if (value is null) diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs index 03d3a74..13a99fc 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs @@ -45,7 +45,7 @@ public abstract partial class TmxReaderBase PropertyType.Int => new IntProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, PropertyType.Float => new FloatProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, PropertyType.Bool => new BoolProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, - PropertyType.Color => new ColorProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, + PropertyType.Color => new ColorProperty { Name = name, Value = r.GetRequiredAttributeParseable("value", s => s == "" ? default : Color.Parse(s, CultureInfo.InvariantCulture)) }, PropertyType.File => new FileProperty { Name = name, Value = r.GetRequiredAttribute("value") }, PropertyType.Object => new ObjectProperty { Name = name, Value = r.GetRequiredAttributeParseable("value") }, PropertyType.Class => throw new XmlException("Class property must have a property type"),