This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
enter_exit.rs
151 lines (136 loc) · 4.15 KB
/
enter_exit.rs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use bevy::prelude::*;
use bevy_quill::prelude::*;
pub struct EnterExitPlugin;
impl Plugin for EnterExitPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, enter_exit_state_machine);
}
}
/// Tracks an enter / exit transition. This is useful for widgets like dialog boxes and popup
/// menus which have an opening and closing animation.
#[derive(Default, Clone, PartialEq)]
pub enum EnterExitState {
/// One-frame delay at start of entering.
EnterStart,
/// Opening animation.
Entering,
/// Fully open
Entered,
/// One frame delay at start of exiting
ExitStart,
// Closing animation
Exiting,
/// Fully closed
#[default]
Exited,
}
impl EnterExitState {
/// Convert an Enter/Exit state into a class name.
pub fn as_class_name(&self) -> &str {
match self {
EnterExitState::EnterStart => "enter-start",
EnterExitState::Entering => "entering",
EnterExitState::Entered => "entered",
EnterExitState::ExitStart => "exit-start",
EnterExitState::Exiting => "exiting",
EnterExitState::Exited => "exited",
}
}
}
#[derive(Component, Default)]
pub struct EnterExit {
pub open: bool,
pub delay: f32,
pub state: EnterExitState,
}
#[derive(Component, Default)]
pub struct EnterExitTimer {
pub timer: f32,
}
/// Trait which adds `use_enter_exit` to [`Cx`].
pub trait EnterExitApi {
fn use_enter_exit(&mut self, open: bool, delay: f32) -> EnterExitState;
}
impl<'w, 'p, Props> EnterExitApi for Cx<'w, 'p, Props> {
fn use_enter_exit(&mut self, open: bool, delay: f32) -> EnterExitState {
self.use_effect(
|mut ve| {
match ve.get_mut::<EnterExit>() {
Some(mut ee) => {
ee.open = open;
}
None => {
ve.insert((
EnterExit {
open,
delay,
..default()
},
EnterExitTimer { ..default() },
));
}
};
},
open,
);
self.use_view_component::<EnterExit>()
.unwrap()
.state
.clone()
}
}
pub fn enter_exit_state_machine(
mut query: Query<(&mut EnterExit, &mut EnterExitTimer)>,
time: Res<Time>,
) {
for (mut ee, mut tt) in query.iter_mut() {
match ee.state {
EnterExitState::EnterStart => {
if ee.open {
ee.state = EnterExitState::Entering;
tt.timer = 0.;
} else {
ee.state = EnterExitState::ExitStart;
}
}
EnterExitState::Entering => {
if ee.open {
tt.timer += time.delta_seconds();
if tt.timer > ee.delay {
ee.state = EnterExitState::Entered;
}
} else {
ee.state = EnterExitState::ExitStart;
}
}
EnterExitState::Entered => {
if !ee.open {
ee.state = EnterExitState::ExitStart;
}
}
EnterExitState::ExitStart => {
if !ee.open {
ee.state = EnterExitState::Exiting;
tt.timer = 0.;
} else {
ee.state = EnterExitState::EnterStart;
}
}
EnterExitState::Exiting => {
if ee.open {
ee.state = EnterExitState::EnterStart;
} else {
tt.timer += time.delta_seconds();
if tt.timer > 0.3 {
ee.state = EnterExitState::Exited;
}
}
}
EnterExitState::Exited => {
if ee.open {
ee.state = EnterExitState::EnterStart;
}
}
}
}
}