Skip to content

Commit 159872f

Browse files
authored
Add support for tile image rect attributes (#277)
1 parent c7e6ed2 commit 159872f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99
### Added
10+
- Support for Tile `x`, `y`, `width` and `height` attributes related to image rects. (#277)
1011
- New `ImageLayer` `repeat_x`/`y` fields are now parsed from map image layers.
1112

1213
## [0.14.0]

src/tile.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ use crate::{
1515
/// A tile ID, local to a tileset.
1616
pub type TileId = u32;
1717

18+
/// Indicates which part of an image should be used for a certain tile.
19+
/// Corresponds to the tile's `x`, `y`, `width` and `height` attributes.
20+
#[derive(Debug, PartialEq, Clone, Copy, Default)]
21+
pub struct ImageRect {
22+
/// The X position of the sub-rectangle representing this tile (default: 0)
23+
pub x: i32,
24+
/// The Y position of the sub-rectangle representing this tile (default: 0)
25+
pub y: i32,
26+
/// The width of the sub-rectangle representing this tile (defaults to the image width)
27+
pub width: i32,
28+
/// The height of the sub-rectangle representing this tile (defaults to the image height)
29+
pub height: i32,
30+
}
31+
1832
/// Raw data belonging to a tile.
1933
#[derive(Debug, PartialEq, Clone, Default)]
2034
pub struct TileData {
@@ -30,6 +44,9 @@ pub struct TileData {
3044
pub user_type: Option<String>,
3145
/// The probability of this tile.
3246
pub probability: f32,
47+
/// The image rect of this tile, which is only used in image collection tilesets and corresponds
48+
/// to the TMX `x`, `y`, `width` and `height` tile attributes.
49+
pub image_rect: Option<ImageRect>,
3350
}
3451

3552
/// Points to a tile belonging to a tileset.
@@ -67,15 +84,20 @@ impl TileData {
6784
reader: &mut impl ResourceReader,
6885
cache: &mut impl ResourceCache,
6986
) -> Result<(TileId, TileData)> {
70-
let ((user_type, user_class, probability), id) = get_attrs!(
87+
let ((user_type, user_class, probability, x, y, width, height), id) = get_attrs!(
7188
for v in attrs {
7289
Some("type") => user_type ?= v.parse(),
7390
Some("class") => user_class ?= v.parse(),
7491
Some("probability") => probability ?= v.parse(),
92+
Some("x") => x ?= v.parse(),
93+
Some("y") => y ?= v.parse(),
94+
Some("width") => width ?= v.parse(),
95+
Some("height") => height ?= v.parse(),
7596
"id" => id ?= v.parse::<u32>(),
7697
}
77-
((user_type, user_class, probability), id)
98+
((user_type, user_class, probability, x, y, width, height), id)
7899
);
100+
79101
let user_type = user_type.or(user_class);
80102
let mut image = Option::None;
81103
let mut properties = HashMap::new();
@@ -101,6 +123,20 @@ impl TileData {
101123
Ok(())
102124
},
103125
});
126+
let image_rect = image.as_ref().map(|image| {
127+
let x = x.unwrap_or(0);
128+
let y = y.unwrap_or(0);
129+
let width = width.unwrap_or(image.width);
130+
let height = height.unwrap_or(image.height);
131+
132+
ImageRect {
133+
x,
134+
y,
135+
width,
136+
height,
137+
}
138+
});
139+
104140
Ok((
105141
id,
106142
TileData {
@@ -110,6 +146,7 @@ impl TileData {
110146
animation,
111147
user_type,
112148
probability: probability.unwrap_or(1.0),
149+
image_rect,
113150
},
114151
))
115152
}

0 commit comments

Comments
 (0)