Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
juliapaci committed Jun 24, 2024
1 parent 0aee85c commit eed2db4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
23 changes: 17 additions & 6 deletions src/bezpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,25 @@ fn interp_pathel(p0: kurbo::Point, pathel: kurbo::PathEl, t: f32) -> kurbo::Path
}

impl VectorBorder for VelloBezPath {
fn border_translation(&self, _time: f32) -> DVec2 {
fn border_translation(&self, time: f64) -> DVec2 {
// TODO: def should not unwrap here
let p = self.path.iter().last().unwrap().end_point().unwrap_or_default().to_vec2();

DVec2::new(p.x, p.y)
let first = self.path.elements()[0]
.end_point()
.unwrap_or_default()
.to_vec2();
let last = self
.path
.iter()
.last()
.unwrap()
.end_point()
.unwrap_or_default()
.to_vec2();

DVec2::new(first.x, first.y).lerp(DVec2::new(last.x, last.y), time)
}

fn border_tangent(&self, _time: f32) -> f64 {
self.border_translation(f32::default()).to_angle()
fn border_tangent(&self, time: f64) -> f64 {
self.border_translation(time).to_angle()
}
}
8 changes: 4 additions & 4 deletions src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl VelloVector for VelloCircle {
}

impl VectorBorder for VelloCircle {
fn border_translation(&self, _time: f32) -> DVec2 {
DVec2::new(0.0, self.radius)
fn border_translation(&self, time: f64) -> DVec2 {
DVec2::new(0.0, self.radius).lerp(DVec2::new(0.0, 0.0), time)
}

fn border_tangent(&self, _time: f32) -> f64 {
0.0
fn border_tangent(&self, time: f64) -> f64 {
self.border_translation(time).to_angle()
}
}
11 changes: 6 additions & 5 deletions src/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub struct ShapeId(Uuid);
#[derive(Component, Default, Copy, Clone)]
pub struct Head {
pub shape_id: ShapeId,
pub time: f64,

pub scale: f32,
pub scale: f64,
pub offset: f32,
pub rotation_offset: f32,
}
Expand All @@ -20,8 +21,8 @@ pub struct Shapes {
}

pub trait VectorBorder {
/// translation of the chosen "apex"
fn border_translation(&self, time: f32) -> DVec2;
/// gradient of the tangent to the border
fn border_tangent(&self, time: f32) -> f64;
/// Translation of the of the border at a specific `time` value.
fn border_translation(&self, time: f64) -> DVec2;
/// The gradient of the tangent to the border at a specific `time` value.
fn border_tangent(&self, time: f64) -> f64;
}
22 changes: 7 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ impl Plugin for VelloGraphicsPlugin {
build_vector::<VelloCircle>(),
build_vector::<VelloLine>(),
build_vector::<VelloBezPath>(),
build_head::<VelloRect>(),
build_head::<VelloCircle>(),
build_head::<VelloLine>(),
build_head::<VelloBezPath>(),
),
);
}
Expand Down Expand Up @@ -74,40 +70,36 @@ pub trait VelloVector {
}
}

pub(crate) fn build_vector<Vector: VelloVector + Component>() -> SystemConfigs {
pub(crate) fn build_vector<Vector: VelloVector + Component + VectorBorder>() -> SystemConfigs {
(
// vector
build_fill_only_vector::<Vector>,
build_stroke_only_vector::<Vector>,
build_fill_and_stroke_vector::<Vector>,
// head
append_heads::<Vector>,
)
.into_configs()
}

pub(crate) fn build_head<HeadEquipt: VelloVector + VectorBorder + Component>() -> SystemConfigs {
append_heads::<HeadEquipt>.into_configs()
}

#[allow(clippy::type_complexity)]
fn append_heads<HeadEquipt: VelloVector + VectorBorder + Component>(
mut q_vectors: Query<
(&HeadEquipt, &Head, &mut VelloScene),
(Without<Stroke>, Or<(Changed<HeadEquipt>, Changed<Fill>)>),
>,
shapes: Res<Shapes>,
time: Res<Time>,
) {
let time = time.elapsed_seconds();

for (vector, head, mut scene) in q_vectors.iter_mut() {
let translation = vector.border_translation(time);
let translation = vector.border_translation(head.time);
let translation = kurbo::Vec2::new(translation.x, translation.y);
let tangent = vector.border_tangent(time);
let tangent = vector.border_tangent(head.time);
let rotation = tangent.atan();

let transform = kurbo::Affine::default()
.with_translation(translation)
.then_rotate(rotation)
.then_scale(head.scale as f64);
.then_scale(head.scale);

let head_scene = shapes.scenes.get(&head.shape_id);
if let Some(head_scene) = head_scene {
Expand Down
6 changes: 3 additions & 3 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl VelloVector for VelloLine {
}

impl VectorBorder for VelloLine {
fn border_translation(&self, _time: f32) -> DVec2 {
self.p1
fn border_translation(&self, time: f64) -> DVec2 {
self.p0.lerp(self.p1, time)
}

fn border_tangent(&self, _time: f32) -> f64 {
fn border_tangent(&self, _time: f64) -> f64 {
(self.p1.y - self.p0.y) / (self.p1.x - self.p0.x)
}
}
12 changes: 9 additions & 3 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ impl VelloVector for VelloRect {
}

impl VectorBorder for VelloRect {
fn border_translation(&self, _time: f32) -> DVec2 {
DVec2::new(self.anchor.x, (1.0 - self.anchor.y) * self.size.y)
fn border_translation(&self, time: f64) -> DVec2 {
DVec2::new(-self.size.x * self.anchor.x, -self.size.y * self.anchor.y).lerp(
DVec2::new(
self.size.x * (1.0 - self.anchor.x),
self.size.y * (1.0 - self.anchor.y),
),
time,
)
}

fn border_tangent(&self, _time: f32) -> f64 {
fn border_tangent(&self, _time: f64) -> f64 {
0.0
}
}

0 comments on commit eed2db4

Please sign in to comment.