Skip to content

Commit

Permalink
Vector -> Coordinate, +Vector
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Aug 30, 2024
1 parent 5e99802 commit d0a4e4d
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 118 deletions.
34 changes: 17 additions & 17 deletions examples/skeleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cushy::{
widgets::{slider::Slidable, Canvas},
Run,
};
use funnybones::{BoneId, BoneKind, Joint, JointId, Rotation, Skeleton, Vector};
use funnybones::{Angle, BoneId, BoneKind, Coordinate, Joint, JointId, Skeleton};

fn main() {
// begin rustme snippet: readme
Expand All @@ -29,7 +29,7 @@ fn main() {
let r_hip = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("r_hip"));
// Connect the right hip to the spine.
skeleton.push_joint(Joint::new(
Rotation::degrees(-90.),
Angle::degrees(-90.),
spine.axis_a(),
r_hip.axis_a(),
));
Expand All @@ -46,15 +46,15 @@ fn main() {

// Connect the right leg to the right hip.
skeleton.push_joint(Joint::new(
Rotation::degrees(-90.),
Angle::degrees(-90.),
r_hip.axis_b(),
r_leg.axis_a(),
));
// Create the right foot.
let r_foot = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("r_foot"));
// Connect the right foot to the right leg.
let r_ankle_id = skeleton.push_joint(Joint::new(
Rotation::degrees(90.),
Angle::degrees(90.),
r_leg.axis_b(),
r_foot.axis_a(),
));
Expand All @@ -63,7 +63,7 @@ fn main() {
// Create the left-half of our lower half.
let l_hip = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("l_hip"));
skeleton.push_joint(Joint::new(
Rotation::degrees(90.),
Angle::degrees(90.),
spine.axis_a(),
l_hip.axis_a(),
));
Expand All @@ -76,21 +76,21 @@ fn main() {
.with_label("l_leg"),
);
skeleton.push_joint(Joint::new(
Rotation::degrees(90.),
Angle::degrees(90.),
l_hip.axis_b(),
l_leg.axis_a(),
));
let l_foot = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("l_foot"));
let l_ankle_id = skeleton.push_joint(Joint::new(
Rotation::degrees(-90.),
Angle::degrees(-90.),
l_leg.axis_b(),
l_foot.axis_a(),
));

// Create our two arms in the same fashion as our leg structure.
let r_shoulder = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("r_shoulder"));
skeleton.push_joint(Joint::new(
Rotation::degrees(-90.),
Angle::degrees(-90.),
spine.axis_b(),
r_shoulder.axis_a(),
));
Expand All @@ -103,20 +103,20 @@ fn main() {
.with_label("r_arm"),
);
let r_arm_socket = skeleton.push_joint(Joint::new(
Rotation::degrees(-90.),
Angle::degrees(-90.),
r_shoulder.axis_b(),
r_arm.axis_a(),
));
let r_hand = skeleton.push_bone(BoneKind::Rigid { length: 0.3 }.with_label("r_hand"));
let r_wrist_id = skeleton.push_joint(Joint::new(
Rotation::degrees(175.),
Angle::degrees(175.),
r_arm.axis_b(),
r_hand.axis_a(),
));

let l_shoulder = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("l_shoulder"));
skeleton.push_joint(Joint::new(
Rotation::degrees(90.),
Angle::degrees(90.),
spine.axis_b(),
l_shoulder.axis_a(),
));
Expand All @@ -129,21 +129,21 @@ fn main() {
.with_label("l_arm"),
);
let l_arm_socket = skeleton.push_joint(Joint::new(
Rotation::degrees(90.),
Angle::degrees(90.),
l_shoulder.axis_b(),
l_arm.axis_a(),
));
let l_hand = skeleton.push_bone(BoneKind::Rigid { length: 0.3 }.with_label("l_hand"));
let l_wrist_id = skeleton.push_joint(Joint::new(
Rotation::degrees(-175.),
Angle::degrees(-175.),
l_arm.axis_b(),
l_hand.axis_a(),
));

// Finally, create a bone to represent our head.
let head = skeleton.push_bone(BoneKind::Rigid { length: 0.5 }.with_label("head"));
let neck = skeleton.push_joint(Joint::new(
Rotation::degrees(180.),
Angle::degrees(180.),
spine.axis_b(),
head.axis_a(),
));
Expand Down Expand Up @@ -255,7 +255,7 @@ fn joint_widget(label: &str, skeleton: &Dynamic<Skeleton>, joint: JointId) -> im
}
})
.persist();
let angle_slider = angle.slider_between(Rotation::degrees(0.), Rotation::degrees(359.9));
let angle_slider = angle.slider_between(Angle::degrees(0.), Angle::degrees(359.9));

label.and(angle_slider).into_rows().contain()
}
Expand All @@ -276,7 +276,7 @@ fn bone_widget(
move |y| {
let mut skeleton = skeleton.lock();
let current_end = skeleton[bone].desired_end().unwrap_or_default();
skeleton[bone].set_desired_end(Some(Vector::new(current_end.x, *y)));
skeleton[bone].set_desired_end(Some(Coordinate::new(current_end.x, *y)));
}
})
.persist();
Expand All @@ -286,7 +286,7 @@ fn bone_widget(
move |x| {
let mut skeleton = skeleton.lock();
let current_end = skeleton[bone].desired_end().unwrap_or_default();
skeleton[bone].set_desired_end(Some(Vector::new(*x, current_end.y)));
skeleton[bone].set_desired_end(Some(Coordinate::new(*x, current_end.y)));
}
})
.persist();
Expand Down
16 changes: 8 additions & 8 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use easing_function::{easings::StandardEasing, Easing};

use crate::{BoneId, JointId, Rotation, Skeleton, Vector};
use crate::{BoneId, JointId, Angle, Skeleton, Coordinate};

#[derive(Default, Debug, PartialEq, Clone)]
pub struct Animation(Arc<AnimationData>);
Expand Down Expand Up @@ -166,8 +166,8 @@ impl From<ChangeKind> for Change {
#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ChangeKind {
Bone { bone: BoneId, position: Vector },
Joint { joint: JointId, rotation: Rotation },
Bone { bone: BoneId, position: Coordinate },
Joint { joint: JointId, rotation: Angle },
}

impl ChangeKind {
Expand All @@ -178,8 +178,8 @@ impl ChangeKind {
}

enum OriginalProperty {
Rotation(Rotation),
Vector(Vector),
Rotation(Angle),
Vector(Coordinate),
}

pub struct RunningAnimation {
Expand Down Expand Up @@ -290,16 +290,16 @@ impl Lerp for f32 {
}
}

impl Lerp for Vector {
impl Lerp for Coordinate {
fn lerp(self, target: Self, percent: f32) -> Self {
Vector::new(
Coordinate::new(
self.x.lerp(target.x, percent),
self.y.lerp(target.y, percent),
)
}
}

impl Lerp for Rotation {
impl Lerp for Angle {
fn lerp(self, target: Self, percent: f32) -> Self {
let delta_neg = self.radians - target.radians;
let delta_pos = target.radians - self.radians;
Expand Down
12 changes: 6 additions & 6 deletions src/cushy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@
use cushy::figures::Ranged;

use crate::{Rotation, Vector};
use crate::{Angle, Coordinate};

pub mod skeleton_canvas;

impl cushy::animation::PercentBetween for Rotation {
impl cushy::animation::PercentBetween for Angle {
fn percent_between(&self, min: &Self, max: &Self) -> cushy::animation::ZeroToOne {
self.radians.percent_between(&min.radians, &max.radians)
}
}

impl cushy::animation::LinearInterpolate for Rotation {
impl cushy::animation::LinearInterpolate for Angle {
fn lerp(&self, target: &Self, percent: f32) -> Self {
Self {
radians: self.radians.lerp(&target.radians, percent),
}
}
}

impl cushy::figures::IntoComponents<f32> for Vector {
impl cushy::figures::IntoComponents<f32> for Coordinate {
fn into_components(self) -> (f32, f32) {
(self.x, self.y)
}
}

impl cushy::figures::FromComponents<f32> for Vector {
impl cushy::figures::FromComponents<f32> for Coordinate {
fn from_components(components: (f32, f32)) -> Self {
Self::new(components.0, components.1)
}
}

impl Ranged for Rotation {
impl Ranged for Angle {
const MIN: Self = Self::MIN;
const MAX: Self = Self::MAX;
}
26 changes: 13 additions & 13 deletions src/cushy/skeleton_canvas.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(missing_docs)]
use core::f32;

use crate::{BoneEnd, BoneId, JointId, Rotation, Skeleton, Vector};
use crate::{BoneEnd, BoneId, JointId, Angle, Skeleton, Coordinate};
use cushy::{
context::{EventContext, GraphicsContext, LayoutContext},
figures::{
Expand Down Expand Up @@ -54,14 +54,14 @@ impl SkeletonCanvas {
self
}

fn vector_position(&self, vector: Vector) -> Point<Px> {
fn vector_position(&self, vector: Coordinate) -> Point<Px> {
(vector * self.scale).to_vec::<Point<f32>>().map(Px::from) + self.offset
}

fn position_to_vector(&self, position: Point<Px>) -> Vector {
fn position_to_vector(&self, position: Point<Px>) -> Coordinate {
(position - self.offset)
.map(FloatConversion::into_float)
.to_vec::<Vector>()
.to_vec::<Coordinate>()
/ self.scale
}
}
Expand All @@ -77,21 +77,21 @@ impl Widget for SkeletonCanvas {
skeleton.solve();
let root_start = skeleton.bones()[0].start();
let (min, max) = skeleton.bones().iter().fold(
(Vector::new(f32::MAX, f32::MAX), Vector::default()),
(Coordinate::new(f32::MAX, f32::MAX), Coordinate::default()),
|(min, max), bone| {
let start = bone.start() - root_start;
let end = bone.end() - root_start;
(
Vector::new(min.x.min(start.x).min(end.x), min.y.min(start.y).min(end.y)),
Vector::new(max.x.max(start.x).max(end.x), max.y.max(start.y).max(end.y)),
Coordinate::new(min.x.min(start.x).min(end.x), min.y.min(start.y).min(end.y)),
Coordinate::new(max.x.max(start.x).max(end.x), max.y.max(start.y).max(end.y)),
)
},
);

let skeleton_extent =
Vector::new(min.x.abs().max(max.x.abs()), min.y.abs().max(max.y.abs()));
Coordinate::new(min.x.abs().max(max.x.abs()), min.y.abs().max(max.y.abs()));

let middle = context.gfx.size().into_float().to_vec::<Vector>() / 2.;
let middle = context.gfx.size().into_float().to_vec::<Coordinate>() / 2.;
let height_ratio = middle.y / skeleton_extent.y;
let width_ratio = middle.x / skeleton_extent.x;
let zero_width = width_ratio.is_nan();
Expand Down Expand Up @@ -300,7 +300,7 @@ impl Widget for SkeletonCanvas {
}
}

fn distance_to_line(test: Vector, p1: Vector, p2: Vector) -> f32 {
fn distance_to_line(test: Coordinate, p1: Coordinate, p2: Coordinate) -> f32 {
let delta = p2 - p1;
let segment_length = delta.magnitude();

Expand Down Expand Up @@ -328,11 +328,11 @@ pub enum Target {
#[derive(Debug)]
struct DragInfo {
target: Target,
last: Vector,
last: Coordinate,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SkeletonMutation {
SetDesiredEnd { bone: BoneId, end: Vector },
SetJointRotation { joint: JointId, rotation: Rotation },
SetDesiredEnd { bone: BoneId, end: Coordinate },
SetJointRotation { joint: JointId, rotation: Angle },
}
8 changes: 4 additions & 4 deletions src/funnybones.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cushy::{
};
use funnybones::{
cushy::skeleton_canvas::{SkeletonCanvas, SkeletonMutation},
BoneAxis, BoneId, BoneKind, Joint, JointId, LabeledBoneKind, Rotation, Skeleton, Vector,
BoneAxis, BoneId, BoneKind, Joint, JointId, LabeledBoneKind, Angle, Skeleton, Coordinate,
};

#[derive(Default, Eq, PartialEq, Debug, Clone, Copy)]
Expand Down Expand Up @@ -200,11 +200,11 @@ impl EditingSkeleton {
struct SkeletalBone {
label: Dynamic<String>,
joint_label: Dynamic<String>,
joint_angle: Dynamic<Rotation>,
joint_angle: Dynamic<Angle>,
length: Dynamic<f32>,
jointed: Dynamic<Option<f32>>,
inverse: Dynamic<bool>,
desired_end: Dynamic<Option<Vector>>,
desired_end: Dynamic<Option<Coordinate>>,
connected_bones: Dynamic<Vec<SkeletalBone>>,
}

Expand All @@ -228,7 +228,7 @@ impl Default for SkeletalBone {
fn default() -> Self {
Self {
joint_label: Dynamic::default(),
joint_angle: Dynamic::new(Rotation::degrees(90.)),
joint_angle: Dynamic::new(Angle::degrees(90.)),
label: Dynamic::default(),
length: Dynamic::new(1.),
jointed: Dynamic::default(),
Expand Down
Loading

0 comments on commit d0a4e4d

Please sign in to comment.