tiled_json_rs/
layer.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use crate::{
6    object::Object,
7    parsers::{parse_color, parse_data, parse_path, parse_property},
8    Color, TiledValue,
9};
10use serde::Deserialize;
11use std::collections::HashMap;
12use std::path::PathBuf;
13
14#[derive(Deserialize, Debug, PartialEq, Clone)]
15pub struct TileLayer {
16    /// Array of chunks (optional, generally infinite maps)
17    pub chunks: Option<Vec<Chunk>>,
18    /// Data consists of the global ID's of tiles making up this
19    /// layer of the map
20    #[serde(deserialize_with = "parse_data", default)]
21    pub data: Vec<u32>,
22    /// Row count. Same as map height for fixed-size maps.
23    pub height: u32,
24    /// Column count. Same as map width for fixed-size maps.
25    pub width: u32,
26}
27
28#[derive(Deserialize, Debug, PartialEq, Clone)]
29pub struct ObjectGroup {
30    /// `TopDown` (default) or `Index`
31    #[serde(rename(deserialize = "draworder"), default)]
32    pub draw_order: DrawOrder,
33    /// The array of `Object` in this layer
34    #[serde(default)]
35    pub objects: Vec<Object>,
36}
37
38/// Contains a file path to an image plus a mix colour
39#[derive(Deserialize, Debug, PartialEq, Clone)]
40pub struct ImageLayer {
41    /// Image used by this layer
42    #[serde(deserialize_with = "parse_path", default)]
43    pub image: PathBuf,
44    #[serde(
45        rename(deserialize = "transparentcolor"),
46        deserialize_with = "parse_color",
47        default
48    )]
49    /// Defaults to 0,0,0,0 (rgba)
50    pub transparent_color: Color,
51}
52
53/// Used to group layers if required
54#[derive(Deserialize, Debug, PartialEq, Clone)]
55pub struct Group {
56    pub layers: Vec<Layer>,
57}
58
59/// Contains the data for this variant of layer
60///
61/// # Example
62///
63/// ``` no-run
64/// match &layer_type {
65///     LayerType::TileLayer(tiles) => {
66///         //do_something(tiles);
67///     }
68///     LayerType::Group { layers } => {
69///         &mut render_layers(layers);
70///     }
71///     LayerType::ImageLayer(image) => {
72///         //do_something_else(image);
73///     }
74///     LayerType::ObjectGroup(objects) => {
75///         //and_another_thing(objects);
76///     }
77/// }
78/// ```
79#[derive(Deserialize, Debug, PartialEq, Clone)]
80#[serde(rename_all(deserialize = "lowercase"), tag = "type")]
81pub enum LayerType {
82    TileLayer(TileLayer),
83    ObjectGroup(ObjectGroup),
84    ImageLayer(ImageLayer),
85    Group { layers: Vec<Layer> },
86}
87
88/// A map can contain any number of layers.
89///
90/// Layers have sub-types such as (enum) `LayerType::TileLayer(TileLayer)`
91/// which contains the data for that sub-type.
92#[derive(Deserialize, Debug, PartialEq, Clone)]
93pub struct Layer {
94    pub name: String,
95    /// Horizontal layer offset in pixels (default: 0)
96    #[serde(rename(deserialize = "offsetx"), default)]
97    pub offset_x: f32,
98    /// Vertical layer offset in pixels (default: 0)
99    #[serde(rename(deserialize = "offsety"), default)]
100    pub offset_y: f32,
101    /// Value between 0 and 1
102    pub opacity: f32,
103    #[serde(deserialize_with = "parse_property", default)]
104    pub properties: HashMap<String, TiledValue>,
105    #[serde(flatten)]
106    /// The `LayerType` object also contains the data relating to the type
107    pub layer_type: LayerType,
108    /// Horizontal layer offset in tiles. Always 0.
109    pub x: i32,
110    /// Vertical layer offset in tiles. Always 0.
111    pub y: i32,
112}
113
114/// Chunks are used to store the tile layer data for infinite maps
115#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
116pub struct Chunk {
117    /// Array of unsigned int (GIDs) or base64-encoded data
118    #[serde(deserialize_with = "parse_data", default)]
119    pub data: Vec<u32>,
120    pub height: u32,
121    pub width: u32,
122    pub x: i32,
123    pub y: i32,
124}
125
126/// Can be `TopDown` (default) or `Index`. Applies to `ObjectGroup` only.
127#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
128#[serde(rename_all(deserialize = "lowercase"))]
129pub enum DrawOrder {
130    TopDown,
131    Index,
132}
133
134impl Default for DrawOrder {
135    fn default() -> Self {
136        DrawOrder::TopDown
137    }
138}