Skip to content

Commit

Permalink
Unset colors are now parsed correctly, color properties have optional…
Browse files Browse the repository at this point in the history
… colors
  • Loading branch information
dcronqvist committed Nov 21, 2024
1 parent 6deb28c commit 94c1ac0
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
]
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
4 changes: 2 additions & 2 deletions src/DotTiled/Properties/ColorProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/DotTiled/Properties/IHasProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ private static object CreatedMappedInstance(object instance, IList<IProperty> pr
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);
Expand Down
2 changes: 1 addition & 1 deletion src/DotTiled/Serialization/Tmj/TmjReaderBase.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal List<IProperty> ReadProperties(JsonElement element) =>
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"),
Expand Down
2 changes: 1 addition & 1 deletion src/DotTiled/Serialization/Tmx/ExtensionsXmlReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static Optional<T> GetOptionalAttributeParseable<T>(this XmlReader read
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)
Expand Down
2 changes: 1 addition & 1 deletion src/DotTiled/Serialization/Tmx/TmxReaderBase.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal List<IProperty> ReadProperties()
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"),
Expand Down

0 comments on commit 94c1ac0

Please sign in to comment.