From 2a7a80d78bf793222f982512d591e834166b24fd Mon Sep 17 00:00:00 2001 From: jon lipstate <jon@lipstate.com> Date: Sun, 26 Apr 2020 00:00:31 -0700 Subject: [PATCH] added tileset properties & associated test --- CHANGELOG.md | 11 ++++++++--- README.md | 12 +++++++----- assets/tiled_base64.tmx | 12 ++++++++---- assets/tiled_base64_gzip.tmx | 3 +++ assets/tiled_base64_zlib.tmx | 3 +++ assets/tiled_csv.tmx | 3 +++ assets/tilesheet.tsx | 3 +++ src/lib.rs | 19 ++++++++++++++++--- tests/lib.rs | 12 ++++++++++++ 9 files changed, 63 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70d2c15..8ae8f0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Changelog + All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), @@ -6,13 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [0.9.0] - 2019-25-11 +## [0.9.2] - 2020-Apr-25 ### Added -- + +- Properties to Tilesets. +- Test verifying `tileset.properties` ### Changed -- Migration to `rust 2018` and `?` + +- ### Removed + - diff --git a/README.md b/README.md index d5cb706..352fe3b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Code contributions are welcome as are bug reports, documentation, suggestions an [There is a package on crates.io](https://crates.io/crates/tiled), to use simply add: ``` -tiled = "0.9.1" +tiled = "0.9.2" ``` to the dependencies section of your Cargo.toml. @@ -38,14 +38,16 @@ fn main() { ``` ### Things missing + There are a few things missing at the moment: - * Terrain - * Tile flipping - * Image layers - * A nice API. At the moment you can access attributes and properties, find tilesets by GID and loop through the tiles. This leaves a user of the library with a bit to do. +- Terrain +- Tile flipping +- Image layers +- A nice API. At the moment you can access attributes and properties, find tilesets by GID and loop through the tiles. This leaves a user of the library with a bit to do. ### Licences + assets/tilesheet.png by Buch (http://blog-buch.rhcloud.com/) Licenced under MIT diff --git a/assets/tiled_base64.tmx b/assets/tiled_base64.tmx index 57d3b81..7ee37de 100644 --- a/assets/tiled_base64.tmx +++ b/assets/tiled_base64.tmx @@ -1,12 +1,16 @@ <?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="32" tileheight="32" backgroundcolor="#ff00ff" nextobjectid="5"> <tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32"> - <image source="tilesheet.png" width="448" height="192"/> - <tile id="1"> <properties> - <property name="a tile property" value="123"/> + <property name="tileset property" value="tsp"/> </properties> - </tile> + <image source="tilesheet.png" width="448" height="192"/> + <tile id="1"> + <properties> + <property name="a tile property" value="123"/> + </properties> + </tile> + </tileset> <layer name="Tile Layer 1" width="100" height="100"> <properties> diff --git a/assets/tiled_base64_gzip.tmx b/assets/tiled_base64_gzip.tmx index dead54e..97eae5e 100644 --- a/assets/tiled_base64_gzip.tmx +++ b/assets/tiled_base64_gzip.tmx @@ -1,6 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="32" tileheight="32" backgroundcolor="#ff00ff" nextobjectid="5"> <tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32"> + <properties> + <property name="tileset property" value="tsp"/> + </properties> <image source="tilesheet.png" width="448" height="192"/> <tile id="1"> <properties> diff --git a/assets/tiled_base64_zlib.tmx b/assets/tiled_base64_zlib.tmx index 9dd37cf..b217f77 100644 --- a/assets/tiled_base64_zlib.tmx +++ b/assets/tiled_base64_zlib.tmx @@ -1,6 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="32" tileheight="32" backgroundcolor="#ff00ff" nextobjectid="5"> <tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32"> + <properties> + <property name="tileset property" value="tsp"/> + </properties> <image source="tilesheet.png" width="448" height="192"/> <tile id="1"> <properties> diff --git a/assets/tiled_csv.tmx b/assets/tiled_csv.tmx index 88773d7..03806c1 100644 --- a/assets/tiled_csv.tmx +++ b/assets/tiled_csv.tmx @@ -1,6 +1,9 @@ <?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="32" tileheight="32" backgroundcolor="#ff00ff" nextobjectid="5"> <tileset firstgid="1" name="tilesheet" tilewidth="32" tileheight="32"> + <properties> + <property name="tileset property" value="tsp"/> + </properties> <image source="tilesheet.png" width="448" height="192"/> <tile id="1"> <properties> diff --git a/assets/tilesheet.tsx b/assets/tilesheet.tsx index f01a9da..05c2c42 100644 --- a/assets/tilesheet.tsx +++ b/assets/tilesheet.tsx @@ -1,5 +1,8 @@ <?xml version="1.0" encoding="UTF-8"?> <tileset name="tilesheet" tilewidth="32" tileheight="32" tilecount="84"> + <properties> + <property name="tileset property" value="tsp"/> + </properties> <image source="tilesheet.png" width="448" height="192"/> <tile id="1"> <properties> diff --git a/src/lib.rs b/src/lib.rs index 1b91600..f4ad7a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -357,6 +357,7 @@ pub struct Tileset { /// is used. Usually you will only use one. pub images: Vec<Image>, pub tiles: Vec<Tile>, + pub properties: Properties, } impl Tileset { @@ -389,11 +390,16 @@ impl Tileset { let mut images = Vec::new(); let mut tiles = Vec::new(); + let mut properties = HashMap::new(); parse_tag!(parser, "tileset", { "image" => |attrs| { images.push(Image::new(parser, attrs)?); Ok(()) }, + "properties" => |_| { + properties = parse_properties(parser)?; + Ok(()) + }, "tile" => |attrs| { tiles.push(Tile::new(parser, attrs)?); Ok(()) @@ -402,13 +408,14 @@ impl Tileset { Ok(Tileset { first_gid: first_gid, - name: name, + name, tile_width: width, tile_height: height, spacing: spacing.unwrap_or(0), margin: margin.unwrap_or(0), - images: images, - tiles: tiles, + images, + tiles, + properties, }) } @@ -485,6 +492,7 @@ impl Tileset { let mut images = Vec::new(); let mut tiles = Vec::new(); + let mut properties = HashMap::new(); parse_tag!(parser, "tileset", { "image" => |attrs| { images.push(Image::new(parser, attrs)?); @@ -494,6 +502,10 @@ impl Tileset { tiles.push(Tile::new(parser, attrs)?); Ok(()) }, + "properties" => |_| { + properties = parse_properties(parser)?; + Ok(()) + }, }); Ok(Tileset { @@ -505,6 +517,7 @@ impl Tileset { margin: margin.unwrap_or(0), images: images, tiles: tiles, + properties, }) } } diff --git a/tests/lib.rs b/tests/lib.rs index 8930381..3aca629 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -88,6 +88,18 @@ fn test_object_group_property() { }; assert!(prop_value); } +#[test] +fn test_tileset_property() { + let r = read_from_file(&Path::new("assets/tiled_base64.tmx")).unwrap(); + let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) = + r.tilesets[0].properties.get("tileset property") + { + v.clone() + } else { + String::new() + }; + assert_eq!("tsp", prop_value); +} #[test] fn test_flipped_gid() { -- GitLab