Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/current' into current_next_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
aleokdev committed Oct 22, 2024
2 parents 87a4be5 + 1ffab03 commit 78eaf51
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 11 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added a `source` member to `Tileset`, `Map` and `Template`, which stores the resource path they have been loaded from.

## [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)
- Extend lifetime of `Layer`s returned from `GroupLayer::get_layer` to the map's. (#307)

## [0.12.0]
### Added
- Add `text`, `width` and `height` members to `ObjectShape::Text`. (#278)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiled"
version = "0.12.0"
version = "0.12.1"
description = "A rust crate for loading maps created by the Tiled editor"
categories = ["game-development"]
keywords = ["gamedev", "tiled", "tmx", "map"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rs-tiled
```toml
tiled = "0.12.0"
tiled = "0.12.1"
```

[![Rust](https://github.com/mapeditor/rs-tiled/actions/workflows/rust.yml/badge.svg)](https://github.com/mapeditor/rs-tiled/actions/workflows/rust.yml)
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>
2 changes: 1 addition & 1 deletion src/layers/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'map> GroupLayer<'map> {
.map(move |layer| Layer::new(map, layer))
}
/// Gets a specific layer from the group by index.
pub fn get_layer(&self, index: usize) -> Option<Layer> {
pub fn get_layer(&self, index: usize) -> Option<Layer<'map>> {
self.data
.layers
.get(index)
Expand Down
30 changes: 27 additions & 3 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) struct MapTilesetGid {
}

/// All Tiled map files will be parsed into this. Holds all the layers and tilesets.
#[derive(PartialEq, Clone, Debug)]
#[derive(PartialEq, Clone)]
pub struct Map {
version: String,
/// The path first used in a [`ResourceReader`] to load this map.
Expand Down Expand Up @@ -73,6 +73,27 @@ pub struct Map {
pub user_type: Option<String>,
}

impl fmt::Debug for Map {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Map")
.field("version", &self.version)
.field("orientation", &self.orientation)
.field("width", &self.width)
.field("height", &self.height)
.field("tile_width", &self.tile_width)
.field("tile_height", &self.tile_height)
.field("stagger_axis", &self.stagger_axis)
.field("stagger_index", &self.stagger_index)
.field("tilesets", &format!("{} tilesets", self.tilesets.len()))
.field("layers", &format!("{} layers", self.layers.len()))
.field("properties", &self.properties)
.field("background_color", &self.background_color)
.field("infinite", &self.infinite)
.field("user_type", &self.user_type)
.finish()
}
}

impl Map {
/// The TMX format version this map was saved to. Equivalent to the map file's `version`
/// attribute.
Expand All @@ -95,7 +116,10 @@ impl Map {
self.tilesets.as_ref()
}

/// Get an iterator over all the layers in the map in ascending order of their layer index.
/// Get an iterator over top-level layers in the map in ascending order of their layer index.
///
/// Note: "top-level" means that if a map has layers of `LayerDataType::Group` type, you
/// need to recursively enumerate those group layers.
///
/// ## Example
/// ```
Expand Down Expand Up @@ -126,7 +150,7 @@ impl Map {
self.layers.iter().map(move |layer| Layer::new(self, layer))
}

/// Returns the layer that has the specified index, if it exists.
/// Returns the top-level layer that has the specified index, if it exists.
pub fn get_layer(&self, index: usize) -> Option<Layer> {
self.layers.get(index).map(|data| Layer::new(self, data))
}
Expand Down
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 @@ -473,6 +473,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 @@ -484,6 +485,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 78eaf51

Please sign in to comment.