Skip to content

Commit

Permalink
add convenience functions to get attachment types from slot
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu committed Oct 27, 2022
1 parent 9136c7b commit 7c11eff
Showing 1 changed file with 67 additions and 4 deletions.
71 changes: 67 additions & 4 deletions src/slot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use crate::{
attachment::Attachment,
bone::Bone,
c::{
spAttachment, spBlendMode, spBone, spBoneData, spSkeleton, spSlot, spSlotData,
spSlotData_setAttachmentName, spSlot_setAttachment, spSlot_setToSetupPose,
spAttachment, spBlendMode, spBone, spBoneData, spBoundingBoxAttachment,
spClippingAttachment, spMeshAttachment, spPointAttachment, spRegionAttachment, spSkeleton,
spSlot, spSlotData, spSlotData_setAttachmentName, spSlot_setAttachment,
spSlot_setToSetupPose,
},
c_interface::{NewFromPtr, SyncPtr},
BoneData, Skeleton,
c_interface::{CTmpRef, NewFromPtr, SyncPtr},
AttachmentType, BoneData, BoundingBoxAttachment, ClippingAttachment, MeshAttachment,
PointAttachment, RegionAttachment, Skeleton,
};

/// A slot for an attachment.
Expand All @@ -27,6 +30,26 @@ impl NewFromPtr<spSlot> for Slot {
}
}

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.
Expand All @@ -48,6 +71,46 @@ impl Slot {
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);
Expand Down

0 comments on commit 7c11eff

Please sign in to comment.