Skip to content

Commit 04ab9a0

Browse files
Move Circle Gizmos to Their Own File (#10631)
## Objective - Give all the intuitive groups of gizmos their own file - don't be a breaking change - don't change Gizmos interface - eventually do arcs too - future types of gizmos should be in their own files - see also #9400 ## Solution - Moved `gizmos.circle`, `gizmos.2d_circle`, and assorted helpers into `circles.rs` - Can also do arcs in this MR if y'all want; just figured I should do one thing at a time. ## Changelog - Changed - `gizmos::CircleBuilder` moved to `gizmos::circles::Circle2dBuilder` - `gizmos::Circle2dBuilder` moved to `gizmos::circles::Circle2dBuilder` ## Migration Guide - change `gizmos::CircleBuilder` to `gizmos::circles::Circle2dBuilder` - change `gizmos::Circle2dBuilder` to `gizmos::circles::Circle2dBuilder` --------- Co-authored-by: François <[email protected]>
1 parent e1c8d60 commit 04ab9a0

File tree

4 files changed

+153
-137
lines changed

4 files changed

+153
-137
lines changed

crates/bevy_gizmos/src/arrows.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
//! Additional Gizmo Functions -- Arrows
1+
//! Additional [`Gizmos`] Functions -- Arrows
2+
//!
3+
//! Includes the implementation of [`Gizmos::arrow`] and [`Gizmos::arrow_2d`],
4+
//! and assorted support items.
25
36
use crate::prelude::Gizmos;
47
use bevy_math::{Quat, Vec2, Vec3};
58
use bevy_render::color::Color;
69

10+
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
711
pub struct ArrowBuilder<'a, 's> {
812
gizmos: &'a mut Gizmos<'s>,
913
start: Vec3,
@@ -12,7 +16,6 @@ pub struct ArrowBuilder<'a, 's> {
1216
tip_length: f32,
1317
}
1418

15-
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
1619
impl ArrowBuilder<'_, '_> {
1720
/// Change the length of the tips to be `length`.
1821
/// The default tip length is [length of the arrow]/10.

crates/bevy_gizmos/src/circles.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//! Additional [`Gizmos`] Functions -- Circles
2+
//!
3+
//! Includes the implementation of [`Gizmos::circle`] and [`Gizmos::circle_2d`],
4+
//! and assorted support items.
5+
6+
use crate::prelude::Gizmos;
7+
use bevy_math::{Quat, Vec2, Vec3};
8+
use bevy_render::color::Color;
9+
use std::f32::consts::TAU;
10+
11+
pub(crate) const DEFAULT_CIRCLE_SEGMENTS: usize = 32;
12+
13+
fn circle_inner(radius: f32, segments: usize) -> impl Iterator<Item = Vec2> {
14+
(0..segments + 1).map(move |i| {
15+
let angle = i as f32 * TAU / segments as f32;
16+
Vec2::from(angle.sin_cos()) * radius
17+
})
18+
}
19+
20+
impl<'s> Gizmos<'s> {
21+
/// Draw a circle in 3D at `position` with the flat side facing `normal`.
22+
///
23+
/// This should be called for each frame the circle needs to be rendered.
24+
///
25+
/// # Example
26+
/// ```
27+
/// # use bevy_gizmos::prelude::*;
28+
/// # use bevy_render::prelude::*;
29+
/// # use bevy_math::prelude::*;
30+
/// fn system(mut gizmos: Gizmos) {
31+
/// gizmos.circle(Vec3::ZERO, Vec3::Z, 1., Color::GREEN);
32+
///
33+
/// // Circles have 32 line-segments by default.
34+
/// // You may want to increase this for larger circles.
35+
/// gizmos
36+
/// .circle(Vec3::ZERO, Vec3::Z, 5., Color::RED)
37+
/// .segments(64);
38+
/// }
39+
/// # bevy_ecs::system::assert_is_system(system);
40+
/// ```
41+
#[inline]
42+
pub fn circle(
43+
&mut self,
44+
position: Vec3,
45+
normal: Vec3,
46+
radius: f32,
47+
color: Color,
48+
) -> CircleBuilder<'_, 's> {
49+
CircleBuilder {
50+
gizmos: self,
51+
position,
52+
normal,
53+
radius,
54+
color,
55+
segments: DEFAULT_CIRCLE_SEGMENTS,
56+
}
57+
}
58+
59+
/// Draw a circle in 2D.
60+
///
61+
/// This should be called for each frame the circle needs to be rendered.
62+
///
63+
/// # Example
64+
/// ```
65+
/// # use bevy_gizmos::prelude::*;
66+
/// # use bevy_render::prelude::*;
67+
/// # use bevy_math::prelude::*;
68+
/// fn system(mut gizmos: Gizmos) {
69+
/// gizmos.circle_2d(Vec2::ZERO, 1., Color::GREEN);
70+
///
71+
/// // Circles have 32 line-segments by default.
72+
/// // You may want to increase this for larger circles.
73+
/// gizmos
74+
/// .circle_2d(Vec2::ZERO, 5., Color::RED)
75+
/// .segments(64);
76+
/// }
77+
/// # bevy_ecs::system::assert_is_system(system);
78+
/// ```
79+
#[inline]
80+
pub fn circle_2d(
81+
&mut self,
82+
position: Vec2,
83+
radius: f32,
84+
color: Color,
85+
) -> Circle2dBuilder<'_, 's> {
86+
Circle2dBuilder {
87+
gizmos: self,
88+
position,
89+
radius,
90+
color,
91+
segments: DEFAULT_CIRCLE_SEGMENTS,
92+
}
93+
}
94+
}
95+
96+
/// A builder returned by [`Gizmos::circle`].
97+
pub struct CircleBuilder<'a, 's> {
98+
gizmos: &'a mut Gizmos<'s>,
99+
position: Vec3,
100+
normal: Vec3,
101+
radius: f32,
102+
color: Color,
103+
segments: usize,
104+
}
105+
106+
impl CircleBuilder<'_, '_> {
107+
/// Set the number of line-segments for this circle.
108+
pub fn segments(mut self, segments: usize) -> Self {
109+
self.segments = segments;
110+
self
111+
}
112+
}
113+
114+
impl Drop for CircleBuilder<'_, '_> {
115+
fn drop(&mut self) {
116+
let rotation = Quat::from_rotation_arc(Vec3::Z, self.normal);
117+
let positions = circle_inner(self.radius, self.segments)
118+
.map(|vec2| (self.position + rotation * vec2.extend(0.)));
119+
self.gizmos.linestrip(positions, self.color);
120+
}
121+
}
122+
123+
/// A builder returned by [`Gizmos::circle_2d`].
124+
pub struct Circle2dBuilder<'a, 's> {
125+
gizmos: &'a mut Gizmos<'s>,
126+
position: Vec2,
127+
radius: f32,
128+
color: Color,
129+
segments: usize,
130+
}
131+
132+
impl Circle2dBuilder<'_, '_> {
133+
/// Set the number of line-segments for this circle.
134+
pub fn segments(mut self, segments: usize) -> Self {
135+
self.segments = segments;
136+
self
137+
}
138+
}
139+
140+
impl Drop for Circle2dBuilder<'_, '_> {
141+
fn drop(&mut self) {
142+
let positions = circle_inner(self.radius, self.segments).map(|vec2| (vec2 + self.position));
143+
self.gizmos.linestrip_2d(positions, self.color);
144+
}
145+
}

crates/bevy_gizmos/src/gizmos.rs

Lines changed: 1 addition & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::{f32::consts::TAU, iter};
44

5+
use crate::circles::DEFAULT_CIRCLE_SEGMENTS;
56
use bevy_ecs::{
67
system::{Deferred, Resource, SystemBuffer, SystemMeta, SystemParam},
78
world::World,
@@ -13,8 +14,6 @@ use bevy_transform::TransformPoint;
1314
type PositionItem = [f32; 3];
1415
type ColorItem = [f32; 4];
1516

16-
const DEFAULT_CIRCLE_SEGMENTS: usize = 32;
17-
1817
#[derive(Resource, Default)]
1918
pub(crate) struct GizmoStorage {
2019
pub list_positions: Vec<PositionItem>,
@@ -201,44 +200,6 @@ impl<'s> Gizmos<'s> {
201200
strip_colors.push([f32::NAN; 4]);
202201
}
203202

204-
/// Draw a circle in 3D at `position` with the flat side facing `normal`.
205-
///
206-
/// This should be called for each frame the circle needs to be rendered.
207-
///
208-
/// # Example
209-
/// ```
210-
/// # use bevy_gizmos::prelude::*;
211-
/// # use bevy_render::prelude::*;
212-
/// # use bevy_math::prelude::*;
213-
/// fn system(mut gizmos: Gizmos) {
214-
/// gizmos.circle(Vec3::ZERO, Vec3::Z, 1., Color::GREEN);
215-
///
216-
/// // Circles have 32 line-segments by default.
217-
/// // You may want to increase this for larger circles.
218-
/// gizmos
219-
/// .circle(Vec3::ZERO, Vec3::Z, 5., Color::RED)
220-
/// .segments(64);
221-
/// }
222-
/// # bevy_ecs::system::assert_is_system(system);
223-
/// ```
224-
#[inline]
225-
pub fn circle(
226-
&mut self,
227-
position: Vec3,
228-
normal: Vec3,
229-
radius: f32,
230-
color: Color,
231-
) -> CircleBuilder<'_, 's> {
232-
CircleBuilder {
233-
gizmos: self,
234-
position,
235-
normal,
236-
radius,
237-
color,
238-
segments: DEFAULT_CIRCLE_SEGMENTS,
239-
}
240-
}
241-
242203
/// Draw a wireframe sphere in 3D made out of 3 circles around the axes.
243204
///
244205
/// This should be called for each frame the sphere needs to be rendered.
@@ -466,42 +427,6 @@ impl<'s> Gizmos<'s> {
466427
self.line_gradient_2d(start, start + vector, start_color, end_color);
467428
}
468429

469-
/// Draw a circle in 2D.
470-
///
471-
/// This should be called for each frame the circle needs to be rendered.
472-
///
473-
/// # Example
474-
/// ```
475-
/// # use bevy_gizmos::prelude::*;
476-
/// # use bevy_render::prelude::*;
477-
/// # use bevy_math::prelude::*;
478-
/// fn system(mut gizmos: Gizmos) {
479-
/// gizmos.circle_2d(Vec2::ZERO, 1., Color::GREEN);
480-
///
481-
/// // Circles have 32 line-segments by default.
482-
/// // You may want to increase this for larger circles.
483-
/// gizmos
484-
/// .circle_2d(Vec2::ZERO, 5., Color::RED)
485-
/// .segments(64);
486-
/// }
487-
/// # bevy_ecs::system::assert_is_system(system);
488-
/// ```
489-
#[inline]
490-
pub fn circle_2d(
491-
&mut self,
492-
position: Vec2,
493-
radius: f32,
494-
color: Color,
495-
) -> Circle2dBuilder<'_, 's> {
496-
Circle2dBuilder {
497-
gizmos: self,
498-
position,
499-
radius,
500-
color,
501-
segments: DEFAULT_CIRCLE_SEGMENTS,
502-
}
503-
}
504-
505430
/// Draw an arc, which is a part of the circumference of a circle, in 2D.
506431
///
507432
/// This should be called for each frame the arc needs to be rendered.
@@ -603,33 +528,6 @@ impl<'s> Gizmos<'s> {
603528
}
604529
}
605530

606-
/// A builder returned by [`Gizmos::circle`].
607-
pub struct CircleBuilder<'a, 's> {
608-
gizmos: &'a mut Gizmos<'s>,
609-
position: Vec3,
610-
normal: Vec3,
611-
radius: f32,
612-
color: Color,
613-
segments: usize,
614-
}
615-
616-
impl CircleBuilder<'_, '_> {
617-
/// Set the number of line-segments for this circle.
618-
pub fn segments(mut self, segments: usize) -> Self {
619-
self.segments = segments;
620-
self
621-
}
622-
}
623-
624-
impl Drop for CircleBuilder<'_, '_> {
625-
fn drop(&mut self) {
626-
let rotation = Quat::from_rotation_arc(Vec3::Z, self.normal);
627-
let positions = circle_inner(self.radius, self.segments)
628-
.map(|vec2| (self.position + rotation * vec2.extend(0.)));
629-
self.gizmos.linestrip(positions, self.color);
630-
}
631-
}
632-
633531
/// A builder returned by [`Gizmos::sphere`].
634532
pub struct SphereBuilder<'a, 's> {
635533
gizmos: &'a mut Gizmos<'s>,
@@ -658,30 +556,6 @@ impl Drop for SphereBuilder<'_, '_> {
658556
}
659557
}
660558

661-
/// A builder returned by [`Gizmos::circle_2d`].
662-
pub struct Circle2dBuilder<'a, 's> {
663-
gizmos: &'a mut Gizmos<'s>,
664-
position: Vec2,
665-
radius: f32,
666-
color: Color,
667-
segments: usize,
668-
}
669-
670-
impl Circle2dBuilder<'_, '_> {
671-
/// Set the number of line-segments for this circle.
672-
pub fn segments(mut self, segments: usize) -> Self {
673-
self.segments = segments;
674-
self
675-
}
676-
}
677-
678-
impl Drop for Circle2dBuilder<'_, '_> {
679-
fn drop(&mut self) {
680-
let positions = circle_inner(self.radius, self.segments).map(|vec2| (vec2 + self.position));
681-
self.gizmos.linestrip_2d(positions, self.color);
682-
}
683-
}
684-
685559
/// A builder returned by [`Gizmos::arc_2d`].
686560
pub struct Arc2dBuilder<'a, 's> {
687561
gizmos: &'a mut Gizmos<'s>,
@@ -730,13 +604,6 @@ fn arc_inner(
730604
})
731605
}
732606

733-
fn circle_inner(radius: f32, segments: usize) -> impl Iterator<Item = Vec2> {
734-
(0..segments + 1).map(move |i| {
735-
let angle = i as f32 * TAU / segments as f32;
736-
Vec2::from(angle.sin_cos()) * radius
737-
})
738-
}
739-
740607
fn rect_inner(size: Vec2) -> [Vec2; 4] {
741608
let half_size = size / 2.;
742609
let tl = Vec2::new(-half_size.x, half_size.y);

crates/bevy_gizmos/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
//!
1616
//! See the documentation on [`Gizmos`] for more examples.
1717
18-
mod arrows;
18+
pub mod arrows;
19+
pub mod circles;
1920
pub mod gizmos;
2021

2122
#[cfg(feature = "bevy_sprite")]

0 commit comments

Comments
 (0)