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