Skip to content

Commit

Permalink
Implement Geometry for Path (#139)
Browse files Browse the repository at this point in the history
* Move `Geometry` impl to wrapper `Path`

* Add path example
  • Loading branch information
Nilirad committed Jan 9, 2022
1 parent 077f0cd commit 762ff4d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
25 changes: 25 additions & 0 deletions examples/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use bevy::prelude::*;
use bevy_prototype_lyon::prelude::*;

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_plugin(ShapePlugin)
.add_startup_system(setup_system)
.run();
}

fn setup_system(mut commands: Commands) {
let mut path_builder = PathBuilder::new();
path_builder.move_to(Vec2::ZERO);
path_builder.line_to(100.0 * Vec2::ONE);
let line = path_builder.build();

commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(GeometryBuilder::build_as(
&line,
DrawMode::Stroke(StrokeMode::new(Color::BLACK, 10.0)),
Transform::default(),
));
}
7 changes: 7 additions & 0 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use lyon_tessellation::{self as tess, FillOptions};

use crate::{
draw::{DrawMode, FillMode},
prelude::Geometry,
render::Shape,
};

Expand Down Expand Up @@ -51,3 +52,9 @@ impl Default for ShapeBundle {
#[allow(missing_docs)]
#[derive(Component)]
pub struct Path(pub tess::path::Path);

impl Geometry for Path {
fn add_geometry(&self, b: &mut tess::path::path::Builder) {
b.concatenate(&[self.0.as_slice()]);
}
}
9 changes: 1 addition & 8 deletions src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Types for defining and using geometries.

use bevy::transform::components::Transform;
use lyon_tessellation::{self as tess, path::path::Builder};
use lyon_tessellation::path::path::Builder;

use crate::{
draw::DrawMode,
Expand Down Expand Up @@ -53,13 +53,6 @@ pub trait Geometry {
fn add_geometry(&self, b: &mut Builder);
}

/// This implementation permits to use a Lyon [`Path`] as a [`Geometry`].
impl Geometry for tess::path::Path {
fn add_geometry(&self, b: &mut Builder) {
b.concatenate(&[self.as_slice()]);
}
}

/// Allows the creation of shapes using geometries added to a path builder.
pub struct GeometryBuilder(Builder);

Expand Down

3 comments on commit 762ff4d

@JavaDerg
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this fix be released?

@Nilirad
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will this fix be released?

This is just a minor ergonomic fix. I don't want to bump a new version only for this. There is a simple workaround in #138.

@JavaDerg
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx

Please sign in to comment.