Commit 6caebf3
BSN Syntax Improvements (#25318)
# Objective
BSN currently treats passed in variables and inline functions as
"scenes", and requires `template_value()` to pass in a component
`Template` value:
```rust
fn widget(node: Node, contents: impl Scene) -> impl Scene {
bsn! {
Widget
template_value(node)
contents
}
}
```
This is a bit unexpected, as `node` is a "peer" of `Widget`, from a
placement perspective. `template_value` comes up _a lot_, and results in
significant confusion and noise.
Other "scene inclusions", such as `@SceneComponent {}` and
`:"scene.bsn"` have explicit visual indicators that they are scenes.
This all results in making `bsn!` harder to write and read than it
should be. We can do better.
Additionally, this pattern shows up a lot:
```rust
bsn! {
template_value(Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y))
}
```
As does this:
```rust
bsn! {
template_value(foo.bar())
}
```
The `template_value` wrapper is required because `bsn!` doesn't
currently support expressions like `foo.bar()`. `template_value` fixes
this by breaking out of `bsn!`'s syntax / reading the Rust expression
directly. We should support this pattern directly and it should behave
as expected!
Additionally, enums (especially enums outside of developers control) are
annoying and weird to work with in BSN, as they require
`VariantDefaults` to support "per variant patching". This is a nice
feature, but it hasn't really come up in practice, and the cost it
incurs is way too high from a usability perspective.
Enums that don't implement VariantDefaults (or implicitly via
FromTemplate) require using template_value:
```rust
bsn! {
template_value(Team::Blue)
}
```
Additionally, things like `NumberInputValue::F32` required
`template_value`, as it is ambiguous whether it is an enum variant an or
"associated const":
```rust
bsn! {
template_value(NumberInputValue::F32(1.0))
}
```
So many annoyances!
## Solution
- Variables, functions, and expressions in "BSN entry" position are now
interpreted as templates.
- All scene inclusions now use `@` syntax, which is now considered to be
the "uncached scene" syntax.
- BSN entries now support `foo.bar()`-style expressions. These are
always interpreted as "template values"
- I have removed `{}` from "BSN entry position", as `Widget {}` and
`Widget { foo }` is ambiguous. This also provides clarity in the "scene
list" case, as `{}` is now only used in list item position.
- I've implemented Template for the `Propagate` component so it can be
used directly
- Enums now require specifying every field value. This means any enum
that implement Default and Clone can now be used with BSN, just like
other types. Structs defined inside enums therefore _also_ require
specifying every field, as they are being written on top. However these
still have "implicit default".
- Thanks to the new enum behavior, the `Type::F32` ambiguity (is it an
enum or an associated const) is no longer a problem. These can both
parse to the same AST and resolve to the same expression.
- "struct update" syntax is now supported
- BSN "codegen" has been refactored for improved clarity and terseness
- Code and examples have been ported
- Added support for `~{TEMPLATE_EXPRESSION}` syntax, which supports
arbitrary rust-code template expressions.
- `template_value` has been deprecated. In 99.99% of cases, BSN code can
remove this wrapper. On the off chance you need an arbitrary template
expression that isn't BSN compatible (ex: multiple statements), you can
now use `~{TEMPLATE_EXPRESSION}` syntax.
This is how it looks now:
```rust
fn widget(node: Node, contents: impl Scene) -> impl Scene {
bsn! {
Widget // component template
node // component template variable
node.clone() // function that returns a component template
@contents // uncached Scene variable
@{contents} // uncached Scene expression
@scene_function() // uncached Scene function
@SceneComponent // uncached Scene Component
// An expression that returns a component template
Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y)
}
}
```
Ultimately I think we should consider deprecating `template_value`, but
we need to keep it around for a bit longer, as it is currently used in
one "corner case" in an example.
---------
Co-authored-by: qoh <1732901+qoh@users.noreply.github.com>
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>1 parent 34acd90 commit 6caebf3
84 files changed
Lines changed: 2187 additions & 1414 deletions
File tree
- _release-content
- migration-guides
- release-notes
- benches/benches/bevy_scene
- crates
- bevy_app/src
- bevy_camera/src/visibility
- bevy_ecs
- macros/src
- src
- bevy_feathers/src
- containers
- controls
- bevy_input_focus/src
- bevy_light/src
- bevy_render/src/view
- bevy_scene
- macros/src
- bsn
- src
- bevy_text/src
- examples
- 2d
- 3d
- animation
- asset
- gizmos
- helpers
- large_scenes/bevy_city/src
- remote
- scene
- state
- ui
- layout
- styling
- widgets
- usage
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 123 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
Lines changed: 123 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
78 | 107 | | |
79 | 108 | | |
80 | 109 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
| 48 | + | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
| |||
951 | 950 | | |
952 | 951 | | |
953 | 952 | | |
954 | | - | |
955 | | - | |
956 | | - | |
957 | | - | |
958 | | - | |
959 | | - | |
960 | | - | |
961 | | - | |
962 | | - | |
0 commit comments