Fix Interaction not resetting to None sometimes (#1315)
* Fix Interaction getting stuck when pressing and releasing mouse button in one frame * Fix Interaction not resetting in some cases with FocusPolicy::Pass
This commit is contained in:
parent
0867dc76a3
commit
1d3dfd3938
@ -33,3 +33,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.4.0" }
|
|||||||
# other
|
# other
|
||||||
stretch = "0.3"
|
stretch = "0.3"
|
||||||
serde = {version = "1", features = ["derive"]}
|
serde = {version = "1", features = ["derive"]}
|
||||||
|
smallvec = "1.4"
|
@ -4,6 +4,7 @@ use bevy_ecs::prelude::*;
|
|||||||
use bevy_input::{mouse::MouseButton, touch::Touches, Input};
|
use bevy_input::{mouse::MouseButton, touch::Touches, Input};
|
||||||
use bevy_transform::components::GlobalTransform;
|
use bevy_transform::components::GlobalTransform;
|
||||||
use bevy_window::Windows;
|
use bevy_window::Windows;
|
||||||
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||||
pub enum Interaction {
|
pub enum Interaction {
|
||||||
@ -32,7 +33,7 @@ impl Default for FocusPolicy {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
hovered_entity: Option<Entity>,
|
entities_to_reset: SmallVec<[Entity; 1]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ui_focus_system(
|
pub fn ui_focus_system(
|
||||||
@ -57,7 +58,16 @@ pub fn ui_focus_system(
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0) {
|
// reset entities that were both clicked and released in the last frame
|
||||||
|
for entity in state.entities_to_reset.drain(..) {
|
||||||
|
if let Ok(mut interaction) = node_query.get_component_mut::<Interaction>(entity) {
|
||||||
|
*interaction = Interaction::None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mouse_released =
|
||||||
|
mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0);
|
||||||
|
if mouse_released {
|
||||||
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()
|
||||||
{
|
{
|
||||||
if let Some(mut interaction) = interaction {
|
if let Some(mut interaction) = interaction {
|
||||||
@ -70,73 +80,66 @@ pub fn ui_focus_system(
|
|||||||
|
|
||||||
let mouse_clicked =
|
let mouse_clicked =
|
||||||
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.just_released(0);
|
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.just_released(0);
|
||||||
let mut hovered_entity = None;
|
|
||||||
|
|
||||||
{
|
let mut moused_over_z_sorted_nodes = node_query
|
||||||
let mut moused_over_z_sorted_nodes = node_query
|
.iter_mut()
|
||||||
.iter_mut()
|
.filter_map(
|
||||||
.filter_map(
|
|(entity, node, global_transform, interaction, focus_policy)| {
|
||||||
|(entity, node, global_transform, interaction, focus_policy)| {
|
let position = global_transform.translation;
|
||||||
let position = global_transform.translation;
|
let ui_position = position.truncate();
|
||||||
let ui_position = position.truncate();
|
let extents = node.size / 2.0;
|
||||||
let extents = node.size / 2.0;
|
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(&cursor_position.x)
|
||||||
if (min.x..max.x).contains(&cursor_position.x)
|
&& (min.y..max.y).contains(&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 {
|
if let Some(mut interaction) = interaction {
|
||||||
if let Some(mut interaction) = interaction {
|
if *interaction == Interaction::Hovered {
|
||||||
if *interaction == Interaction::Hovered {
|
*interaction = Interaction::None;
|
||||||
*interaction = Interaction::None;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
},
|
None
|
||||||
)
|
}
|
||||||
.collect::<Vec<_>>();
|
},
|
||||||
|
)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, z)| -*z);
|
moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, z)| -*z);
|
||||||
for (entity, focus_policy, interaction, _) in moused_over_z_sorted_nodes {
|
|
||||||
if let Some(mut interaction) = interaction {
|
let mut moused_over_z_sorted_nodes = moused_over_z_sorted_nodes.into_iter();
|
||||||
if mouse_clicked {
|
// set Clicked or Hovered on top nodes
|
||||||
// only consider nodes with ClickState "clickable"
|
for (entity, focus_policy, interaction, _) in moused_over_z_sorted_nodes.by_ref() {
|
||||||
if *interaction != Interaction::Clicked {
|
if let Some(mut interaction) = interaction {
|
||||||
*interaction = Interaction::Clicked;
|
if mouse_clicked {
|
||||||
|
// only consider nodes with Interaction "clickable"
|
||||||
|
if *interaction != Interaction::Clicked {
|
||||||
|
*interaction = Interaction::Clicked;
|
||||||
|
// if the mouse was simultaneously released, reset this Interaction in the next frame
|
||||||
|
if mouse_released {
|
||||||
|
state.entities_to_reset.push(entity);
|
||||||
}
|
}
|
||||||
} else if *interaction == Interaction::None {
|
|
||||||
*interaction = Interaction::Hovered;
|
|
||||||
}
|
}
|
||||||
|
} else if *interaction == Interaction::None {
|
||||||
|
*interaction = Interaction::Hovered;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hovered_entity = Some(entity);
|
match focus_policy.cloned().unwrap_or(FocusPolicy::Block) {
|
||||||
|
FocusPolicy::Block => {
|
||||||
match focus_policy.cloned().unwrap_or(FocusPolicy::Block) {
|
break;
|
||||||
FocusPolicy::Block => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
FocusPolicy::Pass => { /* allow the next node to be hovered/clicked */ }
|
|
||||||
}
|
}
|
||||||
|
FocusPolicy::Pass => { /* allow the next node to be hovered/clicked */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// reset lower nodes to None
|
||||||
// if there is a new hovered entity, but an entity is currently hovered, unhover the old entity
|
for (_entity, _focus_policy, interaction, _) in moused_over_z_sorted_nodes {
|
||||||
if let Some(new_hovered_entity) = hovered_entity {
|
if let Some(mut interaction) = interaction {
|
||||||
if let Some(old_hovered_entity) = state.hovered_entity {
|
if *interaction != Interaction::None {
|
||||||
if new_hovered_entity != old_hovered_entity {
|
*interaction = Interaction::None;
|
||||||
if let Ok(mut interaction) =
|
|
||||||
node_query.get_component_mut::<Interaction>(old_hovered_entity)
|
|
||||||
{
|
|
||||||
if *interaction == Interaction::Hovered {
|
|
||||||
*interaction = Interaction::None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
state.hovered_entity = None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state.hovered_entity = hovered_entity;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user