1use std::{collections::HashMap, path::Path};
2
3use xml::attribute::OwnedAttribute;
4
5use crate::{
6 parse_properties,
7 util::{get_attrs, map_wrapper, parse_tag, XmlEventResult},
8 Error, Image, Properties, Result,
9};
10
11#[derive(Debug, PartialEq, Clone)]
13pub struct ImageLayerData {
14 pub image: Option<Image>,
16 pub repeat_x: bool,
18 pub repeat_y: bool,
20}
21
22impl ImageLayerData {
23 pub(crate) fn new(
24 parser: &mut impl Iterator<Item = XmlEventResult>,
25 attrs: Vec<OwnedAttribute>,
26 map_path: &Path,
27 ) -> Result<(Self, Properties)> {
28 let mut image: Option<Image> = None;
29 let mut properties = HashMap::new();
30
31 let path_relative_to = map_path.parent().ok_or(Error::PathIsNotFile)?;
32
33 let (repeat_x, repeat_y) = get_attrs!(
35 for v in attrs {
36 Some("repeatx") => repeat_x ?= v.parse::<i32>().map(|val| val == 1),
37 Some("repeaty") => repeat_y ?= v.parse::<i32>().map(|val| val == 1),
38 }
39 (repeat_x, repeat_y)
40 );
41
42 parse_tag!(parser, "imagelayer", {
43 "image" => |attrs| {
44 image = Some(Image::new(parser, attrs, path_relative_to)?);
45 Ok(())
46 },
47 "properties" => |_| {
48 properties = parse_properties(parser)?;
49 Ok(())
50 },
51 });
52 Ok((
53 ImageLayerData {
54 image,
55 repeat_x: repeat_x.unwrap_or(false),
56 repeat_y: repeat_y.unwrap_or(false),
57 },
58 properties,
59 ))
60 }
61}
62
63map_wrapper!(
64 #[doc = "A layer consisting of a single image."]
65 #[doc = "\nAlso see the [TMX docs](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#imagelayer)."]
66 ImageLayer => ImageLayerData
67);