Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tile image rects in image collection tilesets #277

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 39 additions & 2 deletions src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,6 +44,9 @@ pub struct TileData {
pub user_type: Option<String>,
/// 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<ImageRect>,
}

/// Points to a tile belonging to a tileset.
Expand Down Expand Up @@ -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::<u32>(),
}
((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();
Expand All @@ -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 {
Expand All @@ -110,6 +146,7 @@ impl TileData {
animation,
user_type,
probability: probability.unwrap_or(1.0),
image_rect,
},
))
}
Expand Down
Loading