2023-11-20 09:47:50 +00:00
|
|
|
//! Additional [`Gizmos`] Functions -- Arrows
|
|
|
|
//!
|
|
|
|
//! Includes the implementation of [`Gizmos::arrow`] and [`Gizmos::arrow_2d`],
|
|
|
|
//! and assorted support items.
|
2023-11-15 14:19:15 +00:00
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
use crate::prelude::{GizmoConfigGroup, Gizmos};
|
2023-11-15 14:19:15 +00:00
|
|
|
use bevy_math::{Quat, Vec2, Vec3};
|
2024-02-24 21:35:32 +00:00
|
|
|
use bevy_render::color::LegacyColor;
|
2023-11-15 14:19:15 +00:00
|
|
|
|
2023-11-20 09:47:50 +00:00
|
|
|
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
|
2024-01-18 15:52:50 +00:00
|
|
|
pub struct ArrowBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
|
|
|
|
gizmos: &'a mut Gizmos<'w, 's, T>,
|
2023-11-15 14:19:15 +00:00
|
|
|
start: Vec3,
|
|
|
|
end: Vec3,
|
2024-02-24 21:35:32 +00:00
|
|
|
color: LegacyColor,
|
2023-11-15 14:19:15 +00:00
|
|
|
tip_length: f32,
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<T: GizmoConfigGroup> ArrowBuilder<'_, '_, '_, T> {
|
2023-11-15 14:19:15 +00:00
|
|
|
/// Change the length of the tips to be `length`.
|
|
|
|
/// The default tip length is [length of the arrow]/10.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_gizmos::prelude::*;
|
|
|
|
/// # use bevy_render::prelude::*;
|
|
|
|
/// # use bevy_math::prelude::*;
|
|
|
|
/// fn system(mut gizmos: Gizmos) {
|
2024-02-24 21:35:32 +00:00
|
|
|
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, LegacyColor::GREEN)
|
2023-11-15 14:19:15 +00:00
|
|
|
/// .with_tip_length(3.);
|
|
|
|
/// }
|
|
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
|
|
/// ```
|
|
|
|
#[doc(alias = "arrow_head_length")]
|
|
|
|
pub fn with_tip_length(&mut self, length: f32) {
|
|
|
|
self.tip_length = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> {
|
2023-11-15 14:19:15 +00:00
|
|
|
/// Draws the arrow, by drawing lines with the stored [`Gizmos`]
|
|
|
|
fn drop(&mut self) {
|
2024-01-18 15:52:50 +00:00
|
|
|
if !self.gizmos.enabled {
|
|
|
|
return;
|
|
|
|
}
|
2023-11-15 14:19:15 +00:00
|
|
|
// first, draw the body of the arrow
|
|
|
|
self.gizmos.line(self.start, self.end, self.color);
|
|
|
|
// now the hard part is to draw the head in a sensible way
|
|
|
|
// put us in a coordinate system where the arrow is pointing towards +x and ends at the origin
|
|
|
|
let pointing = (self.end - self.start).normalize();
|
|
|
|
let rotation = Quat::from_rotation_arc(Vec3::X, pointing);
|
|
|
|
let tips = [
|
|
|
|
Vec3::new(-1., 1., 0.),
|
|
|
|
Vec3::new(-1., 0., 1.),
|
|
|
|
Vec3::new(-1., -1., 0.),
|
|
|
|
Vec3::new(-1., 0., -1.),
|
|
|
|
];
|
|
|
|
// - extend the vectors so their length is `tip_length`
|
|
|
|
// - rotate the world so +x is facing in the same direction as the arrow
|
|
|
|
// - translate over to the tip of the arrow
|
|
|
|
let tips = tips.map(|v| rotation * (v.normalize() * self.tip_length) + self.end);
|
|
|
|
for v in tips {
|
|
|
|
// then actually draw the tips
|
|
|
|
self.gizmos.line(self.end, v, self.color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
|
2024-02-02 21:13:03 +00:00
|
|
|
/// Draw an arrow in 3D, from `start` to `end`. Has four tips for convenient viewing from any direction.
|
2023-11-15 14:19:15 +00:00
|
|
|
///
|
|
|
|
/// This should be called for each frame the arrow needs to be rendered.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_gizmos::prelude::*;
|
|
|
|
/// # use bevy_render::prelude::*;
|
|
|
|
/// # use bevy_math::prelude::*;
|
|
|
|
/// fn system(mut gizmos: Gizmos) {
|
2024-02-24 21:35:32 +00:00
|
|
|
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, LegacyColor::GREEN);
|
2023-11-15 14:19:15 +00:00
|
|
|
/// }
|
|
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
|
|
/// ```
|
2024-02-24 21:35:32 +00:00
|
|
|
pub fn arrow(
|
|
|
|
&mut self,
|
|
|
|
start: Vec3,
|
|
|
|
end: Vec3,
|
|
|
|
color: LegacyColor,
|
|
|
|
) -> ArrowBuilder<'_, 'w, 's, T> {
|
2023-11-15 14:19:15 +00:00
|
|
|
let length = (end - start).length();
|
|
|
|
ArrowBuilder {
|
|
|
|
gizmos: self,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
color,
|
|
|
|
tip_length: length / 10.,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw an arrow in 2D (on the xy plane), from `start` to `end`.
|
|
|
|
///
|
|
|
|
/// This should be called for each frame the arrow needs to be rendered.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_gizmos::prelude::*;
|
|
|
|
/// # use bevy_render::prelude::*;
|
|
|
|
/// # use bevy_math::prelude::*;
|
|
|
|
/// fn system(mut gizmos: Gizmos) {
|
2024-02-24 21:35:32 +00:00
|
|
|
/// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, LegacyColor::GREEN);
|
2023-11-15 14:19:15 +00:00
|
|
|
/// }
|
|
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
|
|
/// ```
|
2024-01-18 15:52:50 +00:00
|
|
|
pub fn arrow_2d(
|
|
|
|
&mut self,
|
|
|
|
start: Vec2,
|
|
|
|
end: Vec2,
|
2024-02-24 21:35:32 +00:00
|
|
|
color: LegacyColor,
|
2024-01-18 15:52:50 +00:00
|
|
|
) -> ArrowBuilder<'_, 'w, 's, T> {
|
2023-11-15 14:19:15 +00:00
|
|
|
self.arrow(start.extend(0.), end.extend(0.), color)
|
|
|
|
}
|
|
|
|
}
|