From 7c11eff271a36dfbbc1f9ff2a0381f727f6368f5 Mon Sep 17 00:00:00 2001 From: jabu Date: Wed, 26 Oct 2022 21:03:50 -0700 Subject: [PATCH] add convenience functions to get attachment types from slot --- src/slot.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/src/slot.rs b/src/slot.rs index 4b56640..4916323 100644 --- a/src/slot.rs +++ b/src/slot.rs @@ -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. @@ -27,6 +30,26 @@ impl NewFromPtr for Slot { } } +macro_rules! attachment_accessor { + ($fn:ident, $fn_mut:ident, $type:ident, $c_type:ident, $attachment_type:expr) => { + pub fn $fn(&self) -> Option> { + 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. @@ -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);