2020-07-18 21:08:46 +00:00
|
|
|
use crate::Node;
|
|
|
|
use bevy_app::{EventReader, Events};
|
|
|
|
use bevy_core::FloatOrd;
|
|
|
|
use bevy_ecs::prelude::*;
|
|
|
|
use bevy_input::{mouse::MouseButton, Input};
|
|
|
|
use bevy_math::Vec2;
|
|
|
|
use bevy_transform::components::Transform;
|
2020-07-26 19:27:09 +00:00
|
|
|
use bevy_window::CursorMoved;
|
2020-07-18 21:08:46 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum Click {
|
|
|
|
Released,
|
|
|
|
Pressed,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Click {
|
|
|
|
fn default() -> Self {
|
|
|
|
Click::Released
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
|
|
|
pub enum Hover {
|
|
|
|
Hovered,
|
|
|
|
NotHovered,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Hover {
|
|
|
|
fn default() -> Self {
|
|
|
|
Hover::NotHovered
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
|
|
|
cursor_moved_event_reader: EventReader<CursorMoved>,
|
|
|
|
cursor_position: Vec2,
|
2020-07-20 03:52:15 +00:00
|
|
|
hovered_entity: Option<Entity>,
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ui_focus_system(
|
|
|
|
mut state: Local<State>,
|
|
|
|
mouse_button_input: Res<Input<MouseButton>>,
|
|
|
|
cursor_moved_events: Res<Events<CursorMoved>>,
|
|
|
|
mut node_query: Query<(
|
2020-07-20 03:52:15 +00:00
|
|
|
Entity,
|
2020-07-18 21:08:46 +00:00
|
|
|
&Node,
|
|
|
|
&Transform,
|
|
|
|
Option<&mut Click>,
|
|
|
|
Option<&mut Hover>,
|
|
|
|
Option<&FocusPolicy>,
|
|
|
|
)>,
|
|
|
|
) {
|
|
|
|
if let Some(cursor_moved) = state.cursor_moved_event_reader.latest(&cursor_moved_events) {
|
|
|
|
state.cursor_position = cursor_moved.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
if mouse_button_input.just_released(MouseButton::Left) {
|
2020-07-22 03:12:15 +00:00
|
|
|
for (_entity, _node, _transform, click, _hover, _focus_policy) in &mut node_query.iter() {
|
2020-07-18 21:08:46 +00:00
|
|
|
if let Some(mut click) = click {
|
|
|
|
if *click == Click::Pressed {
|
|
|
|
*click = Click::Released;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mouse_clicked = mouse_button_input.just_pressed(MouseButton::Left);
|
2020-07-20 03:52:15 +00:00
|
|
|
let mut hovered_entity = None;
|
2020-07-18 21:08:46 +00:00
|
|
|
|
2020-07-20 03:52:15 +00:00
|
|
|
{
|
2020-07-22 03:12:15 +00:00
|
|
|
let mut query_iter = node_query.iter();
|
|
|
|
let mut moused_over_z_sorted_nodes = query_iter
|
2020-07-20 03:52:15 +00:00
|
|
|
.iter()
|
|
|
|
.filter_map(|(entity, node, transform, click, hover, focus_policy)| {
|
|
|
|
let position = transform.value.w_axis();
|
2020-07-25 06:04:45 +00:00
|
|
|
let ui_position = position.truncate().truncate();
|
2020-07-20 03:52:15 +00:00
|
|
|
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(&state.cursor_position.x())
|
|
|
|
&& (min.y()..max.y()).contains(&state.cursor_position.y())
|
|
|
|
{
|
|
|
|
Some((entity, focus_policy, click, hover, FloatOrd(position.z())))
|
|
|
|
} else {
|
|
|
|
if let Some(mut hover) = hover {
|
|
|
|
if *hover == Hover::Hovered {
|
|
|
|
*hover = Hover::NotHovered;
|
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2020-07-20 03:52:15 +00:00
|
|
|
None
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
2020-07-20 03:52:15 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2020-07-18 21:08:46 +00:00
|
|
|
|
2020-07-20 03:52:15 +00:00
|
|
|
moused_over_z_sorted_nodes.sort_by_key(|(_, _, _, _, z)| -*z);
|
|
|
|
for (entity, focus_policy, click, hover, _) in moused_over_z_sorted_nodes {
|
|
|
|
if mouse_clicked {
|
|
|
|
// only consider nodes with ClickState "clickable"
|
|
|
|
if let Some(mut click) = click {
|
|
|
|
if *click == Click::Released {
|
|
|
|
*click = Click::Pressed;
|
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 03:52:15 +00:00
|
|
|
// only consider nodes with Hover "hoverable"
|
|
|
|
if let Some(mut hover) = hover {
|
|
|
|
if *hover == Hover::NotHovered {
|
|
|
|
*hover = Hover::Hovered;
|
|
|
|
}
|
|
|
|
|
|
|
|
hovered_entity = Some(entity);
|
|
|
|
}
|
|
|
|
match focus_policy.cloned().unwrap_or(FocusPolicy::Block) {
|
|
|
|
FocusPolicy::Block => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// if there is a new hovered entity, but an entity is currently hovered, unhover the old entity
|
|
|
|
if let Some(new_hovered_entity) = hovered_entity {
|
|
|
|
if let Some(old_hovered_entity) = state.hovered_entity {
|
|
|
|
if new_hovered_entity != old_hovered_entity {
|
|
|
|
if let Ok(mut hover) = node_query.get_mut(old_hovered_entity) {
|
|
|
|
if *hover == Hover::Hovered {
|
|
|
|
*hover = Hover::NotHovered;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.hovered_entity = None;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-20 03:52:15 +00:00
|
|
|
state.hovered_entity = hovered_entity;
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|