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
32 changes: 28 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::CameraUi, 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<&CameraUi>)>,
farend-east marked this conversation as resolved.
Show resolved Hide resolved
windows: Res<Windows>,
mouse_button_input: Res<Input<MouseButton>>,
touches_input: Res<Touches>,
Expand Down Expand Up @@ -88,9 +90,31 @@ 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 cursor_position = camera
.iter()
.find_map(|(camera, camera_ui)| {
// Disabled UI camera should be ignored
if let Some(&CameraUi {
is_enabled: false, ..
}) = camera_ui
{
return None;
}
Copy link
Contributor

@nicopap nicopap Jul 6, 2022

Choose a reason for hiding this comment

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

With the change suggested on the query parameter, you can replace this with if !camera_ui.is_enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The user are able to add in any camera without this component.


match camera.target {
RenderTarget::Window(window_id) => {
// Get the cursor position from the currently focused window with camera
windows.get(window_id).and_then(|window| {
if window.is_focused() {
window.cursor_position()
} else {
None
}
})
}
_ => None,
}
})
farend-east marked this conversation as resolved.
Show resolved Hide resolved
.or_else(|| touches_input.first_pressed_position());

let mut moused_over_z_sorted_nodes = node_query
Expand Down