Skip to content
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

[Merged by Bors] - Disable UI node Interaction when ComputedVisibility is false #5361

Closed
30 changes: 28 additions & 2 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bevy_input::{mouse::MouseButton, touch::Touches, Input};
use bevy_math::Vec2;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::camera::{Camera, RenderTarget};
use bevy_render::view::ComputedVisibility;
use bevy_transform::components::GlobalTransform;
use bevy_utils::FloatOrd;
use bevy_window::Windows;
Expand All @@ -18,6 +19,14 @@ use smallvec::SmallVec;
/// Describes what type of input interaction has occurred for a UI node.
///
/// This is commonly queried with a `Changed<Interaction>` filter.
///
/// Updated in [`ui_focus_system`].
///
/// If a UI node has bothe [`Interaction`] and [`ComputedVisibility`] components,
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// [`Interaction`] will always be [`Interaction::None`]
/// when [`ComputedVisibility::is_visible()] is false.
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
/// 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.
#[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)]
Expand Down Expand Up @@ -51,6 +60,8 @@ pub struct State {
}

/// The system that sets Interaction for all UI elements based on the mouse cursor activity
///
/// Entities with a hidden [`ComputedVisibility`] are always treated as releasd.
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
pub fn ui_focus_system(
mut state: Local<State>,
camera: Query<(&Camera, Option<&UiCameraConfig>)>,
Expand All @@ -64,6 +75,7 @@ pub fn ui_focus_system(
Option<&mut Interaction>,
Option<&FocusPolicy>,
Option<&CalculatedClip>,
Option<&ComputedVisibility>,
)>,
) {
// reset entities that were both clicked and released in the last frame
Expand All @@ -76,7 +88,7 @@ pub fn ui_focus_system(
let mouse_released =
mouse_button_input.just_released(MouseButton::Left) || touches_input.any_just_released();
if mouse_released {
for (_entity, _node, _global_transform, interaction, _focus_policy, _clip) in
for (_entity, _node, _global_transform, interaction, _focus_policy, _clip, _visibility) in
node_query.iter_mut()
{
if let Some(mut interaction) = interaction {
Expand Down Expand Up @@ -111,7 +123,21 @@ pub fn ui_focus_system(
let mut moused_over_z_sorted_nodes = node_query
.iter_mut()
.filter_map(
|(entity, node, global_transform, interaction, focus_policy, clip)| {
|(entity, node, global_transform, interaction, focus_policy, clip, visibility)| {
// Nodes that are not rendered should not be interactable
if let Some(computed_visibility) = visibility {
if !computed_visibility.is_visible() {
// Reset their interaction to None to avoid strange stuck state
if let Some(mut interaction) = interaction {
if *interaction != Interaction::None {
*interaction = Interaction::None;
}
alice-i-cecile marked this conversation as resolved.
Show resolved Hide resolved
}

return None;
Copy link
Contributor

@superdump superdump Jul 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be simpler and skipping the nested branching to just assign Interaction::None, as it is small data-wise, should likely be faster:

Suggested change
if let Some(mut interaction) = interaction {
if *interaction != Interaction::None {
*interaction = Interaction::None;
}
}
return None;
interaction.as_mut().map(|interaction| *interaction = Interaction::None);
return None;

EDIT: Oh, it's an Option, OK. Modified suggestion above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this has the right change detection behavior: that's why this code is so convoluted. I don't want this triggering Interaction change detection every update.

}
}

let position = global_transform.translation();
let ui_position = position.truncate();
let extents = node.size / 2.0;
Expand Down