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 `normal` in `HitData`.
13
14use core::f32;
15
16use bevy_camera::NormalizedRenderTarget;
17use bevy_ecs::prelude::*;
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 pointer_hits_writer: MessageWriter<PointerHits>,
33) {
34    for (pointer_id, pointer_location) in pointers.iter() {
35        if let Some(Location {
36            target: NormalizedRenderTarget::Window(window_ref),
37            position,
38            ..
39        }) = pointer_location.location
40        {
41            let entity = window_ref.entity();
42            let hit_data = HitData::new(entity, 0.0, Some(position.extend(0.0)), None);
43            pointer_hits_writer.write(PointerHits::new(
44                *pointer_id,
45                vec![(entity, hit_data)],
46                f32::NEG_INFINITY,
47            ));
48        }
49    }
50}