Files
bevy/examples/scene/bsn.rs
Carter Anderson 6c0eb84e31 Reduce bsn! codegen / debug symbols by combining scene implementations (#24124)
# Objective

`bsn!` currently generates a lot of new types: one for each patch, which
then get assembled into a single Scene impl by composing them in a
tuple. This gives the compiler more work to do (types to verify, code to
generate, etc). It also makes our debug symbols bigger / harder to read.

## Solution

Combine `bsn!` entries, where possible, into a single `SceneFunction<F>`
implementation of `Scene`, which does all of the "scene patch" logic
internally. This significantly cuts down on codegen, and also makes the
functional behavior of a `bsn!` block more clear when expanded.
2026-05-06 21:16:44 +00:00

62 lines
1.6 KiB
Rust

//! This example demonstrates how to use BSN to compose scenes.
use bevy::{prelude::*, text::FontSourceTemplate};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, scene.spawn())
.run();
}
fn scene() -> impl SceneList {
bsn_list![Camera2d, ui()]
}
fn ui() -> impl Scene {
bsn! {
Node {
width: percent(100),
height: percent(100),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
column_gap: px(5),
}
Children [
(
button("Ok")
on(|_event: On<Pointer<Press>>| println!("Ok pressed!"))
),
(
button("Cancel")
on(|_event: On<Pointer<Press>>| println!("Cancel pressed!"))
BackgroundColor(Color::srgb(0.4, 0.15, 0.15))
),
]
}
}
fn button(label: &str) -> impl Scene {
bsn! {
Button
Node {
width: px(150),
height: px(65),
border: px(5),
border_radius: BorderRadius::MAX,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
}
BorderColor::from(Color::BLACK)
BackgroundColor(Color::srgb(0.15, 0.15, 0.15))
Children [(
Text(label)
TextFont {
font: FontSourceTemplate::Handle("fonts/FiraSans-Bold.ttf"),
font_size: px(33.0),
}
TextColor(Color::srgb(0.9, 0.9, 0.9))
TextShadow
)]
}
}