Modified the "scroll.rs" example to use the new spawning API. (#19592)

# Objective

- Update the scroll example to use the latest API.

## Solution

- It now uses the 'children![]' API.

## Testing

- I manually verified that the scrolling was working

## Limitations
- Unfortunately, I couldn't find a way to spawn observers targeting the
entity inside the "fn() -> impl Bundle" function.
This commit is contained in:
LP 2025-06-11 22:32:18 -04:00 committed by GitHub
parent 0d620cdf29
commit 8ab71a6999
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,6 +3,7 @@
use accesskit::{Node as Accessible, Role};
use bevy::{
a11y::AccessibilityNode,
ecs::spawn::SpawnIter,
input::mouse::{MouseScrollUnit, MouseWheel},
picking::hover::HoverMap,
prelude::*,
@ -26,6 +27,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Camera
commands.spawn((Camera2d, IsDefaultUiCamera));
// Font
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
// root node
commands
.spawn(Node {
@ -49,7 +53,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent.spawn((
Text::new("Horizontally Scrolling list (Ctrl + MouseWheel)"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font: font_handle.clone(),
font_size: FONT_SIZE,
..default()
},
@ -72,8 +76,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
for i in 0..100 {
parent.spawn((Text(format!("Item {i}")),
TextFont {
font: asset_server
.load("fonts/FiraSans-Bold.ttf"),
font: font_handle.clone(),
..default()
},
Label,
@ -101,38 +104,45 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
// container for all other examples
parent
.spawn(Node {
parent.spawn((
Node {
width: Val::Percent(100.),
height: Val::Percent(100.),
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::SpaceBetween,
..default()
})
.with_children(|parent| {
// vertical scroll example
parent
.spawn(Node {
},
children![
vertically_scrolling_list(asset_server.load("fonts/FiraSans-Bold.ttf")),
bidirectional_scrolling_list(asset_server.load("fonts/FiraSans-Bold.ttf")),
nested_scrolling_list(asset_server.load("fonts/FiraSans-Bold.ttf")),
],
));
});
}
fn vertically_scrolling_list(font_handle: Handle<Font>) -> impl Bundle {
(
Node {
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
width: Val::Px(200.),
..default()
})
.with_children(|parent| {
},
children![
(
// Title
parent.spawn((
Text::new("Vertically Scrolling List"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font: font_handle.clone(),
font_size: FONT_SIZE,
..default()
},
Label,
));
),
(
// Scrolling list
parent
.spawn((
Node {
flex_direction: FlexDirection::Column,
align_self: AlignSelf::Stretch,
@ -141,66 +151,57 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
))
.with_children(|parent| {
// List items
for i in 0..25 {
parent
.spawn(Node {
Children::spawn(SpawnIter((0..25).map(move |i| {
(
Node {
min_height: Val::Px(LINE_HEIGHT),
max_height: Val::Px(LINE_HEIGHT),
..default()
})
.insert(Pickable {
},
Pickable {
should_block_lower: false,
..default()
})
.with_children(|parent| {
parent
.spawn((
},
children![(
Text(format!("Item {i}")),
TextFont {
font: asset_server
.load("fonts/FiraSans-Bold.ttf"),
font: font_handle.clone(),
..default()
},
Label,
AccessibilityNode(Accessible::new(
Role::ListItem,
)),
))
.insert(Pickable {
AccessibilityNode(Accessible::new(Role::ListItem)),
Pickable {
should_block_lower: false,
..default()
});
});
}
});
});
)],
)
})))
),
],
)
}
// Bidirectional scroll example
parent
.spawn(Node {
fn bidirectional_scrolling_list(font_handle: Handle<Font>) -> impl Bundle {
(
Node {
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
width: Val::Px(200.),
..default()
})
.with_children(|parent| {
// Title
parent.spawn((
},
children![
(
Text::new("Bidirectionally Scrolling List"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font: font_handle.clone(),
font_size: FONT_SIZE,
..default()
},
Label,
));
// Scrolling list
parent
.spawn((
),
(
Node {
flex_direction: FlexDirection::Column,
align_self: AlignSelf::Stretch,
@ -209,66 +210,60 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
))
.with_children(|parent| {
// Rows in each column
for oi in 0..10 {
parent
.spawn(Node {
Children::spawn(SpawnIter((0..25).map(move |oi| {
(
Node {
flex_direction: FlexDirection::Row,
..default()
})
.insert(Pickable::IGNORE)
.with_children(|parent| {
// Elements in each row
for i in 0..25 {
parent
.spawn((
Text(format!("Item {}", (oi * 25) + i)),
},
Pickable::IGNORE,
Children::spawn(SpawnIter((0..10).map({
let value = font_handle.clone();
move |i| {
(
Text(format!("Item {}", (oi * 10) + i)),
TextFont {
font: asset_server.load(
"fonts/FiraSans-Bold.ttf",
),
font: value.clone(),
..default()
},
Label,
AccessibilityNode(Accessible::new(
Role::ListItem,
)),
))
.insert(Pickable {
AccessibilityNode(Accessible::new(Role::ListItem)),
Pickable {
should_block_lower: false,
..default()
});
},
)
}
});
}))),
)
})))
)
],
)
}
});
});
// Nested scrolls example
parent
.spawn(Node {
fn nested_scrolling_list(font_handle: Handle<Font>) -> impl Bundle {
(
Node {
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
width: Val::Px(200.),
..default()
})
.with_children(|parent| {
},
children![
(
// Title
parent.spawn((
Text::new("Nested Scrolling Lists"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font: font_handle.clone(),
font_size: FONT_SIZE,
..default()
},
Label,
));
),
(
// Outer, horizontal scrolling container
parent
.spawn((
Node {
column_gap: Val::Px(20.),
flex_direction: FlexDirection::Row,
@ -278,12 +273,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
))
.with_children(|parent| {
// Inner, scrolling columns
for oi in 0..30 {
parent
.spawn((
Children::spawn(SpawnIter((0..30).map(move |oi| {
(
Node {
flex_direction: FlexDirection::Column,
align_self: AlignSelf::Stretch,
@ -291,38 +283,33 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
BackgroundColor(Color::srgb(0.05, 0.05, 0.05)),
))
.insert(Pickable {
Pickable {
should_block_lower: false,
..default()
})
.with_children(|parent| {
for i in 0..25 {
parent
.spawn((
},
Children::spawn(SpawnIter((0..30).map({
let value = font_handle.clone();
move |i| {
(
Text(format!("Item {}", (oi * 25) + i)),
TextFont {
font: asset_server.load(
"fonts/FiraSans-Bold.ttf",
),
font: value.clone(),
..default()
},
Label,
AccessibilityNode(Accessible::new(
Role::ListItem,
)),
))
.insert(Pickable {
AccessibilityNode(Accessible::new(Role::ListItem)),
Pickable {
should_block_lower: false,
..default()
});
},
)
}
});
}
});
});
});
});
}))),
)
})))
)
],
)
}
/// Updates the scroll position of scrollable nodes in response to mouse input