2020-07-18 21:08:46 +00:00
|
|
|
use crate::Node;
|
|
|
|
use bevy_core::FloatOrd;
|
|
|
|
use bevy_ecs::prelude::*;
|
2020-11-03 19:32:48 +00:00
|
|
|
use bevy_input::{mouse::MouseButton, touch::Touches, Input};
|
2020-09-14 21:00:32 +00:00
|
|
|
use bevy_transform::components::GlobalTransform;
|
2020-12-23 22:10:39 +00:00
|
|
|
use bevy_window::Windows;
|
2021-02-01 01:03:25 +00:00
|
|
|
use smallvec::SmallVec;
|
2020-07-18 21:08:46 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
2020-07-28 08:20:19 +00:00
|
|
|
pub enum Interaction {
|
|
|
|
Clicked,
|
2020-07-18 21:08:46 +00:00
|
|
|
Hovered,
|
2020-07-28 08:20:19 +00:00
|
|
|
None,
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 08:20:19 +00:00
|
|
|
impl Default for Interaction {
|
2020-07-18 21:08:46 +00:00
|
|
|
fn default() -> Self {
|
2020-07-28 08:20:19 +00:00
|
|
|
Interaction::None
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum FocusPolicy {
|
|
|
|
Block,
|
|
|
|
Pass,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FocusPolicy {
|
|
|
|
fn default() -> Self {
|
|
|
|
FocusPolicy::Block
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct State {
|
2021-02-01 01:03:25 +00:00
|
|
|
entities_to_reset: SmallVec<[Entity; 1]>,
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ui_focus_system(
|
|
|
|
mut state: Local<State>,
|
2020-12-23 22:10:39 +00:00
|
|
|
windows: Res<Windows>,
|
2020-07-18 21:08:46 +00:00
|
|
|
mouse_button_input: Res<Input<MouseButton>>,
|
2020-11-03 19:32:48 +00:00
|
|
|
touches_input: Res<Touches>,
|
2020-07-18 21:08:46 +00:00
|
|
|
mut node_query: Query<(
|
2020-07-20 03:52:15 +00:00
|
|
|
Entity,
|
2020-07-18 21:08:46 +00:00
|
|
|
&Node,
|
2020-09-14 21:00:32 +00:00
|
|
|
&GlobalTransform,
|
2020-07-28 08:20:19 +00:00
|
|
|
Option<&mut Interaction>,
|
2020-07-18 21:08:46 +00:00
|
|
|
Option<&FocusPolicy>,
|
|
|
|
)>,
|
|
|
|
) {
|
2020-12-23 22:10:39 +00:00
|
|
|
let cursor_position = if let Some(cursor_position) = windows
|
|
|
|
.get_primary()
|
|
|
|
.and_then(|window| window.cursor_position())
|
|
|
|
{
|
|
|
|
cursor_position
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
2020-07-18 21:08:46 +00:00
|
|
|
|
2021-02-01 01:03:25 +00:00
|
|
|
// 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 {
|
2020-10-30 06:39:55 +00:00
|
|
|
for (_entity, _node, _global_transform, interaction, _focus_policy) in node_query.iter_mut()
|
2020-09-14 21:00:32 +00:00
|
|
|
{
|
2020-07-28 08:20:19 +00:00
|
|
|
if let Some(mut interaction) = interaction {
|
|
|
|
if *interaction == Interaction::Clicked {
|
|
|
|
*interaction = Interaction::None;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:32:48 +00:00
|
|
|
let mouse_clicked =
|
|
|
|
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.just_released(0);
|
2020-07-18 21:08:46 +00:00
|
|
|
|
2021-02-01 01:03:25 +00:00
|
|
|
let mut moused_over_z_sorted_nodes = node_query
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(
|
|
|
|
|(entity, node, global_transform, interaction, focus_policy)| {
|
|
|
|
let position = global_transform.translation;
|
|
|
|
let ui_position = position.truncate();
|
|
|
|
let extents = node.size / 2.0;
|
|
|
|
let min = 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 (min.x..max.x).contains(&cursor_position.x)
|
|
|
|
&& (min.y..max.y).contains(&cursor_position.y)
|
|
|
|
{
|
|
|
|
Some((entity, focus_policy, interaction, FloatOrd(position.z)))
|
|
|
|
} else {
|
|
|
|
if let Some(mut interaction) = interaction {
|
|
|
|
if *interaction == Interaction::Hovered {
|
|
|
|
*interaction = Interaction::None;
|
2020-07-20 03:52:15 +00:00
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2021-02-01 01:03:25 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.collect::<Vec<_>>();
|
2020-07-18 21:08:46 +00:00
|
|
|
|
2021-02-01 01:03:25 +00:00
|
|
|
moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, z)| -*z);
|
|
|
|
|
|
|
|
let mut moused_over_z_sorted_nodes = moused_over_z_sorted_nodes.into_iter();
|
|
|
|
// set Clicked or Hovered on top nodes
|
|
|
|
for (entity, focus_policy, interaction, _) in moused_over_z_sorted_nodes.by_ref() {
|
|
|
|
if let Some(mut interaction) = interaction {
|
|
|
|
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);
|
2020-07-20 03:52:15 +00:00
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2021-02-01 01:03:25 +00:00
|
|
|
} else if *interaction == Interaction::None {
|
|
|
|
*interaction = Interaction::Hovered;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2021-02-01 01:03:25 +00:00
|
|
|
}
|
2020-07-20 03:52:15 +00:00
|
|
|
|
2021-02-01 01:03:25 +00:00
|
|
|
match focus_policy.cloned().unwrap_or(FocusPolicy::Block) {
|
|
|
|
FocusPolicy::Block => {
|
|
|
|
break;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2021-02-01 01:03:25 +00:00
|
|
|
FocusPolicy::Pass => { /* allow the next node to be hovered/clicked */ }
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2020-07-20 03:52:15 +00:00
|
|
|
}
|
2021-02-01 01:03:25 +00:00
|
|
|
// reset lower nodes to None
|
|
|
|
for (_entity, _focus_policy, interaction, _) in moused_over_z_sorted_nodes {
|
|
|
|
if let Some(mut interaction) = interaction {
|
|
|
|
if *interaction != Interaction::None {
|
|
|
|
*interaction = Interaction::None;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|