Skip to content

Commit

Permalink
Contextual clearing gizmos
Browse files Browse the repository at this point in the history
Fix bug of strips getting extended by list

Document GizmoStorage

Don't import bevy_internal

Formatting

Move clearing the gizmos to FixedLast

Fix some ordering issues with clearing/collecting

Rename Context to Clear

Formatting

Use mem::swap for changing clear context contents

Revert examples

Improve documentation, explain how to setup a custom context

Hide Swap buffer, improve documentation for update_gizmo_meshes

Reword again

more rewording
  • Loading branch information
Aceeri committed Feb 29, 2024
1 parent ca1874e commit ad64888
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 51 deletions.
1 change: 1 addition & 0 deletions crates/bevy_gizmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ bevy_core = { path = "../bevy_core", version = "0.12.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.12.0" }
bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.12.0" }
bevy_transform = { path = "../bevy_transform", version = "0.12.0" }
bevy_time = { path = "../bevy_time", version = "0.12.0" }

[lints]
workspace = true
24 changes: 18 additions & 6 deletions crates/bevy_gizmos/src/arcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use bevy_math::Vec2;
use bevy_render::color::Color;
use std::f32::consts::TAU;

impl<'s> Gizmos<'s> {
impl<'s, Clear> Gizmos<'s, Clear>
where
Clear: 'static + Send + Sync,
{
/// Draw an arc, which is a part of the circumference of a circle, in 2D.
///
/// This should be called for each frame the arc needs to be rendered.
Expand Down Expand Up @@ -46,7 +49,7 @@ impl<'s> Gizmos<'s> {
arc_angle: f32,
radius: f32,
color: Color,
) -> Arc2dBuilder<'_, 's> {
) -> Arc2dBuilder<'_, 's, Clear> {
Arc2dBuilder {
gizmos: self,
position,
Expand All @@ -60,8 +63,11 @@ impl<'s> Gizmos<'s> {
}

/// A builder returned by [`Gizmos::arc_2d`].
pub struct Arc2dBuilder<'a, 's> {
gizmos: &'a mut Gizmos<'s>,
pub struct Arc2dBuilder<'a, 's, Clear>
where
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'s, Clear>,
position: Vec2,
direction_angle: f32,
arc_angle: f32,
Expand All @@ -70,15 +76,21 @@ pub struct Arc2dBuilder<'a, 's> {
segments: Option<usize>,
}

impl Arc2dBuilder<'_, '_> {
impl<Clear> Arc2dBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
/// Set the number of line-segments for this arc.
pub fn segments(mut self, segments: usize) -> Self {
self.segments = Some(segments);
self
}
}

impl Drop for Arc2dBuilder<'_, '_> {
impl<Clear> Drop for Arc2dBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
let segments = match self.segments {
Some(segments) => segments,
Expand Down
31 changes: 24 additions & 7 deletions crates/bevy_gizmos/src/arrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ use bevy_math::{Quat, Vec2, Vec3};
use bevy_render::color::Color;

/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
pub struct ArrowBuilder<'a, 's> {
gizmos: &'a mut Gizmos<'s>,
pub struct ArrowBuilder<'a, 's, Clear>
where
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'s, Clear>,
start: Vec3,
end: Vec3,
color: Color,
tip_length: f32,
}

impl ArrowBuilder<'_, '_> {
impl<Clear> ArrowBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
/// Change the length of the tips to be `length`.
/// The default tip length is [length of the arrow]/10.
///
Expand All @@ -37,7 +43,10 @@ impl ArrowBuilder<'_, '_> {
}
}

impl Drop for ArrowBuilder<'_, '_> {
impl<Clear> Drop for ArrowBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
/// Draws the arrow, by drawing lines with the stored [`Gizmos`]
fn drop(&mut self) {
// first, draw the body of the arrow
Expand All @@ -63,7 +72,10 @@ impl Drop for ArrowBuilder<'_, '_> {
}
}

impl<'s> Gizmos<'s> {
impl<'s, Clear> Gizmos<'s, Clear>
where
Clear: 'static + Send + Sync,
{
/// Draw an arrow in 3D, from `start` to `end`. Has four tips for convienent viewing from any direction.
///
/// This should be called for each frame the arrow needs to be rendered.
Expand All @@ -78,7 +90,7 @@ impl<'s> Gizmos<'s> {
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's> {
pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's, Clear> {
let length = (end - start).length();
ArrowBuilder {
gizmos: self,
Expand All @@ -103,7 +115,12 @@ impl<'s> Gizmos<'s> {
/// }
/// # bevy_ecs::system::assert_is_system(system);
/// ```
pub fn arrow_2d(&mut self, start: Vec2, end: Vec2, color: Color) -> ArrowBuilder<'_, 's> {
pub fn arrow_2d(
&mut self,
start: Vec2,
end: Vec2,
color: Color,
) -> ArrowBuilder<'_, 's, Clear> {
self.arrow(start.extend(0.), end.extend(0.), color)
}
}
43 changes: 32 additions & 11 deletions crates/bevy_gizmos/src/circles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ fn circle_inner(radius: f32, segments: usize) -> impl Iterator<Item = Vec2> {
})
}

impl<'s> Gizmos<'s> {
impl<'s, Clear> Gizmos<'s, Clear>
where
Clear: 'static + Send + Sync,
{
/// Draw a circle in 3D at `position` with the flat side facing `normal`.
///
/// This should be called for each frame the circle needs to be rendered.
Expand Down Expand Up @@ -45,7 +48,7 @@ impl<'s> Gizmos<'s> {
normal: Vec3,
radius: f32,
color: Color,
) -> CircleBuilder<'_, 's> {
) -> CircleBuilder<'_, 's, Clear> {
CircleBuilder {
gizmos: self,
position,
Expand Down Expand Up @@ -82,7 +85,7 @@ impl<'s> Gizmos<'s> {
position: Vec2,
radius: f32,
color: Color,
) -> Circle2dBuilder<'_, 's> {
) -> Circle2dBuilder<'_, 's, Clear> {
Circle2dBuilder {
gizmos: self,
position,
Expand All @@ -94,24 +97,33 @@ impl<'s> Gizmos<'s> {
}

/// A builder returned by [`Gizmos::circle`].
pub struct CircleBuilder<'a, 's> {
gizmos: &'a mut Gizmos<'s>,
pub struct CircleBuilder<'a, 's, Clear>
where
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'s, Clear>,
position: Vec3,
normal: Vec3,
radius: f32,
color: Color,
segments: usize,
}

impl CircleBuilder<'_, '_> {
impl<Clear> CircleBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
/// Set the number of line-segments for this circle.
pub fn segments(mut self, segments: usize) -> Self {
self.segments = segments;
self
}
}

impl Drop for CircleBuilder<'_, '_> {
impl<Clear> Drop for CircleBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
let rotation = Quat::from_rotation_arc(Vec3::Z, self.normal);
let positions = circle_inner(self.radius, self.segments)
Expand All @@ -121,23 +133,32 @@ impl Drop for CircleBuilder<'_, '_> {
}

/// A builder returned by [`Gizmos::circle_2d`].
pub struct Circle2dBuilder<'a, 's> {
gizmos: &'a mut Gizmos<'s>,
pub struct Circle2dBuilder<'a, 's, Clear>
where
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'s, Clear>,
position: Vec2,
radius: f32,
color: Color,
segments: usize,
}

impl Circle2dBuilder<'_, '_> {
impl<Clear> Circle2dBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
/// Set the number of line-segments for this circle.
pub fn segments(mut self, segments: usize) -> Self {
self.segments = segments;
self
}
}

impl Drop for Circle2dBuilder<'_, '_> {
impl<Clear> Drop for Circle2dBuilder<'_, '_, Clear>
where
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
let positions = circle_inner(self.radius, self.segments).map(|vec2| (vec2 + self.position));
self.gizmos.linestrip_2d(positions, self.color);
Expand Down
Loading

0 comments on commit ad64888

Please sign in to comment.