Skip to content

Commit

Permalink
primitive implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
juliapaci committed Jun 24, 2024
1 parent 5f03bf0 commit 0aee85c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
17 changes: 16 additions & 1 deletion src/bezpath.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::prelude::*;
use bevy::{math::DVec2, prelude::*};
use bevy_vello::prelude::*;

use crate::VectorBorder;

use super::VelloVector;

/// Vello Bézier path component.
Expand Down Expand Up @@ -137,3 +139,16 @@ fn interp_pathel(p0: kurbo::Point, pathel: kurbo::PathEl, t: f32) -> kurbo::Path
kurbo::PathEl::ClosePath => kurbo::PathEl::ClosePath,
}
}

impl VectorBorder for VelloBezPath {
fn border_translation(&self, _time: f32) -> 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)
}

fn border_tangent(&self, _time: f32) -> f64 {
self.border_translation(f32::default()).to_angle()
}
}
14 changes: 13 additions & 1 deletion src/circle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::prelude::*;
use bevy::{math::DVec2, prelude::*};
use bevy_vello::prelude::*;

use crate::VectorBorder;

use super::VelloVector;

#[derive(Component, Default, Debug, Clone, Copy)]
Expand All @@ -24,3 +26,13 @@ impl VelloVector for VelloCircle {
kurbo::Circle::new(kurbo::Point::default(), self.radius)
}
}

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

fn border_tangent(&self, _time: f32) -> f64 {
0.0
}
}
9 changes: 5 additions & 4 deletions src/head.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use bevy::{prelude::*, utils::Uuid};
use bevy::{math::DVec2, prelude::*, utils::Uuid};

use bevy_vello::prelude::*;

Expand All @@ -9,7 +9,7 @@ pub struct ShapeId(Uuid);
pub struct Head {
pub shape_id: ShapeId,

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

pub trait VectorBorder {
fn border_translation(&self, time: f32) -> kurbo::Vec2;
/// returns the gradient of the tangent to the border
/// 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;
}
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ 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 @@ -70,19 +74,19 @@ pub trait VelloVector {
}
}

pub(crate) fn build_vector<
Vector: VelloVector + Component,
HeadEquipt: VelloVector + VectorBorder + Component,
>() -> SystemConfigs {
pub(crate) fn build_vector<Vector: VelloVector + Component>() -> SystemConfigs {
(
append_heads::<HeadEquipt>,
build_fill_only_vector::<Vector>,
build_stroke_only_vector::<Vector>,
build_fill_and_stroke_vector::<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<
Expand All @@ -96,13 +100,14 @@ fn append_heads<HeadEquipt: VelloVector + VectorBorder + Component>(

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

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

let head_scene = shapes.scenes.get(&head.shape_id);
if let Some(head_scene) = head_scene {
Expand Down
12 changes: 12 additions & 0 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::{math::DVec2, prelude::*};
use bevy_vello::prelude::*;

use crate::VectorBorder;

use super::VelloVector;

#[derive(Component, Default, Debug, Clone, Copy)]
Expand Down Expand Up @@ -40,3 +42,13 @@ impl VelloVector for VelloLine {
)
}
}

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

fn border_tangent(&self, _time: f32) -> f64 {
(self.p1.y - self.p0.y) / (self.p1.x - self.p0.x)
}
}
12 changes: 12 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::{math::DVec2, prelude::*};
use bevy_vello::prelude::*;

use crate::VectorBorder;

use super::VelloVector;

#[derive(Component, Default, Debug, Clone, Copy)]
Expand Down Expand Up @@ -46,3 +48,13 @@ 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_tangent(&self, _time: f32) -> f64 {
0.0
}
}

0 comments on commit 0aee85c

Please sign in to comment.