From cd1c0d2dc00420adf0a8e2a60e5386e3ef190198 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Wed, 8 Feb 2023 23:24:36 +0000 Subject: [PATCH] Add condition negation (#7559) # Objective Closes #7202 ## Solution ~~Introduce a `not` helper to pipe conditions. Opened mostly for discussion. Maybe create an extension trait with `not` method? Please, advice.~~ Introduce `not(condition)` condition that inverses the result of the passed. --- ## Changelog ### Added - `not` condition. --- crates/bevy_ecs/src/schedule/condition.rs | 40 +++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/schedule/condition.rs b/crates/bevy_ecs/src/schedule/condition.rs index 541ed202957b6c..763d88d8e09015 100644 --- a/crates/bevy_ecs/src/schedule/condition.rs +++ b/crates/bevy_ecs/src/schedule/condition.rs @@ -24,8 +24,11 @@ mod sealed { } pub mod common_conditions { - use crate::schedule::{State, States}; - use crate::system::{Res, Resource}; + use super::Condition; + use crate::{ + schedule::{State, States}, + system::{In, IntoPipeSystem, ReadOnlySystem, Res, Resource}, + }; /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true` /// if the first time the condition is run and false every time after @@ -105,4 +108,37 @@ pub mod common_conditions { None => false, } } + + /// Generates a [`Condition`](super::Condition) that inverses the result of passed one. + /// + /// # Examples + /// + /// ``` + /// use bevy_ecs::prelude::*; + /// // Building a new schedule/app... + /// let mut sched = Schedule::default(); + /// sched.add_system( + /// // This system will never run. + /// my_system.run_if(not(always_true)) + /// ) + /// // ... + /// # ; + /// # let mut world = World::new(); + /// # sched.run(&mut world); + /// + /// // A condition that always returns true. + /// fn always_true() -> bool { + /// true + /// } + /// # + /// # fn my_system() { unreachable!() } + /// ``` + pub fn not>( + condition: C, + ) -> impl ReadOnlySystem + where + C::System: ReadOnlySystem, + { + condition.pipe(|In(val): In| !val) + } }