Skip to content

Commit

Permalink
Fixed template instance size and position overrides (#311)
Browse files Browse the repository at this point in the history
Template instances of rectangle, ellipse and text objects can override
the width and height of the template object. This is now taken into
account when reading out the attributes and when inheriting the shape
from the template.

Template instances of point objects are now also correctly storing the
instance position in the Point shape, instead of the position of the
template object.

Added a small test for the resized templated object case.

Closes #310
  • Loading branch information
bjorn authored Sep 3, 2024
1 parent 5864f8f commit 1ffab03
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased (0.12.2)]
### Fixed
- Fixed template instance size and position overrides in `ObjectData::shape`. (#309)

## [0.12.1]
### Changed
- Improved documentation on `Map::layers` and `Map::get_layer`. (#306)
Expand Down
3 changes: 2 additions & 1 deletion assets/tiled_object_template.tmx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.2" orientation="orthogonal" renderorder="right-down" width="3" height="3" tilewidth="32" tileheight="32" infinite="0" nextlayerid="3" nextobjectid="3">
<map version="1.11" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="3" height="3" tilewidth="32" tileheight="32" infinite="0" nextlayerid="3" nextobjectid="4">
<tileset firstgid="1" source="tilesheet.tsx"/>
<layer id="1" name="Tile Layer 1" width="3" height="3">
<data encoding="csv">
Expand All @@ -14,5 +14,6 @@
</properties>
</object>
<object id="2" gid="45" x="0" y="32" width="32" height="32"/>
<object id="3" template="tiled_object_template.tx" x="0" y="64" width="64" height="32"/>
</objectgroup>
</map>
57 changes: 53 additions & 4 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl ObjectData {
reader: &mut impl ResourceReader,
cache: &mut impl ResourceCache,
) -> Result<ObjectData> {
let (id, tile, mut n, mut t, c, w, h, mut v, mut r, template, x, y) = get_attrs!(
let (id, tile, mut n, mut t, c, mut w, mut h, mut v, mut r, template, x, y) = get_attrs!(
for v in attrs {
Some("id") => id ?= v.parse(),
Some("gid") => tile ?= v.parse::<u32>(),
Expand Down Expand Up @@ -275,6 +275,15 @@ impl ObjectData {
if let Some(templ_tile) = &obj.tile {
tile.get_or_insert_with(|| templ_tile.clone());
}
match &obj.shape {
ObjectShape::Rect { width, height }
| ObjectShape::Ellipse { width, height }
| ObjectShape::Text { width, height, .. } => {
w.get_or_insert(*width);
h.get_or_insert(*height);
}
_ => {}
}
Ok(template)
})
.transpose()?;
Expand Down Expand Up @@ -319,11 +328,51 @@ impl ObjectData {
},
});

// Possibly copy properties from the template into the object
// Any that already exist in the object's map don't get copied over
if let Some(templ) = template {
shape.get_or_insert(templ.object.shape.clone());
shape.get_or_insert_with(|| {
// Inherit the shape from the template but use the size and
// position from the object where relevant
match &templ.object.shape {
ObjectShape::Rect { .. } => ObjectShape::Rect { width, height },
ObjectShape::Ellipse { .. } => ObjectShape::Ellipse { width, height },
ObjectShape::Point(_, _) => ObjectShape::Point(x, y),
ObjectShape::Text {
font_family,
pixel_size,
wrap,
color,
bold,
italic,
underline,
strikeout,
kerning,
halign,
valign,
text,
width: _,
height: _,
} => ObjectShape::Text {
font_family: font_family.clone(),
pixel_size: pixel_size.clone(),
wrap: wrap.clone(),
color: color.clone(),
bold: bold.clone(),
italic: italic.clone(),
underline: underline.clone(),
strikeout: strikeout.clone(),
kerning: kerning.clone(),
halign: halign.clone(),
valign: valign.clone(),
text: text.clone(),
width,
height,
},
shape => shape.clone(),
}
});

// Possibly copy properties from the template into the object
// Any that already exist in the object's map don't get copied over
for (k, v) in &templ.object.properties {
if !properties.contains_key(k) {
properties.insert(k.clone(), v.clone());
Expand Down
8 changes: 8 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ fn test_object_template_property() {
let object_layer = r.get_layer(1).unwrap().as_object_layer().unwrap();
let object = object_layer.get_object(0).unwrap(); // The templated object
let object_nt = object_layer.get_object(1).unwrap(); // The non-templated object
let object_resized = object_layer.get_object(2).unwrap(); // The resized templated object

// Test core properties
assert_eq!(
Expand All @@ -450,6 +451,13 @@ fn test_object_template_property() {
);
assert_eq!(object.x, 32.0);
assert_eq!(object.y, 32.0);
assert_eq!(
object_resized.shape,
ObjectShape::Rect {
width: 64.0,
height: 32.0,
}
);

// Test properties are copied over
assert_eq!(
Expand Down

0 comments on commit 1ffab03

Please sign in to comment.