tiled_json_rs/lib.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//! A handy crate for parsing the Tiled JSON data in to a usable structure.
6//!
7//! The crate includes a few small helper functions on `Map`, `TileSet`, and
8//! `TileLayer`. These functions are for common tasks such as generating a
9//! cloumn/row location (tiles are stored in a 1D array), a located box on an
10//! image for helping with tile-to-tilesheet image picking, and loading files
11//! (or strings).
12//!
13//! # Examples
14//!
15//! ```
16//! # use std::path::PathBuf;
17//! use tiled_json_rs as tiled;
18//! let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json"))
19//! .expect("Failed to load map");
20//! ```
21//!
22//! ```
23//! # use std::path::PathBuf;
24//! # use tiled_json_rs as tiled;
25//! # let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json")).unwrap();
26//! for tileset in &map.tile_sets {
27//! let name = tileset.name.clone();
28//! let mut path = PathBuf::from("assets");
29//! path.push(tileset.image.clone());
30//! // Do stuff
31//! }
32//! ```
33//!
34//! ```
35//! # use std::path::PathBuf;
36//! # use tiled_json_rs as tiled;
37//! # let map = tiled::Map::load_from_file(&PathBuf::from("tests/data/csv.json")).unwrap();
38//! use tiled::Layer;
39//!
40//! fn render_layers(layers: &Vec<Layer>) {
41//! for layer in layers {
42//! match &layer.layer_type {
43//! tiled::LayerType::TileLayer(tiles) => {
44//! //do_something(tiles);
45//! }
46//! tiled::LayerType::Group { layers } => {
47//! &mut render_layers(layers);
48//! }
49//! tiled::LayerType::ImageLayer(image) => {
50//! //do_something_else(image);
51//! }
52//! tiled::LayerType::ObjectGroup(objects) => {
53//! //and_another_thing(objects);
54//! }
55//! }
56//! }
57//! }
58//!
59//! render_layers(&map.layers);
60//! ```
61//!
62//! ## Info
63//!
64//! Tiled can export maps as JSON files. To do so, simply select “File > Export As”
65//! and select the JSON file type. You can export json from the command line with
66//! the `--export-map` option.
67//!
68//! Notes:
69//! - GID for tiles starts at 1 with 0 reserved for *empty* tile
70//! - Local Id starts at 0 for `TileSet`, and only applies to `TileSet`
71//! - Doc comments are only provided where clarification may be useful. In general things
72//! should be named well enough that intention is self-describing.
73//!
74
75use serde::Deserialize;
76
77mod layer;
78mod map;
79mod object;
80mod tile_set;
81mod utils;
82mod wangs;
83
84pub use layer::*;
85pub use map::*;
86pub use object::*;
87pub use tile_set::*;
88pub use utils::*;
89pub use wangs::*;
90
91mod parsers;
92
93use parsers::parse_color;
94
95/// A `TiledValue` is similar to JSON values.
96///
97/// It contains the basic types that Tiled uses.
98/// This is generally used in the properties of layers, tiles, and objects.
99#[derive(Deserialize, Debug, PartialEq, Clone)]
100#[serde(rename_all(deserialize = "lowercase"), tag = "type", content = "value")]
101pub enum TiledValue {
102 Bool(bool),
103 Float(f32),
104 Int(u32),
105 #[serde(deserialize_with = "parse_color")]
106 Color(Color),
107 String(String),
108 File(String),
109}
110
111/// A simple representation of a 2d Vector to pass coords around
112#[derive(Deserialize, Debug, PartialEq, Clone)]
113pub struct Vec2<T> {
114 pub x: T,
115 pub y: T,
116}
117
118/// An RGBA representation of colours
119///
120/// Order of colours in the tuple follow the Red-Green-Blue-Alpha pattern
121#[derive(Debug, Default, PartialEq, Clone)]
122pub struct Color(pub u32, pub u32, pub u32, pub u32);
123
124impl Color {
125 /// Red
126 pub fn r(&self) -> u32 {
127 self.0
128 }
129
130 /// Green
131 pub fn g(&self) -> u32 {
132 self.1
133 }
134
135 /// Blue
136 pub fn b(&self) -> u32 {
137 self.2
138 }
139
140 /// Alpha
141 pub fn a(&self) -> u32 {
142 self.3
143 }
144}
145
146/// Used to provide the location and dimensions of the required
147/// tile on the tiles tileset image.
148///
149/// Functionally similar to SDL2 Rect.
150#[derive(Debug, Default, PartialEq, Clone)]
151pub struct TileRect {
152 pub x: i32,
153 pub y: i32,
154 pub width: u32,
155 pub height: u32,
156}