pub trait Pose {
fn set_from(&mut self, other: &Self);
}
#[derive(Debug, Clone, Default)]
pub struct Posed<P> {
pub pose: P,
pub constrained: P,
is_constrained: bool,
}
impl<P: Pose> Posed<P> {
pub fn new(pose: P, constrained: P) -> Self {
Self {
pose,
constrained,
is_constrained: false,
}
}
#[inline]
pub fn applied(&self) -> &P {
if self.is_constrained {
&self.constrained
} else {
&self.pose
}
}
#[inline]
pub fn applied_mut(&mut self) -> &mut P {
if self.is_constrained {
&mut self.constrained
} else {
&mut self.pose
}
}
#[inline]
pub fn select_mut(&mut self, applied: bool) -> &mut P {
if applied {
self.applied_mut()
} else {
&mut self.pose
}
}
#[inline]
pub fn select(&self, applied: bool) -> &P {
if applied { self.applied() } else { &self.pose }
}
#[inline]
pub fn is_constrained(&self) -> bool {
self.is_constrained
}
pub(crate) fn constrain(&mut self) {
self.is_constrained = true;
}
pub(crate) fn unconstrain(&mut self) {
self.is_constrained = false;
}
pub(crate) fn reset_constrained(&mut self) {
self.constrained.set_from(&self.pose);
}
}
macro_rules! copy_pose {
($($t:ty),*) => {
$(impl Pose for $t {
#[inline]
fn set_from(&mut self, other: &Self) {
*self = *other;
}
})*
};
}
copy_pose!(
crate::data::IkConstraintPose,
crate::data::TransformConstraintPose,
crate::data::PathConstraintPose,
crate::data::PhysicsConstraintPose,
crate::data::SliderPose
);