Skip to content

Commit

Permalink
Fixed template instance size and position overrides
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.

Closes #310
  • Loading branch information
bjorn committed Sep 3, 2024
1 parent 5864f8f commit 3a6798c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 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
59 changes: 55 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,53 @@ 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

0 comments on commit 3a6798c

Please sign in to comment.