do not check for focus until cursor position has been set (#1070)

do not check for focus until cursor position has been set
This commit is contained in:
François 2020-12-23 23:10:39 +01:00 committed by GitHub
parent 61ce3f7bcf
commit 09c15ea890
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,11 +1,9 @@
use crate::Node; use crate::Node;
use bevy_app::{EventReader, Events};
use bevy_core::FloatOrd; use bevy_core::FloatOrd;
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use bevy_input::{mouse::MouseButton, touch::Touches, Input}; use bevy_input::{mouse::MouseButton, touch::Touches, Input};
use bevy_math::Vec2;
use bevy_transform::components::GlobalTransform; use bevy_transform::components::GlobalTransform;
use bevy_window::CursorMoved; use bevy_window::Windows;
#[derive(Copy, Clone, Eq, PartialEq, Debug)] #[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Interaction { pub enum Interaction {
@ -34,15 +32,13 @@ impl Default for FocusPolicy {
#[derive(Default)] #[derive(Default)]
pub struct State { pub struct State {
cursor_moved_event_reader: EventReader<CursorMoved>,
cursor_position: Vec2,
hovered_entity: Option<Entity>, hovered_entity: Option<Entity>,
} }
pub fn ui_focus_system( pub fn ui_focus_system(
mut state: Local<State>, mut state: Local<State>,
windows: Res<Windows>,
mouse_button_input: Res<Input<MouseButton>>, mouse_button_input: Res<Input<MouseButton>>,
cursor_moved_events: Res<Events<CursorMoved>>,
touches_input: Res<Touches>, touches_input: Res<Touches>,
mut node_query: Query<( mut node_query: Query<(
Entity, Entity,
@ -52,12 +48,14 @@ pub fn ui_focus_system(
Option<&FocusPolicy>, Option<&FocusPolicy>,
)>, )>,
) { ) {
if let Some(cursor_moved) = state.cursor_moved_event_reader.latest(&cursor_moved_events) { let cursor_position = if let Some(cursor_position) = windows
state.cursor_position = cursor_moved.position; .get_primary()
} .and_then(|window| window.cursor_position())
if let Some(touch) = touches_input.get_pressed(0) { {
state.cursor_position = touch.position(); cursor_position
} } else {
return;
};
if mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0) { if mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0) {
for (_entity, _node, _global_transform, interaction, _focus_policy) in node_query.iter_mut() for (_entity, _node, _global_transform, interaction, _focus_policy) in node_query.iter_mut()
@ -85,8 +83,8 @@ pub fn ui_focus_system(
let min = ui_position - extents; let min = ui_position - extents;
let max = ui_position + extents; let max = ui_position + extents;
// if the current cursor position is within the bounds of the node, consider it for clicking // if the current cursor position is within the bounds of the node, consider it for clicking
if (min.x..max.x).contains(&state.cursor_position.x) if (min.x..max.x).contains(&cursor_position.x)
&& (min.y..max.y).contains(&state.cursor_position.y) && (min.y..max.y).contains(&cursor_position.y)
{ {
Some((entity, focus_policy, interaction, FloatOrd(position.z))) Some((entity, focus_policy, interaction, FloatOrd(position.z)))
} else { } else {