Files
bevy/examples/ui/widgets/virtual_keyboard.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

69 lines
1.9 KiB
Rust

//! Virtual keyboard example
use bevy::{
color::palettes::css::NAVY,
feathers::{
controls::{VirtualKeyPressed, VirtualKeyboard},
dark_theme::create_dark_theme,
theme::UiTheme,
FeathersPlugins,
},
prelude::*,
};
fn main() {
App::new()
.add_plugins((DefaultPlugins, FeathersPlugins))
.insert_resource(UiTheme(create_dark_theme()))
.add_systems(Startup, scene.spawn())
.run();
}
fn on_virtual_key_pressed(virtual_key_pressed: On<VirtualKeyPressed<&'static str>>) {
println!("key pressed: {}", virtual_key_pressed.key);
}
fn scene() -> impl SceneList {
bsn_list![Camera2d, keyboard()]
}
fn keyboard() -> impl Scene {
let keys = [
vec!["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", ","],
vec!["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
vec!["A", "S", "D", "F", "G", "H", "J", "K", "L", "'"],
vec!["Z", "X", "C", "V", "B", "N", "M", "-", "/"],
vec!["space", "enter", "backspace"],
vec!["left", "right", "up", "down", "home", "end"],
];
bsn! {
Node {
width: percent(100),
height: percent(100),
align_items: AlignItems::End,
justify_content: JustifyContent::Center,
}
Children [(
Node {
flex_direction: FlexDirection::Column,
border: px(5),
row_gap: px(5),
padding: px(5),
align_items: AlignItems::Center,
margin: px(25),
border_radius: BorderRadius::all(px(10)),
}
BackgroundColor(NAVY)
BorderColor::all(Color::WHITE)
Children [
Text("virtual keyboard"),
(
@VirtualKeyboard::<&str> { @keys: keys }
on(on_virtual_key_pressed)
)
]
)]
}
}