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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::path::Path;
use xml::attribute::OwnedAttribute;
use crate::{error::TiledError, properties::Properties, util::*, Map, MapTilesetGid, MapWrapper, Color};
mod image;
pub use image::*;
mod object;
pub use object::*;
mod tile;
pub use tile::*;
#[derive(Clone, PartialEq, Debug)]
pub enum LayerDataType {
TileLayer(TileLayerData),
ObjectLayer(ObjectLayerData),
ImageLayer(ImageLayerData),
// TODO: Support group layers
}
#[derive(Clone, Copy)]
pub(crate) enum LayerTag {
TileLayer,
ObjectLayer,
ImageLayer,
}
#[derive(Clone, PartialEq, Debug)]
pub struct LayerData {
pub name: String,
pub id: u32,
pub visible: bool,
pub offset_x: f32,
pub offset_y: f32,
pub parallax_x: f32,
pub parallax_y: f32,
pub opacity: f32,
pub tint_color: Option<Color>,
pub properties: Properties,
pub layer_type: LayerDataType,
}
impl LayerData {
pub(crate) fn new(
parser: &mut impl Iterator<Item = XmlEventResult>,
attrs: Vec<OwnedAttribute>,
tag: LayerTag,
infinite: bool,
map_path: &Path,
tilesets: &[MapTilesetGid],
) -> Result<Self, TiledError> {
let (
(opacity, tint_color, visible, offset_x, offset_y, parallax_x, parallax_y, name, id),
(),
) = get_attrs!(
attrs,
optionals: [
("opacity", opacity, |v:String| v.parse().ok()),
("tintcolor", tint_color, |v:String| v.parse().ok()),
("visible", visible, |v:String| v.parse().ok().map(|x:i32| x == 1)),
("offsetx", offset_x, |v:String| v.parse().ok()),
("offsety", offset_y, |v:String| v.parse().ok()),
("parallaxx", parallax_x, |v:String| v.parse().ok()),
("parallaxy", parallax_y, |v:String| v.parse().ok()),
("name", name, |v| Some(v)),
("id", id, |v:String| v.parse().ok()),
],
required: [
],
TiledError::MalformedAttributes("layer parsing error, no id attribute found".to_string())
);
let (ty, properties) = match tag {
LayerTag::TileLayer => {
let (ty, properties) = TileLayerData::new(parser, attrs, infinite, tilesets)?;
(LayerDataType::TileLayer(ty), properties)
}
LayerTag::ObjectLayer => {
let (ty, properties) = ObjectLayerData::new(parser, attrs, Some(tilesets))?;
(LayerDataType::ObjectLayer(ty), properties)
}
LayerTag::ImageLayer => {
let (ty, properties) = ImageLayerData::new(parser, map_path)?;
(LayerDataType::ImageLayer(ty), properties)
}
};
Ok(Self {
visible: visible.unwrap_or(true),
offset_x: offset_x.unwrap_or(0.0),
offset_y: offset_y.unwrap_or(0.0),
parallax_x: parallax_x.unwrap_or(1.0),
parallax_y: parallax_y.unwrap_or(1.0),
opacity: opacity.unwrap_or(1.0),
tint_color,
name: name.unwrap_or_default(),
id: id.unwrap_or(0),
properties,
layer_type: ty,
})
}
}
pub type Layer<'map> = MapWrapper<'map, LayerData>;
impl<'map> Layer<'map> {
/// Get the layer's type.
pub fn layer_type(&self) -> LayerType<'map> {
LayerType::new(self.map(), &self.data().layer_type)
}
}
pub enum LayerType<'map> {
TileLayer(TileLayer<'map>),
ObjectLayer(ObjectLayer<'map>),
ImageLayer(ImageLayer<'map>),
// TODO: Support group layers
}
impl<'map> LayerType<'map> {
fn new(map: &'map Map, data: &'map LayerDataType) -> Self {
match data {
LayerDataType::TileLayer(data) => Self::TileLayer(TileLayer::new(map, data)),
LayerDataType::ObjectLayer(data) => Self::ObjectLayer(ObjectLayer::new(map, data)),
LayerDataType::ImageLayer(data) => Self::ImageLayer(ImageLayer::new(map, data)),
}
}
}