diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 0228ec7e35af8d..11a7290bf0e0d1 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -1,4 +1,4 @@ -use crate::{CalculatedClip, Node}; +use crate::{entity::UiCameraConfig, CalculatedClip, Node}; use bevy_ecs::{ entity::Entity, prelude::Component, @@ -8,6 +8,7 @@ use bevy_ecs::{ 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_transform::components::GlobalTransform; use bevy_utils::FloatOrd; use bevy_window::Windows; @@ -52,6 +53,7 @@ pub struct State { /// The system that sets Interaction for all UI elements based on the mouse cursor activity pub fn ui_focus_system( mut state: Local, + camera: Query<(&Camera, Option<&UiCameraConfig>)>, windows: Res, mouse_button_input: Res>, touches_input: Res, @@ -88,9 +90,22 @@ pub fn ui_focus_system( let mouse_clicked = mouse_button_input.just_pressed(MouseButton::Left) || touches_input.any_just_pressed(); - let cursor_position = windows - .get_primary() - .and_then(|window| window.cursor_position()) + let is_ui_disabled = + |camera_ui| matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. })); + + let cursor_position = camera + .iter() + .filter(|(_, camera_ui)| !is_ui_disabled(*camera_ui)) + .filter_map(|(camera, _)| { + if let RenderTarget::Window(window_id) = camera.target { + Some(window_id) + } else { + None + } + }) + .filter_map(|window_id| windows.get(window_id)) + .filter(|window| window.is_focused()) + .find_map(|window| window.cursor_position()) .or_else(|| touches_input.first_pressed_position()); let mut moused_over_z_sorted_nodes = node_query