use crate::PickingPluginState;
use bevy::prelude::*;
#[derive(Debug, Copy, Clone)]
pub struct Selection {
selected: bool,
}
impl Selection {
pub fn selected(&self) -> bool {
self.selected
}
pub fn set_selected(&mut self, selected: bool) {
self.selected = selected;
}
}
impl Default for Selection {
fn default() -> Self {
Selection { selected: false }
}
}
#[derive(Debug, Copy, Clone)]
pub struct NoDeselect;
#[allow(clippy::too_many_arguments)]
pub fn mesh_selection(
state: Res<PickingPluginState>,
mouse_button_input: Res<Input<MouseButton>>,
touches_input: Res<Touches>,
keyboard_input: Res<Input<KeyCode>>,
query_changed: Query<&Interaction, (Changed<Interaction>, Without<NoDeselect>)>,
mut query_all: Query<(&mut Selection, &Interaction)>,
node_query: Query<&Interaction, With<Node>>,
no_deselect_query: Query<&Interaction, With<NoDeselect>>,
) {
if state.paused_for_ui || !state.enabled {
return;
}
let mut new_selection = false;
for interaction in query_changed.iter() {
if *interaction == Interaction::Clicked {
new_selection = true;
}
}
if keyboard_input.pressed(KeyCode::LControl) && keyboard_input.pressed(KeyCode::A) {
for (mut selection, _interaction) in &mut query_all.iter_mut() {
if !selection.selected {
selection.selected = true;
}
}
} else if new_selection {
for (mut selection, interaction) in &mut query_all.iter_mut() {
if selection.selected
&& *interaction != Interaction::Clicked
&& !keyboard_input.pressed(KeyCode::LControl)
{
selection.selected = false;
} else if *interaction == Interaction::Clicked
&& keyboard_input.pressed(KeyCode::LControl)
{
selection.selected = !selection.selected
} else if !selection.selected && *interaction == Interaction::Clicked {
selection.selected = true;
}
}
} else {
let mut ui_not_clicked = true;
for interaction in node_query.iter() {
if *interaction == Interaction::Clicked && !keyboard_input.pressed(KeyCode::LControl) {
ui_not_clicked = false;
}
}
let mut no_deselect_not_clicked = true;
for interaction in no_deselect_query.iter() {
if *interaction == Interaction::Clicked && !keyboard_input.pressed(KeyCode::LControl) {
no_deselect_not_clicked = false;
}
}
let mouse_clicked =
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.just_released(0);
if mouse_clicked && ui_not_clicked && no_deselect_not_clicked {
for (mut selection, _interaction) in &mut query_all.iter_mut() {
if selection.selected {
selection.selected = false;
}
}
}
}
}