forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
system_descriptor.rs
274 lines (231 loc) · 9.43 KB
/
system_descriptor.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use crate::{
schedule::{IntoRunCriteria, RunCriteriaDescriptorOrLabel, SystemLabel, SystemLabelId},
system::{AsSystemLabel, BoxedSystem, IntoSystem},
};
/// Configures ambiguity detection for a single system.
#[derive(Debug, Default)]
pub(crate) enum AmbiguityDetection {
#[default]
Check,
IgnoreAll,
/// Ignore systems with any of these labels.
IgnoreWithLabel(Vec<SystemLabelId>),
}
/// Encapsulates a system and information on when it run in a `SystemStage`.
///
/// Systems can be inserted into 4 different groups within the stage:
/// * Parallel, accepts non-exclusive systems.
/// * At start, accepts exclusive systems; runs before parallel systems.
/// * Before commands, accepts exclusive systems; runs after parallel systems, but before their
/// command buffers are applied.
/// * At end, accepts exclusive systems; runs after parallel systems' command buffers have
/// been applied.
///
/// Systems can have one or more labels attached to them; other systems in the same group
/// can then specify that they have to run before or after systems with that label using the
/// `before` and `after` methods.
///
/// # Example
/// ```
/// # use bevy_ecs::prelude::*;
/// # fn do_something() {}
/// # fn do_the_other_thing() {}
/// # fn do_something_else() {}
/// #[derive(SystemLabel, Debug, Clone, PartialEq, Eq, Hash)]
/// struct Something;
///
/// SystemStage::parallel()
/// .with_system(do_something.label(Something))
/// .with_system(do_the_other_thing.after(Something))
/// .with_system(do_something_else.at_end());
/// ```
#[derive(Debug)]
pub struct SystemDescriptor {
pub(crate) system: BoxedSystem<(), ()>,
pub(crate) exclusive_insertion_point: Option<ExclusiveInsertionPoint>,
pub(crate) run_criteria: Option<RunCriteriaDescriptorOrLabel>,
pub(crate) labels: Vec<SystemLabelId>,
pub(crate) before: Vec<SystemLabelId>,
pub(crate) after: Vec<SystemLabelId>,
pub(crate) ambiguity_detection: AmbiguityDetection,
}
impl SystemDescriptor {
fn new(system: BoxedSystem<(), ()>) -> SystemDescriptor {
SystemDescriptor {
labels: system.default_labels(),
exclusive_insertion_point: if system.is_exclusive() {
Some(ExclusiveInsertionPoint::AtStart)
} else {
None
},
system,
run_criteria: None,
before: Vec::new(),
after: Vec::new(),
ambiguity_detection: Default::default(),
}
}
}
pub trait IntoSystemDescriptor<Params> {
fn into_descriptor(self) -> SystemDescriptor;
/// Assigns a run criteria to the system. Can be a new descriptor or a label of a
/// run criteria defined elsewhere.
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> SystemDescriptor;
/// Assigns a label to the system; there can be more than one, and it doesn't have to be unique.
fn label(self, label: impl SystemLabel) -> SystemDescriptor;
/// Specifies that the system should run before systems with the given label.
fn before<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor;
/// Specifies that the system should run after systems with the given label.
fn after<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor;
/// Marks this system as ambiguous with any system with the specified label.
/// This means that execution order between these systems does not matter,
/// which allows [some warnings](crate::schedule::ReportExecutionOrderAmbiguities) to be silenced.
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor;
/// Specifies that this system should opt out of
/// [execution order ambiguity detection](crate::schedule::ReportExecutionOrderAmbiguities).
fn ignore_all_ambiguities(self) -> SystemDescriptor;
/// Specifies that the system should run with other exclusive systems at the start of stage.
fn at_start(self) -> SystemDescriptor;
/// Specifies that the system should run with other exclusive systems after the parallel
/// systems and before command buffer application.
fn before_commands(self) -> SystemDescriptor;
/// Specifies that the system should run with other exclusive systems at the end of stage.
fn at_end(self) -> SystemDescriptor;
}
impl IntoSystemDescriptor<()> for SystemDescriptor {
fn with_run_criteria<Marker>(
mut self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> SystemDescriptor {
self.run_criteria = Some(run_criteria.into());
self
}
fn label(mut self, label: impl SystemLabel) -> SystemDescriptor {
self.labels.push(label.as_label());
self
}
fn before<Marker>(mut self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
self.before.push(label.as_system_label().as_label());
self
}
fn after<Marker>(mut self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
self.after.push(label.as_system_label().as_label());
self
}
fn ambiguous_with<Marker>(mut self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
match &mut self.ambiguity_detection {
detection @ AmbiguityDetection::Check => {
*detection =
AmbiguityDetection::IgnoreWithLabel(vec![label.as_system_label().as_label()]);
}
AmbiguityDetection::IgnoreWithLabel(labels) => {
labels.push(label.as_system_label().as_label());
}
// This descriptor is already ambiguous with everything.
AmbiguityDetection::IgnoreAll => {}
}
self
}
fn ignore_all_ambiguities(mut self) -> SystemDescriptor {
self.ambiguity_detection = AmbiguityDetection::IgnoreAll;
self
}
fn at_start(mut self) -> SystemDescriptor {
self.exclusive_insertion_point = Some(ExclusiveInsertionPoint::AtStart);
self
}
fn before_commands(mut self) -> SystemDescriptor {
self.exclusive_insertion_point = Some(ExclusiveInsertionPoint::BeforeCommands);
self
}
fn at_end(mut self) -> SystemDescriptor {
self.exclusive_insertion_point = Some(ExclusiveInsertionPoint::AtEnd);
self
}
fn into_descriptor(self) -> SystemDescriptor {
self
}
}
impl<S, Params> IntoSystemDescriptor<Params> for S
where
S: IntoSystem<(), (), Params>,
{
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self)))
.with_run_criteria(run_criteria)
}
fn label(self, label: impl SystemLabel) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).label(label)
}
fn before<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).before(label)
}
fn after<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).after(label)
}
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).ambiguous_with(label)
}
fn ignore_all_ambiguities(self) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).ignore_all_ambiguities()
}
fn at_start(self) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).at_start()
}
fn before_commands(self) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).before_commands()
}
fn at_end(self) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self))).at_end()
}
fn into_descriptor(self) -> SystemDescriptor {
SystemDescriptor::new(Box::new(IntoSystem::into_system(self)))
}
}
impl IntoSystemDescriptor<()> for BoxedSystem<(), ()> {
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> SystemDescriptor {
SystemDescriptor::new(self).with_run_criteria(run_criteria)
}
fn label(self, label: impl SystemLabel) -> SystemDescriptor {
SystemDescriptor::new(self).label(label)
}
fn before<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
SystemDescriptor::new(self).before(label)
}
fn after<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
SystemDescriptor::new(self).after(label)
}
fn ambiguous_with<Marker>(self, label: impl AsSystemLabel<Marker>) -> SystemDescriptor {
SystemDescriptor::new(self).ambiguous_with(label)
}
fn ignore_all_ambiguities(self) -> SystemDescriptor {
SystemDescriptor::new(self).ignore_all_ambiguities()
}
fn at_start(self) -> SystemDescriptor {
SystemDescriptor::new(self).at_start()
}
fn before_commands(self) -> SystemDescriptor {
SystemDescriptor::new(self).before_commands()
}
fn at_end(self) -> SystemDescriptor {
SystemDescriptor::new(self).at_end()
}
fn into_descriptor(self) -> SystemDescriptor {
SystemDescriptor::new(self)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum ExclusiveInsertionPoint {
AtStart,
BeforeCommands,
AtEnd,
}