16
16
//! This has been tested to work with aseprite 1.1.6 and 1.2.25; other
17
17
//! versions have not been tested.
18
18
19
+ use std:: ops:: { Deref , DerefMut } ;
20
+
19
21
use serde:: { Deserialize , Serialize } ;
20
22
23
+ /// 2D Rectangle with a position and a size.
21
24
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone , Copy ) ]
22
25
pub struct Rect {
26
+ /// X coordinate.
23
27
pub x : u32 ,
28
+ /// Y coordinate.
24
29
pub y : u32 ,
30
+ /// Width.
25
31
pub w : u32 ,
32
+ /// Height.
26
33
pub h : u32 ,
27
34
}
28
35
36
+ /// 2D point is space.
29
37
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone , Copy ) ]
30
38
pub struct Point {
39
+ /// X coordinate.
31
40
pub x : u32 ,
41
+ /// Y coordinate.
32
42
pub y : u32 ,
33
43
}
34
44
45
+ /// 2D size.
35
46
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone , Copy ) ]
36
47
pub struct Dimensions {
48
+ /// Width.
37
49
pub w : u32 ,
50
+ /// Height.
38
51
pub h : u32 ,
39
52
}
40
53
54
+ /// RGBA color.
41
55
#[ derive( PartialEq , Eq , Clone , Copy ) ]
42
56
pub struct Color {
57
+ /// Red component.
43
58
pub r : u8 ,
59
+ /// Green component.
44
60
pub g : u8 ,
61
+ /// Blue component.
45
62
pub b : u8 ,
63
+ /// Alpha component.
46
64
pub a : u8 ,
47
65
}
48
66
@@ -53,15 +71,15 @@ impl std::fmt::Debug for Color {
53
71
}
54
72
}
55
73
56
- impl serde :: Serialize for Color {
74
+ impl Serialize for Color {
57
75
fn serialize < S : serde:: Serializer > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error > {
58
76
format ! ( "{:?}" , self ) . serialize ( serializer)
59
77
}
60
78
}
61
79
62
- impl < ' de > serde :: Deserialize < ' de > for Color {
80
+ impl < ' de > Deserialize < ' de > for Color {
63
81
fn deserialize < D : serde:: Deserializer < ' de > > ( deserializer : D ) -> Result < Self , D :: Error > {
64
- let s: String = serde :: Deserialize :: deserialize ( deserializer) ?;
82
+ let s: String = Deserialize :: deserialize ( deserializer) ?;
65
83
if !s. starts_with ( '#' ) {
66
84
Err ( serde:: de:: Error :: custom ( "color doesn't start with '#'" ) )
67
85
} else if !s. len ( ) == 7 {
@@ -80,35 +98,51 @@ impl<'de> serde::Deserialize<'de> for Color {
80
98
}
81
99
}
82
100
101
+ /// A single frame as part of an animation.
102
+ ///
103
+ /// Contains timing and position information.
83
104
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
84
105
pub struct Frame {
106
+ /// Local path on disk.
85
107
pub filename : String ,
108
+ /// Data for this frame.
86
109
#[ serde( flatten) ]
87
110
pub data : FrameData ,
88
111
}
89
112
90
- impl std :: ops :: Deref for Frame {
113
+ impl Deref for Frame {
91
114
type Target = FrameData ;
92
115
fn deref ( & self ) -> & Self :: Target {
93
116
& self . data
94
117
}
95
118
}
96
119
97
- impl std :: ops :: DerefMut for Frame {
120
+ impl DerefMut for Frame {
98
121
fn deref_mut ( & mut self ) -> & mut Self :: Target {
99
122
& mut self . data
100
123
}
101
124
}
102
125
126
+ /// Data for a single frame as part of an animation.
103
127
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
104
128
#[ serde( rename_all = "camelCase" ) ]
105
129
#[ non_exhaustive]
106
130
pub struct FrameData {
131
+ /// Rectangle on the sprite sheet for this sprite including padding.
132
+ ///
133
+ /// If `trimmed` is true and coordinates are zero this is exactly the same as `sprite_source_size`.
107
134
pub frame : Rect ,
135
+ /// Whether the sprite is rotated.
108
136
pub rotated : bool ,
137
+ /// Whether the sprite is trimmed, meaning whether empty pixels from the edge have been removed.
109
138
pub trimmed : bool ,
139
+ /// Trimmed rectangle on the sprite sheet for this sprite.
140
+ ///
141
+ /// If `trimmed` is true and coordinates are zero this is exactly the same as `frame`.
110
142
pub sprite_source_size : Rect ,
143
+ /// Actual frame size.
111
144
pub source_size : Dimensions ,
145
+ /// Frame duration in milliseconds.
112
146
pub duration : u32 ,
113
147
}
114
148
@@ -149,28 +183,40 @@ fn deserialize_frames<'de, D: serde::Deserializer<'de>>(de: D) -> Result<Vec<Fra
149
183
de. deserialize_any ( FramesVisitor )
150
184
}
151
185
186
+ /// Frame animation direction.
152
187
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone , Copy ) ]
153
188
#[ serde( rename_all = "camelCase" ) ]
154
189
#[ non_exhaustive]
155
190
pub enum Direction {
191
+ /// Normal animation direction.
156
192
Forward ,
193
+ /// Reversed animation direction.
157
194
Reverse ,
195
+ /// Animation is played forward and then backward and so on.
158
196
Pingpong ,
159
197
}
160
198
199
+ /// Tagged frame group.
200
+ ///
201
+ /// This is a way to define a single animation within the sprite sheet.
161
202
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
162
203
#[ serde( rename_all = "camelCase" ) ]
163
204
#[ non_exhaustive]
164
205
pub struct Frametag {
206
+ /// Tag name.
165
207
pub name : String ,
208
+ /// Frame start number.
166
209
pub from : u32 ,
210
+ /// Frame end number.
167
211
pub to : u32 ,
212
+ /// Animation direction.
168
213
pub direction : Direction ,
169
214
}
170
215
171
216
// These are listed at:
172
217
// https://github.com/aseprite/aseprite/blob/51b038ac024dd99902ab5b0c0d61524c48856b93/src/doc/blend_mode.cpp#L18-L37
173
218
219
+ /// Layer blend modes.
174
220
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone , Copy ) ]
175
221
#[ serde( rename_all = "snake_case" ) ]
176
222
#[ non_exhaustive]
@@ -229,50 +275,85 @@ pub struct Layer {
229
275
pub data : Option < String > ,
230
276
}
231
277
278
+ /// Slice within the sprite.
232
279
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
233
280
#[ serde( rename_all = "camelCase" ) ]
234
281
#[ non_exhaustive]
235
282
pub struct Slice {
283
+ /// Slice name.
236
284
pub name : String ,
285
+ /// Color with which the slice frame is displayed in Aseprite.
237
286
pub color : Color ,
238
- pub data : Option < String > ,
287
+ /// List of slice keys.
239
288
pub keys : Vec < SliceKey > ,
289
+ /// Custom data.
290
+ pub data : Option < String > ,
240
291
}
241
292
293
+ /// Define the slice rectangle in a specific frame.
242
294
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
243
295
#[ serde( rename_all = "camelCase" ) ]
244
296
#[ non_exhaustive]
245
297
pub struct SliceKey {
298
+ /// Frame number.
246
299
pub frame : u32 ,
300
+ /// Outer slice bounds within the frame.
247
301
pub bounds : Rect ,
302
+ /// Pivot point within the slice.
248
303
pub pivot : Option < Point > ,
304
+ /// Center area of a nine-patch slice.
249
305
pub center : Option < Rect > ,
250
306
}
251
307
308
+ /// Sprite sheet metadata.
252
309
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
253
310
#[ serde( rename_all = "camelCase" ) ]
254
311
#[ non_exhaustive]
255
312
pub struct Metadata {
313
+ /// Which application produced the sprite sheet.
256
314
pub app : String ,
315
+ /// Version of the application used to produce the sprite sheet.
257
316
pub version : String ,
317
+ /// Pixel format of the image file.
258
318
pub format : String ,
319
+ /// Pixel dimensions of the image file.
259
320
pub size : Dimensions ,
260
- pub scale : String , // Surely this should be a number?
321
+ /// Scale of the image.
322
+ ///
323
+ /// Usually this is `1`.
324
+ ///
325
+ /// Unclear why this is a string, most likely to be compatible with the application the JSON is formatted after.
326
+ pub scale : String ,
327
+ /// Relative path to the image file.
328
+ pub image : Option < String > ,
329
+ /// List of tags.
330
+ ///
331
+ /// Only present when "Meta: Tags" is enabled when exporting in Aseprite.
261
332
#[ serde( default ) ]
262
333
pub frame_tags : Vec < Frametag > ,
334
+ /// List of layers.
335
+ ///
336
+ /// Only present when "Meta: Layers" is enabled when exporting in Aseprite.
263
337
#[ serde( default ) ]
264
338
pub layers : Vec < Layer > ,
265
- pub image : Option < String > ,
339
+ /// List of slices.
340
+ ///
341
+ /// Only present when "Meta: Slices" is enabled when exporting in Aseprite.
266
342
#[ serde( default ) ]
267
343
pub slices : Vec < Slice > ,
268
344
}
269
345
346
+ /// Aseprite sprite sheet.
347
+ ///
348
+ /// Root type in an Aseprite JSON file.
270
349
#[ derive( Serialize , Deserialize , Debug , PartialEq , Eq , Clone ) ]
271
350
#[ serde( rename_all = "camelCase" ) ]
272
351
#[ non_exhaustive]
273
352
pub struct SpritesheetData {
353
+ /// List with sprite frame definitions.
274
354
#[ serde( deserialize_with = "deserialize_frames" ) ]
275
355
pub frames : Vec < Frame > ,
356
+ /// Meta data.
276
357
pub meta : Metadata ,
277
358
}
278
359
0 commit comments