Files
bevy/examples/ui/widgets/feathers_counter.rs
laund 5897ed605b BSN: changing prefixes for Template and SceneComponent to allow removing confusing "inheritance" concept (#24367)
# Objective

Changes discussed with cart:
- `:` will not be called "inheritance" anymore, but instead "cacheable"
(name not final)
- the Template patching prefix changes `@` -> `~`
- the SceneComponent prefix changes `:` -> `@`
- the `:` prefix is allowed in front of `@SceneComponent` as well,
marking it as being cached

>> me: do we forbid caching of functions which take an argument?

> cart: We should forbid it until we support it, and I don't plan on
supporting this in the short term, as it adds new controversial design
aspects to the system (building a mapping from parameters to cached
scenes, ensuring the parameters are all hashable, etc).

Link to the discord thread, specifically my [recap
message](https://discord.com/channels/691052431525675048/1264881140007702558/1506422311056576702).

The exact reasoning is in the linked discord discussion, but the tldr
is:
"Inheritance" as a term for the ":" syntax didn't really fit what bsn
was doing, since scenes without `:` produce the same result. All `:` is,
is a way to say "cache this pls" with the design of bsn imposing the
limitation that only the first scene can be cached

That was all fine, except for one case: Disambiguating SceneComponents
from normal components. So if we redefine `:` to just mean "caching" we
needed to untangle it from SceneComponents. That means disambiguating
SceneComponents needed a different syntax, and the decision ended up
being to take the @ prefix from the *much* less common `@Template`
usecase, since `SceneComponent {@prop: val}` already used @ as well and
introduce a new prefix to replace `@Template` instead, replacing it with
`~Template`.

## Solution

Implement these changes in macros.

Notably, this *still* doesn't implement/enable caching right now.  

Most docs are updated, except for the big blocks in
`bevy_scene/src/lib.rs` and `bevy_scene/macros/src/lib.rs` since i've
been working on reworking those on another branch and didn't want to
duplicate whats already been a lot of work and likely to be a bunch more

## Testing

- [x] `bevy_scene` tests passing
- [x] `cargo check --workspace` and `cargo check --workspace --examples`
passing

Since this PR does not yet enable caching, I think passing the current
tests should be enough.

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
Co-authored-by: François Mockers <francois.mockers@vleue.com>
2026-05-21 15:37:35 +00:00

92 lines
2.5 KiB
Rust

//! This example shows how to setup a basic counter app using feathers
//!
//! To use feathers in your bevy app, you need to use the `bevy_feathers` feature
use bevy::{
feathers::{
controls::FeathersButton,
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 [
(
@FeathersButton
on(|_activate: On<Activate>, mut counter: ResMut<Counter>| {
counter.0 -= 1;
})
Children [ (Text("-1") ThemedText) ]
),
(
Node {
margin: UiRect::horizontal(px(10.0)),
}
Text("0") ThemedText CounterText
),
(
@FeathersButton
on(|_activate: On<Activate>, mut counter: ResMut<Counter>| {
counter.0 += 1;
})
Children [ (Text("+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);
}