diff --git a/CHANGELOG.md b/CHANGELOG.md index b33c756f..9fb5e828 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased (next)] ### Added - Implement `ResourceReader` for appropiate functions. (#272) **Read the README's FAQ for more information about this change.** +- Support for Tile `x`, `y`, `width` and `height` attributes related to image rects. (#277) ## [Unreleased] ## Changed diff --git a/src/tile.rs b/src/tile.rs index a0a4ae65..7498ae2f 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -15,6 +15,20 @@ use crate::{ /// A tile ID, local to a tileset. pub type TileId = u32; +/// Indicates which part of an image should be used for a certain tile. +/// Corresponds to the tile's `x`, `y`, `width` and `height` attributes. +#[derive(Debug, PartialEq, Clone, Copy, Default)] +pub struct ImageRect { + /// The X position of the sub-rectangle representing this tile (default: 0) + pub x: i32, + /// The Y position of the sub-rectangle representing this tile (default: 0) + pub y: i32, + /// The width of the sub-rectangle representing this tile (defaults to the image width) + pub width: i32, + /// The height of the sub-rectangle representing this tile (defaults to the image height) + pub height: i32, +} + /// Raw data belonging to a tile. #[derive(Debug, PartialEq, Clone, Default)] pub struct TileData { @@ -30,6 +44,9 @@ pub struct TileData { pub user_type: Option, /// The probability of this tile. pub probability: f32, + /// The image rect of this tile, which is only used in image collection tilesets and corresponds + /// to the TMX `x`, `y`, `width` and `height` tile attributes. + pub image_rect: Option, } /// Points to a tile belonging to a tileset. @@ -67,15 +84,20 @@ impl TileData { reader: &mut impl ResourceReader, cache: &mut impl ResourceCache, ) -> Result<(TileId, TileData)> { - let ((user_type, user_class, probability), id) = get_attrs!( + let ((user_type, user_class, probability, x, y, width, height), id) = get_attrs!( for v in attrs { Some("type") => user_type ?= v.parse(), Some("class") => user_class ?= v.parse(), Some("probability") => probability ?= v.parse(), + Some("x") => x ?= v.parse(), + Some("y") => y ?= v.parse(), + Some("width") => width ?= v.parse(), + Some("height") => height ?= v.parse(), "id" => id ?= v.parse::(), } - ((user_type, user_class, probability), id) + ((user_type, user_class, probability, x, y, width, height), id) ); + let user_type = user_type.or(user_class); let mut image = Option::None; let mut properties = HashMap::new(); @@ -101,6 +123,20 @@ impl TileData { Ok(()) }, }); + let image_rect = image.as_ref().map(|image| { + let x = x.unwrap_or(0); + let y = y.unwrap_or(0); + let width = width.unwrap_or(image.width); + let height = height.unwrap_or(image.height); + + ImageRect { + x, + y, + width, + height, + } + }); + Ok(( id, TileData { @@ -110,6 +146,7 @@ impl TileData { animation, user_type, probability: probability.unwrap_or(1.0), + image_rect, }, )) }