Skip to main content

wde_ui/
interaction.rs

1//! Pointer hit-testing against the laid out UI nodes.
2use bevy::{prelude::*, window::PrimaryWindow};
3
4use crate::node::{ComputedUiNode, UiBackground, UiImage};
5
6/// Makes a [`UiNode`](crate::node::UiNode) react to the pointer. Descendants without their own
7/// [`UiInteraction`] forward the pointer to it, so a button can hold decorations.
8#[derive(Component, Clone, Copy, Default, Debug, PartialEq, Eq)]
9pub enum UiInteraction {
10    #[default]
11    None,
12    Hovered,
13    /// Pressed on this node, until the button is released (even outside of it).
14    Pressed
15}
16
17/// Sent when the left button is pressed and released on the same [`UiInteraction`] node.
18#[derive(Message, Clone, Copy, Debug)]
19pub struct UiClick {
20    pub entity: Entity
21}
22
23/// Pointer state over the UI, for game systems to ignore input meant for it. Mouse presses over
24/// the UI are also consumed: game systems don't see them as just pressed.
25#[derive(Resource, Default, Debug)]
26pub struct UiPointer {
27    /// Whether the pointer is over a visible UI node.
28    pub over_ui: bool,
29    /// The [`UiInteraction`] node under the pointer.
30    pub hovered: Option<Entity>,
31    /// The [`UiInteraction`] node the left button was pressed on.
32    pub pressed: Option<Entity>
33}
34
35/// Nodes that are drawn, so block the pointer.
36type Visible = Or<(With<UiBackground>, With<UiImage>)>;
37
38pub(crate) fn update_interactions(
39    window: Single<&Window, With<PrimaryWindow>>,
40    mut mouse: ResMut<ButtonInput<MouseButton>>,
41    nodes: Query<(Entity, &ComputedUiNode), Visible>,
42    parents: Query<&ChildOf>,
43    mut interactions: Query<(Entity, &mut UiInteraction)>,
44    mut pointer: ResMut<UiPointer>,
45    mut clicks: MessageWriter<UiClick>
46) {
47    let topmost = window.cursor_position().and_then(|cursor| {
48        nodes
49            .iter()
50            .filter(|(_, node)| node.rect().contains(cursor))
51            .max_by_key(|(_, node)| node.order)
52            .map(|(entity, _)| entity)
53    });
54    let target = topmost.and_then(|entity| {
55        std::iter::once(entity)
56            .chain(parents.iter_ancestors(entity))
57            .find(|e| interactions.contains(*e))
58    });
59    pointer.over_ui = topmost.is_some();
60    pointer.hovered = target;
61
62    if mouse.just_pressed(MouseButton::Left) {
63        pointer.pressed = target;
64    }
65    if mouse.just_released(MouseButton::Left)
66        && let Some(pressed) = pointer.pressed.take()
67        && target == Some(pressed)
68    {
69        clicks.write(UiClick { entity: pressed });
70    }
71
72    for (entity, mut interaction) in &mut interactions {
73        let state = if pointer.pressed == Some(entity) {
74            UiInteraction::Pressed
75        } else if target == Some(entity) && pointer.pressed.is_none() {
76            UiInteraction::Hovered
77        } else {
78            UiInteraction::None
79        };
80        interaction.set_if_neq(state);
81    }
82
83    if pointer.over_ui {
84        for button in [MouseButton::Left, MouseButton::Right, MouseButton::Middle] {
85            mouse.clear_just_pressed(button);
86        }
87    }
88}