Added multi windows check for bevy_ui Interaction. (#5225)

# Objective

- Currently bevy_ui only checks for primary window cursor position to determine `Interaction` behavior.
- Added checks for focused window where cursor position is available.
- Fixes #5224.

## Solution

- Added checks for focused windows in `Interaction` focus system.

## Follow Up

- All windows with camera will be rendering the UI elements right now.
- We will need some way to tell which camera to render which UI.

---

Co-authored-by: fadhliazhari <44402264+fadhliazhari@users.noreply.github.com>
This commit is contained in:
fadhliazhari 2022-07-11 14:11:26 +00:00
parent 1fccb99d3a
commit 9b6253b769

View file

@ -1,4 +1,4 @@
use crate::{CalculatedClip, Node}; use crate::{entity::UiCameraConfig, CalculatedClip, Node};
use bevy_ecs::{ use bevy_ecs::{
entity::Entity, entity::Entity,
prelude::Component, prelude::Component,
@ -8,6 +8,7 @@ use bevy_ecs::{
use bevy_input::{mouse::MouseButton, touch::Touches, Input}; use bevy_input::{mouse::MouseButton, touch::Touches, Input};
use bevy_math::Vec2; use bevy_math::Vec2;
use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::camera::{Camera, RenderTarget};
use bevy_transform::components::GlobalTransform; use bevy_transform::components::GlobalTransform;
use bevy_utils::FloatOrd; use bevy_utils::FloatOrd;
use bevy_window::Windows; 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 /// The system that sets Interaction for all UI elements based on the mouse cursor activity
pub fn ui_focus_system( pub fn ui_focus_system(
mut state: Local<State>, mut state: Local<State>,
camera: Query<(&Camera, Option<&UiCameraConfig>)>,
windows: Res<Windows>, windows: Res<Windows>,
mouse_button_input: Res<Input<MouseButton>>, mouse_button_input: Res<Input<MouseButton>>,
touches_input: Res<Touches>, touches_input: Res<Touches>,
@ -88,9 +90,22 @@ pub fn ui_focus_system(
let mouse_clicked = let mouse_clicked =
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.any_just_pressed(); mouse_button_input.just_pressed(MouseButton::Left) || touches_input.any_just_pressed();
let cursor_position = windows let is_ui_disabled =
.get_primary() |camera_ui| matches!(camera_ui, Some(&UiCameraConfig { show_ui: false, .. }));
.and_then(|window| window.cursor_position())
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()); .or_else(|| touches_input.first_pressed_position());
let mut moused_over_z_sorted_nodes = node_query let mut moused_over_z_sorted_nodes = node_query