From 2f9c42bb339deffa9a254244cc7c3bedf7b427f6 Mon Sep 17 00:00:00 2001 From: Alessio Marchi <65831826+kettei-sproutty@users.noreply.github.com> Date: Thu, 20 Jun 2024 03:02:16 +0200 Subject: [PATCH] feat: add insert_after and insert_startup_before (#13941) # Objective Fixes #13866 ## Solution Add `insert_before` in **FixedMainScheduleOrder** and **MainScheduleOrder**, add `insert_startup_before` in **MainScheduleOrder**, applying the same logic as `insert_after`, except for parameters naming and insertion index. --- crates/bevy_app/src/main_schedule.rs | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/crates/bevy_app/src/main_schedule.rs b/crates/bevy_app/src/main_schedule.rs index 1a22dc8a5c..eedca809e2 100644 --- a/crates/bevy_app/src/main_schedule.rs +++ b/crates/bevy_app/src/main_schedule.rs @@ -195,6 +195,16 @@ impl MainScheduleOrder { self.labels.insert(index + 1, schedule.intern()); } + /// Adds the given `schedule` before the `before` schedule in the main list of schedules. + pub fn insert_before(&mut self, before: impl ScheduleLabel, schedule: impl ScheduleLabel) { + let index = self + .labels + .iter() + .position(|current| (**current).eq(&before)) + .unwrap_or_else(|| panic!("Expected {before:?} to exist")); + self.labels.insert(index, schedule.intern()); + } + /// Adds the given `schedule` after the `after` schedule in the list of startup schedules. pub fn insert_startup_after( &mut self, @@ -208,6 +218,20 @@ impl MainScheduleOrder { .unwrap_or_else(|| panic!("Expected {after:?} to exist")); self.startup_labels.insert(index + 1, schedule.intern()); } + + /// Adds the given `schedule` before the `before` schedule in the list of startup schedules. + pub fn insert_startup_before( + &mut self, + before: impl ScheduleLabel, + schedule: impl ScheduleLabel, + ) { + let index = self + .startup_labels + .iter() + .position(|current| (**current).eq(&before)) + .unwrap_or_else(|| panic!("Expected {before:?} to exist")); + self.startup_labels.insert(index, schedule.intern()); + } } impl Main { @@ -291,6 +315,16 @@ impl FixedMainScheduleOrder { .unwrap_or_else(|| panic!("Expected {after:?} to exist")); self.labels.insert(index + 1, schedule.intern()); } + + /// Adds the given `schedule` before the `before` schedule + pub fn insert_before(&mut self, before: impl ScheduleLabel, schedule: impl ScheduleLabel) { + let index = self + .labels + .iter() + .position(|current| (**current).eq(&before)) + .unwrap_or_else(|| panic!("Expected {before:?} to exist")); + self.labels.insert(index, schedule.intern()); + } } impl FixedMain {