-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathlayers.rs
297 lines (271 loc) · 8.78 KB
/
layers.rs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::{collections::HashMap, io::Read, path::Path};
use xml::{attribute::OwnedAttribute, EventReader};
use crate::{
error::TiledError,
image::Image,
objects::Object,
properties::{parse_properties, Color, Properties},
util::*,
};
/// Stores the proper tile gid, along with how it is flipped.
// Maybe PartialEq and Eq should be custom, so that it ignores tile-flipping?
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LayerTile {
pub gid: u32,
pub flip_h: bool,
pub flip_v: bool,
pub flip_d: bool,
}
const FLIPPED_HORIZONTALLY_FLAG: u32 = 0x80000000;
const FLIPPED_VERTICALLY_FLAG: u32 = 0x40000000;
const FLIPPED_DIAGONALLY_FLAG: u32 = 0x20000000;
const ALL_FLIP_FLAGS: u32 =
FLIPPED_HORIZONTALLY_FLAG | FLIPPED_VERTICALLY_FLAG | FLIPPED_DIAGONALLY_FLAG;
impl LayerTile {
pub fn new(id: u32) -> LayerTile {
let flags = id & ALL_FLIP_FLAGS;
let gid = id & !ALL_FLIP_FLAGS;
let flip_d = flags & FLIPPED_DIAGONALLY_FLAG == FLIPPED_DIAGONALLY_FLAG; // Swap x and y axis (anti-diagonally) [flips over y = -x line]
let flip_h = flags & FLIPPED_HORIZONTALLY_FLAG == FLIPPED_HORIZONTALLY_FLAG; // Flip tile over y axis
let flip_v = flags & FLIPPED_VERTICALLY_FLAG == FLIPPED_VERTICALLY_FLAG; // Flip tile over x axis
LayerTile {
gid,
flip_h,
flip_v,
flip_d,
}
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum LayerType {
TileLayer(TileLayer),
ObjectLayer(ObjectLayer),
ImageLayer(ImageLayer),
// TODO: Support group layers
}
#[derive(Clone, Copy)]
pub(crate) enum LayerTag {
TileLayer,
ObjectLayer,
ImageLayer,
}
#[derive(Clone, PartialEq, Debug)]
pub struct Layer {
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 properties: Properties,
pub layer_type: LayerType,
}
impl Layer {
pub(crate) fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
tag: LayerTag,
infinite: bool,
path_relative_to: Option<&Path>,
) -> Result<Self, TiledError> {
let ((opacity, visible, offset_x, offset_y, parallax_x, parallax_y, name, id), ()) = get_attrs!(
attrs,
optionals: [
("opacity", opacity, |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) = TileLayer::new(parser, attrs, infinite)?;
(LayerType::TileLayer(ty), properties)
}
LayerTag::ObjectLayer => {
let (ty, properties) = ObjectLayer::new(parser, attrs)?;
(LayerType::ObjectLayer(ty), properties)
}
LayerTag::ImageLayer => {
let (ty, properties) = ImageLayer::new(parser, path_relative_to)?;
(LayerType::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),
name: name.unwrap_or_default(),
id: id.unwrap_or(0),
properties,
layer_type: ty,
})
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct TileLayer {
pub width: u32,
pub height: u32,
/// The tiles are arranged in rows. Each tile is a number which can be used
/// to find which tileset it belongs to and can then be rendered.
pub tiles: LayerData,
}
impl TileLayer {
pub(crate) fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
infinite: bool,
) -> Result<(TileLayer, Properties), TiledError> {
let ((), (w, h)) = get_attrs!(
attrs,
optionals: [
],
required: [
("width", width, |v: String| v.parse().ok()),
("height", height, |v: String| v.parse().ok()),
],
TiledError::MalformedAttributes("layer parsing error, width and height attributes required".to_string())
);
let mut tiles: LayerData = LayerData::Finite(Default::default());
let mut properties = HashMap::new();
parse_tag!(parser, "layer", {
"data" => |attrs| {
if infinite {
tiles = parse_infinite_data(parser, attrs)?;
} else {
tiles = parse_data(parser, attrs)?;
}
Ok(())
},
"properties" => |_| {
properties = parse_properties(parser)?;
Ok(())
},
});
Ok((
TileLayer {
width: w,
height: h,
tiles: tiles,
},
properties,
))
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum LayerData {
Finite(Vec<LayerTile>),
Infinite(HashMap<(i32, i32), Chunk>),
}
#[derive(Debug, PartialEq, Clone)]
pub struct ImageLayer {
pub image: Option<Image>,
}
impl ImageLayer {
pub(crate) fn new<R: Read>(
parser: &mut EventReader<R>,
path_relative_to: Option<&Path>,
) -> Result<(ImageLayer, Properties), TiledError> {
let mut image: Option<Image> = None;
let mut properties = HashMap::new();
parse_tag!(parser, "imagelayer", {
"image" => |attrs| {
image = Some(Image::new(parser, attrs, path_relative_to.ok_or(TiledError::SourceRequired{object_to_parse: "Image".to_string()})?)?);
Ok(())
},
"properties" => |_| {
properties = parse_properties(parser)?;
Ok(())
},
});
Ok((ImageLayer { image }, properties))
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct ObjectLayer {
pub objects: Vec<Object>,
pub colour: Option<Color>,
}
impl ObjectLayer {
pub(crate) fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
) -> Result<(ObjectLayer, 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(Object::new(parser, attrs)?);
Ok(())
},
"properties" => |_| {
properties = parse_properties(parser)?;
Ok(())
},
});
Ok((
ObjectLayer {
objects: objects,
colour: c,
},
properties,
))
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Chunk {
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
pub tiles: Vec<LayerTile>,
}
impl Chunk {
pub(crate) fn new<R: Read>(
parser: &mut EventReader<R>,
attrs: Vec<OwnedAttribute>,
encoding: Option<String>,
compression: Option<String>,
) -> Result<Chunk, TiledError> {
let ((), (x, y, width, height)) = get_attrs!(
attrs,
optionals: [],
required: [
("x", x, |v: String| v.parse().ok()),
("y", y, |v: String| v.parse().ok()),
("width", width, |v: String| v.parse().ok()),
("height", height, |v: String| v.parse().ok()),
],
TiledError::MalformedAttributes("layer must have a name".to_string())
);
let tiles = parse_data_line(encoding, compression, parser)?;
Ok(Chunk {
x,
y,
width,
height,
tiles,
})
}
}