tex_packer_core/
export.rs1use crate::model::Atlas;
2use serde::Serialize;
3use serde_json::{Value, json};
4
5pub fn to_json_array<K: ToString + Clone + Serialize>(atlas: &Atlas<K>) -> Value {
8 let pages_val = atlas
11 .pages
12 .iter()
13 .map(|p| {
14 let frames_val: Vec<Value> = p
15 .frames
16 .iter()
17 .map(|fr| {
18 let frame = json!({"x": fr.frame.x, "y": fr.frame.y, "w": fr.frame.w, "h": fr.frame.h});
19 let sprite_source_size = json!({"x": fr.source.x, "y": fr.source.y, "w": fr.source.w, "h": fr.source.h});
20 let source_size = json!({"w": fr.source_size.0, "h": fr.source_size.1});
21 let pivot = json!({"x": 0.5, "y": 0.5});
22 json!({
23 "key": fr.key.to_string(),
24 "frame": frame,
25 "rotated": fr.rotated,
26 "trimmed": fr.trimmed,
27 "spriteSourceSize": sprite_source_size,
28 "sourceSize": source_size,
29 "pivot": pivot
30 })
31 })
32 .collect();
33 json!({
34 "id": p.id,
35 "width": p.width,
36 "height": p.height,
37 "frames": frames_val,
38 })
39 })
40 .collect::<Vec<_>>();
41 json!({"pages": pages_val, "meta": &atlas.meta})
42}
43
44pub fn to_json_hash<K: ToString + Clone>(atlas: &Atlas<K>) -> Value {
48 let mut frames = serde_json::Map::new();
50 for page in &atlas.pages {
51 for fr in &page.frames {
52 let key = fr.key.to_string();
53 let frame = json!({"x": fr.frame.x, "y": fr.frame.y, "w": fr.frame.w, "h": fr.frame.h});
54 let sprite_source_size =
55 json!({"x": fr.source.x, "y": fr.source.y, "w": fr.source.w, "h": fr.source.h});
56 let source_size = json!({"w": fr.source_size.0, "h": fr.source_size.1});
57 let pivot = json!({"x": 0.5, "y": 0.5});
58 frames.insert(
59 key,
60 json!({
61 "frame": frame,
62 "rotated": fr.rotated,
63 "trimmed": fr.trimmed,
64 "spriteSourceSize": sprite_source_size,
65 "sourceSize": source_size,
66 "pivot": pivot,
67 "page": page.id,
68 "pageSize": {"w": page.width, "h": page.height},
69 }),
70 );
71 }
72 }
73 json!({ "frames": frames, "meta": &atlas.meta })
74}