Skip to content

Commit

Permalink
Add source members to Tileset, Map and Template
Browse files Browse the repository at this point in the history
  • Loading branch information
aleokdev committed Jun 21, 2024
1 parent a0bc45f commit f569966
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 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.13.0)]
### Added
- A way to obtain where tilesets, templates and maps have been loaded from

## [0.12.0]
### Added
- Add `text`, `width` and `height` members to `ObjectShape::Text`. (#278)
Expand Down
11 changes: 10 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
//! Structures related to Tiled maps.
use std::{collections::HashMap, fmt, path::Path, str::FromStr, sync::Arc};
use std::{
collections::HashMap,
fmt,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
};

use xml::attribute::OwnedAttribute;

Expand All @@ -22,6 +28,8 @@ pub(crate) struct MapTilesetGid {
#[derive(PartialEq, Clone, Debug)]
pub struct Map {
version: String,
/// The path first used in a [`ResourceReader`] to load this map.
pub source: PathBuf,
/// The way tiles are laid out in the map.
pub orientation: Orientation,
/// Width of the map, in tiles.
Expand Down Expand Up @@ -253,6 +261,7 @@ impl Map {

Ok(Map {
version: v,
source: map_path.to_owned(),
orientation: o,
width: w,
height: h,
Expand Down
10 changes: 8 additions & 2 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use xml::EventReader;
Expand All @@ -15,6 +15,8 @@ use crate::{
/// maps.
#[derive(Clone, Debug)]
pub struct Template {
/// The path first used in a [`ResourceReader`] to load this template.
pub source: PathBuf,
/// The tileset this template contains a reference to
pub tileset: Option<Arc<Tileset>>,
/// The object data for this template
Expand Down Expand Up @@ -102,6 +104,10 @@ impl Template {

let object = object.ok_or(Error::TemplateHasNoObject)?;

Ok(Arc::new(Template { tileset, object }))
Ok(Arc::new(Template {
source: template_path.to_owned(),
tileset,
object,
}))
}
}
8 changes: 8 additions & 0 deletions src/tileset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub use wangset::*;
/// Also see the [TMX docs](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tileset).
#[derive(Debug, PartialEq, Clone)]
pub struct Tileset {
/// The path first used in a [`ResourceReader`] to load this tileset.
///
/// For embedded tilesets, this path will be the same as the template or map's source.
pub source: PathBuf,
/// The name of the tileset, set by the user.
pub name: String,
/// The (maximum) width in pixels of the tiles in this tileset. Irrelevant for [image collection]
Expand Down Expand Up @@ -157,6 +161,7 @@ impl Tileset {

Self::finish_parsing_xml(
parser,
path.to_owned(),
TilesetProperties {
spacing,
margin,
Expand Down Expand Up @@ -227,6 +232,7 @@ impl Tileset {

Self::finish_parsing_xml(
parser,
path.to_owned(),
TilesetProperties {
spacing,
margin,
Expand All @@ -245,6 +251,7 @@ impl Tileset {

fn finish_parsing_xml(
parser: &mut impl Iterator<Item = XmlEventResult>,
container_path: PathBuf,
prop: TilesetProperties,
reader: &mut impl ResourceReader,
cache: &mut impl ResourceCache,
Expand Down Expand Up @@ -303,6 +310,7 @@ impl Tileset {
.unwrap_or_else(|| Self::calculate_columns(&image, prop.tile_width, margin, spacing))?;

Ok(Tileset {
source: container_path,
name: prop.name,
user_type: prop.user_type,
tile_width: prop.tile_width,
Expand Down
44 changes: 37 additions & 7 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn as_finite<'map>(data: TileLayer<'map>) -> FiniteTileLayer<'map> {
}
}

fn compare_everything_but_tileset_sources(r: &Map, e: &Map) {
fn compare_everything_but_sources(r: &Map, e: &Map) {
assert_eq!(r.version(), e.version());
assert_eq!(r.orientation, e.orientation);
assert_eq!(r.width, e.width);
Expand All @@ -39,10 +39,10 @@ fn test_gzip_and_zlib_encoded_and_raw_are_the_same() {
.load_tmx_map("assets/tiled_base64_zstandard.tmx")
.unwrap();
let c = Loader::new().load_tmx_map("assets/tiled_csv.tmx").unwrap();
compare_everything_but_tileset_sources(&z, &g);
compare_everything_but_tileset_sources(&z, &r);
compare_everything_but_tileset_sources(&z, &c);
compare_everything_but_tileset_sources(&z, &zstd);
compare_everything_but_sources(&z, &g);
compare_everything_but_sources(&z, &r);
compare_everything_but_sources(&z, &c);
compare_everything_but_sources(&z, &zstd);

let layer = as_finite(c.get_layer(0).unwrap().as_tile_layer().unwrap());
{
Expand All @@ -65,11 +65,11 @@ fn test_external_tileset() {
let e = loader
.load_tmx_map("assets/tiled_base64_external.tmx")
.unwrap();
compare_everything_but_tileset_sources(&r, &e);
compare_everything_but_sources(&r, &e);
}

#[test]
fn test_sources() {
fn test_cache() {
let mut loader = Loader::new();
let e = loader
.load_tmx_map("assets/tiled_base64_external.tmx")
Expand All @@ -84,6 +84,36 @@ fn test_sources() {
);
}

#[test]
fn test_external_sources() {
let mut loader = Loader::new();
let e = loader
.load_tmx_map("assets/tiled_base64_external.tmx")
.unwrap();
assert_eq!(e.source, "assets/tiled_base64_external.tmx".to_owned());
assert_eq!(e.tilesets()[0].source, "assets/tilesheet.tsx".to_owned());

let e = loader
.load_tmx_map("assets/tiled_object_template.tmx")
.unwrap();
assert_eq!(e.source, "assets/tiled_object_template.tmx".to_owned());
assert_eq!(
e.tilesets()[0].source,
"assets/tiled_object_template.tx".to_owned()
);
}

#[test]
fn test_embedded_sources() {
let e = loader.load_tmx_map("assets/tiled_base64_gzip.tmx").unwrap();

assert_eq!(e.source, "assets/tiled_base64_gzip.tmx".to_owned());
assert_eq!(
e.tilesets()[0].source,
"assets/tiled_base64_gzip.tmx".to_owned()
);
}

#[test]
fn test_just_tileset() {
let mut loader = Loader::new();
Expand Down

0 comments on commit f569966

Please sign in to comment.