"git@weirdboi.dev:louis/big-brain.git" did not exist on "6c64df4fc1211abe19919a3628476b930b6e9919"
Newer
Older
use std::path::Path;
use xml::attribute::OwnedAttribute;
use crate::{
error::TiledError, properties::Properties, util::*, Color, Map, MapTilesetGid, MapWrapper,
};
mod image;
pub use image::*;
mod object;
pub use object::*;
mod tile;
pub use tile::*;
#[derive(Clone, PartialEq, Debug)]
TileLayer(TileLayerData),
ObjectLayer(ObjectLayerData),
ImageLayer(ImageLayerData),
}
#[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,
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
}
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)
}
LayerTag::GroupLayer => {
let (ty, properties) = GroupLayerData::new(parser, infinite, map_path, tilesets)?;
(LayerDataType::GroupLayer(ty), properties)
}
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
};
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>),
}
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)),
LayerDataType::GroupLayer(data) => Self::GroupLayer(GroupLayer::new(map, data)),