From 0aee85cc14d13418ddff5b5c0b4f0b40ca42d9fe Mon Sep 17 00:00:00 2001 From: julia Date: Sun, 16 Jun 2024 18:26:42 +1000 Subject: [PATCH] primitive implementations --- src/bezpath.rs | 17 ++++++++++++++++- src/circle.rs | 14 +++++++++++++- src/head.rs | 9 +++++---- src/lib.rs | 17 +++++++++++------ src/line.rs | 12 ++++++++++++ src/rect.rs | 12 ++++++++++++ 6 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/bezpath.rs b/src/bezpath.rs index 613857f..18c8c2c 100644 --- a/src/bezpath.rs +++ b/src/bezpath.rs @@ -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. @@ -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() + } +} diff --git a/src/circle.rs b/src/circle.rs index 13e7a7c..5742255 100644 --- a/src/circle.rs +++ b/src/circle.rs @@ -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)] @@ -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 + } +} diff --git a/src/head.rs b/src/head.rs index 877d8ba..be12e0a 100644 --- a/src/head.rs +++ b/src/head.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*, utils::Uuid}; +use bevy::{math::DVec2, prelude::*, utils::Uuid}; use bevy_vello::prelude::*; @@ -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, } @@ -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; } diff --git a/src/lib.rs b/src/lib.rs index d10863e..651915f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,10 @@ impl Plugin for VelloGraphicsPlugin { build_vector::(), build_vector::(), build_vector::(), + build_head::(), + build_head::(), + build_head::(), + build_head::(), ), ); } @@ -70,12 +74,8 @@ pub trait VelloVector { } } -pub(crate) fn build_vector< - Vector: VelloVector + Component, - HeadEquipt: VelloVector + VectorBorder + Component, ->() -> SystemConfigs { +pub(crate) fn build_vector() -> SystemConfigs { ( - append_heads::, build_fill_only_vector::, build_stroke_only_vector::, build_fill_and_stroke_vector::, @@ -83,6 +83,10 @@ pub(crate) fn build_vector< .into_configs() } +pub(crate) fn build_head() -> SystemConfigs { + append_heads::.into_configs() +} + #[allow(clippy::type_complexity)] fn append_heads( mut q_vectors: Query< @@ -96,13 +100,14 @@ fn append_heads( 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 { diff --git a/src/line.rs b/src/line.rs index 44aacc4..c3808b0 100644 --- a/src/line.rs +++ b/src/line.rs @@ -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)] @@ -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) + } +} diff --git a/src/rect.rs b/src/rect.rs index 2f36f6c..645bd02 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -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)] @@ -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 + } +}