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:
parent
8f38ea352e
commit
25103df301
@ -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![
|
||||||
.with_children(|parent| {
|
GamepadButtonBundle::new(
|
||||||
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,20 +187,17 @@ 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![
|
||||||
.with_children(|parent| {
|
GamepadButtonBundle::new(
|
||||||
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(),
|
||||||
@ -210,8 +206,6 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
|
|||||||
-BUTTON_CLUSTER_RADIUS,
|
-BUTTON_CLUSTER_RADIUS,
|
||||||
)
|
)
|
||||||
.with_rotation(PI),
|
.with_rotation(PI),
|
||||||
);
|
|
||||||
parent.spawn(
|
|
||||||
GamepadButtonBundle::new(
|
GamepadButtonBundle::new(
|
||||||
GamepadButton::DPadLeft,
|
GamepadButton::DPadLeft,
|
||||||
meshes.triangle.clone(),
|
meshes.triangle.clone(),
|
||||||
@ -220,8 +214,6 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
|
|||||||
0.,
|
0.,
|
||||||
)
|
)
|
||||||
.with_rotation(PI / 2.),
|
.with_rotation(PI / 2.),
|
||||||
);
|
|
||||||
parent.spawn(
|
|
||||||
GamepadButtonBundle::new(
|
GamepadButtonBundle::new(
|
||||||
GamepadButton::DPadRight,
|
GamepadButton::DPadRight,
|
||||||
meshes.triangle.clone(),
|
meshes.triangle.clone(),
|
||||||
@ -230,8 +222,8 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
|
|||||||
0.,
|
0.,
|
||||||
)
|
)
|
||||||
.with_rotation(-PI / 2.),
|
.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
|
|
||||||
.spawn((Transform::from_xyz(x_pos, y_pos, 0.), Visibility::default()))
|
|
||||||
.with_children(|parent| {
|
|
||||||
// full extent
|
|
||||||
parent.spawn(Sprite::from_color(
|
|
||||||
DEAD_COLOR,
|
|
||||||
Vec2::splat(STICK_BOUNDS_SIZE * 2.),
|
|
||||||
));
|
|
||||||
// live zone
|
|
||||||
parent.spawn((
|
|
||||||
Sprite::from_color(LIVE_COLOR, Vec2::splat(live_size)),
|
|
||||||
Transform::from_xyz(live_mid, live_mid, 2.),
|
|
||||||
));
|
|
||||||
// dead zone
|
|
||||||
parent.spawn((
|
|
||||||
Sprite::from_color(DEAD_COLOR, Vec2::splat(dead_size)),
|
|
||||||
Transform::from_xyz(dead_mid, dead_mid, 3.),
|
|
||||||
));
|
|
||||||
// text
|
|
||||||
let style = TextFont {
|
let style = TextFont {
|
||||||
font_size: 13.,
|
font_size: 13.,
|
||||||
..default()
|
..default()
|
||||||
};
|
};
|
||||||
parent
|
commands.spawn((
|
||||||
.spawn((
|
Transform::from_xyz(x_pos, y_pos, 0.),
|
||||||
|
Visibility::default(),
|
||||||
|
children![
|
||||||
|
Sprite::from_color(DEAD_COLOR, Vec2::splat(STICK_BOUNDS_SIZE * 2.),),
|
||||||
|
(
|
||||||
|
Sprite::from_color(LIVE_COLOR, Vec2::splat(live_size)),
|
||||||
|
Transform::from_xyz(live_mid, live_mid, 2.),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
Sprite::from_color(DEAD_COLOR, Vec2::splat(dead_size)),
|
||||||
|
Transform::from_xyz(dead_mid, dead_mid, 3.),
|
||||||
|
),
|
||||||
|
(
|
||||||
Text2d::default(),
|
Text2d::default(),
|
||||||
Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.),
|
Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.),
|
||||||
Anchor::BottomCenter,
|
Anchor::BottomCenter,
|
||||||
TextWithAxes { x_axis, y_axis },
|
TextWithAxes { x_axis, y_axis },
|
||||||
))
|
children![
|
||||||
.with_children(|p| {
|
(TextSpan(format!("{:.3}", 0.)), style.clone()),
|
||||||
p.spawn((TextSpan(format!("{:.3}", 0.)), style.clone()));
|
(TextSpan::new(", "), style.clone()),
|
||||||
p.spawn((TextSpan::new(", "), style.clone()));
|
(TextSpan(format!("{:.3}", 0.)), style),
|
||||||
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,16 +332,15 @@ 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 {
|
||||||
@ -364,8 +348,8 @@ fn setup_triggers(
|
|||||||
..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,8 +358,7 @@ 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,
|
||||||
@ -384,8 +367,8 @@ fn setup_connected(mut commands: Commands) {
|
|||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
ConnectedGamepadsText,
|
ConnectedGamepadsText,
|
||||||
))
|
children![TextSpan::new("None")],
|
||||||
.with_child(TextSpan::new("None"));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_buttons(
|
fn update_buttons(
|
||||||
|
Loading…
Reference in New Issue
Block a user