Skip to content

Commit

Permalink
Capture pointer when used with bevy_picking
Browse files Browse the repository at this point in the history
This fixes "clicking through" egui when using bevy_picking

Co-authored-by: Robin Gloster <[email protected]>
  • Loading branch information
aevyrie and globin committed Dec 9, 2024
1 parent 581a0e1 commit 4088a80
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ bevy_ecs = "0.15.0"
bevy_input = "0.15.0"
bevy_log = "0.15.0"
bevy_math = "0.15.0"
bevy_picking = "0.15.0"
bevy_reflect = "0.15.0"
bevy_time = "0.15.0"
bevy_utils = "0.15.0"
Expand Down
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ mod text_agent;
))]
pub mod web_clipboard;

use bevy_picking::{
backend::{HitData, PointerHits},
pointer::{PointerId, PointerLocation},
};
use bevy_render::camera::NormalizedRenderTarget;
pub use egui;

use crate::systems::*;
Expand Down Expand Up @@ -155,6 +160,8 @@ pub struct EguiSettings {
/// If not specified, `_self` will be used. Only matters in a web browser.
#[cfg(feature = "open_url")]
pub default_open_url_target: Option<String>,
/// Controls if Egui should capture pointer input when using bevy_picking
pub capture_pointer_input: bool,
}

// Just to keep the PartialEq
Expand All @@ -175,6 +182,7 @@ impl Default for EguiSettings {
scale_factor: 1.0,
#[cfg(feature = "open_url")]
default_open_url_target: None,
capture_pointer_input: true,
}
}
}
Expand Down Expand Up @@ -803,6 +811,7 @@ impl Plugin for EguiPlugin {
PostUpdate,
process_output_system.in_set(EguiSet::ProcessOutput),
);
app.add_systems(PostUpdate, capture_pointer_input);

#[cfg(feature = "render")]
app.add_systems(
Expand Down Expand Up @@ -949,6 +958,33 @@ pub fn setup_new_windows_system(
}
}

/// The ordering value used for bevy_picking
pub const PICKING_ORDER: f32 = 1_000_000.0;
/// Captures pointers on egui windows for bevy_picking
pub fn capture_pointer_input(
pointers: Query<(&PointerId, &PointerLocation)>,
mut egui_context: Query<(Entity, &mut EguiContext, &EguiSettings)>,
mut output: EventWriter<PointerHits>,
) {
for (pointer, location) in pointers
.iter()
.filter_map(|(i, p)| p.location.as_ref().map(|l| (i, l)))
{
if let NormalizedRenderTarget::Window(id) = location.target {
if let Ok((entity, mut ctx, settings)) = egui_context.get_mut(id.entity()) {
if settings.capture_pointer_input && ctx.get_mut().wants_pointer_input() {
let entry = (entity, HitData::new(entity, 0.0, None, None));
output.send(PointerHits::new(
*pointer,
Vec::from([entry]),
PICKING_ORDER,
));
}
}
}
}
}

/// Adds bevy_egui components to newly created windows.
#[cfg(feature = "render")]
pub fn setup_render_to_image_handles_system(
Expand Down

0 comments on commit 4088a80

Please sign in to comment.