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