docs/bevy/examples/ui/widgets/feathers_counter.rs 2.4 K · 92 lines · raw · history

1 //! This example shows how to setup a basic counter app using feathers
2 //!
3 //! To use feathers in your bevy app, you need to use the `bevy_feathers` feature
4
5 use bevy::{
6 feathers::{
7 controls::FeathersButton,
8 dark_theme::create_dark_theme,
9 display::caption,
10 theme::{ThemeBackgroundColor, UiTheme},
11 tokens, FeathersPlugins,
12 },
13 prelude::*,
14 ui_widgets::Activate,
15 };
16
17 #[derive(Resource)]
18 struct Counter(i32);
19
20 #[derive(Component, Default, Clone)]
21 struct CounterText;
22
23 fn main() {
24 App::new()
25 .add_plugins((
26 DefaultPlugins,
27 // Don't forget to add the plugin.
28 // Make sure you are using FeathersPlugins with an `s`
29 FeathersPlugins,
30 ))
31 // Configure feathers to use the dark theme
32 .insert_resource(UiTheme(create_dark_theme()))
33 .insert_resource(Counter(0))
34 .add_systems(Startup, scene.spawn())
35 .add_systems(
36 Update,
37 update_counter_text.run_if(resource_changed::<Counter>),
38 )
39 .run();
40 }
41
42 fn scene() -> impl SceneList {
43 bsn_list! {
44 Camera2d
45 --
46 @demo_root()
47 }
48 }
49
50 fn demo_root() -> impl Scene {
51 bsn! {
52 Node {
53 width: percent(100),
54 height: percent(100),
55 align_items: AlignItems::Center,
56 justify_content: JustifyContent::Center,
57 }
58 ThemeBackgroundColor(tokens::WINDOW_BG)
59 Children[
60 Node {
61 align_items: AlignItems::Center,
62 justify_content: JustifyContent::Center,
63 }
64 Children [
65 @FeathersButton
66 on(|_activate: On<Activate>, mut counter: ResMut<Counter>| {
67 counter.0 -= 1;
68 })
69 Children [ @caption("-1") ]
70 --
71 Node {
72 margin: UiRect::horizontal(px(10.0)),
73 }
74 @caption("0") CounterText
75 --
76 @FeathersButton
77 on(|_activate: On<Activate>, mut counter: ResMut<Counter>| {
78 counter.0 += 1;
79 })
80 Children [ @caption("+1") ]
81 ]
82 ]
83 }
84 }
85
86 fn update_counter_text(
87 counter: Res<Counter>,
88 mut counter_text: Single<&mut Text, With<CounterText>>,
89 ) {
90 info!("Counter updated");
91 counter_text.0 = format!("{}", counter.0);
92 }