Skip to content

Add a component to enable/disable interactions on a UI node #16270

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
57 changes: 46 additions & 11 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// when [`ViewVisibility::get()`] is false.
/// This ensures that hidden UI nodes are not interactable,
/// and do not end up stuck in an active state if hidden at the wrong time.
///
/// If a UI node has both [`Interaction`] and [`Interactivity`] components,
/// [`Interaction`] will always be [`Interaction::None`]
/// when [`Interactivity::Disabled`] is used.
///
/// Note that you can also control the visibility of a node using the [`Display`](crate::ui_node::Display) property,
/// which fully collapses it during layout calculations.
Expand Down Expand Up @@ -70,6 +74,31 @@ impl Default for Interaction {
}
}

/// Describes if an interactive UI node can be interacted with.
///
/// This is commoly queried with [`Interaction`]
///
/// Note: click/press-release actions will still be registered. To block propagation see [`FocusPolicy`]
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Component, Default, Debug, PartialEq)]
#[require(Interaction)]
pub enum Interactivity {
/// The node can be interacted with, it's [`Interaction`] componnent will be updated.
Enabled,
/// The node can't be interacted with, it's [`Interaction`] componnent will always be [`Interaction::None`].
Disabled,
}

impl Interactivity {
const DEFAULT: Self = Self::Enabled;
}

impl Default for Interactivity {
fn default() -> Self {
Self::DEFAULT
}
}

/// A component storing the position of the mouse relative to the node, (0., 0.) being the top-left corner and (1., 1.) being the bottom-right
/// If the mouse is not over the node, the value will go beyond the range of (0., 0.) to (1., 1.)
///
Expand Down Expand Up @@ -139,6 +168,7 @@ pub struct NodeQuery {
node: &'static ComputedNode,
global_transform: &'static GlobalTransform,
interaction: Option<&'static mut Interaction>,
interactivity: Option<&'static Interactivity>,
relative_cursor_position: Option<&'static mut RelativeCursorPosition>,
focus_policy: Option<&'static FocusPolicy>,
calculated_clip: Option<&'static CalculatedClip>,
Expand Down Expand Up @@ -311,19 +341,24 @@ pub fn ui_focus_system(
// the iteration will stop on it because it "captures" the interaction.
let mut iter = node_query.iter_many_mut(hovered_nodes.by_ref());
while let Some(node) = iter.fetch_next() {
if let Some(mut interaction) = node.interaction {
if mouse_clicked {
// only consider nodes with Interaction "pressed"
if *interaction != Interaction::Pressed {
*interaction = Interaction::Pressed;
// if the mouse was simultaneously released, reset this Interaction in the next
// frame
if mouse_released {
state.entities_to_reset.push(node.entity);

// Block interaction of disabled nodes
if node.interactivity.is_none() || node.interactivity.is_some_and(|inter| *inter == Interactivity::Enabled)
{
if let Some(mut interaction) = node.interaction {
if mouse_clicked {
// only consider nodes with Interaction "pressed"
if *interaction != Interaction::Pressed {
*interaction = Interaction::Pressed;
// if the mouse was simultaneously released, reset this Interaction in the next
// frame
if mouse_released {
state.entities_to_reset.push(node.entity);
}
}
} else if *interaction == Interaction::None {
*interaction = Interaction::Hovered;
}
} else if *interaction == Interaction::None {
*interaction = Interaction::Hovered;
}
}

Expand Down
Loading