|
| 1 | +use crate::schedule_v3::{CurrentState, State}; |
| 2 | +use crate::system::{BoxedSystem, NonSend, Res, Resource}; |
| 3 | + |
| 4 | +pub type BoxedRunCondition = BoxedSystem<(), bool>; |
| 5 | + |
| 6 | +/// Implemented for functions and closures that convert into [`System<In=(), Out=bool>`](crate::system::System) |
| 7 | +/// types that have [read-only](crate::system::ReadOnlySystemParamFetch) data access. |
| 8 | +pub trait IntoRunCondition<Params>: sealed::IntoRunCondition<Params> {} |
| 9 | + |
| 10 | +impl<Params, F> IntoRunCondition<Params> for F where F: sealed::IntoRunCondition<Params> {} |
| 11 | + |
| 12 | +mod sealed { |
| 13 | + use crate::system::{ |
| 14 | + IntoSystem, IsFunctionSystem, ReadOnlySystemParamFetch, SystemParam, SystemParamFunction, |
| 15 | + }; |
| 16 | + |
| 17 | + // This trait is private to prevent implementations for systems that aren't read-only. |
| 18 | + pub trait IntoRunCondition<Params>: IntoSystem<(), bool, Params> {} |
| 19 | + |
| 20 | + impl<Params, Marker, F> IntoRunCondition<(IsFunctionSystem, Params, Marker)> for F |
| 21 | + where |
| 22 | + F: SystemParamFunction<(), bool, Params, Marker> + Send + Sync + 'static, |
| 23 | + Params: SystemParam + 'static, |
| 24 | + Params::Fetch: ReadOnlySystemParamFetch, |
| 25 | + Marker: 'static, |
| 26 | + { |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 31 | +/// if the resource exists. |
| 32 | +pub fn resource_exists<T>() -> impl FnMut(Option<Res<T>>) -> bool |
| 33 | +where |
| 34 | + T: Resource, |
| 35 | +{ |
| 36 | + move |res: Option<Res<T>>| res.is_some() |
| 37 | +} |
| 38 | + |
| 39 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 40 | +/// if the resource is equal to `value`. |
| 41 | +/// |
| 42 | +/// # Panics |
| 43 | +/// |
| 44 | +/// The condition will panic if the resource does not exist. |
| 45 | +pub fn resource_equals<T>(value: T) -> impl FnMut(Res<T>) -> bool |
| 46 | +where |
| 47 | + T: Resource + PartialEq, |
| 48 | +{ |
| 49 | + move |res: Res<T>| *res == value |
| 50 | +} |
| 51 | + |
| 52 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 53 | +/// if the resource exists and is equal to `value`. |
| 54 | +pub fn resource_exists_and_equals<T>(value: T) -> impl FnMut(Option<Res<T>>) -> bool |
| 55 | +where |
| 56 | + T: Resource + PartialEq, |
| 57 | +{ |
| 58 | + move |res: Option<Res<T>>| match res { |
| 59 | + Some(res) => *res == value, |
| 60 | + None => false, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 65 | +/// if the non-[`Send`] resource exists. |
| 66 | +pub fn non_send_resource_exists<T>() -> impl FnMut(Option<NonSend<T>>) -> bool |
| 67 | +where |
| 68 | + T: Resource, |
| 69 | +{ |
| 70 | + move |res: Option<NonSend<T>>| res.is_some() |
| 71 | +} |
| 72 | + |
| 73 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 74 | +/// if the non-`Send` resource is equal to `value`. |
| 75 | +/// |
| 76 | +/// # Panics |
| 77 | +/// |
| 78 | +/// The condition will panic if the resource does not exist. |
| 79 | +pub fn non_send_resource_equals<T>(value: T) -> impl FnMut(NonSend<T>) -> bool |
| 80 | +where |
| 81 | + T: Resource + PartialEq, |
| 82 | +{ |
| 83 | + move |res: NonSend<T>| *res == value |
| 84 | +} |
| 85 | + |
| 86 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 87 | +/// if the non-[`Send`] resource exists and is equal to `value`. |
| 88 | +pub fn non_send_resource_exists_and_equals<T>(value: T) -> impl FnMut(Option<NonSend<T>>) -> bool |
| 89 | +where |
| 90 | + T: Resource + PartialEq, |
| 91 | +{ |
| 92 | + move |res: Option<NonSend<T>>| match res { |
| 93 | + Some(res) => *res == value, |
| 94 | + None => false, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 99 | +/// if the state machine exists. |
| 100 | +pub fn state_machine_exists<S: State>() -> impl FnMut(Option<Res<CurrentState<S>>>) -> bool { |
| 101 | + move |current_state: Option<Res<CurrentState<S>>>| current_state.is_some() |
| 102 | +} |
| 103 | + |
| 104 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 105 | +/// if the state machine is currently in `state`. |
| 106 | +/// |
| 107 | +/// # Panics |
| 108 | +/// |
| 109 | +/// The condition will panic if the resource does not exist. |
| 110 | +pub fn state_equals<S: State>(state: S) -> impl FnMut(Res<CurrentState<S>>) -> bool { |
| 111 | + move |current_state: Res<CurrentState<S>>| current_state.0 == state |
| 112 | +} |
| 113 | + |
| 114 | +/// Convenience function that generates an [`IntoRunCondition`]-compatible closure that returns `true` |
| 115 | +/// if the state machine exists and is currently in `state`. |
| 116 | +pub fn state_machine_exists_and_equals<S: State>( |
| 117 | + state: S, |
| 118 | +) -> impl FnMut(Option<Res<CurrentState<S>>>) -> bool { |
| 119 | + move |current_state: Option<Res<CurrentState<S>>>| match current_state { |
| 120 | + Some(current_state) => current_state.0 == state, |
| 121 | + None => false, |
| 122 | + } |
| 123 | +} |
0 commit comments