docs/bevy/examples/scene/bsn.rs 1.6 K · 62 lines · raw · history

1 //! This example demonstrates how to use BSN to compose scenes.
2 use bevy::{prelude::*, text::FontSourceTemplate, ui_widgets::Button};
3
4 fn main() {
5 App::new()
6 .add_plugins(DefaultPlugins)
7 .add_systems(Startup, scene.spawn())
8 .run();
9 }
10
11 fn scene() -> impl SceneList {
12 bsn_list! {
13 Camera2d
14 --
15 @ui()
16 }
17 }
18
19 fn ui() -> impl Scene {
20 bsn! {
21 Node {
22 width: percent(100),
23 height: percent(100),
24 align_items: AlignItems::Center,
25 justify_content: JustifyContent::Center,
26 column_gap: px(5),
27 }
28 Children [
29 @button("Ok")
30 on(|_event: On<PointerPress>| println!("Ok pressed!"))
31 --
32 @button("Cancel")
33 on(|_event: On<PointerPress>| println!("Cancel pressed!"))
34 BackgroundColor(Color::srgb(0.4, 0.15, 0.15))
35 ]
36 }
37 }
38
39 fn button(label: &str) -> impl Scene {
40 bsn! {
41 Button
42 Node {
43 width: px(150),
44 height: px(65),
45 border: px(5),
46 border_radius: BorderRadius::MAX,
47 justify_content: JustifyContent::Center,
48 align_items: AlignItems::Center,
49 }
50 BorderColor::from(Color::BLACK)
51 BackgroundColor(Color::srgb(0.15, 0.15, 0.15))
52 Children [
53 Text(label)
54 TextFont {
55 font: FontSourceTemplate::Handle("fonts/FiraSans-Bold.ttf"),
56 font_size: px(33.0),
57 }
58 TextColor(Color::srgb(0.9, 0.9, 0.9))
59 TextShadow
60 ]
61 }
62 }