Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
juliapaci committed Jun 30, 2024
1 parent 2f9bc29 commit 5f60334
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
7 changes: 4 additions & 3 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ fn render_shapes(mut commands: Commands, shapes: Option<ResMut<Shapes>>) {
// Head
let head_scene = vello::Scene::default();

let head = (VelloCircle::new(1.0), Fill::new().with_color(Color::WHITE));
let head_shape = (VelloCircle::new(10.0), Fill::new().with_color(Color::WHITE));
commands
.spawn(VelloSceneBundle {
scene: VelloScene::from(head_scene.clone()),
// visibility: Visibility::Hidden,
..default()
})
.insert(head);
.insert(head_shape);

// Line
let line = (
VelloLine::new(DVec2::new(0.0, 100.0), DVec2::new(0.0, -100.0)),
Stroke::new(5.0).with_color(Color::WHITE),
Transform::from_xyz(-300.0, 0.0, 0.0),
Head::default().with_shape_id(shapes.insert(head_scene)),
Head::new(shapes.insert(head_scene), 10.0, DVec2::splat(0.0), 0.0),
);

// Rectangle
Expand Down
32 changes: 24 additions & 8 deletions src/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ use bevy::{math::DVec2, prelude::*, utils::Uuid};

use bevy_vello::prelude::*;

#[derive(Default, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Default, Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct ShapeId(Uuid);
impl ShapeId {
pub fn get(&self) -> Uuid {
self.0
}
}

impl From<Uuid> for ShapeId {
fn from(value: Uuid) -> Self {
Self(value)
}
}

#[derive(Component, Copy, Clone)]
#[derive(Component, Copy, Clone, Debug)]
pub struct Head {
pub shape_id: ShapeId,
pub time: f64,

pub scale: f64,
pub offset: f32,
pub rotation_offset: f32,
pub offset: DVec2,
pub rotation_offset: f64,
}

impl Default for Head {
Expand All @@ -21,14 +32,14 @@ impl Default for Head {
shape_id: ShapeId(Uuid::new_v4()),
time: 1.0,
scale: 1.0,
offset: 0.0,
offset: DVec2::default(),
rotation_offset: 0.0,
}
}
}

impl Head {
pub fn new(shape_id: ShapeId, scale: f64, offset: f32, rotation_offset: f32) -> Self {
pub fn new(shape_id: ShapeId, scale: f64, offset: DVec2, rotation_offset: f64) -> Self {
Self {
shape_id,
scale,
Expand All @@ -48,12 +59,17 @@ impl Head {
self
}

pub fn with_offset(mut self, offset: f32) -> Self {
pub fn with_offset(mut self, offset: DVec2) -> Self {
self.offset = offset;
self
}

pub fn with_rotation_offset(mut self, rotation_offset: f32) -> Self {
pub fn with_offset_splat(mut self, offset: f64) -> Self {
self.offset = DVec2::splat(offset);
self
}

pub fn with_rotation_offset(mut self, rotation_offset: f64) -> Self {
self.rotation_offset = rotation_offset;
self
}
Expand Down
17 changes: 5 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,24 @@ pub(crate) fn build_vector<Vector: VelloVector + Component + VectorBorder>() ->

#[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>)>),
>,
mut q_vectors: Query<(&HeadEquipt, &Head, &mut VelloScene)>,

shapes: Option<Res<Shapes>>,
) {
let Some(shapes) = shapes else { return };

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

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

let head_scene = shapes.scenes.get(&head.shape_id);
if let Some(head_scene) = head_scene {
if let Some(head_scene) = shapes.scenes.get(&head.shape_id) {
scene.append(head_scene, Some(transform));
}
}
Expand All @@ -117,10 +113,7 @@ fn append_heads<HeadEquipt: VelloVector + VectorBorder + Component>(
fn build_fill_only_vector<Vector: VelloVector + Component>(
mut q_vectors: Query<
(&Vector, &Fill, &mut VelloScene),
(
Without<Stroke>,
Or<(Without<Head>, Changed<Vector>, Changed<Fill>)>,
),
(Without<Stroke>, Or<(Changed<Vector>, Changed<Fill>)>),
>,
) {
for (vector, fill, mut scene) in q_vectors.iter_mut() {
Expand Down

0 comments on commit 5f60334

Please sign in to comment.