-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathslot.rs
202 lines (181 loc) · 5.83 KB
/
slot.rs
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
use std::ffi::CString;
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::{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: *const spSlot) -> Self {
Self {
c_slot: SyncPtr(c_slot as *mut spSlot),
}
}
}
macro_rules! attachment_accessor {
($fn:ident, $fn_mut:ident, $type:ident, $c_type:ident, $attachment_type:expr) => {
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 as *mut $c_type))
}))
} else {
None
}
} else {
None
}
}
};
}
impl Slot {
/// Sets the attachment for this slot. This function is unsafe because there is no way to know
/// if the attachment is compatible with this slot and may segfault if used incorrectly.
pub unsafe fn set_attachment(&mut self, attachment: Option<Attachment>) {
if let Some(attachment) = attachment {
spSlot_setAttachment(self.c_ptr(), attachment.c_ptr());
} else {
spSlot_setAttachment(self.c_ptr(), std::ptr::null_mut());
}
}
pub fn set_to_setup_pose(&mut self) {
unsafe {
spSlot_setToSetupPose(self.c_ptr());
}
}
pub fn handle(&self) -> SlotHandle {
SlotHandle::new(self.c_ptr(), unsafe { self.bone().c_ptr_mut().skeleton })
}
attachment_accessor!(
region_attachment,
region_attachment_mut,
RegionAttachment,
spRegionAttachment,
AttachmentType::Region
);
attachment_accessor!(
bounding_box_attachment,
bounding_box_attachment_mut,
BoundingBoxAttachment,
spBoundingBoxAttachment,
AttachmentType::BoundingBox
);
attachment_accessor!(
mesh_attachment,
mesh_attachment_mut,
MeshAttachment,
spMeshAttachment,
AttachmentType::Mesh
);
attachment_accessor!(
point_attachment,
point_attachment_mut,
PointAttachment,
spPointAttachment,
AttachmentType::Point
);
attachment_accessor!(
clipping_attachment,
clipping_attachment_mut,
ClippingAttachment,
spClippingAttachment,
AttachmentType::Clipping
);
c_accessor_color_mut!(color, color_mut, color);
c_accessor_color_optional!(dark_color, darkColor);
c_accessor_tmp_ptr!(data, data_mut, data, SlotData, spSlotData);
c_accessor_tmp_ptr!(bone, bone_mut, bone, Bone, spBone);
c_accessor_tmp_ptr_optional!(
attachment,
attachment_mut,
attachment,
Attachment,
spAttachment
);
c_ptr!(c_slot, spSlot);
c_accessor!(sequence_index, sequenceIndex, i32);
// 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();
/// 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.
#[derive(Debug)]
pub struct SlotData {
c_slot_data: SyncPtr<spSlotData>,
}
impl NewFromPtr<spSlotData> for SlotData {
unsafe fn new_from_ptr(c_slot_data: *const spSlotData) -> Self {
Self {
c_slot_data: SyncPtr(c_slot_data as *mut spSlotData),
}
}
}
impl SlotData {
pub fn set_attachment_name(&mut self, attachment_name: &str) {
let c_attachment_name = CString::new(attachment_name).unwrap();
unsafe { spSlotData_setAttachmentName(self.c_ptr(), c_attachment_name.as_ptr()) }
}
c_accessor!(index, index, i32);
c_accessor_string!(name, name);
c_accessor_tmp_ptr!(bone_data, bone_data_mut, boneData, BoneData, spBoneData);
c_accessor_string!(attachment_name, attachmentName);
c_accessor_color_mut!(color, color_mut, color);
c_accessor_color_optional!(dark_color, darkColor);
c_accessor_enum_mut!(blend_mode, set_blend_mode, blendMode, BlendMode);
c_ptr!(c_slot_data, spSlotData);
}
#[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 {
0 => Self::Normal,
1 => Self::Additive,
2 => Self::Multiply,
3 => Self::Screen,
_ => Self::Normal,
}
}
}