Merge pull request #63 from dcronqvist/color-bug

Unset colors are now parsed correctly, color properties have optional colors
This commit is contained in:
dcronqvist 2024-11-22 21:18:59 +01:00 committed by GitHub
commit f3c4478125
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 16 additions and 7 deletions

View file

@ -57,7 +57,8 @@ public partial class TestData
new FloatProperty { Name = "floatprop", Value = 4.2f }, new FloatProperty { Name = "floatprop", Value = 4.2f },
new IntProperty { Name = "intprop", Value = 8 }, new IntProperty { Name = "intprop", Value = 8 },
new ObjectProperty { Name = "objectprop", Value = 5 }, 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<Color>.Empty }
] ]
}; };
} }

View file

@ -58,6 +58,11 @@
"name":"stringprop", "name":"stringprop",
"type":"string", "type":"string",
"value":"This is a string, hello world!" "value":"This is a string, hello world!"
},
{
"name":"unsetcolorprop",
"type":"color",
"value":""
}], }],
"renderorder":"right-down", "renderorder":"right-down",
"tiledversion":"1.11.0", "tiledversion":"1.11.0",

View file

@ -8,6 +8,7 @@
<property name="intprop" type="int" value="8"/> <property name="intprop" type="int" value="8"/>
<property name="objectprop" type="object" value="5"/> <property name="objectprop" type="object" value="5"/>
<property name="stringprop" value="This is a string, hello world!"/> <property name="stringprop" value="This is a string, hello world!"/>
<property name="unsetcolorprop" type="color" value=""/>
</properties> </properties>
<layer id="1" name="Tile Layer 1" width="5" height="5"> <layer id="1" name="Tile Layer 1" width="5" height="5">
<data encoding="csv"> <data encoding="csv">

View file

@ -3,7 +3,7 @@ namespace DotTiled;
/// <summary> /// <summary>
/// Represents a color property. /// Represents a color property.
/// </summary> /// </summary>
public class ColorProperty : IProperty<Color> public class ColorProperty : IProperty<Optional<Color>>
{ {
/// <inheritdoc/> /// <inheritdoc/>
public required string Name { get; set; } public required string Name { get; set; }
@ -14,7 +14,7 @@ public class ColorProperty : IProperty<Color>
/// <summary> /// <summary>
/// The color value of the property. /// The color value of the property.
/// </summary> /// </summary>
public required Color Value { get; set; } public required Optional<Color> Value { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public IProperty Clone() => new ColorProperty public IProperty Clone() => new ColorProperty

View file

@ -105,7 +105,9 @@ public abstract class HasPropertiesBase : IHasProperties
type.GetProperty(prop.Name)?.SetValue(instance, boolProp.Value); type.GetProperty(prop.Name)?.SetValue(instance, boolProp.Value);
break; break;
case ColorProperty colorProp: 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; break;
case FloatProperty floatProp: case FloatProperty floatProp:
type.GetProperty(prop.Name)?.SetValue(instance, floatProp.Value); type.GetProperty(prop.Name)?.SetValue(instance, floatProp.Value);

View file

@ -36,7 +36,7 @@ public abstract partial class TmjReaderBase
PropertyType.Int => new IntProperty { Name = name, Value = e.GetRequiredProperty<int>("value") }, PropertyType.Int => new IntProperty { Name = name, Value = e.GetRequiredProperty<int>("value") },
PropertyType.Float => new FloatProperty { Name = name, Value = e.GetRequiredProperty<float>("value") }, PropertyType.Float => new FloatProperty { Name = name, Value = e.GetRequiredProperty<float>("value") },
PropertyType.Bool => new BoolProperty { Name = name, Value = e.GetRequiredProperty<bool>("value") }, PropertyType.Bool => new BoolProperty { Name = name, Value = e.GetRequiredProperty<bool>("value") },
PropertyType.Color => new ColorProperty { Name = name, Value = e.GetRequiredPropertyParseable<Color>("value") }, PropertyType.Color => new ColorProperty { Name = name, Value = e.GetRequiredPropertyParseable<Color>("value", s => s == "" ? default : Color.Parse(s, CultureInfo.InvariantCulture)) },
PropertyType.File => new FileProperty { Name = name, Value = e.GetRequiredProperty<string>("value") }, PropertyType.File => new FileProperty { Name = name, Value = e.GetRequiredProperty<string>("value") },
PropertyType.Object => new ObjectProperty { Name = name, Value = e.GetRequiredProperty<uint>("value") }, PropertyType.Object => new ObjectProperty { Name = name, Value = e.GetRequiredProperty<uint>("value") },
PropertyType.Class => throw new JsonException("Class property must have a property type"), PropertyType.Class => throw new JsonException("Class property must have a property type"),

View file

@ -45,7 +45,7 @@ internal static class ExtensionsXmlReader
return T.Parse(value, CultureInfo.InvariantCulture); return T.Parse(value, CultureInfo.InvariantCulture);
} }
internal static Optional<T> GetOptionalAttributeParseable<T>(this XmlReader reader, string attribute, Func<string, T> parser) where T : struct internal static Optional<T> GetOptionalAttributeParseable<T>(this XmlReader reader, string attribute, Func<string, T> parser)
{ {
var value = reader.GetAttribute(attribute); var value = reader.GetAttribute(attribute);
if (value is null) if (value is null)

View file

@ -45,7 +45,7 @@ public abstract partial class TmxReaderBase
PropertyType.Int => new IntProperty { Name = name, Value = r.GetRequiredAttributeParseable<int>("value") }, PropertyType.Int => new IntProperty { Name = name, Value = r.GetRequiredAttributeParseable<int>("value") },
PropertyType.Float => new FloatProperty { Name = name, Value = r.GetRequiredAttributeParseable<float>("value") }, PropertyType.Float => new FloatProperty { Name = name, Value = r.GetRequiredAttributeParseable<float>("value") },
PropertyType.Bool => new BoolProperty { Name = name, Value = r.GetRequiredAttributeParseable<bool>("value") }, PropertyType.Bool => new BoolProperty { Name = name, Value = r.GetRequiredAttributeParseable<bool>("value") },
PropertyType.Color => new ColorProperty { Name = name, Value = r.GetRequiredAttributeParseable<Color>("value") }, PropertyType.Color => new ColorProperty { Name = name, Value = r.GetRequiredAttributeParseable<Color>("value", s => s == "" ? default : Color.Parse(s, CultureInfo.InvariantCulture)) },
PropertyType.File => new FileProperty { Name = name, Value = r.GetRequiredAttribute("value") }, PropertyType.File => new FileProperty { Name = name, Value = r.GetRequiredAttribute("value") },
PropertyType.Object => new ObjectProperty { Name = name, Value = r.GetRequiredAttributeParseable<uint>("value") }, PropertyType.Object => new ObjectProperty { Name = name, Value = r.GetRequiredAttributeParseable<uint>("value") },
PropertyType.Class => throw new XmlException("Class property must have a property type"), PropertyType.Class => throw new XmlException("Class property must have a property type"),