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] - Added multi windows check for bevy_ui Interaction. #5225

Closed
wants to merge 7 commits into from
23 changes: 19 additions & 4 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{CalculatedClip, Node};
use crate::{entity::UiCameraConfig, CalculatedClip, Node};
use bevy_ecs::{
entity::Entity,
prelude::Component,
Expand All @@ -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;
Expand Down Expand Up @@ -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<State>,
camera: Query<(&Camera, Option<&UiCameraConfig>)>,
windows: Res<Windows>,
mouse_button_input: Res<Input<MouseButton>>,
touches_input: Res<Touches>,
Expand Down Expand Up @@ -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
Expand Down