use core::f32::consts::FRAC_PI_2;
use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup};
use bevy_color::Color;
use bevy_math::{Isometry2d, Isometry3d, Quat, Vec2, Vec3};
use bevy_transform::components::Transform;
pub struct RoundedRectBuilder<'a, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
size: Vec2,
gizmos: &'a mut GizmoBuffer<Config, Clear>,
config: RoundedBoxConfig,
}
pub struct RoundedCuboidBuilder<'a, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
size: Vec3,
gizmos: &'a mut GizmoBuffer<Config, Clear>,
config: RoundedBoxConfig,
}
struct RoundedBoxConfig {
isometry: Isometry3d,
color: Color,
corner_radius: f32,
arc_resolution: u32,
}
impl<Config, Clear> RoundedRectBuilder<'_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
pub fn corner_radius(mut self, corner_radius: f32) -> Self {
self.config.corner_radius = corner_radius;
self
}
pub fn arc_resolution(mut self, arc_resolution: u32) -> Self {
self.config.arc_resolution = arc_resolution;
self
}
}
impl<Config, Clear> RoundedCuboidBuilder<'_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
pub fn edge_radius(mut self, edge_radius: f32) -> Self {
self.config.corner_radius = edge_radius;
self
}
pub fn arc_resolution(mut self, arc_resolution: u32) -> Self {
self.config.arc_resolution = arc_resolution;
self
}
}
impl<Config, Clear> Drop for RoundedRectBuilder<'_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
if !self.gizmos.enabled {
return;
}
let config = &self.config;
let mut outer_half_size = self.size.abs() / 2.0;
let inner_half_size =
(outer_half_size - Vec2::splat(config.corner_radius.abs())).max(Vec2::ZERO);
let corner_radius = (outer_half_size - inner_half_size).min_element();
let mut inner_half_size = outer_half_size - Vec2::splat(corner_radius);
if config.corner_radius < 0. {
core::mem::swap(&mut outer_half_size, &mut inner_half_size);
}
if outer_half_size.x * outer_half_size.y == 0. {
self.gizmos.line(
config.isometry * -outer_half_size.extend(0.),
config.isometry * outer_half_size.extend(0.),
config.color,
);
return;
}
if corner_radius == 0. {
self.gizmos.rect(config.isometry, self.size, config.color);
return;
}
let vertices = [
Vec3::new(inner_half_size.x, outer_half_size.y, 0.),
Vec3::new(inner_half_size.x, inner_half_size.y, 0.),
Vec3::new(outer_half_size.x, inner_half_size.y, 0.),
Vec3::new(outer_half_size.x, -inner_half_size.y, 0.),
Vec3::new(inner_half_size.x, -inner_half_size.y, 0.),
Vec3::new(inner_half_size.x, -outer_half_size.y, 0.),
Vec3::new(-inner_half_size.x, -outer_half_size.y, 0.),
Vec3::new(-inner_half_size.x, -inner_half_size.y, 0.),
Vec3::new(-outer_half_size.x, -inner_half_size.y, 0.),
Vec3::new(-outer_half_size.x, inner_half_size.y, 0.),
Vec3::new(-inner_half_size.x, inner_half_size.y, 0.),
Vec3::new(-inner_half_size.x, outer_half_size.y, 0.),
]
.map(|vec3| config.isometry * vec3);
for chunk in vertices.chunks_exact(3) {
self.gizmos
.short_arc_3d_between(chunk[1], chunk[0], chunk[2], config.color)
.resolution(config.arc_resolution);
}
let edges = if config.corner_radius > 0. {
[
(vertices[2], vertices[3]),
(vertices[5], vertices[6]),
(vertices[8], vertices[9]),
(vertices[11], vertices[0]),
]
} else {
[
(vertices[0], vertices[5]),
(vertices[3], vertices[8]),
(vertices[6], vertices[11]),
(vertices[9], vertices[2]),
]
};
for (start, end) in edges {
self.gizmos.line(start, end, config.color);
}
}
}
impl<Config, Clear> Drop for RoundedCuboidBuilder<'_, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
fn drop(&mut self) {
if !self.gizmos.enabled {
return;
}
let config = &self.config;
let outer_half_size = self.size.abs() / 2.0;
let inner_half_size =
(outer_half_size - Vec3::splat(config.corner_radius.abs())).max(Vec3::ZERO);
let mut edge_radius = (outer_half_size - inner_half_size).min_element();
let inner_half_size = outer_half_size - Vec3::splat(edge_radius);
edge_radius *= config.corner_radius.signum();
if edge_radius == 0.0 {
let transform = Transform::from_translation(config.isometry.translation.into())
.with_rotation(config.isometry.rotation)
.with_scale(self.size);
self.gizmos.cuboid(transform, config.color);
return;
}
let rects = [
(
Vec3::X,
Vec2::new(self.size.z, self.size.y),
Quat::from_rotation_y(FRAC_PI_2),
),
(
Vec3::Y,
Vec2::new(self.size.x, self.size.z),
Quat::from_rotation_x(FRAC_PI_2),
),
(Vec3::Z, Vec2::new(self.size.x, self.size.y), Quat::IDENTITY),
];
for (position, size, rotation) in rects {
let local_position = position * inner_half_size;
self.gizmos
.rounded_rect(
config.isometry * Isometry3d::new(local_position, rotation),
size,
config.color,
)
.arc_resolution(config.arc_resolution)
.corner_radius(edge_radius);
self.gizmos
.rounded_rect(
config.isometry * Isometry3d::new(-local_position, rotation),
size,
config.color,
)
.arc_resolution(config.arc_resolution)
.corner_radius(edge_radius);
}
}
}
impl<Config, Clear> GizmoBuffer<Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
pub fn rounded_rect(
&mut self,
isometry: impl Into<Isometry3d>,
size: Vec2,
color: impl Into<Color>,
) -> RoundedRectBuilder<'_, Config, Clear> {
let corner_radius = size.min_element() * DEFAULT_CORNER_RADIUS;
RoundedRectBuilder {
gizmos: self,
config: RoundedBoxConfig {
isometry: isometry.into(),
color: color.into(),
corner_radius,
arc_resolution: DEFAULT_ARC_RESOLUTION,
},
size,
}
}
pub fn rounded_rect_2d(
&mut self,
isometry: impl Into<Isometry2d>,
size: Vec2,
color: impl Into<Color>,
) -> RoundedRectBuilder<'_, Config, Clear> {
let isometry = isometry.into();
let corner_radius = size.min_element() * DEFAULT_CORNER_RADIUS;
RoundedRectBuilder {
gizmos: self,
config: RoundedBoxConfig {
isometry: Isometry3d::new(
isometry.translation.extend(0.0),
Quat::from_rotation_z(isometry.rotation.as_radians()),
),
color: color.into(),
corner_radius,
arc_resolution: DEFAULT_ARC_RESOLUTION,
},
size,
}
}
pub fn rounded_cuboid(
&mut self,
isometry: impl Into<Isometry3d>,
size: Vec3,
color: impl Into<Color>,
) -> RoundedCuboidBuilder<'_, Config, Clear> {
let corner_radius = size.min_element() * DEFAULT_CORNER_RADIUS;
RoundedCuboidBuilder {
gizmos: self,
config: RoundedBoxConfig {
isometry: isometry.into(),
color: color.into(),
corner_radius,
arc_resolution: DEFAULT_ARC_RESOLUTION,
},
size,
}
}
}
const DEFAULT_ARC_RESOLUTION: u32 = 8;
const DEFAULT_CORNER_RADIUS: f32 = 0.1;