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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::ffi::{CStr, CString};
use std::{path::Path, ptr::null_mut};

use crate::c::{
    spAtlasFilter, spAtlasFormat, spAtlasRegion, spAtlasWrap, spAtlas_createFromFile,
    spTextureRegion,
};
use crate::c_interface::{CTmpRef, NewFromPtr, SyncPtr};
use crate::texture_region::TextureRegion;
use crate::{
    c::{c_int, spAtlas, spAtlasPage, spAtlas_create, spAtlas_dispose},
    error::SpineError,
};

use atlas::*;

#[cfg(feature = "mint")]
use mint::Vector2;

/// An atlas loaded from Spine's `.atlas` file format.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#Atlas)
#[derive(Debug)]
pub struct Atlas {
    c_atlas: SyncPtr<spAtlas>,
    owns_memory: bool,
}

impl NewFromPtr<spAtlas> for Atlas {
    unsafe fn new_from_ptr(c_atlas: *mut spAtlas) -> Atlas {
        Atlas {
            c_atlas: SyncPtr(c_atlas),
            owns_memory: false,
        }
    }
}

impl Atlas {
    /// Create an Atlas from an in-memory vector.
    ///
    /// ```
    /// use rusty_spine::Atlas;
    /// fn load_atlas() -> Atlas {
    ///     let atlas_file = std::fs::read("skeleton.atlas").unwrap();
    ///     Atlas::new(&atlas_file, "").unwrap()
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns the [`SpineError::NulError`] if `dir` or `data` contain an internal 0 byte. Returns
    /// [`SpineError::PathNotUtf8`] if the specified `dir` is not utf-8. This function does not
    /// error if the atlas file is invalid or malformed. The file is parsed line-by-line and invalid
    /// lines are simply ignored.
    pub fn new<P: AsRef<Path>>(data: &[u8], dir: P) -> Result<Atlas, SpineError> {
        let c_data = CString::new(data)?;
        let Some(dir_path) = dir.as_ref().to_str() else {
            return Err(SpineError::PathNotUtf8);
        };
        let c_dir = CString::new(dir_path)?;
        let c_atlas = unsafe {
            spAtlas_create(
                c_data.as_ptr(),
                data.len() as c_int,
                c_dir.as_ptr(),
                null_mut(),
            )
        };
        Ok(Self {
            c_atlas: SyncPtr(c_atlas),
            owns_memory: true,
        })
    }

    /// Create an Atlas from a file.
    /// ```
    /// use rusty_spine::Atlas;
    /// fn load_atlas() -> Result<Atlas, rusty_spine::SpineError>{
    ///     Ok(Atlas::new_from_file("skeleton.json")?)
    /// }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`SpineError::FailedToReadFile`] if the file could not be read, returns
    /// [`SpineError::NulError`] if `path` contains an internal 0 byte or if the loaded atlas
    /// contains a 0 byte. Returns  [`SpineError::PathNotUtf8`] if the specified `path` is not
    /// utf-8.
    pub fn new_from_file<P: AsRef<Path>>(path: P) -> Result<Atlas, SpineError> {
        let Some(path_str) = path.as_ref().to_str() else {
            return Err(SpineError::PathNotUtf8);
        };
        let c_path = CString::new(path_str)?;
        let c_atlas = unsafe { spAtlas_createFromFile(c_path.as_ptr(), null_mut()) };
        if !c_atlas.is_null() {
            Ok(Self {
                c_atlas: SyncPtr(c_atlas),
                owns_memory: true,
            })
        } else {
            Err(SpineError::FailedToReadFile {
                file: path_str.to_owned(),
            })
        }
    }

    /// Iterator over the [`AtlasPage`] list in this atlas.
    #[must_use]
    pub fn pages(&self) -> AtlasPageIterator {
        AtlasPageIterator {
            _atlas: self,
            page: unsafe { self.c_ptr_mut().pages },
        }
    }

    /// Find an [`AtlasPage`] in this atlas by name.
    #[must_use]
    pub fn find_page(&self, name: &str) -> Option<CTmpRef<Self, AtlasPage>> {
        self.pages().find(|page| page.name() == name)
    }

    /// Iterator over the [`AtlasRegion`] list in this atlas, across all pages.
    #[must_use]
    pub fn regions(&self) -> AtlasRegionIterator {
        AtlasRegionIterator {
            _atlas: self,
            region: unsafe { self.c_ptr_mut().regions },
        }
    }

    /// Find an [`AtlasRegion`] in this atlas by name, across all pages.
    #[must_use]
    pub fn find_region(&self, name: &str) -> Option<CTmpRef<Self, AtlasRegion>> {
        self.regions().find(|region| region.name() == name)
    }

    c_accessor_renderer_object!();
    c_ptr!(c_atlas, spAtlas);
}

impl Drop for Atlas {
    fn drop(&mut self) {
        if self.owns_memory {
            unsafe {
                spAtlas_dispose(self.c_atlas.0);
            }
        }
    }
}

pub mod atlas {
    //! Types related to atlases.
    //!
    //! To load an atlas file, see [`Atlas`].

    use crate::c_interface::from_c_str;

    use super::*;

    /// Settings for an atlas backing texture contained in [`Atlas`].
    ///
    /// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#AtlasPage)
    #[derive(Debug)]
    pub struct AtlasPage {
        c_atlas_page: SyncPtr<spAtlasPage>,
    }

    impl NewFromPtr<spAtlasPage> for AtlasPage {
        unsafe fn new_from_ptr(c_atlas_page: *mut spAtlasPage) -> Self {
            Self {
                c_atlas_page: SyncPtr(c_atlas_page),
            }
        }
    }

    impl AtlasPage {
        c_accessor_tmp_ptr!(
            /// The [`Atlas`] this page belongs to.
            atlas,
            atlas,
            Atlas,
            spAtlas
        );
        c_accessor_string!(
            /// The name of the image file for the texture.
            name,
            name
        );
        c_accessor_enum!(
            /// The memory format to use for the texture.
            format,
            format,
            AtlasFormat
        );
        c_accessor_enum!(
            /// The texture's magnification filter.
            mag_filter,
            magFilter,
            AtlasFilter
        );
        c_accessor_enum!(
            /// The texture's minification filter.
            min_filter,
            minFilter,
            AtlasFilter
        );
        c_accessor_enum!(
            /// The X axis texture wrap setting.
            u_wrap,
            uWrap,
            AtlasWrap
        );
        c_accessor_enum!(
            /// The Y axis texture wrap setting.
            v_wrap,
            vWrap,
            AtlasWrap
        );
        c_accessor!(
            /// The width in pixels of the image file.
            width,
            width,
            i32
        );
        c_accessor!(
            /// The height in pixels of the image file.
            height,
            height,
            i32
        );
        c_accessor_bool!(
            /// The premultiplied alpha setting.
            pma,
            pma
        );
        c_accessor_renderer_object!();
        c_ptr!(c_atlas_page, spAtlasPage);
    }

    /// Functions available if using the `mint` feature.
    #[cfg(feature = "mint")]
    impl AtlasPage {
        /// The width and height in pixels of the image file.
        #[must_use]
        pub fn size(&self) -> Vector2<i32> {
            Vector2 {
                x: self.width(),
                y: self.height(),
            }
        }
    }

    /// An iterator over each [`AtlasPage`] in an [`Atlas`].
    pub struct AtlasPageIterator<'a> {
        pub(crate) _atlas: &'a Atlas,
        pub(crate) page: *mut spAtlasPage,
    }

    impl<'a> Iterator for AtlasPageIterator<'a> {
        type Item = CTmpRef<'a, Atlas, AtlasPage>;

        fn next(&mut self) -> Option<Self::Item> {
            if !self.page.is_null() {
                let page = unsafe { AtlasPage::new_from_ptr(self.page) };
                self.page = unsafe { (*self.page).next };
                Some(CTmpRef::new(self._atlas, page))
            } else {
                None
            }
        }
    }

    /// The texture format setting to use for an [`AtlasPage`].
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum AtlasFormat {
        UnknownFormat = 0,
        Alpha = 1,
        Intensity = 2,
        LuminanceAlpha = 3,
        RGB565 = 4,
        RGBA4444 = 5,
        RGB888 = 6,
        RGBA8888 = 7,
    }

    impl From<spAtlasFormat> for AtlasFormat {
        fn from(format: spAtlasFormat) -> Self {
            match format {
                1 => Self::Alpha,
                2 => Self::Intensity,
                3 => Self::LuminanceAlpha,
                4 => Self::RGB565,
                5 => Self::RGBA4444,
                6 => Self::RGB888,
                7 => Self::RGBA8888,
                _ => Self::UnknownFormat,
            }
        }
    }

    /// The texture filter setting to use for an [`AtlasPage`].
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum AtlasFilter {
        UnknownFilter = 0,
        Nearest = 1,
        Linear = 2,
        Mipmap = 3,
        MipmapNearestNearest = 4,
        MipmapLinearNearest = 5,
        MipmapNearestLinear = 6,
        MipmapLinearLinear = 7,
    }

    impl From<spAtlasFilter> for AtlasFilter {
        fn from(filter: spAtlasFilter) -> Self {
            match filter {
                1 => Self::Nearest,
                2 => Self::Linear,
                3 => Self::Mipmap,
                4 => Self::MipmapNearestNearest,
                5 => Self::MipmapLinearNearest,
                6 => Self::MipmapNearestLinear,
                7 => Self::MipmapLinearLinear,
                _ => Self::UnknownFilter,
            }
        }
    }

    /// The texture wrapping setting to use for an [`AtlasPage`].
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum AtlasWrap {
        MirroredRepeat = 0,
        ClampToEdge = 1,
        Repeat = 2,
        Unknown = 99,
    }

    impl From<spAtlasWrap> for AtlasWrap {
        fn from(wrap: spAtlasWrap) -> Self {
            match wrap {
                0 => Self::MirroredRepeat,
                1 => Self::ClampToEdge,
                2 => Self::Repeat,
                _ => Self::Unknown,
            }
        }
    }

    /// A texture region on an [`AtlasPage`].
    ///
    /// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#AtlasRegion)
    #[derive(Debug)]
    pub struct AtlasRegion {
        c_atlas_region: SyncPtr<spAtlasRegion>,
    }

    impl NewFromPtr<spAtlasRegion> for AtlasRegion {
        unsafe fn new_from_ptr(c_atlas_region: *mut spAtlasRegion) -> Self {
            Self {
                c_atlas_region: SyncPtr(c_atlas_region),
            }
        }
    }

    impl AtlasRegion {
        c_accessor_super!(
            texture_region,
            texture_region_mut,
            TextureRegion,
            spTextureRegion
        );
        c_accessor_string!(name, name);
        c_accessor!(x, x, i32);
        c_accessor!(y, y, i32);
        c_accessor!(index, index, usize);
        c_accessor_fixed_slice_optional!(splits, splits, &[c_int; 4], 4);
        c_accessor_fixed_slice_optional!(pads, pads, &[c_int; 4], 4);
        c_accessor_tmp_ptr!(page, page, AtlasPage, spAtlasPage);

        #[must_use]
        pub fn key_values(&self) -> Vec<KeyValue> {
            let mut vec = vec![];
            unsafe {
                let array = &mut *self.c_ptr_ref().keyValues;
                for i in 0..array.size {
                    let item = &*array.items.offset(i as isize);
                    let name = String::from(from_c_str(CStr::from_ptr(item.name)));
                    let values = item.values;
                    vec.push(KeyValue { name, values });
                }
            }
            vec
        }

        c_ptr!(c_atlas_region, spAtlasRegion);
    }

    /// Additional key-value pairs in an [`AtlasRegion`].
    #[derive(Debug)]
    pub struct KeyValue {
        pub name: String,
        pub values: [f32; 5],
    }

    /// An iterator over each [`AtlasRegion`] in an [`Atlas`].
    pub struct AtlasRegionIterator<'a> {
        pub(crate) _atlas: &'a Atlas,
        pub(crate) region: *mut spAtlasRegion,
    }

    impl<'a> Iterator for AtlasRegionIterator<'a> {
        type Item = CTmpRef<'a, Atlas, AtlasRegion>;

        fn next(&mut self) -> Option<Self::Item> {
            if !self.region.is_null() {
                let page = unsafe { AtlasRegion::new_from_ptr(self.region) };
                self.region = unsafe { (*self.region).next };
                Some(CTmpRef::new(self._atlas, page))
            } else {
                None
            }
        }
    }

    /// Functions available if using the `mint` feature.
    #[cfg(feature = "mint")]
    impl AtlasRegion {
        #[must_use]
        pub fn translation(&self) -> Vector2<i32> {
            Vector2 {
                x: self.x(),
                y: self.y(),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::test::TestAsset;

    use super::Atlas;

    #[test]
    fn new_from_file() {
        for test_example_asset in TestAsset::all() {
            let atlas = Atlas::new_from_file(test_example_asset.atlas_file);
            assert!(atlas.is_ok());
        }

        let atlas = Atlas::new_from_file(format!("missing/{}", TestAsset::spineboy().atlas_file));
        assert!(atlas.is_err());
    }
}