Skip to content

Commit fd6f231

Browse files
committed
glizmos
1 parent 86224b5 commit fd6f231

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

crates/bevy_gizmos/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ repository = "https://github.com/bevyengine/bevy"
88
license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010

11+
[features]
12+
global_gizmos = []
13+
1114
[dependencies]
1215
# Bevy
1316
bevy_app = { path = "../bevy_app", version = "0.18.0-dev" }

crates/bevy_gizmos/src/global.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
GlobalGizmos {
48+
guard: GLOBAL_GIZMO.lock().unwrap(),
49+
}
50+
}
51+
52+
struct GlobalGizmos<'a> {
53+
guard: std::sync::MutexGuard<'a, GizmoBuffer<DefaultGizmoConfigGroup, ()>>,
54+
}
55+
56+
impl<'a> core::ops::Deref for GlobalGizmos<'a> {
57+
type Target = GizmoBuffer<DefaultGizmoConfigGroup, ()>;
58+
59+
fn deref(&self) -> &Self::Target {
60+
&self.guard
61+
}
62+
}
63+
64+
impl<'a> core::ops::DerefMut for GlobalGizmos<'a> {
65+
fn deref_mut(&mut self) -> &mut Self::Target {
66+
&mut self.guard
67+
}
68+
}

crates/bevy_gizmos/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub mod config;
3030
pub mod cross;
3131
pub mod curves;
3232
pub mod gizmos;
33+
#[cfg(feature = "global_gizmos")]
34+
mod global;
3335
pub mod grid;
3436
pub mod primitives;
3537
pub mod retained;
@@ -45,6 +47,8 @@ pub mod prelude {
4547
#[doc(hidden)]
4648
pub use crate::aabb::{AabbGizmoConfigGroup, ShowAabbGizmo};
4749

50+
#[cfg(feature = "global_gizmos")]
51+
pub use crate::global::gizmo;
4852
#[doc(hidden)]
4953
pub use crate::{
5054
config::{
@@ -94,6 +98,9 @@ impl Plugin for GizmoPlugin {
9498

9599
app.add_plugins(aabb::AabbGizmoPlugin);
96100

101+
#[cfg(feature = "global_gizmos")]
102+
app.add_plugins(global::GlobalGizmosPlugin);
103+
97104
#[cfg(feature = "bevy_light")]
98105
app.add_plugins(LightGizmoPlugin);
99106
}

0 commit comments

Comments
 (0)