Skip to content

Commit ad64888

Browse files
committed
Contextual clearing gizmos
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
1 parent ca1874e commit ad64888

File tree

6 files changed

+301
-51
lines changed

6 files changed

+301
-51
lines changed

crates/bevy_gizmos/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ bevy_core = { path = "../bevy_core", version = "0.12.0" }
2525
bevy_reflect = { path = "../bevy_reflect", version = "0.12.0" }
2626
bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.12.0" }
2727
bevy_transform = { path = "../bevy_transform", version = "0.12.0" }
28+
bevy_time = { path = "../bevy_time", version = "0.12.0" }
2829

2930
[lints]
3031
workspace = true

crates/bevy_gizmos/src/arcs.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use bevy_math::Vec2;
99
use bevy_render::color::Color;
1010
use std::f32::consts::TAU;
1111

12-
impl<'s> Gizmos<'s> {
12+
impl<'s, Clear> Gizmos<'s, Clear>
13+
where
14+
Clear: 'static + Send + Sync,
15+
{
1316
/// Draw an arc, which is a part of the circumference of a circle, in 2D.
1417
///
1518
/// This should be called for each frame the arc needs to be rendered.
@@ -46,7 +49,7 @@ impl<'s> Gizmos<'s> {
4649
arc_angle: f32,
4750
radius: f32,
4851
color: Color,
49-
) -> Arc2dBuilder<'_, 's> {
52+
) -> Arc2dBuilder<'_, 's, Clear> {
5053
Arc2dBuilder {
5154
gizmos: self,
5255
position,
@@ -60,8 +63,11 @@ impl<'s> Gizmos<'s> {
6063
}
6164

6265
/// A builder returned by [`Gizmos::arc_2d`].
63-
pub struct Arc2dBuilder<'a, 's> {
64-
gizmos: &'a mut Gizmos<'s>,
66+
pub struct Arc2dBuilder<'a, 's, Clear>
67+
where
68+
Clear: 'static + Send + Sync,
69+
{
70+
gizmos: &'a mut Gizmos<'s, Clear>,
6571
position: Vec2,
6672
direction_angle: f32,
6773
arc_angle: f32,
@@ -70,15 +76,21 @@ pub struct Arc2dBuilder<'a, 's> {
7076
segments: Option<usize>,
7177
}
7278

73-
impl Arc2dBuilder<'_, '_> {
79+
impl<Clear> Arc2dBuilder<'_, '_, Clear>
80+
where
81+
Clear: 'static + Send + Sync,
82+
{
7483
/// Set the number of line-segments for this arc.
7584
pub fn segments(mut self, segments: usize) -> Self {
7685
self.segments = Some(segments);
7786
self
7887
}
7988
}
8089

81-
impl Drop for Arc2dBuilder<'_, '_> {
90+
impl<Clear> Drop for Arc2dBuilder<'_, '_, Clear>
91+
where
92+
Clear: 'static + Send + Sync,
93+
{
8294
fn drop(&mut self) {
8395
let segments = match self.segments {
8496
Some(segments) => segments,

crates/bevy_gizmos/src/arrows.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ use bevy_math::{Quat, Vec2, Vec3};
88
use bevy_render::color::Color;
99

1010
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
11-
pub struct ArrowBuilder<'a, 's> {
12-
gizmos: &'a mut Gizmos<'s>,
11+
pub struct ArrowBuilder<'a, 's, Clear>
12+
where
13+
Clear: 'static + Send + Sync,
14+
{
15+
gizmos: &'a mut Gizmos<'s, Clear>,
1316
start: Vec3,
1417
end: Vec3,
1518
color: Color,
1619
tip_length: f32,
1720
}
1821

19-
impl ArrowBuilder<'_, '_> {
22+
impl<Clear> ArrowBuilder<'_, '_, Clear>
23+
where
24+
Clear: 'static + Send + Sync,
25+
{
2026
/// Change the length of the tips to be `length`.
2127
/// The default tip length is [length of the arrow]/10.
2228
///
@@ -37,7 +43,10 @@ impl ArrowBuilder<'_, '_> {
3743
}
3844
}
3945

40-
impl Drop for ArrowBuilder<'_, '_> {
46+
impl<Clear> Drop for ArrowBuilder<'_, '_, Clear>
47+
where
48+
Clear: 'static + Send + Sync,
49+
{
4150
/// Draws the arrow, by drawing lines with the stored [`Gizmos`]
4251
fn drop(&mut self) {
4352
// first, draw the body of the arrow
@@ -63,7 +72,10 @@ impl Drop for ArrowBuilder<'_, '_> {
6372
}
6473
}
6574

66-
impl<'s> Gizmos<'s> {
75+
impl<'s, Clear> Gizmos<'s, Clear>
76+
where
77+
Clear: 'static + Send + Sync,
78+
{
6779
/// Draw an arrow in 3D, from `start` to `end`. Has four tips for convienent viewing from any direction.
6880
///
6981
/// This should be called for each frame the arrow needs to be rendered.
@@ -78,7 +90,7 @@ impl<'s> Gizmos<'s> {
7890
/// }
7991
/// # bevy_ecs::system::assert_is_system(system);
8092
/// ```
81-
pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's> {
93+
pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 's, Clear> {
8294
let length = (end - start).length();
8395
ArrowBuilder {
8496
gizmos: self,
@@ -103,7 +115,12 @@ impl<'s> Gizmos<'s> {
103115
/// }
104116
/// # bevy_ecs::system::assert_is_system(system);
105117
/// ```
106-
pub fn arrow_2d(&mut self, start: Vec2, end: Vec2, color: Color) -> ArrowBuilder<'_, 's> {
118+
pub fn arrow_2d(
119+
&mut self,
120+
start: Vec2,
121+
end: Vec2,
122+
color: Color,
123+
) -> ArrowBuilder<'_, 's, Clear> {
107124
self.arrow(start.extend(0.), end.extend(0.), color)
108125
}
109126
}

crates/bevy_gizmos/src/circles.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ fn circle_inner(radius: f32, segments: usize) -> impl Iterator<Item = Vec2> {
1717
})
1818
}
1919

20-
impl<'s> Gizmos<'s> {
20+
impl<'s, Clear> Gizmos<'s, Clear>
21+
where
22+
Clear: 'static + Send + Sync,
23+
{
2124
/// Draw a circle in 3D at `position` with the flat side facing `normal`.
2225
///
2326
/// This should be called for each frame the circle needs to be rendered.
@@ -45,7 +48,7 @@ impl<'s> Gizmos<'s> {
4548
normal: Vec3,
4649
radius: f32,
4750
color: Color,
48-
) -> CircleBuilder<'_, 's> {
51+
) -> CircleBuilder<'_, 's, Clear> {
4952
CircleBuilder {
5053
gizmos: self,
5154
position,
@@ -82,7 +85,7 @@ impl<'s> Gizmos<'s> {
8285
position: Vec2,
8386
radius: f32,
8487
color: Color,
85-
) -> Circle2dBuilder<'_, 's> {
88+
) -> Circle2dBuilder<'_, 's, Clear> {
8689
Circle2dBuilder {
8790
gizmos: self,
8891
position,
@@ -94,24 +97,33 @@ impl<'s> Gizmos<'s> {
9497
}
9598

9699
/// A builder returned by [`Gizmos::circle`].
97-
pub struct CircleBuilder<'a, 's> {
98-
gizmos: &'a mut Gizmos<'s>,
100+
pub struct CircleBuilder<'a, 's, Clear>
101+
where
102+
Clear: 'static + Send + Sync,
103+
{
104+
gizmos: &'a mut Gizmos<'s, Clear>,
99105
position: Vec3,
100106
normal: Vec3,
101107
radius: f32,
102108
color: Color,
103109
segments: usize,
104110
}
105111

106-
impl CircleBuilder<'_, '_> {
112+
impl<Clear> CircleBuilder<'_, '_, Clear>
113+
where
114+
Clear: 'static + Send + Sync,
115+
{
107116
/// Set the number of line-segments for this circle.
108117
pub fn segments(mut self, segments: usize) -> Self {
109118
self.segments = segments;
110119
self
111120
}
112121
}
113122

114-
impl Drop for CircleBuilder<'_, '_> {
123+
impl<Clear> Drop for CircleBuilder<'_, '_, Clear>
124+
where
125+
Clear: 'static + Send + Sync,
126+
{
115127
fn drop(&mut self) {
116128
let rotation = Quat::from_rotation_arc(Vec3::Z, self.normal);
117129
let positions = circle_inner(self.radius, self.segments)
@@ -121,23 +133,32 @@ impl Drop for CircleBuilder<'_, '_> {
121133
}
122134

123135
/// A builder returned by [`Gizmos::circle_2d`].
124-
pub struct Circle2dBuilder<'a, 's> {
125-
gizmos: &'a mut Gizmos<'s>,
136+
pub struct Circle2dBuilder<'a, 's, Clear>
137+
where
138+
Clear: 'static + Send + Sync,
139+
{
140+
gizmos: &'a mut Gizmos<'s, Clear>,
126141
position: Vec2,
127142
radius: f32,
128143
color: Color,
129144
segments: usize,
130145
}
131146

132-
impl Circle2dBuilder<'_, '_> {
147+
impl<Clear> Circle2dBuilder<'_, '_, Clear>
148+
where
149+
Clear: 'static + Send + Sync,
150+
{
133151
/// Set the number of line-segments for this circle.
134152
pub fn segments(mut self, segments: usize) -> Self {
135153
self.segments = segments;
136154
self
137155
}
138156
}
139157

140-
impl Drop for Circle2dBuilder<'_, '_> {
158+
impl<Clear> Drop for Circle2dBuilder<'_, '_, Clear>
159+
where
160+
Clear: 'static + Send + Sync,
161+
{
141162
fn drop(&mut self) {
142163
let positions = circle_inner(self.radius, self.segments).map(|vec2| (vec2 + self.position));
143164
self.gizmos.linestrip_2d(positions, self.color);

0 commit comments

Comments
 (0)