Skip to content

Commit 1e5d238

Browse files
committed
Added some basic documentation
1 parent 827fb3b commit 1e5d238

File tree

4 files changed

+143
-14
lines changed

4 files changed

+143
-14
lines changed

examples/basic_usage.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use bevy::{
2+
prelude::*,
3+
};
4+
5+
use bevy_lyon::{
6+
shapes,
7+
LyonMeshBuilder,
8+
math
9+
};
10+
11+
fn main() {
12+
App::build()
13+
.add_default_plugins()
14+
.add_startup_system(setup_system.system())
15+
.run();
16+
}
17+
18+
fn setup_system(
19+
mut commands: Commands,
20+
mut materials: ResMut<Assets<ColorMaterial>>,
21+
mut meshes: ResMut<Assets<Mesh>>,
22+
) {
23+
let red = materials.add(Color::RED.into());
24+
let green = materials.add(Color::GREEN.into());
25+
let blue = materials.add(Color::BLUE.into());
26+
27+
let fill_circle = meshes.add(LyonMeshBuilder::with_only(
28+
shapes::FillCircle {
29+
center: math::point(-100.0, 0.0),
30+
..Default::default()
31+
}
32+
));
33+
34+
let stroke_circle = meshes.add(LyonMeshBuilder::with_only(
35+
shapes::StrokeCircle {
36+
center: math::point(-100.0, 0.0),
37+
radius: 35.0,
38+
..Default::default()
39+
}
40+
));
41+
42+
let ellipse = meshes.add(LyonMeshBuilder::with_only(
43+
shapes::StrokeEllipse {
44+
center: math::point(50.0, 25.0),
45+
..Default::default()
46+
}
47+
));
48+
49+
let convex_polyline = meshes.add(LyonMeshBuilder::with_only(
50+
shapes::FillConvexPolyline {
51+
points: vec![
52+
math::point(0.0, 0.0),
53+
math::point(25.0, 50.0),
54+
math::point(50.0, 0.0),
55+
math::point(50.0, -100.0),
56+
math::point(25.0, -150.0),
57+
math::point(0.0, -100.0)
58+
],
59+
..Default::default()
60+
}
61+
));
62+
63+
commands
64+
.spawn(Camera2dComponents::default())
65+
.spawn(SpriteComponents {
66+
mesh: fill_circle,
67+
material: red,
68+
sprite: Sprite { size: Vec2::new(1.0, 1.0) },
69+
..Default::default()
70+
})
71+
.spawn(SpriteComponents {
72+
mesh: stroke_circle,
73+
material: green,
74+
sprite: Sprite { size: Vec2::new(1.0, 1.0) },
75+
..Default::default()
76+
})
77+
.spawn(SpriteComponents {
78+
mesh: ellipse,
79+
material: red,
80+
sprite: Sprite { size: Vec2::new(1.0, 1.0) },
81+
..Default::default()
82+
})
83+
.spawn(SpriteComponents {
84+
mesh: convex_polyline,
85+
material: blue,
86+
sprite: Sprite { size: Vec2::new(1.0, 1.0) },
87+
translation: Translation::new(25.0, 75.0, 0.0),
88+
..Default::default()
89+
});
90+
91+
}

src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod mesh_builder;
2+
23
pub mod shapes;
34

45
#[doc(inline)]
@@ -15,3 +16,6 @@ pub use mesh_builder::{
1516

1617
LyonMeshBuilder,
1718
};
19+
20+
#[doc(no_inline)]
21+
pub use lyon::math;

src/mesh_builder.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//!
2+
//!
3+
//!
4+
//!
5+
//!
6+
//!
7+
//!
8+
9+
110
use bevy::render::{
211
mesh::{
312
VertexAttribute,
@@ -16,14 +25,14 @@ use lyon::{
1625

1726
use super::shapes::LyonShapeBuilder;
1827

19-
/// Type alias for the type of a mesh index in bevy.
28+
/// Type alias for the type of a mesh index in [`bevy`].
2029
pub type BevyIndex = u32;
21-
/// Type alias for a `VertexBuffers` of `BevyVertex`'s and `BevyIndex`'s.
30+
/// Type alias for a [`VertexBuffers`](tess::VertexBuffers) of [`BevyVertex`](BevyVertex)'s and [`BevyIndex`](BevyIndex)'s.
2231
pub type BevyVertexBuffers = tess::VertexBuffers<BevyVertex, BevyIndex>;
23-
/// Type alias for a buffer builder that contains the information to properly convert lyon points to `BevyVertex`'s and `BevyIndex`'s.
32+
/// Type alias for a [`BuffersBuilder`](tess::BuffersBuilder) that contains the information to properly convert [`lyon`] points to [`BevyVertex`]'s and [`BevyIndex`]'s.
2433
pub type BevyBuffersBuilder<'a> = tess::BuffersBuilder<'a, BevyVertex, BevyIndex, BevyVertexConstructor>;
2534

26-
/// Builder that provides customizable functionality to create `lyon` tessellated meshes and build them so `bevy` can consume them.
35+
/// Builder that provides customizable functionality to create [`lyon`](lyon) tessellated meshes and build them so [`bevy`](bevy) can consume them.
2736
#[derive(Debug, Clone)]
2837
pub struct LyonMeshBuilder
2938
{
@@ -42,15 +51,16 @@ impl LyonMeshBuilder
4251

4352
/// Finish the building and produce the final mesh.
4453
///
45-
/// Uses TriangleStrip as the default primitive topology.
54+
/// Uses [`TriangleStrip`](PrimitiveTopology::TriangleStrip) as the default primitive topology.
4655
pub fn build(self) -> Mesh
4756
{
57+
LyonMeshBuilder::
4858
self.build_with_topology(PrimitiveTopology::TriangleStrip)
4959
}
5060

51-
/// Finishes a mesh using a custom specified `PrimitiveTopology`.
61+
/// Finishes a mesh using a specific [`PrimitiveTopology`].
5262
///
53-
/// Prefer using `build()` as its default works in the vast majority of cases.
63+
/// Prefer using [`LyonMeshBuilder::build`] as its default topology works in the vast majority of cases.
5464
pub fn build_with_topology(self, topology: PrimitiveTopology) -> Mesh
5565
{
5666
Mesh {
@@ -60,15 +70,30 @@ impl LyonMeshBuilder
6070
}
6171
}
6272

63-
/// Adds a shape specified by its `LyonShapeBuilder` implementation to the mesh being constructed.
73+
/// Adds a shape specified by argument's [`LyonShapeBuilder`] implementation to the mesh being constructed.
6474
pub fn with(mut self, shape: impl LyonShapeBuilder) -> Self
6575
{
6676
shape.build(&mut self.buffers_builder());
6777
self
6878
}
6979

70-
/// A convenience function that makes a new `LyonMeshBuilder` and builds it with only the single shape provided.
71-
pub fn only(shape: impl LyonShapeBuilder) -> Mesh
80+
/// A convenience function that makes a new [`LyonMeshBuilder`] and builds it with only the single shape provided.
81+
///
82+
/// This is equivalent to calling:
83+
/// ```rust
84+
/// # use bevy::render::mesh::Mesh;
85+
/// # use bevy_lyon::{
86+
/// # LyonShapeBuilder,
87+
/// # LyonMeshBuilder
88+
/// # };
89+
/// # fn with_only_example(shape: impl LyonShapeBuilder) -> Mesh
90+
/// # {
91+
/// LyonMeshBuilder::new()
92+
/// .with(shape)
93+
/// .build()
94+
/// # }
95+
/// ```
96+
pub fn with_only(shape: impl LyonShapeBuilder) -> Mesh
7297
{
7398
LyonMeshBuilder::new()
7499
.with(shape)

src/shapes.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
//! Shapes for easily constructing basic meshes with [`LyonMeshBuilder`].
2+
//!
3+
//! # Overview
4+
//!
5+
//! This module provides a set of shapes consumable by the [`LyonMeshBuilder`] which draws some simple basic shapes.
6+
//! The shapes provided here match with the shapes that have simple tesselators provided by `lyon`.
7+
//!
8+
//! [`LyonMeshBuilder`]: crate::mesh_builder::LyonMeshBuilder
9+
110
use smart_default::*;
211

312
use lyon::{
@@ -6,19 +15,17 @@ use lyon::{
615
self as tess,
716
basic_shapes,
817
},
9-
self
1018
};
1119

1220
use super::mesh_builder::BevyBuffersBuilder;
1321

14-
/// Represents something capable of being built into a shape with the `LyonMeshBuilder`.
22+
/// Represents something capable of being built into a shape with the [`LyonMeshBuilder`](crate::mesh_builder::LyonMeshBuilder).
1523
pub trait LyonShapeBuilder
1624
{
1725
fn build(self, builder: &mut BevyBuffersBuilder);
1826
}
1927

20-
/// Allow all closures and functions that take in a mutable reference to a `BevyBuffersBuilder` to be considered a shape builder.
21-
///
28+
/// Allow all closures and functions that take in a mutable reference to a [`BevyBuffersBuilder`] to be considered a shape builder.
2229
/// Permits ergonomically using a closure (or function) for complicated custom meshes.
2330
impl<F> LyonShapeBuilder for F
2431
where
@@ -50,6 +57,7 @@ impl LyonShapeBuilder for FillCircle<'_>
5057
}
5158
}
5259

60+
/// Requires the points to represent a convex shape. If the shape is concave the result will likely be incorrect.
5361
#[derive(Debug, SmartDefault)]
5462
pub struct FillConvexPolyline<'a, I, G>
5563
where
@@ -223,6 +231,7 @@ where
223231
I: IntoIterator<Item=math::Point> + Default
224232
{
225233
pub points: I,
234+
#[default = true]
226235
pub is_closed: bool,
227236
#[default(&tess::StrokeOptions::DEFAULT)]
228237
pub options: &'a tess::StrokeOptions,

0 commit comments

Comments
 (0)