diff --git a/CHANGELOG.md b/CHANGELOG.md
index c268984f..5676588a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,6 +16,7 @@ objects. As such, `ResourceCache` has now methods for both getting and inserting
### Changed
- `LayerType` variants have been stripped from the `Layer` suffix (#203).
- `ResourceCache::get_or_try_insert_tileset_with` has been replaced by `ResourceCache::insert_tileset`.
+- `DefaultResourceCache`'s members have been made public.
## [0.10.2]
### Added
diff --git a/assets/templates/corner.tx b/assets/templates/corner.tx
new file mode 100644
index 00000000..d0dd5ba6
--- /dev/null
+++ b/assets/templates/corner.tx
@@ -0,0 +1,4 @@
+
+
+
+
diff --git a/assets/templates/edge.tx b/assets/templates/edge.tx
new file mode 100644
index 00000000..bab6d7ad
--- /dev/null
+++ b/assets/templates/edge.tx
@@ -0,0 +1,4 @@
+
+
+
+
diff --git a/assets/templates/example.tmx b/assets/templates/example.tmx
new file mode 100644
index 00000000..cc81717e
--- /dev/null
+++ b/assets/templates/example.tmx
@@ -0,0 +1,118 @@
+
+
diff --git a/assets/templates/grass_walls.tsx b/assets/templates/grass_walls.tsx
new file mode 100644
index 00000000..37bd765a
--- /dev/null
+++ b/assets/templates/grass_walls.tsx
@@ -0,0 +1,111 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/assets/templates/simple_figure.tsx b/assets/templates/simple_figure.tsx
new file mode 100644
index 00000000..9fe794b7
--- /dev/null
+++ b/assets/templates/simple_figure.tsx
@@ -0,0 +1,4 @@
+
+
+
+
diff --git a/assets/templates/simple_figure.tx b/assets/templates/simple_figure.tx
new file mode 100644
index 00000000..da61da2b
--- /dev/null
+++ b/assets/templates/simple_figure.tx
@@ -0,0 +1,9 @@
+
+
+
+
+
diff --git a/src/cache.rs b/src/cache.rs
index 68cf7a62..60951ea2 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -49,11 +49,13 @@ pub trait ResourceCache {
fn insert_template(&mut self, path: impl AsRef, tileset: Arc);
}
-/// A cache that identifies resources by their path, storing a map of them.
+/// A cache that identifies resources by their path, storing them in a [`HashMap`].
#[derive(Debug, Default)]
pub struct DefaultResourceCache {
- tilesets: HashMap>,
- templates: HashMap>,
+ /// The tilesets cached until now.
+ pub tilesets: HashMap>,
+ /// The templates cached until now.
+ pub templates: HashMap>,
}
impl DefaultResourceCache {
diff --git a/src/layers/mod.rs b/src/layers/mod.rs
index f5067c49..e564d2a7 100644
--- a/src/layers/mod.rs
+++ b/src/layers/mod.rs
@@ -102,7 +102,7 @@ impl LayerData {
attrs,
Some(tilesets),
for_tileset,
- map_path,
+ map_path.parent().ok_or(crate::Error::PathIsNotFile)?,
reader,
cache,
)?;
diff --git a/src/layers/object.rs b/src/layers/object.rs
index 3cc7de82..c8f77f8b 100644
--- a/src/layers/object.rs
+++ b/src/layers/object.rs
@@ -25,6 +25,7 @@ impl ObjectLayerData {
attrs: Vec,
tilesets: Option<&[MapTilesetGid]>,
for_tileset: Option>,
+ // path_relative_to is a directory to which all other files are relative to
path_relative_to: &Path,
reader: &mut impl ResourceReader,
cache: &mut impl ResourceCache,
diff --git a/src/objects.rs b/src/objects.rs
index 45ca89f4..5cd5c5fa 100644
--- a/src/objects.rs
+++ b/src/objects.rs
@@ -186,6 +186,7 @@ impl ObjectData {
attrs: Vec,
tilesets: Option<&[MapTilesetGid]>,
for_tileset: Option>,
+ // Base path is a directory to which all other files are relative to
base_path: &Path,
reader: &mut impl ResourceReader,
cache: &mut impl ResourceCache,
@@ -211,8 +212,7 @@ impl ObjectData {
// If the template attribute is there, we need to go fetch the template file
let template = template
.map(|template_path: String| {
- let parent_dir = base_path.parent().ok_or(Error::PathIsNotFile)?;
- let template_path = parent_dir.join(Path::new(&template_path));
+ let template_path = base_path.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) {
diff --git a/src/template.rs b/src/template.rs
index ffa84775..104178a8 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -73,7 +73,7 @@ impl Template {
parse_tag!(parser, "template", {
"object" => |attrs| {
- object = Some(ObjectData::new(parser, attrs, Some(&tileset_gid), tileset.clone(), template_path, reader, cache)?);
+ object = Some(ObjectData::new(parser, attrs, Some(&tileset_gid), tileset.clone(), template_path.parent().ok_or(Error::PathIsNotFile)?, reader, cache)?);
Ok(())
},
"tileset" => |attrs: Vec| {
diff --git a/tests/lib.rs b/tests/lib.rs
index 4de74995..60b43367 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -467,3 +467,30 @@ fn test_object_template_property() {
assert_eq!(object.get_tile().unwrap().id(), 44);
assert_eq!(object_nt.get_tile().unwrap().id(), 44);
}
+
+#[test]
+fn test_templates() {
+ let mut loader = Loader::new();
+ let map = loader.load_tmx_map("assets/templates/example.tmx").unwrap();
+
+ assert_eq!(loader.cache().templates.len(), 3);
+ assert_eq!(
+ if let LayerType::Tiles(x) = map.get_layer(0).unwrap().layer_type() {
+ x
+ } else {
+ panic!()
+ }
+ .get_tile(0, 0)
+ .unwrap()
+ .get_tileset()
+ .image
+ .as_ref()
+ .unwrap()
+ .source
+ .canonicalize()
+ .unwrap(),
+ PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/tilesheet.png"))
+ .canonicalize()
+ .unwrap()
+ );
+}