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