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 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<Color>.Empty }
]
};
}

View file

@ -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",

View file

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

View file

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

View file

@ -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);

View file

@ -36,7 +36,7 @@ public abstract partial class TmjReaderBase
PropertyType.Int => new IntProperty { Name = name, Value = e.GetRequiredProperty<int>("value") },
PropertyType.Float => new FloatProperty { Name = name, Value = e.GetRequiredProperty<float>("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.Object => new ObjectProperty { Name = name, Value = e.GetRequiredProperty<uint>("value") },
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);
}
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);
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.Float => new FloatProperty { Name = name, Value = r.GetRequiredAttributeParseable<float>("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.Object => new ObjectProperty { Name = name, Value = r.GetRequiredAttributeParseable<uint>("value") },
PropertyType.Class => throw new XmlException("Class property must have a property type"),