From f2106bb3ce4e1a451aa49d3dd658a94f27486c66 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Thu, 6 Oct 2022 13:33:29 +0000 Subject: [PATCH] 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 --- examples/tools/gamepad_viewer.rs | 232 ++++++++++++++++--------------- 1 file changed, 121 insertions(+), 111 deletions(-) diff --git a/examples/tools/gamepad_viewer.rs b/examples/tools/gamepad_viewer.rs index a191f81f07..f5bef89524 100644 --- a/examples/tools/gamepad_viewer.rs +++ b/examples/tools/gamepad_viewer.rs @@ -88,6 +88,37 @@ impl FromWorld for FontHandle { } } +#[derive(Bundle)] +struct GamepadButtonBundle { + mesh_bundle: MaterialMesh2dBundle, + react_to: ReactTo, +} + +impl GamepadButtonBundle { + pub fn new( + button_type: GamepadButtonType, + mesh: Mesh2dHandle, + material: Handle, + 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() { App::new() .add_plugins(DefaultPlugins) @@ -116,65 +147,52 @@ fn setup(mut commands: Commands, meshes: Res, materials: Res, materials: Res