From 530422cfae347a956d33de388e4d331346fd5fb2 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 15 Aug 2023 18:35:23 +0200 Subject: [PATCH 1/3] add egui mouse checking --- .../src/egui_mouse_check.rs | 45 +++++++++++++++++++ crates/bevy-inspector-egui/src/lib.rs | 2 + 2 files changed, 47 insertions(+) create mode 100644 crates/bevy-inspector-egui/src/egui_mouse_check.rs diff --git a/crates/bevy-inspector-egui/src/egui_mouse_check.rs b/crates/bevy-inspector-egui/src/egui_mouse_check.rs new file mode 100644 index 00000000..19ab5f76 --- /dev/null +++ b/crates/bevy-inspector-egui/src/egui_mouse_check.rs @@ -0,0 +1,45 @@ +use bevy_ecs::prelude::*; +use bevy_egui::EguiContexts; +use bevy_log::error; +use bevy_window::PrimaryWindow; + +#[derive(Resource)] +pub struct EguiMousePointerCheck { + pointer_is_valid: bool, + primary_window: Option, +} + +impl Default for EguiMousePointerCheck { + fn default() -> EguiMousePointerCheck { + EguiMousePointerCheck { + pointer_is_valid: true, + primary_window: None, + } + } +} + +pub fn initialize_egui_mouse_check( + mut egui_check: ResMut, + window_q: Query>, +) { + if let Ok(window_id) = window_q.get_single() { + egui_check.primary_window = Some(window_id); + } else { + error!("could not get Primary Window"); + } +} + +pub fn update_egui_mouse_check( + mut egui_checker: ResMut, + mut egui_ctxs: EguiContexts, +) { + if let Some(window_id) = egui_checker.primary_window { + egui_checker.pointer_is_valid = !egui_ctxs + .ctx_for_window_mut(window_id) + .wants_pointer_input(); + } +} + +pub fn mouse_pointer_valid() -> impl FnMut(Res) -> bool + Clone { + move |egui_mouse_check: Res| egui_mouse_check.pointer_is_valid +} diff --git a/crates/bevy-inspector-egui/src/lib.rs b/crates/bevy-inspector-egui/src/lib.rs index 0a86b26d..778331ec 100644 --- a/crates/bevy-inspector-egui/src/lib.rs +++ b/crates/bevy-inspector-egui/src/lib.rs @@ -137,6 +137,8 @@ pub mod quick; pub mod reflect_inspector; pub mod restricted_world_view; +pub mod egui_mouse_check; + mod egui_utils; mod utils; From cd3a6334ace9d79c66b6382ee69486f77736b1e1 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 15 Aug 2023 18:35:43 +0200 Subject: [PATCH 2/3] add mouse check example --- crates/bevy-inspector-egui/Cargo.toml | 4 + crates/bevy-inspector-egui/examples/README.md | 1 + .../quick/world_inspector_mouse_check.rs | 99 +++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs diff --git a/crates/bevy-inspector-egui/Cargo.toml b/crates/bevy-inspector-egui/Cargo.toml index d302b03c..1807dc63 100644 --- a/crates/bevy-inspector-egui/Cargo.toml +++ b/crates/bevy-inspector-egui/Cargo.toml @@ -87,6 +87,10 @@ path = "examples/quick/asset_inspector.rs" name = "world_inspector" path = "examples/quick/world_inspector.rs" +[[example]] +name = "world_inspector_mouse_check" +path = "examples/quick/world_inspector_mouse_check.rs" + [[example]] name = "world_inspector_assets" path = "examples/quick/world_inspector_assets.rs" diff --git a/crates/bevy-inspector-egui/examples/README.md b/crates/bevy-inspector-egui/examples/README.md index 85c6e326..91501f9b 100644 --- a/crates/bevy-inspector-egui/examples/README.md +++ b/crates/bevy-inspector-egui/examples/README.md @@ -5,6 +5,7 @@ - [`resource_inspector_manual.rs`](./basic/resource_inspector_manual.rs) Shows how to customize and build your own inspector windows - `quick` - Demonstrations of the quick plugins - [`world_inspector.rs`](./quick/world_inspector.rs) Example of the `WorldInspectorPlugin` + - [`world_inspector_mouse_check.rs`](./quick/world_inspector_mouse_check.rs) Example of the `WorldInspectorPlugin` with camera and checking for egui components - [`resource_inspector.rs`](./quick/resource_inspector.rs) Example of the `ResourceInspectorPlugin` - [`filter_query_inspector.rs`](./quick/filter_query_inspector.rs) Example of the `FilterQueryInspectorPlugin` - [`asset_inspector.rs`](./quick/asset_inspector.rs) Example of the `AssetInspectorPlugin` diff --git a/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs b/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs new file mode 100644 index 00000000..61d38669 --- /dev/null +++ b/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs @@ -0,0 +1,99 @@ +use bevy::input::common_conditions::input_toggle_active; +use bevy::input::mouse; +use bevy::prelude::*; +use bevy_inspector_egui::egui_mouse_check; +use bevy_inspector_egui::quick::WorldInspectorPlugin; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugins( + WorldInspectorPlugin::default().run_if(input_toggle_active(true, KeyCode::Escape)), + ) + .init_resource::() + .add_systems( + Startup, + (setup, egui_mouse_check::initialize_egui_mouse_check), + ) + .add_systems(PreUpdate, egui_mouse_check::update_egui_mouse_check) + .add_systems( + Update, + (camera_pan, camera_zoom).run_if(egui_mouse_check::mouse_pointer_valid()), + ) + .run(); +} + +#[derive(Component)] +struct MainCamera; + +/// set up a simple 3D scene +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // plane + commands.spawn(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))), + material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), + ..default() + }); + // cube + commands.spawn(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()), + transform: Transform::from_xyz(0.0, 0.5, 0.0), + ..default() + }); + // light + commands.spawn(PointLightBundle { + point_light: PointLight { + intensity: 1500.0, + shadows_enabled: true, + ..default() + }, + transform: Transform::from_xyz(4.0, 8.0, 4.0), + ..default() + }); + // camera + commands.spawn(( + MainCamera, + Camera3dBundle { + transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + ..default() + }, + )); +} + +fn camera_pan( + mouse: Res>, + mut motion_evr: EventReader, + mut camera_q: Query<&mut Transform, With>, +) { + const SENSITIVITY: f32 = 0.005; + let mut camera_transform = camera_q.single_mut(); + + if mouse.any_pressed([MouseButton::Left, MouseButton::Right]) { + for e in motion_evr.iter() { + let y_rotation = Quat::from_rotation_y(e.delta.x * SENSITIVITY); + camera_transform.rotate_around(Vec3::ZERO, y_rotation); + } + } +} + +fn camera_zoom( + mut scroll_evr: EventReader, + mut camera_q: Query<&mut Transform, With>, +) { + let mut camera_transform = camera_q.single_mut(); + + for e in scroll_evr.iter() { + let dir = -e.y.signum(); + let n = camera_transform.translation.normalize() * dir * 2.; + + camera_transform.translation += n; + camera_transform + .translation + .clamp(Vec3::splat(10.0), Vec3::splat(25.0)); + } +} From 9c16f8018b5b39be8ce26a05309d9bd2ac7646bc Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Aug 2023 11:00:32 +0200 Subject: [PATCH 3/3] refactor: EguiMouseCheck plugin --- .../examples/quick/world_inspector_mouse_check.rs | 12 ++++-------- crates/bevy-inspector-egui/src/egui_mouse_check.rs | 14 +++++++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs b/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs index 61d38669..da8ffe15 100644 --- a/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs +++ b/crates/bevy-inspector-egui/examples/quick/world_inspector_mouse_check.rs @@ -7,15 +7,11 @@ use bevy_inspector_egui::quick::WorldInspectorPlugin; fn main() { App::new() .add_plugins(DefaultPlugins) - .add_plugins( + .add_plugins(( WorldInspectorPlugin::default().run_if(input_toggle_active(true, KeyCode::Escape)), - ) - .init_resource::() - .add_systems( - Startup, - (setup, egui_mouse_check::initialize_egui_mouse_check), - ) - .add_systems(PreUpdate, egui_mouse_check::update_egui_mouse_check) + egui_mouse_check::EguiMouseCheck::default(), + )) + .add_systems(Startup, setup) .add_systems( Update, (camera_pan, camera_zoom).run_if(egui_mouse_check::mouse_pointer_valid()), diff --git a/crates/bevy-inspector-egui/src/egui_mouse_check.rs b/crates/bevy-inspector-egui/src/egui_mouse_check.rs index 19ab5f76..3faa5182 100644 --- a/crates/bevy-inspector-egui/src/egui_mouse_check.rs +++ b/crates/bevy-inspector-egui/src/egui_mouse_check.rs @@ -1,8 +1,20 @@ +use bevy_app::{App, Plugin, PreUpdate, Startup}; use bevy_ecs::prelude::*; use bevy_egui::EguiContexts; use bevy_log::error; use bevy_window::PrimaryWindow; +#[derive(Default)] +pub struct EguiMouseCheck; + +impl Plugin for EguiMouseCheck { + fn build(&self, app: &mut App) { + app.init_resource::() + .add_systems(Startup, initialize_egui_mouse_check) + .add_systems(PreUpdate, update_egui_mouse_check); + } +} + #[derive(Resource)] pub struct EguiMousePointerCheck { pointer_is_valid: bool, @@ -40,6 +52,6 @@ pub fn update_egui_mouse_check( } } -pub fn mouse_pointer_valid() -> impl FnMut(Res) -> bool + Clone { +pub fn mouse_pointer_valid() -> impl Fn(Res) -> bool + Clone { move |egui_mouse_check: Res| egui_mouse_check.pointer_is_valid }