Skip to content

Commit

Permalink
Added multi windows check for bevy_ui Interaction. (#5225)
Browse files Browse the repository at this point in the history
# 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 <[email protected]>
  • Loading branch information
farend-east and farend-east committed Jul 11, 2022
1 parent 1fccb99 commit 9b6253b
Showing 1 changed file with 19 additions and 4 deletions.
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

0 comments on commit 9b6253b

Please sign in to comment.