Skip to content

Commit

Permalink
add constraints api
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu committed May 4, 2024
1 parent d0b7fe0 commit 77c7071
Show file tree
Hide file tree
Showing 10 changed files with 734 additions and 9 deletions.
59 changes: 59 additions & 0 deletions src/ik_constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::{
c::{
spBone, spIkConstraint, spIkConstraintData, spIkConstraint_setToSetupPose,
spIkConstraint_update,
},
c_interface::{NewFromPtr, SyncPtr},
Bone, IkConstraintData,
};

/// [Spine API Reference](https://esotericsoftware.com/spine-api-reference#IkConstraint)
#[derive(Debug)]
pub struct IkConstraint {
c_ik_constraint: SyncPtr<spIkConstraint>,
}

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

impl IkConstraint {
pub fn update(&self) {
unsafe {
spIkConstraint_update(self.c_ik_constraint.0);
}
}

pub fn set_to_setup_pose(&self) {
unsafe {
spIkConstraint_setToSetupPose(self.c_ik_constraint.0);
}
}

c_accessor_tmp_ptr_mut!(data, data_mut, data, IkConstraintData, spIkConstraintData);

c_accessor_bool!(active, active);
c_accessor_mut!(bend_direction, set_bend_direction, bendDirection, i32);
c_accessor_bool_mut!(compress, set_compress, compress);
c_accessor_mut!(mix, set_mix, mix, f32);
c_accessor_mut!(softness, set_softness, softness, f32);
c_accessor_bool_mut!(stretch, set_stretch, stretch);

c_accessor!(bones_count, bonesCount, usize);
c_accessor_array!(
bones,
bone_at_index,
IkConstraint,
Bone,
spBone,
bones,
bones_count
);
c_accessor_tmp_ptr_mut!(target, target_mut, target, Bone, spBone);

c_ptr!(c_ik_constraint, spIkConstraint);
}
46 changes: 46 additions & 0 deletions src/ik_constraint_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
c::{spBoneData, spIkConstraintData},
c_interface::{NewFromPtr, SyncPtr},
BoneData,
};

/// [Spine API Reference](https://esotericsoftware.com/spine-api-reference#IkConstraintData)
#[derive(Debug)]
pub struct IkConstraintData {
c_ik_constraint_data: SyncPtr<spIkConstraintData>,
}

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

impl IkConstraintData {
c_accessor_string!(name, name);
c_accessor!(order, order, i32);
c_accessor_bool!(skin_required, skinRequired);

c_accessor!(bend_direction, bendDirection, i32);
c_accessor_bool!(compress, compress);
c_accessor!(mix, mix, f32);
c_accessor!(softness, softness, f32);
c_accessor_bool!(stretch, stretch);
c_accessor_bool!(uniform, uniform);

c_accessor!(bones_count, bonesCount, usize);
c_accessor_array!(
bones,
bone_at_index,
IkConstraintData,
BoneData,
spBoneData,
bones,
bones_count
);
c_accessor_tmp_ptr!(target, target, BoneData, spBoneData);

c_ptr!(c_ik_constraint_data, spIkConstraintData);
}
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,15 @@ mod clipping_attachment;
mod color;
mod error;
mod event;
mod ik_constraint;
mod ik_constraint_data;
mod mesh_attachment;
mod path_attachment;
mod path_constraint;
mod path_constraint_data;
mod physics;
mod physics_constraint;
mod physics_constraint_data;
mod point_attachment;
mod region_attachment;
mod renderer_object;
Expand All @@ -69,6 +75,8 @@ mod skeleton_json;
mod skin;
mod slot;
mod texture_region;
mod transform_constraint;
mod transform_constraint_data;

pub use animation::*;
pub use animation_state::*;
Expand All @@ -82,9 +90,15 @@ pub use clipping_attachment::*;
pub use color::*;
pub use error::*;
pub use event::*;
pub use ik_constraint::*;
pub use ik_constraint_data::*;
pub use mesh_attachment::*;
pub use path_attachment::*;
pub use path_constraint::*;
pub use path_constraint_data::*;
pub use physics::*;
pub use physics_constraint::*;
pub use physics_constraint_data::*;
pub use point_attachment::*;
pub use region_attachment::*;
pub use renderer_object::*;
Expand All @@ -96,6 +110,8 @@ pub use skeleton_json::*;
pub use skin::*;
pub use slot::*;
pub use texture_region::*;
pub use transform_constraint::*;
pub use transform_constraint_data::*;

#[cfg(test)]
pub mod test;
65 changes: 65 additions & 0 deletions src/path_constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::{
c::{
spBone, spPathConstraint, spPathConstraintData, spPathConstraint_setToSetupPose,
spPathConstraint_update, spSlot,
},
c_interface::{NewFromPtr, SyncPtr},
Bone, PathConstraintData, Slot,
};

/// [Spine API Reference](https://esotericsoftware.com/spine-api-reference#PathConstraint)
#[derive(Debug)]
pub struct PathConstraint {
c_path_constraint: SyncPtr<spPathConstraint>,
}

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

impl PathConstraint {
pub fn update(&self) {
unsafe {
spPathConstraint_update(self.c_path_constraint.0);
}
}

pub fn set_to_setup_pose(&self) {
unsafe {
spPathConstraint_setToSetupPose(self.c_path_constraint.0);
}
}

c_accessor_tmp_ptr_mut!(
data,
data_mut,
data,
PathConstraintData,
spPathConstraintData
);

c_accessor_bool!(active, active);
c_accessor_mut!(mix_rotate, set_mix_rotate, mixRotate, f32);
c_accessor_mut!(mix_x, set_mix_x, mixX, f32);
c_accessor_mut!(mix_y, set_mix_y, mixY, f32);
c_accessor_mut!(position, set_position, position, f32);
c_accessor_mut!(spacing, set_spacing, spacing, f32);

c_accessor!(bones_count, bonesCount, usize);
c_accessor_array!(
bones,
bone_at_index,
PathConstraint,
Bone,
spBone,
bones,
bones_count
);
c_accessor_tmp_ptr_mut!(target, target_mut, target, Slot, spSlot);

c_ptr!(c_path_constraint, spPathConstraint);
}
121 changes: 121 additions & 0 deletions src/path_constraint_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use crate::{
c::{
spBoneData, spPathConstraintData, spPositionMode, spRotateMode, spSlotData, spSpacingMode,
},
c_interface::{NewFromPtr, SyncPtr},
BoneData, SlotData,
};

#[cfg(feature = "mint")]
use mint::Vector2;

/// [Spine API Reference](https://esotericsoftware.com/spine-api-reference#PathConstraintData)
#[derive(Debug)]
pub struct PathConstraintData {
c_path_constraint_data: SyncPtr<spPathConstraintData>,
}

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

impl PathConstraintData {
c_accessor_string!(name, name);
c_accessor!(order, order, i32);
c_accessor_bool!(skin_required, skinRequired);

c_accessor_enum!(position_mode, positionMode, PositionMode);
c_accessor_enum!(spacing_mode, spacingMode, SpacingMode);
c_accessor_enum!(rotate_mode, rotateMode, RotateMode);

c_accessor!(offset_rotation, offsetRotation, f32);
c_accessor!(position, position, f32);
c_accessor!(spacing, spacing, f32);
c_accessor!(mix_rotate, mixRotate, f32);
c_accessor!(mix_x, mixX, f32);
c_accessor!(mix_y, mixY, f32);

c_accessor!(bones_count, bonesCount, usize);
c_accessor_array!(
bones,
bone_at_index,
PathConstraintData,
BoneData,
spBoneData,
bones,
bones_count
);
c_accessor_tmp_ptr!(target, target, SlotData, spSlotData);

c_ptr!(c_path_constraint_data, spPathConstraintData);
}

/// Functions available if using the `mint` feature.
#[cfg(feature = "mint")]
impl PathConstraintData {
#[must_use]
pub fn mix(&self) -> Vector2<f32> {
Vector2 {
x: self.mix_x(),
y: self.mix_y(),
}
}
}

pub enum PositionMode {
Fixed = 0,
Percent = 1,
Unknown = 99,
}

impl From<spPositionMode> for PositionMode {
fn from(mode: spPositionMode) -> Self {
match mode {
0 => Self::Fixed,
1 => Self::Percent,
_ => Self::Unknown,
}
}
}

pub enum SpacingMode {
Length = 0,
Fixed = 1,
Percent = 2,
Proportional = 3,
Unknown = 99,
}

impl From<spSpacingMode> for SpacingMode {
fn from(mode: spSpacingMode) -> Self {
match mode {
0 => Self::Length,
1 => Self::Fixed,
2 => Self::Percent,
3 => Self::Proportional,
_ => Self::Unknown,
}
}
}

pub enum RotateMode {
Tangent = 0,
Chain = 1,
ChainScale = 2,
Unknown = 99,
}

impl From<spRotateMode> for RotateMode {
fn from(mode: spRotateMode) -> Self {
match mode {
0 => Self::Tangent,
1 => Self::Chain,
2 => Self::ChainScale,
_ => Self::Unknown,
}
}
}
Loading

0 comments on commit 77c7071

Please sign in to comment.