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