1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//! Structures related to tile animations.
use xml::attribute::OwnedAttribute;
use crate::{
error::{Error, Result},
util::{get_attrs, parse_tag, XmlEventResult},
};
/// A structure describing a [frame] of a [TMX tile animation].
///
/// [frame]: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-frame
/// [TMX tile animation]: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#animation
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Frame {
/// The local ID of a tile within the parent tileset.
pub tile_id: u32,
/// How long (in milliseconds) this frame should be displayed before advancing to the next frame.
pub duration: u32,
}
impl Frame {
pub(crate) fn new(attrs: Vec<OwnedAttribute>) -> Result<Frame> {
let (tile_id, duration) = get_attrs!(
for v in attrs {
"tileid" => tile_id ?= v.parse::<u32>(),
"duration" => duration ?= v.parse::<u32>(),
}
(tile_id, duration)
);
Ok(Frame { tile_id, duration })
}
}
pub(crate) fn parse_animation(
parser: &mut impl Iterator<Item = XmlEventResult>,
) -> Result<Vec<Frame>> {
let mut animation = Vec::new();
parse_tag!(parser, "animation", {
"frame" => |attrs| {
animation.push(Frame::new(attrs)?);
Ok(())
},
});
Ok(animation)
}