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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use std::{borrow::Cow, sync::Arc};

use crate::{
    bone::Bone,
    c::{
        spBone, spIkConstraint, spPathConstraint, spPhysics, spPhysicsConstraint, spSkeleton,
        spSkeletonData, spSkeleton_create, spSkeleton_dispose,
        spSkeleton_getAttachmentForSlotIndex, spSkeleton_getAttachmentForSlotName,
        spSkeleton_setAttachment, spSkeleton_setBonesToSetupPose, spSkeleton_setSkin,
        spSkeleton_setSkinByName, spSkeleton_setSlotsToSetupPose, spSkeleton_setToSetupPose,
        spSkeleton_update, spSkeleton_updateCache, spSkeleton_updateWorldTransform,
        spSkeleton_updateWorldTransformWith, spSkin, spSlot, spTransformConstraint,
    },
    c_interface::{to_c_str, CTmpMut, CTmpRef, NewFromPtr, SyncPtr},
    error::SpineError,
    skeleton_data::SkeletonData,
    skin::Skin,
    slot::Slot,
    Attachment, IkConstraint, PathConstraint, Physics, PhysicsConstraint, TransformConstraint,
};

#[allow(unused_imports)]
use crate::{SkeletonBinary, SkeletonJson};

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

/// A live Skeleton instance created from [`SkeletonData`].
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#Skeleton)
#[derive(Debug)]
pub struct Skeleton {
    c_skeleton: SyncPtr<spSkeleton>,
    owns_memory: bool,
    _skeleton_data: Arc<SkeletonData>,
    _skin: Option<Skin>, // keep-alive for user created skins
}

impl Skeleton {
    /// Create a new instance of the skeleton loaded in [`SkeletonData`].
    ///
    /// See [`SkeletonJson`] or [`SkeletonBinary`] for a complete example of loading a skeleton.
    #[must_use]
    pub fn new(skeleton_data: Arc<SkeletonData>) -> Self {
        let c_skeleton = unsafe { spSkeleton_create(skeleton_data.c_ptr()) };
        Self {
            c_skeleton: SyncPtr(c_skeleton),
            owns_memory: true,
            _skeleton_data: skeleton_data,
            _skin: None, // keep alive user-created skins
        }
    }

    pub fn update(&mut self, delta: f32) {
        unsafe {
            spSkeleton_update(self.c_ptr(), delta);
        }
    }

    /// Caches information about bones and constraints. Must be called if the skin is modified or if
    /// bones, constraints, or weighted path attachments are added or removed.
    pub fn update_cache(&mut self) {
        unsafe {
            spSkeleton_updateCache(self.c_ptr());
        }
    }

    /// Updates the world transform for each bone and applies all constraints.
    ///
    /// See
    /// [`World transforms`](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms)
    /// in the Spine Runtimes Guide.
    pub fn update_world_transform(&mut self, physics: Physics) {
        unsafe {
            spSkeleton_updateWorldTransform(self.c_ptr(), physics as spPhysics);
        }
    }

    /// Temporarily sets the root bone as a child of the specified bone, then updates the world
    /// transform for each bone and applies all constraints.
    ///
    /// See
    /// [`World transforms`](http://esotericsoftware.com/spine-runtime-skeletons#World-transforms)
    /// in the Spine Runtimes Guide.
    ///
    /// # Safety
    ///
    /// The bone must originate from this skeleton.
    pub unsafe fn update_world_transform_with(&mut self, parent: &Bone, physics: Physics) {
        spSkeleton_updateWorldTransformWith(self.c_ptr(), parent.c_ptr(), physics as spPhysics);
    }

    /// Sets the bones, constraints, slots, and draw order to their setup pose values.
    pub fn set_to_setup_pose(&mut self) {
        unsafe {
            spSkeleton_setToSetupPose(self.c_ptr());
        }
    }

    /// Sets the bones and constraints to their setup pose values.
    pub fn set_bones_to_setup_pose(&mut self) {
        unsafe {
            spSkeleton_setBonesToSetupPose(self.c_ptr());
        }
    }

    /// Sets the slots and draw order to their setup pose values.
    pub fn set_slots_to_setup_pose(&mut self) {
        unsafe {
            spSkeleton_setSlotsToSetupPose(self.c_ptr());
        }
    }

    /// Set the skeleton's skin. If the skin is a user-created one (via [`Skin::new`]), then a
    /// clone is created and used instead, to help ensure memory safety. If this behavior is not
    /// desired then [`Skeleton::set_skin_unchecked`] can be used instead.
    ///
    /// # Safety
    ///
    /// The skin must originate from the same [`SkeletonData`] that this skeleton uses.
    pub unsafe fn set_skin(&mut self, skin: &Skin) {
        if skin.owns_memory {
            let cloned_skin = skin.clone();
            unsafe { spSkeleton_setSkin(self.c_ptr(), cloned_skin.c_ptr()) };
            self._skin = Some(cloned_skin);
        } else {
            unsafe { spSkeleton_setSkin(self.c_ptr(), skin.c_ptr()) };
            self._skin = None;
        }
        self.set_to_setup_pose();
    }

    /// Set the skeleton's skin.
    ///
    /// # Safety
    ///
    /// The skin must originate from the same [`SkeletonData`] that this skeleton uses.
    ///
    /// The [`Skin`] struct will destroy the underlying C representation of the skin in its [`Drop`]
    /// implementation. Skins assigned to a skeleton must live as long as the skeletons using them
    /// or else the skeleton may cause a segfault.
    pub unsafe fn set_skin_unchecked(&mut self, skin: &Skin) {
        spSkeleton_setSkin(self.c_ptr(), skin.c_ptr());
    }

    /// Set the skeleton's skin by name.
    ///
    /// # Safety
    ///
    /// This function should only be called with valid skin names. It is faster than the safe
    /// alternative, [`Skeleton::set_skin_by_name`], but will likely segfault if the skin does not
    /// exist.
    pub unsafe fn set_skin_by_name_unchecked(&mut self, skin_name: &str) {
        let c_skin_name = to_c_str(skin_name);
        spSkeleton_setSkinByName(self.c_ptr(), c_skin_name.as_ptr());
        self._skin = None;
    }

    /// Set the skeleton's skin by name.
    ///
    /// # Errors
    ///
    /// Returns [`SpineError::NotFound`] if the specified skin name doesn't exist.
    pub fn set_skin_by_name(&mut self, skin_name: &str) -> Result<(), SpineError> {
        if self.data().skins().any(|skin| skin.name() == skin_name) {
            unsafe { self.set_skin_by_name_unchecked(skin_name) };
            Ok(())
        } else {
            Err(SpineError::new_not_found("Skin", skin_name))
        }
    }

    /// Create a conglomerate skin containing `skin_names` and attach to this skeleton.
    ///
    /// ```
    /// # #[path="./test.rs"]
    /// # mod test;
    /// # use rusty_spine::{AnimationState, AnimationEvent};
    /// # let (mut skeleton, _) = test::TestAsset::spineboy().instance(true);
    /// skeleton.set_skins_by_name("combined-skin", ["hat", "suit", "tie"]);
    /// ```
    ///
    /// The name assigned to this skin (via `combined_skin_name`) is unimportant and does not need
    /// to be unique.
    ///
    /// A faster (but `unsafe`) way to create conglomerate skins is to use [`Skin::new`] and
    /// [`Skin::add_skin`] to create a pre-configured skin that can be attached at any time with
    /// [`Skeleton::set_skin`].
    ///
    /// # Errors
    ///
    /// Returns [`SpineError::NotFound`] if any of the specified skin names do not exist (in this
    /// case, the current skin remains unchanged).
    pub fn set_skins_by_name<'a, T>(
        &mut self,
        combined_skin_name: &str,
        skin_names: impl IntoIterator<Item = T>,
    ) -> Result<(), SpineError>
    where
        Cow<'a, str>: From<T>,
    {
        let mut combined_skin = Skin::new(combined_skin_name);
        for skin_name in skin_names {
            let skin_name_str = &*Cow::<'a, str>::from(skin_name);
            unsafe {
                combined_skin.add_skin(
                    self.data()
                        .find_skin(skin_name_str)
                        .ok_or_else(|| SpineError::new_not_found("Skin", skin_name_str))?
                        .as_ref(),
                );
            }
        }
        unsafe {
            self.set_skin(&combined_skin);
        }
        Ok(())
    }

    /// The root bone, or [`None`] if the skeleton has no bones.
    #[must_use]
    pub fn bone_root(&self) -> CTmpRef<Skeleton, Bone> {
        CTmpRef::new(self, unsafe { Bone::new_from_ptr(self.c_ptr_mut().root) })
    }

    /// The mutable root bone, or [`None`] if the skeleton has no bones.
    #[must_use]
    pub fn bone_root_mut(&mut self) -> CTmpMut<Skeleton, Bone> {
        CTmpMut::new(self, unsafe { Bone::new_from_ptr(self.c_ptr_mut().root) })
    }

    #[must_use]
    pub fn find_bone(&self, name: &str) -> Option<CTmpRef<Skeleton, Bone>> {
        self.bones().find(|bone| bone.data().name() == name)
    }

    #[must_use]
    pub fn find_bone_mut(&mut self, name: &str) -> Option<CTmpMut<Skeleton, Bone>> {
        self.bones_mut().find(|bone| bone.data().name() == name)
    }

    #[must_use]
    pub fn find_slot(&self, name: &str) -> Option<CTmpRef<Skeleton, Slot>> {
        self.slots().find(|slot| slot.data().name() == name)
    }

    #[must_use]
    pub fn find_slot_mut(&mut self, name: &str) -> Option<CTmpMut<Skeleton, Slot>> {
        self.slots_mut().find(|slot| slot.data().name() == name)
    }

    #[must_use]
    pub fn find_ik_constraint(&self, name: &str) -> Option<CTmpRef<Skeleton, IkConstraint>> {
        self.ik_constraints()
            .find(|ik_constraint| ik_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_ik_constraint_mut(
        &mut self,
        name: &str,
    ) -> Option<CTmpMut<Skeleton, IkConstraint>> {
        self.ik_constraints_mut()
            .find(|ik_constraint| ik_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_path_constraint(&self, name: &str) -> Option<CTmpRef<Skeleton, PathConstraint>> {
        self.path_constraints()
            .find(|path_constraint| path_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_path_constraint_mut(
        &mut self,
        name: &str,
    ) -> Option<CTmpMut<Skeleton, PathConstraint>> {
        self.path_constraints_mut()
            .find(|path_constraint| path_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_physics_constraint(
        &self,
        name: &str,
    ) -> Option<CTmpRef<Skeleton, PhysicsConstraint>> {
        self.physics_constraints()
            .find(|physics_constraint| physics_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_physics_constraint_mut(
        &mut self,
        name: &str,
    ) -> Option<CTmpMut<Skeleton, PhysicsConstraint>> {
        self.physics_constraints_mut()
            .find(|physics_constraint| physics_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_transform_constraint(
        &self,
        name: &str,
    ) -> Option<CTmpRef<Skeleton, TransformConstraint>> {
        self.transform_constraints()
            .find(|transform_constraint| transform_constraint.data().name() == name)
    }

    #[must_use]
    pub fn find_transform_constraint_mut(
        &mut self,
        name: &str,
    ) -> Option<CTmpMut<Skeleton, TransformConstraint>> {
        self.transform_constraints_mut()
            .find(|transform_constraint| transform_constraint.data().name() == name)
    }

    pub fn set_attachment(&mut self, slot_name: &str, attachment_name: Option<&str>) -> bool {
        let c_slot_name = to_c_str(slot_name);
        attachment_name.map_or_else(
            || unsafe {
                spSkeleton_setAttachment(self.c_ptr(), c_slot_name.as_ptr(), std::ptr::null()) != 0
            },
            |attachment_name| {
                let c_attachment_name = to_c_str(attachment_name);
                unsafe {
                    spSkeleton_setAttachment(
                        self.c_ptr(),
                        c_slot_name.as_ptr(),
                        c_attachment_name.as_ptr(),
                    ) != 0
                }
            },
        )
    }

    pub fn get_attachment_for_slot_name(
        &mut self,
        slot_name: &str,
        attachment_name: &str,
    ) -> Option<Attachment> {
        let c_slot_name = to_c_str(slot_name);
        let c_attachment_name = to_c_str(attachment_name);
        unsafe {
            let c_attachment = spSkeleton_getAttachmentForSlotName(
                self.c_ptr(),
                c_slot_name.as_ptr(),
                c_attachment_name.as_ptr(),
            );
            if !c_attachment.is_null() {
                Some(Attachment::new_from_ptr(c_attachment))
            } else {
                None
            }
        }
    }

    pub fn get_attachment_for_slot_index(
        &mut self,
        slot_index: usize,
        attachment_name: &str,
    ) -> Option<Attachment> {
        let c_attachment_name = to_c_str(attachment_name);
        unsafe {
            let c_attachment = spSkeleton_getAttachmentForSlotIndex(
                self.c_ptr(),
                slot_index as i32,
                c_attachment_name.as_ptr(),
            );
            if !c_attachment.is_null() {
                Some(Attachment::new_from_ptr(c_attachment))
            } else {
                None
            }
        }
    }

    // TODO: iterators for ik, transform, path constraints

    c_accessor_tmp_ptr_mut!(
        /// The skeleton's setup pose data.
        data,
        /// The skeleton's mutable setup pose data.
        data_mut,
        data,
        SkeletonData,
        spSkeletonData
    );
    c_accessor_color_mut!(
        /// The color to tint all the skeleton's attachments.
        color,
        /// Set the color to tint all the skeleton's attachments.
        color_mut,
        color
    );
    c_accessor!(
        /// The number of bones in this skeleton.
        bones_count,
        bonesCount,
        usize
    );
    c_accessor!(
        /// The number of slots in this skeleton.
        slots_count,
        slotsCount,
        usize
    );
    c_accessor!(
        /// The number of IK constraints in this skeleton.
        ik_contraints_count,
        ikConstraintsCount,
        usize
    );
    c_accessor!(
        /// The number of path constraints in this skeleton.
        path_contraints_count,
        pathConstraintsCount,
        usize
    );
    c_accessor!(
        /// The number of physics constraints in this skeleton.
        physics_contraints_count,
        physicsConstraintsCount,
        usize
    );
    c_accessor!(
        /// The number of transform constraints in this skeleton.
        transform_contraints_count,
        transformConstraintsCount,
        usize
    );
    c_accessor_mut!(
        /// Scales the entire skeleton on the X axis.
        scale_x,
        /// Sets the scale the entire skeleton on the X axis.
        /// Bones that do not inherit scale are still affected by this property.
        set_scale_x,
        scaleX,
        f32
    );
    c_accessor_mut!(
        /// Scales the entire skeleton on the Y axis.
        scale_y,
        /// Sets the scale the entire skeleton on the Y axis.
        /// Bones that do not inherit scale are still affected by this property.
        set_scale_y,
        scaleY,
        f32
    );
    c_accessor_mut!(
        /// The skeleton X translation, which is added to the root bone worldX translation.
        x,
        /// Sets the skeleton X translation, which is added to the root bone worldX translation.
        /// Bones that do not inherit translation are still affected by this property.
        set_x,
        x,
        f32
    );
    c_accessor_mut!(
        /// The skeleton Y translation, which is added to the root bone worldY translation.
        y,
        /// Sets the skeleton Y translation, which is added to the root bone worldY translation.
        /// Bones that do not inherit translation are still affected by this property.
        set_y,
        y,
        f32
    );
    c_accessor_array_mut!(
        /// An iterator to the skeleton's bones.
        bones,
        /// A mutable iterator to the skeleton's bones.
        bones_mut,
        /// The nth bone in the skeleton.
        bone_at_index,
        /// The nth mutable bone in the skeleton.
        bone_at_index_mut,
        Skeleton,
        Bone,
        spBone,
        bones,
        bones_count
    );
    c_accessor_array_mut!(
        /// An iterator to the skeleton's slots.
        slots,
        /// A mutable iterator to the skeleton's slots.
        slots_mut,
        /// The nth slot in the skeleton.
        slot_at_index,
        /// The nth mutable slot in the skeleton.
        slot_at_index_mut,
        Skeleton,
        Slot,
        spSlot,
        slots,
        slots_count
    );
    c_accessor_array_mut!(
        /// An iterator to the skeleton's slots in the order they should be drawn.
        draw_order,
        /// A mutable iterator to the skeleton's slots in the order they should be drawn.
        draw_order_mut,
        /// The nth skeleton slot, indexed in the order they should be drawn.
        draw_order_at_index,
        /// The nth mutable skeleton slot, indexed in the order they should be drawn.
        draw_order_at_index_mut,
        Skeleton,
        Slot,
        spSlot,
        drawOrder,
        slots_count
    );
    c_accessor_array_mut!(
        ik_constraints,
        ik_constraints_mut,
        ik_contraint_at_index,
        ik_constraint_at_index_mut,
        Skeleton,
        IkConstraint,
        spIkConstraint,
        ikConstraints,
        ik_contraints_count
    );
    c_accessor_array_mut!(
        path_constraints,
        path_constraints_mut,
        path_contraint_at_index,
        path_constraint_at_index_mut,
        Skeleton,
        PathConstraint,
        spPathConstraint,
        pathConstraints,
        path_contraints_count
    );
    c_accessor_array_mut!(
        physics_constraints,
        physics_constraints_mut,
        physics_contraint_at_index,
        physics_constraint_at_index_mut,
        Skeleton,
        PhysicsConstraint,
        spPhysicsConstraint,
        physicsConstraints,
        physics_contraints_count
    );
    c_accessor_array_mut!(
        transform_constraints,
        transform_constraints_mut,
        transform_contraint_at_index,
        transform_constraint_at_index_mut,
        Skeleton,
        TransformConstraint,
        spTransformConstraint,
        transformConstraints,
        transform_contraints_count
    );
    c_accessor_tmp_ptr_optional_mut!(skin, skin_mut, skin, Skin, spSkin);
    c_ptr!(c_skeleton, spSkeleton);
}

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

    pub fn set_translation(&mut self, translation: impl Into<Vector2<f32>>) {
        let translation: Vector2<f32> = translation.into();
        self.set_x(translation.x);
        self.set_y(translation.y);
    }

    #[must_use]
    pub fn scale(&self) -> Vector2<f32> {
        Vector2 {
            x: self.scale_x(),
            y: self.scale_y(),
        }
    }

    pub fn set_scale(&mut self, scale: impl Into<Vector2<f32>>) {
        let scale: Vector2<f32> = scale.into();
        self.set_scale_x(scale.x);
        self.set_scale_y(scale.y);
    }
}

impl Drop for Skeleton {
    fn drop(&mut self) {
        if self.owns_memory {
            unsafe {
                spSkeleton_dispose(self.c_skeleton.0);
            }
        }
    }
}