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
use crate::{
    attachment::Attachment,
    bone::Bone,
    c::{
        spAttachment, spBlendMode, spBone, spBoneData, spBoundingBoxAttachment,
        spClippingAttachment, spMeshAttachment, spPointAttachment, spRegionAttachment, spSkeleton,
        spSlot, spSlotData, spSlotData_setAttachmentName, spSlot_setAttachment,
        spSlot_setToSetupPose,
    },
    c_interface::{to_c_str, CTmpRef, NewFromPtr, SyncPtr},
    AttachmentType, BoneData, BoundingBoxAttachment, ClippingAttachment, MeshAttachment,
    PointAttachment, RegionAttachment, Skeleton,
};

/// A slot for an attachment.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#Slot)
#[derive(Debug)]
pub struct Slot {
    c_slot: SyncPtr<spSlot>,
}

impl NewFromPtr<spSlot> for Slot {
    unsafe fn new_from_ptr(c_slot: *mut spSlot) -> Self {
        Self {
            c_slot: SyncPtr(c_slot),
        }
    }
}

macro_rules! attachment_accessor {
    // TODO: fn_mut is unused? should fix this?
    ($(#[$($attrss1:tt)*])* $fn:ident, $(#[$($attrss2:tt)*])* $fn_mut:ident, $type:ident, $c_type:ident, $attachment_type:expr) => {
        $(#[$($attrss1)*])*
        #[must_use]
        pub fn $fn(&self) -> Option<CTmpRef<Self, $type>> {
            let attachment = unsafe { self.c_ptr_ref().attachment };
            if !attachment.is_null() {
                if AttachmentType::from(unsafe { (*attachment).type_0 }) == $attachment_type {
                    #[allow(unused_unsafe)]
                    Some(CTmpRef::new(self, unsafe {
                        ($type::new_from_ptr(attachment.cast::<$c_type>()))
                    }))
                } else {
                    None
                }
            } else {
                None
            }
        }
    };
}

impl Slot {
    // TODO: add attachment() accessor?

    /// Sets the attachment for this slot.
    ///
    /// # Safety
    ///
    /// The attachment must be compatible with this slot, usually by originating from it.
    pub unsafe fn set_attachment(&mut self, attachment: Option<Attachment>) {
        attachment.map_or_else(
            || {
                spSlot_setAttachment(self.c_ptr(), std::ptr::null_mut());
            },
            |attachment| {
                spSlot_setAttachment(self.c_ptr(), attachment.c_ptr());
            },
        );
    }

    /// Sets this slot to the setup pose.
    pub fn set_to_setup_pose(&mut self) {
        unsafe {
            spSlot_setToSetupPose(self.c_ptr());
        }
    }

    /// Create a persistent [`SlotHandle`] to this [`Slot`].
    #[must_use]
    pub fn handle(&self) -> SlotHandle {
        SlotHandle::new(self.c_ptr(), unsafe { self.bone().c_ptr_mut().skeleton })
    }

    attachment_accessor!(
        /// The [`RegionAttachment`] attached to this slot, or [`None`] if the attachment is a
        /// different type.
        region_attachment,
        /// The mutable [`RegionAttachment`] attached to this slot, or [`None`] if the attachment is
        /// a different type.
        region_attachment_mut,
        RegionAttachment,
        spRegionAttachment,
        AttachmentType::Region
    );

    attachment_accessor!(
        /// The [`BoundingBoxAttachment`] attached to this slot, or [`None`] if the attachment is a
        /// different type.
        bounding_box_attachment,
        /// The mutable [`BoundingBoxAttachment`] attached to this slot, or [`None`] if the
        /// attachment is a different type.
        bounding_box_attachment_mut,
        BoundingBoxAttachment,
        spBoundingBoxAttachment,
        AttachmentType::BoundingBox
    );

    attachment_accessor!(
        /// The [`MeshAttachment`] attached to this slot, or [`None`] if the attachment is a
        /// different type.
        mesh_attachment,
        /// The mutable [`MeshAttachment`] attached to this slot, or [`None`] if the attachment is a
        /// different type.
        mesh_attachment_mut,
        MeshAttachment,
        spMeshAttachment,
        AttachmentType::Mesh
    );

    attachment_accessor!(
        /// The [`PointAttachment`] attached to this slot, or [`None`] if the attachment is a
        /// different type.
        point_attachment,
        /// The mutable [`PointAttachment`] attached to this slot, or [`None`] if the attachment is
        /// a different type.
        point_attachment_mut,
        PointAttachment,
        spPointAttachment,
        AttachmentType::Point
    );

    attachment_accessor!(
        /// The [`ClippingAttachment`] attached to this slot, or [`None`] if the attachment is a
        /// different type.
        clipping_attachment,
        /// The mutable [`ClippingAttachment`] attached to this slot, or [`None`] if the attachment
        /// is a different type.
        clipping_attachment_mut,
        ClippingAttachment,
        spClippingAttachment,
        AttachmentType::Clipping
    );

    c_accessor_color_mut!(
        /// The color used to tint the slot's attachment. If [`dark_color`](`Self::dark_color`) is
        /// set, this is used as the light color for two color tinting.
        color,
        /// Set the color used to tint the slot's attachment. If [`dark_color`](`Self::dark_color`)
        /// is set, this is used as the light color for two color tinting.
        color_mut,
        color
    );
    c_accessor_color_optional!(
        /// The dark color used to tint the slot's attachment for two color tinting, or [`None`] if
        /// two color tinting is not used. The dark color's alpha is not used.
        dark_color,
        darkColor
    );
    c_accessor_tmp_ptr_mut!(
        /// The slot's setup pose data.
        data,
        /// The slot's mutable setup pose data.
        data_mut,
        data,
        SlotData,
        spSlotData
    );
    c_accessor_tmp_ptr_mut!(
        /// The bone this slot belongs to.
        bone,
        /// The mutable bone this slot belongs to.
        bone_mut,
        bone,
        Bone,
        spBone
    );
    c_accessor_tmp_ptr_optional_mut!(
        /// The current attachment for the slot, or [`None`] if the slot has no attachment.
        attachment,
        /// The current mutable attachment for the slot, or [`None`] if the slot has no attachment.
        attachment_mut,
        attachment,
        Attachment,
        spAttachment
    );
    c_ptr!(c_slot, spSlot);
    c_accessor!(sequence_index, sequenceIndex, usize);

    // TODO: accessors for deform
}

c_handle_decl!(
    /// A storeable reference to a [`Slot`].
    ///
    /// Can be acquired from any instance of [`Slot`].
    ///
    /// ```
    /// # #[path="./test.rs"]
    /// # mod test;
    /// # use rusty_spine::{AnimationState, EventType, SlotHandle};
    /// # let (skeleton, _) = test::TestAsset::spineboy().instance(true);
    /// let slot_handles: Vec<SlotHandle> = skeleton.slots().map(|slot| slot.handle()).collect();
    /// for slot_handle in slot_handles.iter() {
    ///     let slot = slot_handle.get(&skeleton).unwrap();
    ///     println!("{}", slot.data().name());
    /// }
    /// ```
    SlotHandle,
    Slot,
    Skeleton,
    spSlot,
    spSkeleton
);

/// Static slot data imported from Spine.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#SlotData)
#[derive(Debug)]
pub struct SlotData {
    c_slot_data: SyncPtr<spSlotData>,
}

impl NewFromPtr<spSlotData> for SlotData {
    unsafe fn new_from_ptr(c_slot_data: *mut spSlotData) -> Self {
        Self {
            c_slot_data: SyncPtr(c_slot_data),
        }
    }
}

impl SlotData {
    pub fn set_attachment_name(&mut self, attachment_name: &str) {
        let c_attachment_name = to_c_str(attachment_name);
        unsafe { spSlotData_setAttachmentName(self.c_ptr(), c_attachment_name.as_ptr()) }
    }

    c_accessor!(
        /// The index of the slot in [`Skeleton::slots`].
        index,
        index,
        usize
    );
    c_accessor_string!(
        /// The name of the slot, which is unique across all slots in the skeleton.
        name,
        name
    );
    c_accessor_tmp_ptr!(
        /// The bone this slot belongs to.
        bone_data,
        boneData,
        BoneData,
        spBoneData
    );
    c_accessor_string_optional!(
        /// The name of the attachment that is visible for this slot in the setup pose, or [`None`]
        /// if no attachment is visible.
        attachment_name,
        attachmentName
    );
    c_accessor_color!(
        /// The color used to tint the slot's attachment. If [`dark_color`](`Self::dark_color`) is
        /// set, this is used as the light color for two color tinting.
        color,
        color
    );
    c_accessor_color_optional!(
        /// The dark color used to tint the slot's attachment for two color tinting, or [`None`] if
        /// two color tinting is not used. The dark color's alpha is not used.
        dark_color,
        darkColor
    );
    c_accessor_enum!(
        /// The blend mode for drawing the slot's attachment.
        blend_mode,
        blendMode,
        BlendMode
    );
    c_ptr!(c_slot_data, spSlotData);
}

/// The variants of blend modes supported by Spine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlendMode {
    Normal = 0,
    Additive = 1,
    Multiply = 2,
    Screen = 3,
}

impl From<spBlendMode> for BlendMode {
    fn from(attachment_type: spBlendMode) -> Self {
        match attachment_type {
            1 => Self::Additive,
            2 => Self::Multiply,
            3 => Self::Screen,
            _ => Self::Normal,
        }
    }
}