tiled_json_rs/
map.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
5//! The final parsed structure is in some ways not suitable
6//! for use so this crate is designed to produce a structure
7//! to be *consumed* by your own implementations.
8//!
9//! There are many parts of the Tiled JSON that are parsed,
10//! and which may be of no use to you. Where possible, there
11//! are helper functions to transform some types of data in
12//! to more usable data, eg: HashMaps, if desired. In all
13//! other cases non-copy types are passed by value.
14
15use serde::Deserialize;
16use std::collections::HashMap;
17
18use crate::{
19    layer::Layer,
20    parsers::{parse_color, parse_property},
21    tile_set::TileSet,
22    Color, TiledValue,
23};
24
25/// The base structure which contains all data - as in the root of a tree
26#[derive(Deserialize, Debug, PartialEq, Clone)]
27pub struct Map {
28    #[serde(
29        rename(deserialize = "backgroundcolor"),
30        deserialize_with = "parse_color",
31        default
32    )]
33    /// The background colour is translated from the hex representation
34    pub background_color: Color,
35    /// Length of the side of a hex tile in pixels
36    #[serde(rename(deserialize = "hexsidelength"))]
37    pub hex_side_length: Option<u32>,
38    /// Whether the map has infinite dimensions
39    #[serde(default)]
40    pub infinite: bool,
41    /// Layers are generally stored *in order*. So that the first layer in
42    /// the array will be drawn first and so on. Nested layers follow the
43    /// same principle.
44    pub layers: Vec<Layer>,
45    /// `Orthogonal`, `Isometric`, `Staggered` or `Hexagonal`
46    pub orientation: Orientation,
47    /// Rendering direction (orthogonal maps only)
48    #[serde(rename(deserialize = "renderorder"))]
49    pub render_order: Option<RenderOrder>,
50    /// `X` or `Y` (staggered / hexagonal maps only)
51    #[serde(rename(deserialize = "staggeraxis"))]
52    pub stagger_axis: Option<StaggerAxis>,
53    /// `Odd` or `Even` (staggered / hexagonal maps only)
54    #[serde(rename(deserialize = "staggerindex"))]
55    pub stagger_index: Option<StaggerIndex>,
56    /// Height in pixels for tiles in this map
57    #[serde(rename(deserialize = "tileheight"))]
58    pub tile_height: u32,
59    /// Width in pixels for tiles in this map
60    #[serde(rename(deserialize = "tilewidth"))]
61    pub tile_width: u32,
62    /// The number of tile rows for the map
63    pub height: u32,
64    /// The number of tile columns for the map
65    pub width: u32,
66    #[serde(rename(deserialize = "tilesets"))]
67    pub tile_sets: Vec<TileSet>,
68    #[serde(deserialize_with = "parse_property", default)]
69    pub properties: HashMap<String, TiledValue>,
70}
71
72/// Rendering direction. Applies only to orthogonal maps
73#[derive(Deserialize, Debug, PartialEq, Clone)]
74pub enum RenderOrder {
75    #[serde(rename(deserialize = "right-down"))]
76    RightDown,
77    #[serde(rename(deserialize = "right-up"))]
78    RightUp,
79    #[serde(rename(deserialize = "left-down"))]
80    LeftDown,
81    #[serde(rename(deserialize = "left-up"))]
82    LeftUp,
83}
84
85/// Applies only to staggered or hexagonal maps
86#[derive(Deserialize, Debug, PartialEq, Clone)]
87#[serde(rename_all(deserialize = "lowercase"))]
88pub enum StaggerAxis {
89    X,
90    Y,
91}
92
93/// Applies only to staggered or hexagonal maps
94#[derive(Deserialize, Debug, PartialEq, Clone)]
95#[serde(rename_all(deserialize = "lowercase"))]
96pub enum StaggerIndex {
97    Odd,
98    Even,
99}
100
101/// The orientation of the `Map`
102#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
103#[serde(rename_all(deserialize = "lowercase"))]
104pub enum Orientation {
105    Orthogonal,
106    Isometric,
107    Staggered,
108    Hexagonal,
109}