1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::PausedForBlockers;
use bevy::prelude::*;
#[derive(Component, Debug, Default, 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;
}
}
#[derive(Component, Debug, Copy, Clone)]
pub struct NoDeselect;
#[allow(clippy::too_many_arguments)]
pub fn mesh_selection(
paused: Option<Res<PausedForBlockers>>,
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 let Some(paused) = paused {
if paused.0 {
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) {
query_all.for_each_mut(|(mut selection, _)| {
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.iter_just_pressed().next().is_some();
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;
}
}
}
}
}