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

Support Tiled 1.9 class property #238

Merged
merged 5 commits into from
Dec 12, 2022
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Support for Wang sets.
- Support for Tiled 1.9 `Class` property. Maps, tilesets and layers now have a `user_type` property.

### Deprecated
- `Tile::tile_type`: Use `Tile::user_type` instead.
- `Object::obj_type` Use `Object::user_type` instead.
bjorn marked this conversation as resolved.
Show resolved Hide resolved

### Changed
- Update `zstd` to `0.12.0`.
Expand Down
21 changes: 19 additions & 2 deletions src/layers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ pub struct LayerData {
pub tint_color: Option<Color>,
/// The layer's custom properties, as arbitrarily set by the user.
pub properties: Properties,
/// The layer's type, which is arbitrarily setby the user.
pub user_type: Option<String>,
layer_type: LayerDataType,
}

Expand All @@ -70,7 +72,19 @@ impl LayerData {
map_path: &Path,
tilesets: &[MapTilesetGid],
) -> Result<Self> {
let (opacity, tint_color, visible, offset_x, offset_y, parallax_x, parallax_y, name, id) = get_attrs!(
let (
opacity,
tint_color,
visible,
offset_x,
offset_y,
parallax_x,
parallax_y,
name,
id,
user_type,
user_class,
) = get_attrs!(
for v in attrs {
Some("opacity") => opacity ?= v.parse(),
Some("tintcolor") => tint_color ?= v.parse(),
Expand All @@ -81,8 +95,10 @@ impl LayerData {
Some("parallaxy") => parallax_y ?= v.parse(),
Some("name") => name = v,
Some("id") => id ?= v.parse(),
Some("type") => user_type ?= v.parse(),
Some("class") => user_class ?= v.parse(),
}
(opacity, tint_color, visible, offset_x, offset_y, parallax_x, parallax_y, name, id)
(opacity, tint_color, visible, offset_x, offset_y, parallax_x, parallax_y, name, id, user_type, user_class)
);

let (ty, properties) = match tag {
Expand Down Expand Up @@ -114,6 +130,7 @@ impl LayerData {
tint_color,
name: name.unwrap_or_default(),
id: id.unwrap_or(0),
user_type: user_type.or(user_class),
properties,
layer_type: ty,
})
Expand Down
10 changes: 8 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct Map {
/// The background color of this map, if any.
pub background_color: Option<Color>,
infinite: bool,
/// The type of the map, which is arbitrary and set by the user.
pub user_type: Option<String>,
}

impl Map {
Expand Down Expand Up @@ -157,21 +159,24 @@ impl Map {
map_path: &Path,
cache: &mut impl ResourceCache,
) -> Result<Map> {
let ((c, infinite), (v, o, w, h, tw, th)) = get_attrs!(
let ((c, infinite, user_type, user_class), (v, o, w, h, tw, th)) = get_attrs!(
for v in attrs {
Some("backgroundcolor") => colour ?= v.parse(),
Some("infinite") => infinite = v == "1",
Some("type") => user_type ?= v.parse(),
Some("class") => user_class ?= v.parse(),
"version" => version = v,
"orientation" => orientation ?= v.parse::<Orientation>(),
"width" => width ?= v.parse::<u32>(),
"height" => height ?= v.parse::<u32>(),
"tilewidth" => tile_width ?= v.parse::<u32>(),
"tileheight" => tile_height ?= v.parse::<u32>(),
}
((colour, infinite), (version, orientation, width, height, tile_width, tile_height))
((colour, infinite, user_type, user_class), (version, orientation, width, height, tile_width, tile_height))
);

let infinite = infinite.unwrap_or(false);
let user_type = user_type.or(user_class);

// We can only parse sequentally, but tilesets are guaranteed to appear before layers.
// So we can pass in tileset data to layer construction without worrying about unfinished
Expand Down Expand Up @@ -260,6 +265,7 @@ impl Map {
properties,
background_color: c,
infinite,
user_type,
})
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pub struct ObjectData {
/// The name of the object, which is arbitrary and set by the user.
pub name: String,
/// The type of the object, which is arbitrary and set by the user.
pub user_type: String,
/// This property has been renamed to `user_type`.
#[deprecated(since = "0.10.3", note = "Use [`ObjectData::user_type`] instead")]
pub obj_type: String,
/// The width of the object, if applicable. This refers to the attribute in `object`.
/// Since it is duplicate or irrelevant information in all cases, use the equivalent
Expand Down Expand Up @@ -81,12 +84,13 @@ impl ObjectData {
attrs: Vec<OwnedAttribute>,
tilesets: Option<&[MapTilesetGid]>,
) -> Result<ObjectData> {
let ((id, tile, n, t, w, h, v, r), (x, y)) = get_attrs!(
let ((id, tile, n, t, c, w, h, v, r), (x, y)) = get_attrs!(
for v in attrs {
Some("id") => id ?= v.parse(),
Some("gid") => tile ?= v.parse(),
Some("name") => name ?= v.parse(),
Some("type") => obj_type ?= v.parse(),
Some("type") => user_type ?= v.parse(),
Some("class") => user_class ?= v.parse(),
Some("width") => width ?= v.parse(),
Some("height") => height ?= v.parse(),
Some("visible") => visible ?= v.parse().map(|x:i32| x == 1),
Expand All @@ -95,7 +99,7 @@ impl ObjectData {
"x" => x ?= v.parse::<f32>(),
"y" => y ?= v.parse::<f32>(),
}
((id, tile, name, obj_type, width, height, visible, rotation), (x, y))
((id, tile, name, user_type, user_class, width, height, visible, rotation), (x, y))
);
let tile = tile.and_then(|bits| LayerTileData::from_bits(bits, tilesets?));
let visible = v.unwrap_or(true);
Expand All @@ -104,7 +108,7 @@ impl ObjectData {
let rotation = r.unwrap_or(0f32);
let id = id.unwrap_or(0u32);
let name = n.unwrap_or_default();
let obj_type = t.unwrap_or_default();
let user_type: String = t.or(c).unwrap_or_default();
let mut shape = None;
let mut properties = HashMap::new();

Expand Down Expand Up @@ -141,7 +145,8 @@ impl ObjectData {
id,
tile,
name,
obj_type,
obj_type: user_type.clone(),
user_type,
width,
height,
x,
Expand Down
16 changes: 11 additions & 5 deletions src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct TileData {
/// The animation frames of this tile.
pub animation: Option<Vec<Frame>>,
/// The type of this tile.
pub user_type: Option<String>,
/// This property has been renamed to `user_type`.
#[deprecated(since = "0.10.3", note = "Use [`TileData::user_type`] instead")]
pub tile_type: Option<String>,
/// The probability of this tile.
pub probability: f32,
Expand Down Expand Up @@ -65,15 +68,16 @@ impl TileData {
attrs: Vec<OwnedAttribute>,
path_relative_to: &Path,
) -> Result<(TileId, TileData)> {
let ((tile_type, probability), id) = get_attrs!(
let ((user_type, user_class, probability), id) = get_attrs!(
for v in attrs {
Some("type") => tile_type ?= v.parse(),
Some("type") => user_type ?= v.parse(),
Some("class") => user_class ?= v.parse(),
Some("probability") => probability ?= v.parse(),
"id" => id ?= v.parse::<u32>(),
}
((tile_type, probability), id)
((user_type, user_class, probability), id)
);

let user_type = user_type.or(user_class);
let mut image = Option::None;
let mut properties = HashMap::new();
let mut objectgroup = None;
Expand All @@ -98,12 +102,14 @@ impl TileData {
});
Ok((
id,
#[allow(deprecated)]
TileData {
image,
properties,
collision: objectgroup,
animation,
tile_type,
tile_type: user_type.clone(),
user_type,
probability: probability.unwrap_or(1.0),
},
))
Expand Down
26 changes: 22 additions & 4 deletions src/tileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub struct Tileset {

/// The custom properties of the tileset.
pub properties: Properties,

/// The custom tileset type, arbitrarily set by the user.
pub user_type: Option<String>,
}

pub(crate) enum EmbeddedParseResultType {
Expand All @@ -80,6 +83,7 @@ struct TilesetProperties {
tilecount: u32,
columns: Option<u32>,
name: String,
user_type: Option<String>,
tile_width: u32,
tile_height: u32,
/// The root all non-absolute paths contained within the tileset are relative to.
Expand Down Expand Up @@ -143,18 +147,24 @@ impl Tileset {
attrs: &[OwnedAttribute],
map_path: &Path,
) -> Result<EmbeddedParseResult> {
let ((spacing, margin, columns, name), (tilecount, first_gid, tile_width, tile_height)) = get_attrs!(
let (
(spacing, margin, columns, name, user_type, user_class),
(tilecount, first_gid, tile_width, tile_height),
) = get_attrs!(
for v in attrs {
Some("spacing") => spacing ?= v.parse(),
Some("margin") => margin ?= v.parse(),
Some("columns") => columns ?= v.parse(),
Some("name") => name = v,
Some("type") => user_type ?= v.parse(),
Some("class") => user_class ?= v.parse(),

"tilecount" => tilecount ?= v.parse::<u32>(),
"firstgid" => first_gid ?= v.parse::<u32>().map(Gid),
"tilewidth" => tile_width ?= v.parse::<u32>(),
"tileheight" => tile_height ?= v.parse::<u32>(),
}
((spacing, margin, columns, name), (tilecount, first_gid, tile_width, tile_height))
((spacing, margin, columns, name, user_type, user_class), (tilecount, first_gid, tile_width, tile_height))
);

let root_path = map_path.parent().ok_or(Error::PathIsNotFile)?.to_owned();
Expand All @@ -165,6 +175,7 @@ impl Tileset {
spacing,
margin,
name: name.unwrap_or_default(),
user_type: user_type.or(user_class),
root_path,
columns,
tilecount,
Expand Down Expand Up @@ -203,18 +214,23 @@ impl Tileset {
attrs: &[OwnedAttribute],
path: &Path,
) -> Result<Tileset> {
let ((spacing, margin, columns, name), (tilecount, tile_width, tile_height)) = get_attrs!(
let (
(spacing, margin, columns, name, user_type, user_class),
(tilecount, tile_width, tile_height),
) = get_attrs!(
for v in attrs {
Some("spacing") => spacing ?= v.parse(),
Some("margin") => margin ?= v.parse(),
Some("columns") => columns ?= v.parse(),
Some("name") => name = v,
Some("type") => user_type ?= v.parse(),
Some("class") => user_class ?= v.parse(),

"tilecount" => tilecount ?= v.parse::<u32>(),
"tilewidth" => tile_width ?= v.parse::<u32>(),
"tileheight" => tile_height ?= v.parse::<u32>(),
}
((spacing, margin, columns, name), (tilecount, tile_width, tile_height))
((spacing, margin, columns, name, user_type, user_class), (tilecount, tile_width, tile_height))
);

let root_path = path.parent().ok_or(Error::PathIsNotFile)?.to_owned();
Expand All @@ -225,6 +241,7 @@ impl Tileset {
spacing,
margin,
name: name.unwrap_or_default(),
user_type: user_type.or(user_class),
root_path,
columns,
tilecount,
Expand Down Expand Up @@ -282,6 +299,7 @@ impl Tileset {

Ok(Tileset {
name: prop.name,
user_type: prop.user_type,
tile_width: prop.tile_width,
tile_height: prop.tile_height,
spacing,
Expand Down