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 template support #170

Merged
merged 21 commits into from
May 10, 2022
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
16 changes: 7 additions & 9 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl ObjectData {
reader: &mut impl ResourceReader,
cache: &mut impl ResourceCache,
) -> Result<ObjectData> {
let (mut id, mut tile, mut x, mut y, mut n, mut t, mut w, mut h, mut v, mut r, template) = get_attrs!(
let (id, mut tile, x, y, mut n, mut t, mut w, mut h, mut v, mut r, template) = get_attrs!(
attrs,
optionals: [
("id", id, |v:String| v.parse().ok()),
Expand All @@ -210,10 +210,9 @@ impl ObjectData {

// If the template attribute is there, we need to go fetch the template file
let template = template
.map(|template| {
let s: String = template;
.map(|template_path: String| {
let parent_dir = base_path.parent().ok_or(Error::PathIsNotFile)?;
let template_path = parent_dir.join(Path::new(&s));
let template_path = parent_dir.join(Path::new(&template_path));

// Check the cache to see if this template exists
let template = if let Some(templ) = cache.get_template(&template_path) {
Expand All @@ -227,15 +226,12 @@ impl ObjectData {

// The template sets the default values for the object
let obj = &template.object;
x.get_or_insert(obj.x);
y.get_or_insert(obj.y);
v.get_or_insert(obj.visible);
#[allow(deprecated)]
w.get_or_insert(obj.width);
#[allow(deprecated)]
h.get_or_insert(obj.height);
r.get_or_insert(obj.rotation);
id.get_or_insert(obj.id);
n.get_or_insert(obj.name.clone());
t.get_or_insert(obj.obj_type.clone());
if let Some(templ_tile) = obj.tile.clone() {
Expand Down Expand Up @@ -283,18 +279,20 @@ impl ObjectData {
},
});

let shape = shape.unwrap_or(ObjectShape::Rect { width, height });

// 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());
bjorn marked this conversation as resolved.
Show resolved Hide resolved

for (k, v) in &templ.object.properties {
if !properties.contains_key(k) {
properties.insert(k.clone(), v.clone());
}
}
}

let shape = shape.unwrap_or(ObjectShape::Rect { width, height });

#[allow(deprecated)]
Ok(ObjectData {
id,
Expand Down