Skip to content

Commit 5971710

Browse files
authored
Global Gizmos (#22107)
# Objective - use gizmos in non-system contexts, such as deep in a math library or callback without ecs access. - kind of upstream https://github.com/atlv24/glizmo - builds off of #22105 ## Solution - mutex + deref ## Testing -
1 parent 545a97d commit 5971710

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

crates/bevy_gizmos/src/global.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use std::sync::Mutex;
2+
3+
use bevy_app::{App, Last, Plugin};
4+
use bevy_ecs::schedule::IntoScheduleConfigs;
5+
6+
use crate::{
7+
config::DefaultGizmoConfigGroup,
8+
gizmos::{GizmoBuffer, Gizmos},
9+
GizmoMeshSystems,
10+
};
11+
12+
static GLOBAL_GIZMO: Mutex<GizmoBuffer<DefaultGizmoConfigGroup, ()>> =
13+
Mutex::new(GizmoBuffer::new());
14+
15+
/// Lets you use bevy gizmos outside of systems.
16+
pub struct GlobalGizmosPlugin;
17+
18+
impl Plugin for GlobalGizmosPlugin {
19+
fn build(&self, app: &mut App) {
20+
app.add_systems(Last, flush_global_gizmos.before(GizmoMeshSystems));
21+
}
22+
}
23+
24+
fn flush_global_gizmos(mut gizmos: Gizmos) {
25+
let mut buffer = GizmoBuffer::new();
26+
{
27+
core::mem::swap(&mut buffer, &mut GLOBAL_GIZMO.lock().unwrap());
28+
}
29+
gizmos.strip_positions.extend(buffer.strip_positions);
30+
gizmos.strip_colors.extend(buffer.strip_colors);
31+
gizmos.list_positions.extend(buffer.list_positions);
32+
gizmos.list_colors.extend(buffer.list_colors);
33+
}
34+
35+
/// A global gizmo context for use outside of bevy systems.
36+
///
37+
/// # Example
38+
/// ```
39+
/// # use bevy_gizmos::prelude::*;
40+
/// # use bevy_math::prelude::*;
41+
/// # use bevy_color::palettes::basic::WHITE;
42+
/// fn draw() {
43+
/// gizmo().sphere(Isometry3d::IDENTITY, 0.5, WHITE);
44+
/// }
45+
/// ```
46+
pub fn gizmo() -> impl core::ops::DerefMut<Target = GizmoBuffer<DefaultGizmoConfigGroup, ()>> {
47+
GLOBAL_GIZMO.lock().unwrap()
48+
}

crates/bevy_gizmos/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub mod config;
3030
pub mod cross;
3131
pub mod curves;
3232
pub mod gizmos;
33+
mod global;
3334
pub mod grid;
3435
pub mod primitives;
3536
pub mod retained;
@@ -52,6 +53,7 @@ pub mod prelude {
5253
GizmoLineConfig, GizmoLineJoint, GizmoLineStyle,
5354
},
5455
gizmos::Gizmos,
56+
global::gizmo,
5557
primitives::{dim2::GizmoPrimitive2d, dim3::GizmoPrimitive3d},
5658
retained::Gizmo,
5759
AppGizmoBuilder, GizmoAsset,
@@ -92,7 +94,7 @@ impl Plugin for GizmoPlugin {
9294
// We insert the Resource GizmoConfigStore into the world implicitly here if it does not exist.
9395
.init_gizmo_group::<DefaultGizmoConfigGroup>();
9496

95-
app.add_plugins(aabb::AabbGizmoPlugin);
97+
app.add_plugins((aabb::AabbGizmoPlugin, global::GlobalGizmosPlugin));
9698

9799
#[cfg(feature = "bevy_light")]
98100
app.add_plugins(LightGizmoPlugin);

0 commit comments

Comments
 (0)