Skip to content

Commit

Permalink
Picking capture pointer (#338)
Browse files Browse the repository at this point in the history
* Capture pointer when used with bevy_picking

This fixes "clicking through" egui when using bevy_picking

Co-authored-by: Robin Gloster <[email protected]>

* Gate bevy_picking support behind the render feature

* Fix the linux dependencies documentation

---------

Co-authored-by: Aevyrie <[email protected]>
Co-authored-by: Robin Gloster <[email protected]>
  • Loading branch information
3 people authored Dec 21, 2024
1 parent 54ed4d4 commit 45b79ab
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ default_fonts = ["egui/default_fonts"]
render = [
"bevy_asset",
"bevy_image",
"bevy_picking",
"bevy_render",
"encase",
"bytemuck",
Expand Down Expand Up @@ -80,6 +81,7 @@ webbrowser = { version = "1.0.1", optional = true }
bytemuck = { version = "1", optional = true }
bevy_asset = { version = "0.15.0", optional = true }
bevy_image = { version = "0.15.0", optional = true }
bevy_picking = { version = "0.15.0", optional = true }
bevy_render = { version = "0.15.0", optional = true }
encase = { version = "0.10", optional = true }
wgpu-types = { version = "23.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ An example WASM project is live at [vladbat00.github.io/bevy_egui_web_showcase](

On Linux, this crate requires certain parts of [XCB](https://xcb.freedesktop.org/) to be installed on your system. On Debian-based systems, these can be installed with the following command:

```
$ sudo apt install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev
```bash
sudo apt install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev
```

## Usage
Expand Down
46 changes: 44 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//!
//! On Linux, this crate requires certain parts of [XCB](https://xcb.freedesktop.org/) to be installed on your system. On Debian-based systems, these can be installed with the following command:
//!
//! ```
//! $ sudo apt install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev
//! ```bash
//! sudo apt install libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev
//! ```
//!
//! ## Usage
Expand Down Expand Up @@ -115,9 +115,15 @@ use bevy_ecs::{
#[cfg(feature = "render")]
use bevy_image::{Image, ImageSampler};
use bevy_input::InputSystem;
#[cfg(feature = "render")]
use bevy_picking::{
backend::{HitData, PointerHits},
pointer::{PointerId, PointerLocation},
};
use bevy_reflect::Reflect;
#[cfg(feature = "render")]
use bevy_render::{
camera::NormalizedRenderTarget,
extract_component::{ExtractComponent, ExtractComponentPlugin},
extract_resource::{ExtractResource, ExtractResourcePlugin},
render_resource::{LoadOp, SpecializedRenderPipelines},
Expand Down Expand Up @@ -162,6 +168,9 @@ 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`].
#[cfg(feature = "render")]
pub capture_pointer_input: bool,
}

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

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

/// The ordering value used for bevy_picking.
#[cfg(feature = "render")]
pub const PICKING_ORDER: f32 = 1_000_000.0;
/// Captures pointers on egui windows for bevy_picking.
#[cfg(feature = "render")]
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 45b79ab

Please sign in to comment.