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
105
106
107
108
109
110
111
112
113
use super::selection::*;
use crate::PausedForBlockers;
use bevy::{asset::Asset, prelude::*};
#[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component)]
pub struct Highlight;
#[derive(Component, Clone, Debug, Reflect)]
pub struct Highlighting<T: Asset> {
pub initial: Handle<T>,
pub hovered: Option<Handle<T>>,
pub pressed: Option<Handle<T>>,
pub selected: Option<Handle<T>>,
}
#[derive(Clone, Debug, Resource)]
pub struct DefaultHighlighting<T: Asset> {
pub hovered: Handle<T>,
pub pressed: Handle<T>,
pub selected: Handle<T>,
}
#[allow(clippy::type_complexity)]
pub fn get_initial_mesh_highlight_asset<T: Asset>(
mut commands: Commands,
entity_asset_query: Query<(Entity, &Handle<T>), Added<Highlight>>,
mut highlighting_query: Query<Option<&mut Highlighting<T>>>,
) {
for (entity, material) in entity_asset_query.iter() {
match highlighting_query.get_mut(entity) {
Ok(Some(mut highlighting)) => highlighting.initial = material.to_owned(),
_ => {
let init_component = Highlighting {
initial: material.to_owned(),
hovered: None,
pressed: None,
selected: None,
};
commands.entity(entity).insert(init_component);
}
}
}
}
#[allow(clippy::type_complexity)]
pub fn mesh_highlighting<T: 'static + Asset + Send + Sync>(
paused: Option<Res<PausedForBlockers>>,
global_default_highlight: Res<DefaultHighlighting<T>>,
mut interaction_query: Query<
(
&Interaction,
&mut Handle<T>,
Option<&Selection>,
&Highlighting<T>,
),
Or<(Changed<Interaction>, Changed<Selection>)>,
>,
) {
if let Some(paused) = paused {
if paused.is_paused() {
for (_, mut material, selection, highlight) in interaction_query.iter_mut() {
*material = if selection.filter(|s| s.selected()).is_some() {
if let Some(highlight_asset) = &highlight.selected {
highlight_asset
} else {
&global_default_highlight.selected
}
} else {
&highlight.initial
}
.to_owned();
}
return;
}
}
for (interaction, mut material, selection, highlight) in interaction_query.iter_mut() {
*material = match *interaction {
Interaction::Clicked => {
if let Some(highlight_asset) = &highlight.pressed {
highlight_asset
} else {
&global_default_highlight.pressed
}
}
Interaction::Hovered => {
if let Some(highlight_asset) = &highlight.hovered {
highlight_asset
} else {
&global_default_highlight.hovered
}
}
Interaction::None => {
if selection.filter(|s| s.selected()).is_some() {
if let Some(highlight_asset) = &highlight.selected {
highlight_asset
} else {
&global_default_highlight.selected
}
} else {
&highlight.initial
}
}
}
.to_owned();
}
}