Newer
Older
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
47
48
49
50
51
52
53
54
55
56
57
58
59
use std::collections::HashMap;
use xml::attribute::OwnedAttribute;
use crate::{
parse_properties,
util::{get_attrs, parse_tag, XmlEventResult},
Color, MapTilesetGid, MapWrapper, Object, ObjectData, Properties, TiledError,
};
#[derive(Debug, PartialEq, Clone)]
pub struct ObjectLayerData {
pub objects: Vec<ObjectData>,
pub colour: Option<Color>,
}
impl ObjectLayerData {
/// If it is known that there are no objects with tile images in it (i.e. collision data)
/// then we can pass in [`None`] as the tilesets
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
tilesets: Option<&[MapTilesetGid]>,
) -> Result<(ObjectLayerData, Properties), TiledError> {
let (c, ()) = get_attrs!(
attrs,
optionals: [
("color", colour, |v:String| v.parse().ok()),
],
required: [],
// this error should never happen since there are no required attrs
TiledError::MalformedAttributes("object group parsing error".to_string())
);
let mut objects = Vec::new();
let mut properties = HashMap::new();
parse_tag!(parser, "objectgroup", {
"object" => |attrs| {
objects.push(ObjectData::new(parser, attrs, tilesets)?);
Ok(())
},
"properties" => |_| {
properties = parse_properties(parser)?;
Ok(())
},
});
Ok((ObjectLayerData { objects, colour: c }, properties))
}
}
pub type ObjectLayer<'map> = MapWrapper<'map, ObjectLayerData>;
impl<'map> ObjectLayer<'map> {
pub fn get_object(&self, idx: usize) -> Option<Object<'map>> {
self.data()
.objects
.get(idx)
.map(|data| Object::new(self.map(), data))
}
}