Reduced code duplication in gamepad_viewer example (#6175)

# Objective

- Reduce code duplication in the `gamepad_viewer` example.
- Fixes #6164 

## Solution

- Added a custom Bundle called `GamepadButtonBundle` to avoid repeating similar code throughout the example.
- Created a `new()` method on `GamepadButtonBundle`.



Co-authored-by: Alvin Philips <alvinphilips257@gmail.com>
This commit is contained in:
Alvin Philips 2022-10-06 13:33:29 +00:00
parent 087f1c66aa
commit f2106bb3ce

View file

@ -88,6 +88,37 @@ impl FromWorld for FontHandle {
} }
} }
#[derive(Bundle)]
struct GamepadButtonBundle {
mesh_bundle: MaterialMesh2dBundle<ColorMaterial>,
react_to: ReactTo,
}
impl GamepadButtonBundle {
pub fn new(
button_type: GamepadButtonType,
mesh: Mesh2dHandle,
material: Handle<ColorMaterial>,
x: f32,
y: f32,
) -> Self {
Self {
mesh_bundle: MaterialMesh2dBundle {
mesh,
material,
transform: Transform::from_xyz(x, y, 0.),
..default()
},
react_to: ReactTo(button_type),
}
}
pub fn with_rotation(mut self, angle: f32) -> Self {
self.mesh_bundle.transform.rotation = Quat::from_rotation_z(angle);
self
}
}
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins) .add_plugins(DefaultPlugins)
@ -116,65 +147,52 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
..default() ..default()
}) })
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::North,
mesh: meshes.circle.clone(), meshes.circle.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(0., BUTTON_CLUSTER_RADIUS, 0.), 0.,
..default() BUTTON_CLUSTER_RADIUS,
},
ReactTo(GamepadButtonType::North),
)); ));
parent.spawn(( parent.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::South,
mesh: meshes.circle.clone(), meshes.circle.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(0., -BUTTON_CLUSTER_RADIUS, 0.), 0.,
..default() -BUTTON_CLUSTER_RADIUS,
},
ReactTo(GamepadButtonType::South),
)); ));
parent.spawn(( parent.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::West,
mesh: meshes.circle.clone(), meshes.circle.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(-BUTTON_CLUSTER_RADIUS, 0., 0.), -BUTTON_CLUSTER_RADIUS,
..default() 0.,
},
ReactTo(GamepadButtonType::West),
)); ));
parent.spawn(( parent.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::East,
mesh: meshes.circle.clone(), meshes.circle.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(BUTTON_CLUSTER_RADIUS, 0., 0.), BUTTON_CLUSTER_RADIUS,
0.,
..default()
},
ReactTo(GamepadButtonType::East),
)); ));
}); });
// Start and Pause // Start and Pause
commands.spawn(( commands.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::Select,
mesh: meshes.start_pause.clone(), meshes.start_pause.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(-30., BUTTONS_Y, 0.), -30.,
..default() BUTTONS_Y,
},
ReactTo(GamepadButtonType::Select),
)); ));
commands.spawn(( commands.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::Start,
mesh: meshes.start_pause.clone(), meshes.start_pause.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(30., BUTTONS_Y, 0.), 30.,
..default() BUTTONS_Y,
},
ReactTo(GamepadButtonType::Start),
)); ));
// D-Pad // D-Pad
@ -185,67 +203,61 @@ fn setup(mut commands: Commands, meshes: Res<ButtonMeshes>, materials: Res<Butto
..default() ..default()
}) })
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::DPadUp,
mesh: meshes.triangle.clone(), meshes.triangle.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(0., BUTTON_CLUSTER_RADIUS, 0.), 0.,
..default() BUTTON_CLUSTER_RADIUS,
},
ReactTo(GamepadButtonType::DPadUp),
));
parent.spawn((
MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(0., -BUTTON_CLUSTER_RADIUS, 0.)
.with_rotation(Quat::from_rotation_z(PI)),
..default()
},
ReactTo(GamepadButtonType::DPadDown),
));
parent.spawn((
MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(-BUTTON_CLUSTER_RADIUS, 0., 0.)
.with_rotation(Quat::from_rotation_z(PI / 2.)),
..default()
},
ReactTo(GamepadButtonType::DPadLeft),
));
parent.spawn((
MaterialMesh2dBundle {
mesh: meshes.triangle.clone(),
material: materials.normal.clone(),
transform: Transform::from_xyz(BUTTON_CLUSTER_RADIUS, 0., 0.)
.with_rotation(Quat::from_rotation_z(-PI / 2.)),
..default()
},
ReactTo(GamepadButtonType::DPadRight),
)); ));
parent.spawn(
GamepadButtonBundle::new(
GamepadButtonType::DPadDown,
meshes.triangle.clone(),
materials.normal.clone(),
0.,
-BUTTON_CLUSTER_RADIUS,
)
.with_rotation(PI),
);
parent.spawn(
GamepadButtonBundle::new(
GamepadButtonType::DPadLeft,
meshes.triangle.clone(),
materials.normal.clone(),
-BUTTON_CLUSTER_RADIUS,
0.,
)
.with_rotation(PI / 2.),
);
parent.spawn(
GamepadButtonBundle::new(
GamepadButtonType::DPadRight,
meshes.triangle.clone(),
materials.normal.clone(),
BUTTON_CLUSTER_RADIUS,
0.,
)
.with_rotation(-PI / 2.),
);
}); });
// Triggers // Triggers
commands.spawn(( commands.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::LeftTrigger,
mesh: meshes.trigger.clone(), meshes.trigger.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(-BUTTONS_X, BUTTONS_Y + 115., 0.), -BUTTONS_X,
..default() BUTTONS_Y + 115.,
},
ReactTo(GamepadButtonType::LeftTrigger),
)); ));
commands.spawn(( commands.spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { GamepadButtonType::RightTrigger,
mesh: meshes.trigger.clone(), meshes.trigger.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(BUTTONS_X, BUTTONS_Y + 115., 0.), BUTTONS_X,
..default() BUTTONS_Y + 115.,
},
ReactTo(GamepadButtonType::RightTrigger),
)); ));
} }
@ -373,14 +385,12 @@ fn setup_triggers(
) { ) {
let mut spawn_trigger = |x, y, button_type| { let mut spawn_trigger = |x, y, button_type| {
commands commands
.spawn(( .spawn(GamepadButtonBundle::new(
MaterialMesh2dBundle { button_type,
mesh: meshes.trigger.clone(), meshes.trigger.clone(),
material: materials.normal.clone(), materials.normal.clone(),
transform: Transform::from_xyz(x, y, 0.), x,
..default() y,
},
ReactTo(button_type),
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((