Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture pointer when used with bevy_picking #331

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading