use crate::{ component::Component, schedule::{ AmbiguitySetLabel, BoxedAmbiguitySetLabel, BoxedSystemLabel, IntoRunCriteria, RunCriteriaDescriptorOrLabel, State, SystemDescriptor, SystemLabel, }, }; use std::{fmt::Debug, hash::Hash}; /// A builder for describing several systems at the same time. pub struct SystemSet { pub(crate) systems: Vec, pub(crate) run_criteria: Option, pub(crate) labels: Vec, pub(crate) before: Vec, pub(crate) after: Vec, pub(crate) ambiguity_sets: Vec, } impl Default for SystemSet { fn default() -> SystemSet { SystemSet { systems: Vec::new(), run_criteria: None, labels: Vec::new(), before: Vec::new(), after: Vec::new(), ambiguity_sets: Vec::new(), } } } impl SystemSet { pub fn new() -> Self { Default::default() } pub fn on_update(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_update(s)) } pub fn on_inactive_update(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_inactive_update(s)) } pub fn on_in_stack_update(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_in_stack_update(s)) } pub fn on_enter(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_enter(s)) } pub fn on_exit(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_exit(s)) } pub fn on_pause(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_pause(s)) } pub fn on_resume(s: T) -> SystemSet where T: Component + Debug + Clone + Eq + Hash, { Self::new().with_run_criteria(State::::on_resume(s)) } pub fn in_ambiguity_set(mut self, set: impl AmbiguitySetLabel) -> Self { self.ambiguity_sets.push(Box::new(set)); self } pub fn with_system(mut self, system: impl Into) -> Self { self.systems.push(system.into()); self } pub fn with_run_criteria(mut self, run_criteria: impl IntoRunCriteria) -> Self { self.run_criteria = Some(run_criteria.into()); self } pub fn label(mut self, label: impl SystemLabel) -> Self { self.labels.push(Box::new(label)); self } pub fn before(mut self, label: impl SystemLabel) -> Self { self.before.push(Box::new(label)); self } pub fn after(mut self, label: impl SystemLabel) -> Self { self.after.push(Box::new(label)); self } pub(crate) fn bake(self) -> (Option, Vec) { let SystemSet { mut systems, run_criteria, labels, before, after, ambiguity_sets, } = self; for descriptor in &mut systems { match descriptor { SystemDescriptor::Parallel(descriptor) => { descriptor.labels.extend(labels.iter().cloned()); descriptor.before.extend(before.iter().cloned()); descriptor.after.extend(after.iter().cloned()); descriptor .ambiguity_sets .extend(ambiguity_sets.iter().cloned()); } SystemDescriptor::Exclusive(descriptor) => { descriptor.labels.extend(labels.iter().cloned()); descriptor.before.extend(before.iter().cloned()); descriptor.after.extend(after.iter().cloned()); descriptor .ambiguity_sets .extend(ambiguity_sets.iter().cloned()); } } } (run_criteria, systems) } }