Skip to content

Commit

Permalink
Fix up primitives/grid
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Mar 11, 2024
1 parent ffff860 commit 99bdbe7
Show file tree
Hide file tree
Showing 4 changed files with 329 additions and 91 deletions.
30 changes: 23 additions & 7 deletions crates/bevy_gizmos/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ use bevy_color::LinearRgba;
use bevy_math::{Quat, UVec2, Vec2, Vec3};

/// A builder returned by [`Gizmos::grid`] and [`Gizmos::grid_2d`]
pub struct GridBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
gizmos: &'a mut Gizmos<'w, 's, T>,
pub struct GridBuilder<'a, 'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,
position: Vec3,
rotation: Quat,
spacing: Vec2,
Expand All @@ -19,7 +23,11 @@ pub struct GridBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
color: LinearRgba,
}

impl<T: GizmoConfigGroup> GridBuilder<'_, '_, '_, T> {
impl<Config, Clear> GridBuilder<'_, '_, '_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
/// Skews the grid by `tan(skew)` in the x direction.
/// `skew` is in radians
pub fn skew_x(mut self, skew: f32) -> Self {
Expand Down Expand Up @@ -47,7 +55,11 @@ impl<T: GizmoConfigGroup> GridBuilder<'_, '_, '_, T> {
}
}

impl<T: GizmoConfigGroup> Drop for GridBuilder<'_, '_, '_, T> {
impl<Config, Clear> Drop for GridBuilder<'_, '_, '_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
/// Draws a grid, by drawing lines with the stored [`Gizmos`]
fn drop(&mut self) {
if !self.gizmos.enabled {
Expand Down Expand Up @@ -103,7 +115,11 @@ impl<T: GizmoConfigGroup> Drop for GridBuilder<'_, '_, '_, T> {
}
}

impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
/// Draw a 2D grid in 3D.
///
/// This should be called for each frame the grid needs to be rendered.
Expand Down Expand Up @@ -147,7 +163,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
cell_count: UVec2,
spacing: Vec2,
color: impl Into<LinearRgba>,
) -> GridBuilder<'_, 'w, 's, T> {
) -> GridBuilder<'_, 'w, 's, Config, Clear> {
GridBuilder {
gizmos: self,
position,
Expand Down Expand Up @@ -203,7 +219,7 @@ impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
cell_count: UVec2,
spacing: Vec2,
color: impl Into<LinearRgba>,
) -> GridBuilder<'_, 'w, 's, T> {
) -> GridBuilder<'_, 'w, 's, Config, Clear> {
GridBuilder {
gizmos: self,
position: position.extend(0.),
Expand Down
130 changes: 104 additions & 26 deletions crates/bevy_gizmos/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ pub trait GizmoPrimitive2d<P: Primitive2d> {

// direction 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Dir2> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Dir2> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self : 'a;

fn primitive_2d(
Expand All @@ -62,7 +66,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Dir2> for Gizmos<'w, 's, T> {

// circle 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Circle> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Circle> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand All @@ -82,7 +90,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Circle> for Gizmos<'w, 's, T>

// ellipse 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Ellipse> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Ellipse> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand All @@ -102,7 +114,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Ellipse> for Gizmos<'w, 's, T

// capsule 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Capsule2d> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Capsule2d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand Down Expand Up @@ -168,8 +184,12 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Capsule2d> for Gizmos<'w, 's,
// line 2d
//
/// Builder for configuring the drawing options of [`Line2d`].
pub struct Line2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
gizmos: &'a mut Gizmos<'w, 's, T>,
pub struct Line2dBuilder<'a, 'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,

direction: Dir2, // Direction of the line

Expand All @@ -180,16 +200,24 @@ pub struct Line2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
draw_arrow: bool, // decides whether to indicate the direction of the line with an arrow
}

impl<T: GizmoConfigGroup> Line2dBuilder<'_, '_, '_, T> {
impl<Config, Clear> Line2dBuilder<'_, '_, '_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
/// Set the drawing mode of the line (arrow vs. plain line)
pub fn draw_arrow(mut self, is_enabled: bool) -> Self {
self.draw_arrow = is_enabled;
self
}
}

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Line2d> for Gizmos<'w, 's, T> {
type Output<'a> = Line2dBuilder<'a, 'w, 's, T> where Self: 'a;
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Line2d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = Line2dBuilder<'a, 'w, 's, Config, Clear> where Self: 'a;

fn primitive_2d(
&mut self,
Expand All @@ -209,7 +237,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Line2d> for Gizmos<'w, 's, T>
}
}

impl<T: GizmoConfigGroup> Drop for Line2dBuilder<'_, '_, '_, T> {
impl<Config, Clear> Drop for Line2dBuilder<'_, '_, '_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
if !self.gizmos.enabled {
return;
Expand Down Expand Up @@ -239,7 +271,11 @@ impl<T: GizmoConfigGroup> Drop for Line2dBuilder<'_, '_, '_, T> {

// plane 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Plane2d> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Plane2d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand Down Expand Up @@ -289,8 +325,12 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Plane2d> for Gizmos<'w, 's, T
// segment 2d

/// Builder for configuring the drawing options of [`Segment2d`].
pub struct Segment2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
gizmos: &'a mut Gizmos<'w, 's, T>,
pub struct Segment2dBuilder<'a, 'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
gizmos: &'a mut Gizmos<'w, 's, Config, Clear>,

direction: Dir2, // Direction of the line segment
half_length: f32, // Half-length of the line segment
Expand All @@ -302,16 +342,24 @@ pub struct Segment2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
draw_arrow: bool, // decides whether to draw just a line or an arrow
}

impl<T: GizmoConfigGroup> Segment2dBuilder<'_, '_, '_, T> {
impl<Config, Clear> Segment2dBuilder<'_, '_, '_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
/// Set the drawing mode of the line (arrow vs. plain line)
pub fn draw_arrow(mut self, is_enabled: bool) -> Self {
self.draw_arrow = is_enabled;
self
}
}

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Segment2d> for Gizmos<'w, 's, T> {
type Output<'a> = Segment2dBuilder<'a, 'w, 's, T> where Self: 'a;
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Segment2d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = Segment2dBuilder<'a, 'w, 's, Config, Clear> where Self: 'a;

fn primitive_2d(
&mut self,
Expand All @@ -334,7 +382,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Segment2d> for Gizmos<'w, 's,
}
}

impl<T: GizmoConfigGroup> Drop for Segment2dBuilder<'_, '_, '_, T> {
impl<Config, Clear> Drop for Segment2dBuilder<'_, '_, '_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
if !self.gizmos.enabled {
return;
Expand All @@ -354,8 +406,11 @@ impl<T: GizmoConfigGroup> Drop for Segment2dBuilder<'_, '_, '_, T> {

// polyline 2d

impl<'w, 's, const N: usize, T: GizmoConfigGroup> GizmoPrimitive2d<Polyline2d<N>>
for Gizmos<'w, 's, T>
impl<'w, 's, const N: usize, Config, Clear> GizmoPrimitive2d<Polyline2d<N>>
for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

Expand Down Expand Up @@ -383,7 +438,11 @@ impl<'w, 's, const N: usize, T: GizmoConfigGroup> GizmoPrimitive2d<Polyline2d<N>

// boxed polyline 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<BoxedPolyline2d> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<BoxedPolyline2d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand All @@ -410,7 +469,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<BoxedPolyline2d> for Gizmos<'

// triangle 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Triangle2d> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Triangle2d> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand All @@ -431,7 +494,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Triangle2d> for Gizmos<'w, 's

// rectangle 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Rectangle> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<Rectangle> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand Down Expand Up @@ -459,8 +526,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<Rectangle> for Gizmos<'w, 's,

// polygon 2d

impl<'w, 's, const N: usize, T: GizmoConfigGroup> GizmoPrimitive2d<Polygon<N>>
for Gizmos<'w, 's, T>
impl<'w, 's, const N: usize, Config, Clear> GizmoPrimitive2d<Polygon<N>>
for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

Expand Down Expand Up @@ -498,7 +568,11 @@ impl<'w, 's, const N: usize, T: GizmoConfigGroup> GizmoPrimitive2d<Polygon<N>>

// boxed polygon 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<BoxedPolygon> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<BoxedPolygon> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand Down Expand Up @@ -533,7 +607,11 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<BoxedPolygon> for Gizmos<'w,

// regular polygon 2d

impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive2d<RegularPolygon> for Gizmos<'w, 's, T> {
impl<'w, 's, Config, Clear> GizmoPrimitive2d<RegularPolygon> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
Expand Down
Loading

0 comments on commit 99bdbe7

Please sign in to comment.