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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// TODO: handle separated tilesets import

//! A handy crate for parsing the Tiled JSON data in to a useable structure.
//!
//! Notes:
//! - GID starts at 0 for None
//! - Local Id starts at 0 for TileSet

use serde::Deserialize;

mod layer;
mod map;
mod object;
mod tile_set;
mod utils;
mod wangs;

pub use layer::*;
pub use map::*;
pub use object::*;
pub use tile_set::*;
pub use utils::*;
pub use wangs::*;

mod parsers;

use parsers::parse_color;

/// A `TiledValue` is similar to JSON values. It contains the basic
/// types that Tiled uses. This is generally used in the properties
/// of layers, tiles, and objects.
#[derive(Deserialize, Debug, PartialEq, Clone)]
#[serde(rename_all(deserialize = "lowercase"), tag = "type", content = "value")]
pub enum TiledValue {
    Bool(bool),
    Float(f32),
    Int(u32),
    #[serde(deserialize_with = "parse_color")]
    Color(Color),
    String(String),
    File(String),
}

/// A simple representation of a 2d Vector to pass coords
#[derive(Deserialize, Debug, PartialEq, Clone)]
pub struct Vec2<T> {
    pub x: T,
    pub y: T,
}

#[derive(Debug, Default, PartialEq, Clone)]
pub struct Color(pub u32, pub u32, pub u32, pub u32);

impl Color {
    /// Red
    pub fn r(&self) -> u32 {
        self.0
    }

    /// Green
    pub fn g(&self) -> u32 {
        self.1
    }

    /// Blue
    pub fn b(&self) -> u32 {
        self.2
    }

    /// Alpha
    pub fn a(&self) -> u32 {
        self.3
    }
}

/// Used to provide the location and dimensions of the required
/// tile on the tiles tileset image
#[derive(Debug, Default, PartialEq, Clone)]
pub struct TileRect {
    pub x:      i32,
    pub y:      i32,
    pub width:  u32,
    pub height: u32,
}