use std::sync::Arc;
use crate::{
animation::Animation,
bone::BoneData,
c::{
spAnimation, spBoneData, spIkConstraintData, spPathConstraintData, spPhysicsConstraintData,
spSkeletonData, spSkeletonData_dispose, spSkin, spSlotData, spTransformConstraintData,
},
c_interface::{CTmpRef, NewFromPtr, SyncPtr},
skin::Skin,
slot::SlotData,
Atlas, IkConstraintData, PathConstraintData, PhysicsConstraintData, TransformConstraintData,
};
#[cfg(feature = "mint")]
use mint::Vector2;
#[derive(Debug)]
pub struct SkeletonData {
c_skeleton_data: SyncPtr<spSkeletonData>,
owns_memory: bool,
_atlas: Option<Arc<Atlas>>,
}
impl NewFromPtr<spSkeletonData> for SkeletonData {
unsafe fn new_from_ptr(c_skeleton_data: *mut spSkeletonData) -> Self {
Self {
c_skeleton_data: SyncPtr(c_skeleton_data),
owns_memory: false,
_atlas: None,
}
}
}
impl SkeletonData {
pub(crate) const fn new(
c_skeleton_data: *mut spSkeletonData,
atlas: Option<Arc<Atlas>>,
) -> Self {
Self {
c_skeleton_data: SyncPtr(c_skeleton_data),
owns_memory: true,
_atlas: atlas,
}
}
#[must_use]
pub fn find_bone(&self, name: &str) -> Option<CTmpRef<SkeletonData, BoneData>> {
self.bones().find(|bone| bone.name() == name)
}
#[must_use]
pub fn find_slot(&self, name: &str) -> Option<CTmpRef<SkeletonData, SlotData>> {
self.slots().find(|slot| slot.name() == name)
}
#[must_use]
pub fn find_skin(&self, name: &str) -> Option<CTmpRef<SkeletonData, Skin>> {
self.skins().find(|skin| skin.name() == name)
}
#[must_use]
pub fn find_animation(&self, name: &str) -> Option<CTmpRef<SkeletonData, Animation>> {
self.animations().find(|animation| animation.name() == name)
}
#[must_use]
pub fn find_ik_constraint(
&self,
name: &str,
) -> Option<CTmpRef<SkeletonData, IkConstraintData>> {
self.ik_constraints()
.find(|ik_constraint| ik_constraint.name() == name)
}
#[must_use]
pub fn find_path_constraint(
&self,
name: &str,
) -> Option<CTmpRef<SkeletonData, PathConstraintData>> {
self.path_constraints()
.find(|path_constraint| path_constraint.name() == name)
}
#[must_use]
pub fn find_physics_constraint(
&self,
name: &str,
) -> Option<CTmpRef<SkeletonData, PhysicsConstraintData>> {
self.physics_constraints()
.find(|physics_constraint| physics_constraint.name() == name)
}
#[must_use]
pub fn find_transform_constraint(
&self,
name: &str,
) -> Option<CTmpRef<SkeletonData, TransformConstraintData>> {
self.transform_constraints()
.find(|transform_constraint| transform_constraint.name() == name)
}
c_accessor_string_optional!(
version,
version
);
c_accessor_string!(
hash,
hash
);
c_accessor_string_optional!(
images_path,
imagesPath
);
c_accessor_string_optional!(
audio_path,
audioPath
);
c_accessor!(
x,
x,
f32
);
c_accessor!(
y,
y,
f32
);
c_accessor!(
width,
width,
f32
);
c_accessor!(
height,
height,
f32
);
c_accessor!(bones_count, bonesCount, usize);
c_accessor!(slots_count, slotsCount, usize);
c_accessor!(skins_count, skinsCount, usize);
c_accessor!(events_count, eventsCount, usize);
c_accessor!(animations_count, animationsCount, usize);
c_accessor!(
ik_contraints_count,
ikConstraintsCount,
usize
);
c_accessor!(
path_contraints_count,
pathConstraintsCount,
usize
);
c_accessor!(
physics_contraints_count,
physicsConstraintsCount,
usize
);
c_accessor!(
transform_contraints_count,
transformConstraintsCount,
usize
);
c_accessor_array!(
bones,
bone_at_index,
SkeletonData,
BoneData,
spBoneData,
bones,
bones_count
);
c_accessor_array!(
slots,
slot_at_index,
SkeletonData,
SlotData,
spSlotData,
slots,
slots_count
);
c_accessor_array!(
skins,
skin_at_index,
SkeletonData,
Skin,
spSkin,
skins,
skins_count
);
c_accessor_tmp_ptr!(default_skin, defaultSkin, Skin, spSkin);
c_accessor_array!(
animations,
animation_at_index,
SkeletonData,
Animation,
spAnimation,
animations,
animations_count
);
c_accessor_array!(
ik_constraints,
ik_contraint_at_index,
SkeletonData,
IkConstraintData,
spIkConstraintData,
ikConstraints,
ik_contraints_count
);
c_accessor_array!(
path_constraints,
path_contraint_at_index,
SkeletonData,
PathConstraintData,
spPathConstraintData,
pathConstraints,
path_contraints_count
);
c_accessor_array!(
physics_constraints,
physics_contraint_at_index,
SkeletonData,
PhysicsConstraintData,
spPhysicsConstraintData,
physicsConstraints,
physics_contraints_count
);
c_accessor_array!(
transform_constraints,
transform_contraint_at_index,
SkeletonData,
TransformConstraintData,
spTransformConstraintData,
transformConstraints,
transform_contraints_count
);
c_ptr!(c_skeleton_data, spSkeletonData);
}
#[cfg(feature = "mint")]
impl SkeletonData {
#[must_use]
pub fn translation(&self) -> Vector2<f32> {
Vector2 {
x: self.x(),
y: self.y(),
}
}
#[must_use]
pub fn size(&self) -> Vector2<f32> {
Vector2 {
x: self.width(),
y: self.height(),
}
}
}
impl Drop for SkeletonData {
fn drop(&mut self) {
if self.owns_memory {
unsafe {
spSkeletonData_dispose(self.c_skeleton_data.0);
}
}
}
}