Update gamepad_viewer to use children macro (#18282)

# Objective

Contributes to #18238 
Updates the `gamepad_viewer`, example to use the `children!` macro.  

## Solution

Updates examples to use the Improved Spawning API merged in
https://github.com/bevyengine/bevy/pull/17521

## Testing

- Did you test these changes? If so, how?
- Opened the examples before and after and verified the same behavior
was observed. I did this on Ubuntu 24.04.2 LTS using `--features
wayland`.
- Are there any parts that need more testing?
- Other OS's and features can't hurt, but this is such a small change it
shouldn't be a problem.
- How can other people (reviewers) test your changes? Is there anything
specific they need to know?
  - Run the examples yourself with and without these changes.
- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test?
  - see above

---

## Showcase

n/a

## Migration Guide

n/a
This commit is contained in:
krunchington 2025-03-12 16:52:39 -07:00 committed by GitHub
parent 8f38ea352e
commit 25103df301
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -132,41 +132,40 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
// Buttons // Buttons
commands commands.spawn((
.spawn(( Transform::from_xyz(BUTTONS_X, BUTTONS_Y, 0.),
Transform::from_xyz(BUTTONS_X, BUTTONS_Y, 0.), Visibility::default(),
Visibility::default(), children![
)) GamepadButtonBundle::new(
.with_children(|parent| {
parent.spawn(GamepadButtonBundle::new(
GamepadButton::North, GamepadButton::North,
meshes.circle.clone(), meshes.circle.clone(),
materials.normal.clone(), materials.normal.clone(),
0., 0.,
BUTTON_CLUSTER_RADIUS, BUTTON_CLUSTER_RADIUS,
)); ),
parent.spawn(GamepadButtonBundle::new( GamepadButtonBundle::new(
GamepadButton::South, GamepadButton::South,
meshes.circle.clone(), meshes.circle.clone(),
materials.normal.clone(), materials.normal.clone(),
0., 0.,
-BUTTON_CLUSTER_RADIUS, -BUTTON_CLUSTER_RADIUS,
)); ),
parent.spawn(GamepadButtonBundle::new( GamepadButtonBundle::new(
GamepadButton::West, GamepadButton::West,
meshes.circle.clone(), meshes.circle.clone(),
materials.normal.clone(), materials.normal.clone(),
-BUTTON_CLUSTER_RADIUS, -BUTTON_CLUSTER_RADIUS,
0., 0.,
)); ),
parent.spawn(GamepadButtonBundle::new( GamepadButtonBundle::new(
GamepadButton::East, GamepadButton::East,
meshes.circle.clone(), meshes.circle.clone(),
materials.normal.clone(), materials.normal.clone(),
BUTTON_CLUSTER_RADIUS, BUTTON_CLUSTER_RADIUS,
0., 0.,
)); ),
}); ],
));
// Start and Pause // Start and Pause
@ -188,50 +187,43 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
// D-Pad // D-Pad
commands commands.spawn((
.spawn(( Transform::from_xyz(-BUTTONS_X, BUTTONS_Y, 0.),
Transform::from_xyz(-BUTTONS_X, BUTTONS_Y, 0.), Visibility::default(),
Visibility::default(), children![
)) GamepadButtonBundle::new(
.with_children(|parent| {
parent.spawn(GamepadButtonBundle::new(
GamepadButton::DPadUp, GamepadButton::DPadUp,
meshes.triangle.clone(), meshes.triangle.clone(),
materials.normal.clone(), materials.normal.clone(),
0., 0.,
BUTTON_CLUSTER_RADIUS, BUTTON_CLUSTER_RADIUS,
)); ),
parent.spawn( GamepadButtonBundle::new(
GamepadButtonBundle::new( GamepadButton::DPadDown,
GamepadButton::DPadDown, meshes.triangle.clone(),
meshes.triangle.clone(), materials.normal.clone(),
materials.normal.clone(), 0.,
0., -BUTTON_CLUSTER_RADIUS,
-BUTTON_CLUSTER_RADIUS, )
) .with_rotation(PI),
.with_rotation(PI), GamepadButtonBundle::new(
); GamepadButton::DPadLeft,
parent.spawn( meshes.triangle.clone(),
GamepadButtonBundle::new( materials.normal.clone(),
GamepadButton::DPadLeft, -BUTTON_CLUSTER_RADIUS,
meshes.triangle.clone(), 0.,
materials.normal.clone(), )
-BUTTON_CLUSTER_RADIUS, .with_rotation(PI / 2.),
0., GamepadButtonBundle::new(
) GamepadButton::DPadRight,
.with_rotation(PI / 2.), meshes.triangle.clone(),
); materials.normal.clone(),
parent.spawn( BUTTON_CLUSTER_RADIUS,
GamepadButtonBundle::new( 0.,
GamepadButton::DPadRight, )
meshes.triangle.clone(), .with_rotation(-PI / 2.),
materials.normal.clone(), ],
BUTTON_CLUSTER_RADIUS, ));
0.,
)
.with_rotation(-PI / 2.),
);
});
// Triggers // Triggers
@ -275,43 +267,35 @@ fn setup_sticks(
let live_mid = (live_lower + live_upper) / 2.0; let live_mid = (live_lower + live_upper) / 2.0;
let mut spawn_stick = |x_pos, y_pos, x_axis, y_axis, button| { let mut spawn_stick = |x_pos, y_pos, x_axis, y_axis, button| {
commands let style = TextFont {
.spawn((Transform::from_xyz(x_pos, y_pos, 0.), Visibility::default())) font_size: 13.,
.with_children(|parent| { ..default()
// full extent };
parent.spawn(Sprite::from_color( commands.spawn((
DEAD_COLOR, Transform::from_xyz(x_pos, y_pos, 0.),
Vec2::splat(STICK_BOUNDS_SIZE * 2.), Visibility::default(),
)); children![
// live zone Sprite::from_color(DEAD_COLOR, Vec2::splat(STICK_BOUNDS_SIZE * 2.),),
parent.spawn(( (
Sprite::from_color(LIVE_COLOR, Vec2::splat(live_size)), Sprite::from_color(LIVE_COLOR, Vec2::splat(live_size)),
Transform::from_xyz(live_mid, live_mid, 2.), Transform::from_xyz(live_mid, live_mid, 2.),
)); ),
// dead zone (
parent.spawn((
Sprite::from_color(DEAD_COLOR, Vec2::splat(dead_size)), Sprite::from_color(DEAD_COLOR, Vec2::splat(dead_size)),
Transform::from_xyz(dead_mid, dead_mid, 3.), Transform::from_xyz(dead_mid, dead_mid, 3.),
)); ),
// text (
let style = TextFont { Text2d::default(),
font_size: 13., Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.),
..default() Anchor::BottomCenter,
}; TextWithAxes { x_axis, y_axis },
parent children![
.spawn(( (TextSpan(format!("{:.3}", 0.)), style.clone()),
Text2d::default(), (TextSpan::new(", "), style.clone()),
Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.), (TextSpan(format!("{:.3}", 0.)), style),
Anchor::BottomCenter, ]
TextWithAxes { x_axis, y_axis }, ),
)) (
.with_children(|p| {
p.spawn((TextSpan(format!("{:.3}", 0.)), style.clone()));
p.spawn((TextSpan::new(", "), style.clone()));
p.spawn((TextSpan(format!("{:.3}", 0.)), style));
});
// cursor
parent.spawn((
meshes.circle.clone(), meshes.circle.clone(),
materials.normal.clone(), materials.normal.clone(),
Transform::from_xyz(0., 0., 5.).with_scale(Vec2::splat(0.15).extend(1.)), Transform::from_xyz(0., 0., 5.).with_scale(Vec2::splat(0.15).extend(1.)),
@ -321,8 +305,9 @@ fn setup_sticks(
scale: STICK_BOUNDS_SIZE, scale: STICK_BOUNDS_SIZE,
}, },
ReactTo(button), ReactTo(button),
)); ),
}); ],
));
}; };
spawn_stick( spawn_stick(
@ -347,25 +332,24 @@ fn setup_triggers(
materials: Res<ButtonMaterials>, materials: Res<ButtonMaterials>,
) { ) {
let mut spawn_trigger = |x, y, button_type| { let mut spawn_trigger = |x, y, button_type| {
commands commands.spawn((
.spawn(GamepadButtonBundle::new( GamepadButtonBundle::new(
button_type, button_type,
meshes.trigger.clone(), meshes.trigger.clone(),
materials.normal.clone(), materials.normal.clone(),
x, x,
y, y,
)) ),
.with_children(|parent| { children![(
parent.spawn(( Transform::from_xyz(0., 0., 1.),
Transform::from_xyz(0., 0., 1.), Text(format!("{:.3}", 0.)),
Text(format!("{:.3}", 0.)), TextFont {
TextFont { font_size: 13.,
font_size: 13., ..default()
..default() },
}, TextWithButtonValue(button_type),
TextWithButtonValue(button_type), )],
)); ));
});
}; };
spawn_trigger(-BUTTONS_X, BUTTONS_Y + 145., GamepadButton::LeftTrigger2); spawn_trigger(-BUTTONS_X, BUTTONS_Y + 145., GamepadButton::LeftTrigger2);
@ -374,18 +358,17 @@ fn setup_triggers(
fn setup_connected(mut commands: Commands) { fn setup_connected(mut commands: Commands) {
// This is UI text, unlike other text in this example which is 2d. // This is UI text, unlike other text in this example which is 2d.
commands commands.spawn((
.spawn(( Text::new("Connected Gamepads:\n"),
Text::new("Connected Gamepads:\n"), Node {
Node { position_type: PositionType::Absolute,
position_type: PositionType::Absolute, top: Val::Px(12.),
top: Val::Px(12.), left: Val::Px(12.),
left: Val::Px(12.), ..default()
..default() },
}, ConnectedGamepadsText,
ConnectedGamepadsText, children![TextSpan::new("None")],
)) ));
.with_child(TextSpan::new("None"));
} }
fn update_buttons( fn update_buttons(