diff --git a/assets/tiled_group_layers.tmx b/assets/tiled_group_layers.tmx index b027a4e8d71b07c1096a11391c343ac226f5aac0..7a93fcc68bab1c9a83ebd4ae3a3b5fd726c6a123 100644 --- a/assets/tiled_group_layers.tmx +++ b/assets/tiled_group_layers.tmx @@ -18,7 +18,7 @@ </layer> <group id="3" name="group-1"> <properties> - <property name="key" value="value4"/> + <property name="key" type="color" value="#12345678"/> </properties> <layer id="5" name="tile-2" width="8" height="8"> <properties> diff --git a/src/properties.rs b/src/properties.rs index 7fda017f11d3fda1ec741a2915d468fe52e7b533..5c481f8ae8d0f0b656ed0e8fc91f3fabad7f8f67 100644 --- a/src/properties.rs +++ b/src/properties.rs @@ -58,7 +58,7 @@ impl FromStr for Color { } /// Represents a custom property's value. -/// +/// /// Also read the [TMX docs](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-properties). #[derive(Debug, PartialEq, Clone)] pub enum PropertyValue { @@ -69,7 +69,7 @@ pub enum PropertyValue { /// A signed integer value. Corresponds to the `int` property type. IntValue(i32), /// A color value. Corresponds to the `color` property type. - ColorValue(u32), + ColorValue(Color), /// A string value. Corresponds to the `string` property type. StringValue(String), /// A filepath value. Corresponds to the `file` property type. @@ -102,12 +102,11 @@ impl PropertyValue { description: err.to_string(), }), }, - "color" if value.len() > 1 => match u32::from_str_radix(&value[1..], 16) { - Ok(color) => Ok(PropertyValue::ColorValue(color)), - Err(err) => Err(TiledError::InvalidPropertyValue { - description: err.to_string(), + "color" if value.len() > 1 => Color::from_str(&value) + .map(|color| PropertyValue::ColorValue(color)) + .map_err(|_| TiledError::InvalidPropertyValue { + description: "Couldn't parse color".to_string(), }), - }, "string" => Ok(PropertyValue::StringValue(value)), "object" => match value.parse() { Ok(val) => Ok(PropertyValue::ObjectValue(val)), diff --git a/tests/lib.rs b/tests/lib.rs index 1a405ff6236a0c8e81489c9130f340ea0d2058fa..01b2d0f0ae69ce07e13230a11c22d3c168c0af91 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -377,7 +377,12 @@ fn test_group_layers() { layer_tile_1.properties().get("key") ); assert_eq!( - Some(&PropertyValue::StringValue("value4".to_string())), + Some(&PropertyValue::ColorValue(Color { + alpha: 0x12, + red: 0x34, + green: 0x56, + blue: 0x78 + })), layer_group_1.properties().get("key") ); assert_eq!(