Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/bevy_scene/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ pub mod prelude {
pub use crate::{
bsn, bsn_list, on, template_value, CommandsSceneExt, EntityCommandsSceneExt,
EntityWorldMutSceneExt, PatchFromTemplate, PatchTemplate, Scene, SceneList,
ScenePatchInstance, WorldSceneExt,
ScenePatchInstance, SpawnListSystem, SpawnSystem, WorldSceneExt,
};
}

Expand All @@ -523,13 +523,15 @@ mod scene;
mod scene_list;
mod scene_patch;
mod spawn;
mod spawn_system;

pub use bevy_scene_macros::*;
pub use resolved_scene::*;
pub use scene::*;
pub use scene_list::*;
pub use scene_patch::*;
pub use spawn::*;
pub use spawn_system::*;

use bevy_app::{App, Plugin, SceneSpawnerSystems, SpawnScene};
use bevy_asset::AssetApp;
Expand Down
35 changes: 35 additions & 0 deletions crates/bevy_scene/src/spawn_system.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::{Scene, SceneList, WorldSceneExt};
use bevy_ecs::{error::Result, world::World};

/// Returns a system that spawns the given [`Scene`]. This should generally only be added to
/// schedules that run once, such as [`Startup`](bevy_app::Startup).
pub trait SpawnSystem {
/// Returns a system that spawns the given [`Scene`]. This should generally only be added to
/// schedules that run once, such as [`Startup`](bevy_app::Startup).
fn spawn(self) -> impl FnMut(&mut World) -> Result;
}

impl<F: FnMut() -> S + Send + Sync + 'static, S: Scene> SpawnSystem for F {
fn spawn(mut self) -> impl FnMut(&mut World) -> Result {
move |world: &mut World| -> Result {
world.spawn_scene(self())?;
Ok(())
}
}
}

/// Returns a system that spawns the given [`SceneList`]. This should generally only be added to
/// schedules that run once, such as [`Startup`](bevy_app::Startup).
pub trait SpawnListSystem {
/// Returns a system that spawns the given [`SceneList`]. This should generally only be added to
/// schedules that run once, such as [`Startup`](bevy_app::Startup).
fn spawn(self) -> impl FnMut(&mut World) -> Result;
}
impl<F: FnMut() -> S + Send + Sync + 'static, S: SceneList> SpawnListSystem for F {
fn spawn(mut self) -> impl FnMut(&mut World) -> Result {
move |world: &mut World| -> Result {
world.spawn_scene_list(self())?;
Ok(())
}
}
}
7 changes: 3 additions & 4 deletions examples/scene/bsn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ use bevy::{prelude::*, text::FontSourceTemplate};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Startup, scene.spawn())
.run();
}

fn setup(world: &mut World) -> Result {
world.spawn_scene_list(bsn_list![Camera2d, ui()])?;
Ok(())
fn scene() -> impl SceneList {
bsn_list![Camera2d, ui()]
}

fn ui() -> impl Scene {
Expand Down
8 changes: 3 additions & 5 deletions examples/ui/widgets/feathers_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use bevy::{
tokens, FeathersPlugins,
},
prelude::*,
scene::prelude::Scene,
ui_widgets::Activate,
};

Expand All @@ -31,17 +30,16 @@ fn main() {
// Configure feathers to use the dark theme
.insert_resource(UiTheme(create_dark_theme()))
.insert_resource(Counter(0))
.add_systems(Startup, setup)
.add_systems(Startup, scene.spawn())
.add_systems(
Update,
update_counter_text.run_if(resource_changed::<Counter>),
)
.run();
}

fn setup(world: &mut World) -> Result {
world.spawn_scene_list(bsn_list![Camera2d, demo_root()])?;
Ok(())
fn scene() -> impl SceneList {
bsn_list![Camera2d, demo_root()]
}

fn demo_root() -> impl Scene {
Expand Down
8 changes: 3 additions & 5 deletions examples/ui/widgets/feathers_gallery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use bevy::{
},
input_focus::{tab_navigation::TabGroup, AutoFocus, InputFocus},
prelude::*,
scene::prelude::Scene,
text::{EditableText, TextEdit, TextEditChange},
ui::{Checked, InteractionDisabled},
ui_widgets::{
Expand Down Expand Up @@ -64,14 +63,13 @@ fn main() {
rgb_color: palettes::tailwind::EMERALD_800.with_alpha(0.7),
hsl_color: palettes::tailwind::AMBER_800.into(),
})
.add_systems(Startup, setup)
.add_systems(Startup, scene.spawn())
.add_systems(Update, update_colors)
.run();
}

fn setup(world: &mut World) -> Result {
world.spawn_scene_list(bsn_list![Camera2d, demo_root()])?;
Ok(())
fn scene() -> impl SceneList {
bsn_list![Camera2d, demo_root()]
}

fn demo_root() -> impl Scene {
Expand Down
7 changes: 3 additions & 4 deletions examples/ui/widgets/virtual_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ fn main() {
App::new()
.add_plugins((DefaultPlugins, FeathersPlugins))
.insert_resource(UiTheme(create_dark_theme()))
.add_systems(Startup, setup)
.add_systems(Startup, scene.spawn())
.run();
}

fn on_virtual_key_pressed(virtual_key_pressed: On<VirtualKeyPressed<&'static str>>) {
println!("key pressed: {}", virtual_key_pressed.key);
}

fn setup(world: &mut World) -> Result {
world.spawn_scene_list(bsn_list![Camera2d, keyboard()])?;
Ok(())
fn scene() -> impl SceneList {
bsn_list![Camera2d, keyboard()]
}

fn keyboard() -> impl Scene {
Expand Down