Newer
Older
use xml::attribute::OwnedAttribute;
/// A reference to an image stored somewhere within the filesystem.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Image {
/// The filepath of the image.
///
/// ## Note
/// The crate does not currently support embedded images (Even though Tiled
/// does not allow creating maps with embedded image data, the TMX format does; [source])
///
/// [source]: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#image
// TODO: Embedded images
pub source: PathBuf,
/// The width in pixels of the image.
/// The height in pixels of the image.
/// A color that should be interpreted as transparent (0 alpha), if any.
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
) -> Result<Image, TiledError> {
let (c, (s, w, h)) = get_attrs!(
attrs,
optionals: [
("trans", trans, |v:String| v.parse().ok()),
],
required: [
("source", source, |v| Some(v)),
("width", width, |v:String| v.parse().ok()),
("height", height, |v:String| v.parse().ok()),
],
TiledError::MalformedAttributes("Image must have a source, width and height with correct types".to_string())
width: w,
height: h,
transparent_colour: c,
})
}
}