bevy_picking/window.rs
1//! This module contains a basic backend that implements picking for window
2//! entities.
3//!
4//! Pointers can exist on windows, images, and gpu texture views. With
5//! [`update_window_hits`] enabled, when a pointer hovers over a window that
6//! window will be inserted as a pointer hit, listed behind all other pointer
7//! hits. This means that when the pointer isn't hovering any other entities,
8//! the picking events will be routed to the window.
9//!
10//! ## Implementation Notes
11//!
12//! - This backend does not provide `position` or `normal` in `HitData`.
13
14use core::f32;
15
16use bevy_ecs::prelude::*;
17use bevy_render::camera::NormalizedRenderTarget;
18
19use crate::{
20 backend::{HitData, PointerHits},
21 pointer::{Location, PointerId, PointerLocation},
22};
23
24/// Generates pointer hit events for window entities.
25///
26/// A pointer is treated as hitting a window when it is located on that window. The order
27/// of the hit event is negative infinity, meaning it should appear behind all other entities.
28///
29/// The depth of the hit will be listed as zero.
30pub fn update_window_hits(
31 pointers: Query<(&PointerId, &PointerLocation)>,
32 mut output_events: EventWriter<PointerHits>,
33) {
34 for (pointer_id, pointer_location) in pointers.iter() {
35 if let Some(Location {
36 target: NormalizedRenderTarget::Window(window_ref),
37 ..
38 }) = pointer_location.location
39 {
40 let entity = window_ref.entity();
41 let hit_data = HitData::new(entity, 0.0, None, None);
42 output_events.write(PointerHits::new(
43 *pointer_id,
44 vec![(entity, hit_data)],
45 f32::NEG_INFINITY,
46 ));
47 }
48 }
49}