tiled 0.15.0

A rust crate for loading maps created by the Tiled editor
Documentation
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use std::{collections::HashMap, path::Path, sync::Arc};

use xml::attribute::OwnedAttribute;

use crate::{
    error::{Error, Result},
    properties::{parse_properties, Properties},
    template::Template,
    util::{get_attrs, map_wrapper, parse_tag, XmlEventResult},
    Color, Gid, MapTilesetGid, ResourceCache, ResourceReader, Tile, TileId, Tileset,
};

/// The location of the tileset this tile is in
///
/// Tilesets can be contained within either a map or a template.
#[derive(Clone, Debug, PartialEq)]
pub enum TilesetLocation {
    /// Index into the Map's tileset list, guaranteed to be a valid index of the map tileset container.
    Map(usize),
    /// Arc of the tileset itself if and only if this location is from a template.
    Template(Arc<Tileset>),
}

/// Stores the internal tile gid about a layer tile, along with how it is flipped.
#[derive(Clone, Debug, PartialEq)]
pub struct ObjectTileData {
    /// A valid TilesetLocation that points to a tileset that **may or may not contain** this tile.
    tileset_location: TilesetLocation,
    /// The local ID of the tile in the tileset it's in.
    id: TileId,
    /// Whether this tile is flipped on its Y axis (horizontally).
    pub flip_h: bool,
    /// Whether this tile is flipped on its X axis (vertically).
    pub flip_v: bool,
    /// Whether this tile is flipped diagonally.
    pub flip_d: bool,
}

impl ObjectTileData {
    /// Get the layer tile's local id within its parent tileset.
    #[inline]
    pub fn id(&self) -> TileId {
        self.id
    }

    /// Get a reference to the object tile data's tileset location, which points to a tileset that
    /// **may or may not contain** this tile.
    #[inline]
    pub fn tileset_location(&self) -> &TilesetLocation {
        &self.tileset_location
    }

    const FLIPPED_HORIZONTALLY_FLAG: u32 = 0x80000000;
    const FLIPPED_VERTICALLY_FLAG: u32 = 0x40000000;
    const FLIPPED_DIAGONALLY_FLAG: u32 = 0x20000000;
    const ALL_FLIP_FLAGS: u32 = Self::FLIPPED_HORIZONTALLY_FLAG
        | Self::FLIPPED_VERTICALLY_FLAG
        | Self::FLIPPED_DIAGONALLY_FLAG;

    /// Creates a new [`ObjectTileData`] from a [`Gid`] plus its flipping bits.
    pub(crate) fn from_bits(
        bits: u32,
        tilesets: &[MapTilesetGid],
        for_tileset: Option<Arc<Tileset>>,
    ) -> Option<Self> {
        let flags = bits & Self::ALL_FLIP_FLAGS;
        let gid = Gid(bits & !Self::ALL_FLIP_FLAGS);
        let flip_d = flags & Self::FLIPPED_DIAGONALLY_FLAG == Self::FLIPPED_DIAGONALLY_FLAG; // Swap x and y axis (anti-diagonally) [flips over y = -x line]
        let flip_h = flags & Self::FLIPPED_HORIZONTALLY_FLAG == Self::FLIPPED_HORIZONTALLY_FLAG; // Flip tile over y axis
        let flip_v = flags & Self::FLIPPED_VERTICALLY_FLAG == Self::FLIPPED_VERTICALLY_FLAG; // Flip tile over x axis

        if gid == Gid::EMPTY {
            None
        } else {
            let (tileset_location, id) = match for_tileset {
                Some(tileset) => (TilesetLocation::Template(tileset), gid.0 - 1),
                None => {
                    let (tileset_index, tileset) = crate::util::get_tileset_for_gid(tilesets, gid)?;
                    let id = gid.0 - tileset.first_gid.0;
                    (TilesetLocation::Map(tileset_index), id)
                }
            };

            Some(Self {
                tileset_location,
                id,
                flip_h,
                flip_v,
                flip_d,
            })
        }
    }
}

map_wrapper!(
    #[doc = "An instance of a [`Tile`] present in an [`Object`]."]
    ObjectTile => ObjectTileData
);

impl<'map> ObjectTile<'map> {
    /// Get a reference to the object tile's referenced tile, if it exists.
    #[inline]
    pub fn get_tile(&self) -> Option<Tile<'map>> {
        self.get_tileset().get_tile(self.data.id)
    }
    /// Get a reference to the object tile's referenced tileset.
    #[inline]
    pub fn get_tileset(&self) -> &'map Tileset {
        match &self.data.tileset_location {
            // SAFETY: `tileset_index` is guaranteed to be valid
            TilesetLocation::Map(n) => &self.map.tilesets()[*n],
            TilesetLocation::Template(t) => t,
        }
    }
}

/// A structure describing an [`Object`]'s shape.
///
/// Also see the [TMX docs](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-object).
#[derive(Debug, PartialEq, Clone)]
#[allow(missing_docs)]
pub enum ObjectShape {
    Rect {
        width: f32,
        height: f32,
    },
    Ellipse {
        width: f32,
        height: f32,
    },
    Polyline {
        points: Vec<(f32, f32)>,
    },
    Polygon {
        points: Vec<(f32, f32)>,
    },
    Point(f32, f32),
    Text {
        font_family: String,
        pixel_size: usize,
        wrap: bool,
        color: Color,
        bold: bool,
        italic: bool,
        underline: bool,
        strikeout: bool,
        kerning: bool,
        halign: HorizontalAlignment,
        valign: VerticalAlignment,
        /// The actual text content of this object.
        text: String,
        width: f32,
        height: f32,
    },
}

/// The horizontal alignment of an [`ObjectShape::Text`].
#[derive(Debug, PartialEq, Clone, Copy, Default)]
#[allow(missing_docs)]
pub enum HorizontalAlignment {
    #[default]
    Left,
    Center,
    Right,
    Justify,
}

/// The vertical alignment of an [`ObjectShape::Text`].
#[derive(Debug, PartialEq, Clone, Copy, Default)]
#[allow(missing_docs)]
pub enum VerticalAlignment {
    #[default]
    Top,
    Center,
    Bottom,
}

/// Raw data belonging to an object. Used internally and for tile collisions.
///
/// Also see the [TMX docs](https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-object).
#[derive(Debug, PartialEq, Clone)]
pub struct ObjectData {
    id: u32,
    tile: Option<ObjectTileData>,
    /// The name of the object, which is arbitrary and set by the user.
    pub name: String,
    /// The type of the object, which is arbitrary and set by the user.
    pub user_type: String,
    /// The X coordinate of this object in pixels.
    pub x: f32,
    /// The Y coordinate of this object in pixels.
    pub y: f32,
    /// The clockwise rotation of this object around (x,y) in degrees.
    pub rotation: f32,
    /// Whether the object is shown or hidden.
    pub visible: bool,
    /// The object's shape.
    pub shape: ObjectShape,
    /// The object's custom properties as set by the user.
    pub properties: Properties,
}

impl ObjectData {
    /// ID of the object, which is unique per map since Tiled 0.11.
    ///
    /// On older versions this value is defaulted to 0.
    #[inline]
    pub fn id(&self) -> u32 {
        self.id
    }

    /// Returns the data of the tile that this object is referencing, if it exists.
    #[inline]
    pub fn tile_data(&self) -> Option<ObjectTileData> {
        self.tile.clone()
    }
}

impl ObjectData {
    /// If it is known that the object has no 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]>,
        for_tileset: Option<Arc<Tileset>>,
        // Base path is a directory to which all other files are relative to
        base_path: &Path,
        reader: &mut impl ResourceReader,
        cache: &mut impl ResourceCache,
    ) -> Result<ObjectData> {
        let (id, tile, mut n, mut t, c, mut w, mut h, mut v, mut r, template, x, y) = get_attrs!(
            for v in attrs {
                Some("id") => id ?= v.parse(),
                Some("gid") => tile ?= v.parse::<u32>(),
                Some("name") => name ?= v.parse(),
                Some("type") => user_type ?= v.parse(),
                Some("class") => user_class ?= v.parse(),
                Some("width") => width ?= v.parse(),
                Some("height") => height ?= v.parse(),
                Some("visible") => visible ?= v.parse().map(|x:i32| x == 1),
                Some("rotation") => rotation ?= v.parse(),
                Some("template") => template ?= v.parse(),
                Some("x") => x ?= v.parse::<f32>(),
                Some("y") => y ?= v.parse::<f32>(),
            }
            (id, tile, name, user_type, user_class, width, height, visible, rotation, template, x, y)
        );
        let x = x.unwrap_or(0.);
        let y = y.unwrap_or(0.);
        let mut tile = tile.and_then(|bits| {
            ObjectTileData::from_bits(bits, tilesets?, for_tileset.as_ref().cloned())
        });
        // If the template attribute is there, we need to go fetch the template file
        let template = template
            .map(|template_path: String| {
                let template_path = base_path.join(Path::new(&template_path));

                // Check the cache to see if this template exists
                let template = if let Some(templ) = cache.get_template(&template_path) {
                    templ
                } else {
                    let template = Template::parse_template(&template_path, reader, cache)?;
                    // Insert it into the cache
                    cache.insert_template(&template_path, template.clone());
                    template
                };

                // The template sets the default values for the object
                let obj = &template.object;
                v.get_or_insert(obj.visible);
                r.get_or_insert(obj.rotation);
                n.get_or_insert_with(|| obj.name.clone());
                t.get_or_insert_with(|| obj.user_type.clone());
                if let Some(templ_tile) = &obj.tile {
                    tile.get_or_insert_with(|| templ_tile.clone());
                }
                match &obj.shape {
                    ObjectShape::Rect { width, height }
                    | ObjectShape::Ellipse { width, height }
                    | ObjectShape::Text { width, height, .. } => {
                        w.get_or_insert(*width);
                        h.get_or_insert(*height);
                    }
                    _ => {}
                }
                Ok(template)
            })
            .transpose()?;

        let visible = v.unwrap_or(true);
        let width = w.unwrap_or(0f32);
        let height = h.unwrap_or(0f32);
        let rotation = r.unwrap_or(0f32);
        let id = id.unwrap_or(0u32);
        let name = n.unwrap_or_default();
        let user_type: String = t.or(c).unwrap_or_default();
        let mut shape = None;
        let mut properties = HashMap::new();

        parse_tag!(parser, "object", {
            "ellipse" => |_| {
                shape = Some(ObjectShape::Ellipse {
                    width,
                    height,
                });
                Ok(())
            },
            "polyline" => |attrs| {
                shape = Some(ObjectData::new_polyline(attrs)?);
                Ok(())
            },
            "polygon" => |attrs| {
                shape = Some(ObjectData::new_polygon(attrs)?);
                Ok(())
            },
            "point" => |_| {
                shape = Some(ObjectShape::Point(x, y));
                Ok(())
            },
            "text" => |attrs| {
                shape = Some(ObjectData::new_text(attrs, parser, width, height)?);
                Ok(())
            },
            "properties" => |_| {
                properties = parse_properties(parser)?;
                Ok(())
            },
        });

        if let Some(templ) = template {
            shape.get_or_insert_with(|| {
                // Inherit the shape from the template but use the size and
                // position from the object where relevant
                match &templ.object.shape {
                    ObjectShape::Rect { .. } => ObjectShape::Rect { width, height },
                    ObjectShape::Ellipse { .. } => ObjectShape::Ellipse { width, height },
                    ObjectShape::Point(_, _) => ObjectShape::Point(x, y),
                    ObjectShape::Text {
                        font_family,
                        pixel_size,
                        wrap,
                        color,
                        bold,
                        italic,
                        underline,
                        strikeout,
                        kerning,
                        halign,
                        valign,
                        text,
                        width: _,
                        height: _,
                    } => ObjectShape::Text {
                        font_family: font_family.clone(),
                        pixel_size: pixel_size.clone(),
                        wrap: wrap.clone(),
                        color: color.clone(),
                        bold: bold.clone(),
                        italic: italic.clone(),
                        underline: underline.clone(),
                        strikeout: strikeout.clone(),
                        kerning: kerning.clone(),
                        halign: halign.clone(),
                        valign: valign.clone(),
                        text: text.clone(),
                        width,
                        height,
                    },
                    shape => shape.clone(),
                }
            });

            // Possibly copy properties from the template into the object
            // Any that already exist in the object's map don't get copied over
            for (k, v) in &templ.object.properties {
                if !properties.contains_key(k) {
                    properties.insert(k.clone(), v.clone());
                }
            }
        }

        let shape = shape.unwrap_or(ObjectShape::Rect { width, height });

        Ok(ObjectData {
            id,
            tile,
            name,
            user_type,
            x,
            y,
            rotation,
            visible,
            shape,
            properties,
        })
    }
}

impl ObjectData {
    fn new_polyline(attrs: Vec<OwnedAttribute>) -> Result<ObjectShape> {
        let points = get_attrs!(
            for v in attrs {
                "points" => points ?= ObjectData::parse_points(v),
            }
            points
        );
        Ok(ObjectShape::Polyline { points })
    }

    fn new_polygon(attrs: Vec<OwnedAttribute>) -> Result<ObjectShape> {
        let points = get_attrs!(
            for v in attrs {
                "points" => points ?= ObjectData::parse_points(v),
            }
            points
        );
        Ok(ObjectShape::Polygon { points })
    }

    fn new_text(
        attrs: Vec<OwnedAttribute>,
        parser: &mut impl Iterator<Item = XmlEventResult>,
        width: f32,
        height: f32,
    ) -> Result<ObjectShape> {
        let (
            font_family,
            pixel_size,
            wrap,
            color,
            bold,
            italic,
            underline,
            strikeout,
            kerning,
            halign,
            valign,
        ) = get_attrs!(
            for v in attrs {
                Some("fontfamily") => font_family = v,
                Some("pixelsize") => pixel_size ?= v.parse(),
                Some("wrap") => wrap ?= v.parse(),
                Some("color") => color ?= v.parse(),
                Some("bold") => bold ?= v.parse(),
                Some("italic") => italic ?= v.parse(),
                Some("underline") => underline ?= v.parse(),
                Some("strikeout") => strikeout ?= v.parse(),
                Some("kerning") => kerning ?= v.parse::<i32>(),
                Some("halign") => halign = match v.as_str() {
                    "left" => HorizontalAlignment::Left,
                    "center" => HorizontalAlignment::Center,
                    "right" => HorizontalAlignment::Right,
                    "justify" => HorizontalAlignment::Justify,
                    _ => return Err(Error::MalformedAttributes("`halign` property did not contain a valid value of 'left', 'center', 'right' or 'justify'".to_string()))
                },
                Some("valign") => valign = match v.as_str() {
                    "top" => VerticalAlignment::Top,
                    "center" => VerticalAlignment::Center,
                    "bottom" => VerticalAlignment::Bottom,
                    _ => return Err(Error::MalformedAttributes(
                        "`halign` property did not contain a valid value of 'top', 'center' or 'bottom'"
                            .to_string(),
                    )),
                },
            }
            (
                font_family,
                pixel_size,
                wrap,
                color,
                bold,
                italic,
                underline,
                strikeout,
                kerning,
                halign,
                valign,
            )
        );
        let font_family = font_family.unwrap_or_else(|| "sans-serif".to_string());
        let pixel_size = pixel_size.unwrap_or(16);
        let color = color.unwrap_or(Color {
            red: 0,
            green: 0,
            blue: 0,
            alpha: 255,
        });
        let wrap = wrap == Some(1);
        let bold = bold == Some(1);
        let italic = italic == Some(1);
        let underline = underline == Some(1);
        let strikeout = strikeout == Some(1);
        let kerning = kerning.map_or(true, |k| k == 1);
        let halign = halign.unwrap_or_default();
        let valign = valign.unwrap_or_default();
        let contents = match parser.next().map_or_else(
            || {
                Err(Error::PrematureEnd(
                    "XML stream ended when trying to parse text contents".to_owned(),
                ))
            },
            |r| r.map_err(Error::XmlDecodingError),
        )? {
            xml::reader::XmlEvent::Characters(contents) => contents,
            _ => {
                return Err(Error::InvalidObjectData {
                    description: "Text attribute contained anything but characters as content"
                        .into(),
                })
            }
        };

        Ok(ObjectShape::Text {
            font_family,
            pixel_size,
            wrap,
            color,
            bold,
            italic,
            underline,
            strikeout,
            kerning,
            halign,
            valign,
            text: contents,
            width,
            height,
        })
    }

    fn parse_points(s: String) -> Result<Vec<(f32, f32)>> {
        let pairs = s.split(' ');
        pairs
            .map(|point| point.split(','))
            .map(|components| {
                let v: Vec<&str> = components.collect();
                if v.len() != 2 {
                    return Err(Error::MalformedAttributes(
                        "one of a polyline's points does not have an x and y coordinate"
                            .to_string(),
                    ));
                }
                let (x, y) = (v[0].parse().ok(), v[1].parse().ok());
                match (x, y) {
                    (Some(x), Some(y)) => Ok((x, y)),
                    _ => Err(Error::MalformedAttributes(
                        "one of polyline's points does not have i32eger coordinates".to_string(),
                    )),
                }
            })
            .collect()
    }
}

map_wrapper!(
    #[doc = "Wrapper over an [`ObjectData`] that contains both a reference to the data as well as
    to the map it is contained in."]
    Object => ObjectData
);

impl<'map> Object<'map> {
    /// Returns the tile that the object is using as image, if any.
    pub fn get_tile(&self) -> Option<ObjectTile<'map>> {
        self.data
            .tile
            .as_ref()
            .map(|tile| ObjectTile::new(self.map, tile))
    }
}