-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhighlight.rs
More file actions
61 lines (55 loc) · 1.47 KB
/
Copy pathhighlight.rs
File metadata and controls
61 lines (55 loc) · 1.47 KB
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
use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
type OnEnter = On<Pointer<Over>>;
type OnExit = On<Pointer<Out>>;
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct Highlight {
highlight: Color,
previous: Option<Color>,
}
impl Highlight {
#[must_use]
pub fn new(highlight: Color) -> Self {
Self { highlight, previous: None }
}
}
fn insert_events(mut cmds: Commands, query: Query<Entity, (With<Highlight>, Without<OnEnter>)>) {
for entity in &query {
cmds.entity(entity)
.insert((OnEnter::run(highlight), OnExit::run(unhighlight)));
}
}
fn highlight(
event: Res<ListenerInput<Pointer<Over>>>,
mut query: Query<(&mut Highlight, &mut BackgroundColor)>,
) {
let Ok((mut high, mut bg)) = query.get_mut(event.listener()) else {
return;
};
high.previous = Some(bg.0);
bg.0 = high.highlight;
}
fn unhighlight(
event: Res<ListenerInput<Pointer<Out>>>,
mut query: Query<(&mut Highlight, &mut BackgroundColor)>,
) {
let Ok((mut high, mut bg)) = query.get_mut(event.listener()) else {
return;
};
let Some(prev) = high.previous else {
return;
};
if high.highlight == bg.0 {
bg.0 = prev;
} else {
high.previous = Some(bg.0);
}
}
pub struct HighlightPlugin;
impl Plugin for HighlightPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Highlight>()
.add_systems(Update, insert_events);
}
}