From 44dedaae6359337e696621cf1041b4399c229065 Mon Sep 17 00:00:00 2001 From: Michael Dorst Date: Tue, 18 Jan 2022 21:40:03 -0800 Subject: [PATCH 01/11] Add `gamepad_stick_input` example --- Cargo.toml | 4 ++ assets/textures/crosshair.png | Bin 0 -> 345 bytes examples/input/gamepad_stick_input.rs | 58 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 assets/textures/crosshair.png create mode 100644 examples/input/gamepad_stick_input.rs diff --git a/Cargo.toml b/Cargo.toml index 226d640bcdff7..944cf175e53ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -993,6 +993,10 @@ description = "Iterates and prints gamepad input and connection events" category = "Input" wasm = false +[[example]] +name = "gamepad_stick_input" +path = "examples/input/gamepad_stick_input.rs" + [[example]] name = "keyboard_input" path = "examples/input/keyboard_input.rs" diff --git a/assets/textures/crosshair.png b/assets/textures/crosshair.png new file mode 100644 index 0000000000000000000000000000000000000000..e4d6770aeeec5df7ce254be12071bf32e471dff4 GIT binary patch literal 345 zcmV-f0jBPx$6G=otR5(v{Q%RDht15vYkiF5K0g9X8J_LH>aDwo=q>vL;BR0Z5 zVtr}Q;JW}bfL0fc%LT6JP2z1JO`^nE#5N7xqxmg9G6Y=FWpsZ~u=tUSy@VT5bq@cZ z=NS?DURb38nv$$xYc^GJj)woqMG9T|3ClA)U&MmFgB00000NkvXXu0mjfpnsXf literal 0 HcmV?d00001 diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs new file mode 100644 index 0000000000000..a9c1ae9486c45 --- /dev/null +++ b/examples/input/gamepad_stick_input.rs @@ -0,0 +1,58 @@ +use bevy::prelude::*; + +const WINDOW_SIZE: f32 = 300.0; +const CROSSHAIR_SIZE: f32 = 32.0; + +#[derive(Component)] +struct Crosshair; + +fn main() { + App::new() + .insert_resource(WindowDescriptor { + title: "Gamepad Stick Input".to_owned(), + width: WINDOW_SIZE, + height: WINDOW_SIZE, + ..Default::default() + }) + .insert_resource(ClearColor(Color::GRAY)) + .add_plugins(DefaultPlugins) + .add_startup_system(setup) + .add_system(move_crosshair) + .run(); +} + +fn move_crosshair( + mut query: Query<&mut Transform, With>, + gamepads: Res, + axes: Res>, +) { + let mut xform = query.single_mut(); + for gamepad in gamepads.iter() { + let left_stick_x = axes + .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickX)) + .unwrap(); + let left_stick_y = axes + .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickY)) + .unwrap(); + xform.translation.x = left_stick_x * WINDOW_SIZE / 2.0; + xform.translation.y = left_stick_y * WINDOW_SIZE / 2.0; + } +} + +fn setup(mut commands: Commands, asset_server: Res) { + // Spawn camera + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + + // Spawn crosshair + let texture = asset_server.load("textures/crosshair.png"); + commands + .spawn_bundle(SpriteBundle { + texture, + sprite: Sprite { + custom_size: Some(Vec2::new(CROSSHAIR_SIZE, CROSSHAIR_SIZE)), + ..Default::default() + }, + ..Default::default() + }) + .insert(Crosshair); +} From 3f9502f20a847dc75fb6894f395364ef894900b9 Mon Sep 17 00:00:00 2001 From: Michael Dorst Date: Tue, 18 Jan 2022 22:34:52 -0800 Subject: [PATCH 02/11] Add coordinate text to `gamepad_stick_input` example --- examples/input/gamepad_stick_input.rs | 49 +++++++++++++++++++++------ 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index a9c1ae9486c45..188dfc642f9ab 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -1,11 +1,16 @@ use bevy::prelude::*; const WINDOW_SIZE: f32 = 300.0; -const CROSSHAIR_SIZE: f32 = 32.0; +const CROSSHAIR_SIZE: f32 = 24.0; +const FONT: &str = "fonts/FiraMono-Medium.ttf"; +const FONT_SIZE: f32 = 18.0; #[derive(Component)] struct Crosshair; +#[derive(Component)] +struct CoordinateText; + fn main() { App::new() .insert_resource(WindowDescriptor { @@ -17,30 +22,35 @@ fn main() { .insert_resource(ClearColor(Color::GRAY)) .add_plugins(DefaultPlugins) .add_startup_system(setup) - .add_system(move_crosshair) + .add_system(update_position) .run(); } -fn move_crosshair( - mut query: Query<&mut Transform, With>, +fn update_position( + mut xform_query: Query<&mut Transform, With>, + mut text_query: Query<&mut Text, With>, gamepads: Res, axes: Res>, ) { - let mut xform = query.single_mut(); + let mut xform = xform_query.single_mut(); + let mut text = text_query.single_mut(); for gamepad in gamepads.iter() { - let left_stick_x = axes + // We only use input from the left stick. + let x = axes .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickX)) .unwrap(); - let left_stick_y = axes + let y = axes .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickY)) .unwrap(); - xform.translation.x = left_stick_x * WINDOW_SIZE / 2.0; - xform.translation.y = left_stick_y * WINDOW_SIZE / 2.0; + xform.translation.x = x * WINDOW_SIZE / 2.0; + xform.translation.y = y * WINDOW_SIZE / 2.0; + text.sections[0].value = format!("({:6.3}, {:6.3})", x, y); } } fn setup(mut commands: Commands, asset_server: Res) { - // Spawn camera + // Spawn cameras + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(OrthographicCameraBundle::new_2d()); // Spawn crosshair @@ -55,4 +65,23 @@ fn setup(mut commands: Commands, asset_server: Res) { ..Default::default() }) .insert(Crosshair); + + // Spawn text + let font = asset_server.load(FONT); + let text_style = TextStyle { + font, + font_size: FONT_SIZE, + color: Color::BLACK, + }; + let text_alignment = TextAlignment { + vertical: VerticalAlign::Bottom, + horizontal: HorizontalAlign::Left, + }; + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("(0.000, 0.000)", text_style, text_alignment), + transform: Transform::from_xyz(-WINDOW_SIZE / 2.0, -WINDOW_SIZE / 2.0, 0.0), + ..Default::default() + }) + .insert(CoordinateText); } From 6b99b2ddb29f912399d14c021be262a37b583ae2 Mon Sep 17 00:00:00 2001 From: Michael Dorst Date: Wed, 19 Jan 2022 00:39:40 -0800 Subject: [PATCH 03/11] Add deadzone indicator --- examples/input/gamepad_stick_input.rs | 43 ++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 188dfc642f9ab..82102cf97a50e 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -1,7 +1,7 @@ -use bevy::prelude::*; +use bevy::{input::gamepad::GamepadSettings, prelude::*}; const WINDOW_SIZE: f32 = 300.0; -const CROSSHAIR_SIZE: f32 = 24.0; +const CROSSHAIR_SIZE: f32 = 16.0; const FONT: &str = "fonts/FiraMono-Medium.ttf"; const FONT_SIZE: f32 = 18.0; @@ -11,6 +11,9 @@ struct Crosshair; #[derive(Component)] struct CoordinateText; +#[derive(Component)] +struct DeadzoneBox; + fn main() { App::new() .insert_resource(WindowDescriptor { @@ -48,9 +51,12 @@ fn update_position( } } -fn setup(mut commands: Commands, asset_server: Res) { - // Spawn cameras - commands.spawn_bundle(OrthographicCameraBundle::new_2d()); +fn setup( + mut commands: Commands, + asset_server: Res, + gamepad_settings: Res, +) { + // Spawn camera commands.spawn_bundle(OrthographicCameraBundle::new_2d()); // Spawn crosshair @@ -62,6 +68,8 @@ fn setup(mut commands: Commands, asset_server: Res) { custom_size: Some(Vec2::new(CROSSHAIR_SIZE, CROSSHAIR_SIZE)), ..Default::default() }, + // Make sure it is in the foreground with a Z value > 0.0 + transform: Transform::from_xyz(0.0, 0.0, 1.0), ..Default::default() }) .insert(Crosshair); @@ -79,9 +87,30 @@ fn setup(mut commands: Commands, asset_server: Res) { }; commands .spawn_bundle(Text2dBundle { - text: Text::with_section("(0.000, 0.000)", text_style, text_alignment), - transform: Transform::from_xyz(-WINDOW_SIZE / 2.0, -WINDOW_SIZE / 2.0, 0.0), + text: Text::with_section("( 0.000, 0.000)", text_style, text_alignment), + transform: Transform::from_xyz(-WINDOW_SIZE / 2.0, -WINDOW_SIZE / 2.0, 1.0), ..Default::default() }) .insert(CoordinateText); + + // Get deadzone info + let deadzone_upperbound = gamepad_settings.default_axis_settings.positive_low; + let deadzone_lowerbound = gamepad_settings.default_axis_settings.negative_low; + let deadzone_midpoint = (deadzone_lowerbound + deadzone_upperbound) / 2.0; + let deadzone_size = deadzone_upperbound - deadzone_lowerbound; + let deadzone_box_midpoint = deadzone_midpoint * WINDOW_SIZE / 2.0; + let deadzone_box_size = deadzone_size * WINDOW_SIZE / 2.0; + + // Spawn deadzone box + commands + .spawn_bundle(SpriteBundle { + sprite: Sprite { + custom_size: Some(Vec2::new(deadzone_box_size, deadzone_box_size)), + color: Color::rgb(0.4, 0.4, 0.4), + ..Default::default() + }, + transform: Transform::from_xyz(deadzone_box_midpoint, deadzone_box_midpoint, 0.0), + ..Default::default() + }) + .insert(DeadzoneBox); } From fe89e889e40fbc07f2f6e9ee19c5f30349c20e40 Mon Sep 17 00:00:00 2001 From: Michael Dorst Date: Wed, 19 Jan 2022 01:02:14 -0800 Subject: [PATCH 04/11] Add outer deadzone --- examples/input/gamepad_stick_input.rs | 70 ++++++++++++++++++--------- 1 file changed, 47 insertions(+), 23 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 82102cf97a50e..352e8371a4400 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -4,6 +4,8 @@ const WINDOW_SIZE: f32 = 300.0; const CROSSHAIR_SIZE: f32 = 16.0; const FONT: &str = "fonts/FiraMono-Medium.ttf"; const FONT_SIZE: f32 = 18.0; +const LIVEZONE_COLOR: Color = Color::GRAY; +const DEADZONE_COLOR: Color = Color::rgb(0.4, 0.4, 0.4); #[derive(Component)] struct Crosshair; @@ -22,7 +24,7 @@ fn main() { height: WINDOW_SIZE, ..Default::default() }) - .insert_resource(ClearColor(Color::GRAY)) + .insert_resource(ClearColor(DEADZONE_COLOR)) .add_plugins(DefaultPlugins) .add_startup_system(setup) .add_system(update_position) @@ -74,43 +76,65 @@ fn setup( }) .insert(Crosshair); - // Spawn text - let font = asset_server.load(FONT); - let text_style = TextStyle { - font, - font_size: FONT_SIZE, - color: Color::BLACK, - }; - let text_alignment = TextAlignment { - vertical: VerticalAlign::Bottom, - horizontal: HorizontalAlign::Left, - }; - commands - .spawn_bundle(Text2dBundle { - text: Text::with_section("( 0.000, 0.000)", text_style, text_alignment), - transform: Transform::from_xyz(-WINDOW_SIZE / 2.0, -WINDOW_SIZE / 2.0, 1.0), - ..Default::default() - }) - .insert(CoordinateText); - - // Get deadzone info + // Get live/deadzone info + let livezone_upperbound = gamepad_settings.default_axis_settings.positive_high; + let livezone_lowerbound = gamepad_settings.default_axis_settings.negative_high; let deadzone_upperbound = gamepad_settings.default_axis_settings.positive_low; let deadzone_lowerbound = gamepad_settings.default_axis_settings.negative_low; + let livezone_midpoint = (livezone_lowerbound + livezone_upperbound) / 2.0; let deadzone_midpoint = (deadzone_lowerbound + deadzone_upperbound) / 2.0; + let livezone_size = livezone_upperbound - livezone_lowerbound; let deadzone_size = deadzone_upperbound - deadzone_lowerbound; + let livezone_box_midpoint = livezone_midpoint * WINDOW_SIZE / 2.0; let deadzone_box_midpoint = deadzone_midpoint * WINDOW_SIZE / 2.0; + let livezone_box_size = livezone_size * WINDOW_SIZE / 2.0; let deadzone_box_size = deadzone_size * WINDOW_SIZE / 2.0; + // For text placement + let livezone_lower_left_corner = (livezone_box_midpoint - livezone_box_size) / 2.0; + // Spawn livezone box + commands.spawn_bundle(SpriteBundle { + sprite: Sprite { + custom_size: Some(Vec2::new(livezone_box_size, livezone_box_size)), + color: LIVEZONE_COLOR, + ..Default::default() + }, + transform: Transform::from_xyz(livezone_box_midpoint, livezone_box_midpoint, 0.0), + ..Default::default() + }); // Spawn deadzone box commands .spawn_bundle(SpriteBundle { sprite: Sprite { custom_size: Some(Vec2::new(deadzone_box_size, deadzone_box_size)), - color: Color::rgb(0.4, 0.4, 0.4), + color: DEADZONE_COLOR, ..Default::default() }, - transform: Transform::from_xyz(deadzone_box_midpoint, deadzone_box_midpoint, 0.0), + transform: Transform::from_xyz(deadzone_box_midpoint, deadzone_box_midpoint, 0.1), ..Default::default() }) .insert(DeadzoneBox); + + // Spawn text + let font = asset_server.load(FONT); + let text_style = TextStyle { + font, + font_size: FONT_SIZE, + color: Color::BLACK, + }; + let text_alignment = TextAlignment { + vertical: VerticalAlign::Bottom, + horizontal: HorizontalAlign::Left, + }; + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("( 0.000, 0.000)", text_style, text_alignment), + transform: Transform::from_xyz( + livezone_lower_left_corner, + livezone_lower_left_corner, + 1.0, + ), + ..Default::default() + }) + .insert(CoordinateText); } From f53ea26d3a659f3faadea54179c53f737e284ac8 Mon Sep 17 00:00:00 2001 From: Michael Dorst Date: Wed, 19 Jan 2022 01:22:41 -0800 Subject: [PATCH 05/11] Remove unnecessary `DeadzoneBox` struct --- examples/input/gamepad_stick_input.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 352e8371a4400..89539e7d7bccf 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -13,9 +13,6 @@ struct Crosshair; #[derive(Component)] struct CoordinateText; -#[derive(Component)] -struct DeadzoneBox; - fn main() { App::new() .insert_resource(WindowDescriptor { @@ -103,17 +100,15 @@ fn setup( ..Default::default() }); // Spawn deadzone box - commands - .spawn_bundle(SpriteBundle { - sprite: Sprite { - custom_size: Some(Vec2::new(deadzone_box_size, deadzone_box_size)), - color: DEADZONE_COLOR, - ..Default::default() - }, - transform: Transform::from_xyz(deadzone_box_midpoint, deadzone_box_midpoint, 0.1), + commands.spawn_bundle(SpriteBundle { + sprite: Sprite { + custom_size: Some(Vec2::new(deadzone_box_size, deadzone_box_size)), + color: DEADZONE_COLOR, ..Default::default() - }) - .insert(DeadzoneBox); + }, + transform: Transform::from_xyz(deadzone_box_midpoint, deadzone_box_midpoint, 0.1), + ..Default::default() + }); // Spawn text let font = asset_server.load(FONT); From 5721d261c4c2a65dafc0accfdf558599f98ce776 Mon Sep 17 00:00:00 2001 From: Michael Dorst Date: Wed, 19 Jan 2022 19:19:05 -0800 Subject: [PATCH 06/11] Implement suggestions from rparrett --- examples/input/gamepad_stick_input.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 89539e7d7bccf..8f9e2cfedef5f 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -29,12 +29,12 @@ fn main() { } fn update_position( - mut xform_query: Query<&mut Transform, With>, + mut crosshair_query: Query<&mut Transform, With>, mut text_query: Query<&mut Text, With>, gamepads: Res, axes: Res>, ) { - let mut xform = xform_query.single_mut(); + let mut transform = crosshair_query.single_mut(); let mut text = text_query.single_mut(); for gamepad in gamepads.iter() { // We only use input from the left stick. @@ -44,8 +44,8 @@ fn update_position( let y = axes .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickY)) .unwrap(); - xform.translation.x = x * WINDOW_SIZE / 2.0; - xform.translation.y = y * WINDOW_SIZE / 2.0; + transform.translation.x = x * WINDOW_SIZE / 2.0; + transform.translation.y = y * WINDOW_SIZE / 2.0; text.sections[0].value = format!("({:6.3}, {:6.3})", x, y); } } @@ -64,7 +64,7 @@ fn setup( .spawn_bundle(SpriteBundle { texture, sprite: Sprite { - custom_size: Some(Vec2::new(CROSSHAIR_SIZE, CROSSHAIR_SIZE)), + custom_size: Some(Vec2::splat(CROSSHAIR_SIZE)), ..Default::default() }, // Make sure it is in the foreground with a Z value > 0.0 @@ -92,7 +92,7 @@ fn setup( // Spawn livezone box commands.spawn_bundle(SpriteBundle { sprite: Sprite { - custom_size: Some(Vec2::new(livezone_box_size, livezone_box_size)), + custom_size: Some(Vec2::splat(livezone_box_size)), color: LIVEZONE_COLOR, ..Default::default() }, @@ -102,7 +102,7 @@ fn setup( // Spawn deadzone box commands.spawn_bundle(SpriteBundle { sprite: Sprite { - custom_size: Some(Vec2::new(deadzone_box_size, deadzone_box_size)), + custom_size: Some(Vec2::splat(deadzone_box_size)), color: DEADZONE_COLOR, ..Default::default() }, From b95515a2f51ee8eb8e5f27044737c9cd84bed0e2 Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 21 Sep 2022 16:43:44 -0400 Subject: [PATCH 07/11] Added link and description of gamepad_stick_input to README.md --- examples/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/README.md b/examples/README.md index 8f8b57759727a..a3b88791f03bd 100644 --- a/examples/README.md +++ b/examples/README.md @@ -221,6 +221,7 @@ Example | Description [Char Input Events](../examples/input/char_input_events.rs) | Prints out all chars as they are inputted [Gamepad Input](../examples/input/gamepad_input.rs) | Shows handling of gamepad input, connections, and disconnections [Gamepad Input Events](../examples/input/gamepad_input_events.rs) | Iterates and prints gamepad input and connection events +[Gamepad Stick Input](../examples/input/gamepad_stick_input.rs) | Shows gamepad stick input graphically [Keyboard Input](../examples/input/keyboard_input.rs) | Demonstrates handling a key press/release [Keyboard Input Events](../examples/input/keyboard_input_events.rs) | Prints out all keyboard events [Keyboard Modifiers](../examples/input/keyboard_modifiers.rs) | Demonstrates using key modifiers (ctrl, shift) From 6524c8307fc5546542cc9ac7c3958f3f7481cbeb Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 21 Sep 2022 17:11:01 -0400 Subject: [PATCH 08/11] Updated example code to run with 0.8 changes. --- examples/input/gamepad_stick_input.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 8f9e2cfedef5f..83f02b15ffd56 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -39,10 +39,16 @@ fn update_position( for gamepad in gamepads.iter() { // We only use input from the left stick. let x = axes - .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickX)) + .get(GamepadAxis { + gamepad, + axis_type: GamepadAxisType::LeftStickX, + }) .unwrap(); let y = axes - .get(GamepadAxis(*gamepad, GamepadAxisType::LeftStickY)) + .get(GamepadAxis { + gamepad, + axis_type: GamepadAxisType::LeftStickY, + }) .unwrap(); transform.translation.x = x * WINDOW_SIZE / 2.0; transform.translation.y = y * WINDOW_SIZE / 2.0; @@ -56,7 +62,7 @@ fn setup( gamepad_settings: Res, ) { // Spawn camera - commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(Camera2dBundle::default()); // Spawn crosshair let texture = asset_server.load("textures/crosshair.png"); @@ -123,7 +129,7 @@ fn setup( }; commands .spawn_bundle(Text2dBundle { - text: Text::with_section("( 0.000, 0.000)", text_style, text_alignment), + text: Text::from_section("( 0.000, 0.000)", text_style).with_alignment(text_alignment), transform: Transform::from_xyz( livezone_lower_left_corner, livezone_lower_left_corner, From 08b491acaafe5237f634208f26a7903b4c7f5d25 Mon Sep 17 00:00:00 2001 From: targrub Date: Wed, 21 Sep 2022 17:28:15 -0400 Subject: [PATCH 09/11] Added metadata for gamepad_stick_input example --- Cargo.toml | 6 ++++++ examples/README.md | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 944cf175e53ec..76989f0608fc0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -997,6 +997,12 @@ wasm = false name = "gamepad_stick_input" path = "examples/input/gamepad_stick_input.rs" +[package.metadata.example.gamepad_stick_input] +name = "Gamepad Stick Input" +description = "Displays gamepad stick input graphically" +category = "Input" +wasm = false + [[example]] name = "keyboard_input" path = "examples/input/keyboard_input.rs" diff --git a/examples/README.md b/examples/README.md index a3b88791f03bd..f2999ee8d2b17 100644 --- a/examples/README.md +++ b/examples/README.md @@ -221,7 +221,7 @@ Example | Description [Char Input Events](../examples/input/char_input_events.rs) | Prints out all chars as they are inputted [Gamepad Input](../examples/input/gamepad_input.rs) | Shows handling of gamepad input, connections, and disconnections [Gamepad Input Events](../examples/input/gamepad_input_events.rs) | Iterates and prints gamepad input and connection events -[Gamepad Stick Input](../examples/input/gamepad_stick_input.rs) | Shows gamepad stick input graphically +[Gamepad Stick Input](../examples/input/gamepad_stick_input.rs) | Displays gamepad stick input graphically [Keyboard Input](../examples/input/keyboard_input.rs) | Demonstrates handling a key press/release [Keyboard Input Events](../examples/input/keyboard_input_events.rs) | Prints out all keyboard events [Keyboard Modifiers](../examples/input/keyboard_modifiers.rs) | Demonstrates using key modifiers (ctrl, shift) From 1425c8c2369eb6cc543ab660c094575dd3839ba7 Mon Sep 17 00:00:00 2001 From: targrub Date: Thu, 22 Sep 2022 10:57:46 -0400 Subject: [PATCH 10/11] Size window to minimum screen dimension. --- examples/input/gamepad_stick_input.rs | 57 +++++++++++++++++++++------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 83f02b15ffd56..0d4fd27bd3323 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -1,6 +1,5 @@ use bevy::{input::gamepad::GamepadSettings, prelude::*}; -const WINDOW_SIZE: f32 = 300.0; const CROSSHAIR_SIZE: f32 = 16.0; const FONT: &str = "fonts/FiraMono-Medium.ttf"; const FONT_SIZE: f32 = 18.0; @@ -13,14 +12,20 @@ struct Crosshair; #[derive(Component)] struct CoordinateText; +#[derive(Resource)] +struct VisualizationSize { + size: f32, +} + +impl Default for VisualizationSize { + fn default() -> Self { + VisualizationSize { size: 300.0 } + } +} + fn main() { App::new() - .insert_resource(WindowDescriptor { - title: "Gamepad Stick Input".to_owned(), - width: WINDOW_SIZE, - height: WINDOW_SIZE, - ..Default::default() - }) + .insert_resource(VisualizationSize::default()) .insert_resource(ClearColor(DEADZONE_COLOR)) .add_plugins(DefaultPlugins) .add_startup_system(setup) @@ -33,6 +38,7 @@ fn update_position( mut text_query: Query<&mut Text, With>, gamepads: Res, axes: Res>, + visualization_size: Res, ) { let mut transform = crosshair_query.single_mut(); let mut text = text_query.single_mut(); @@ -50,8 +56,8 @@ fn update_position( axis_type: GamepadAxisType::LeftStickY, }) .unwrap(); - transform.translation.x = x * WINDOW_SIZE / 2.0; - transform.translation.y = y * WINDOW_SIZE / 2.0; + transform.translation.x = x * visualization_size.size / 2.0; + transform.translation.y = y * visualization_size.size / 2.0; text.sections[0].value = format!("({:6.3}, {:6.3})", x, y); } } @@ -60,7 +66,32 @@ fn setup( mut commands: Commands, asset_server: Res, gamepad_settings: Res, + mut visualization_window: ResMut, + mut windows: ResMut, ) { + let window = windows.get_primary_mut().unwrap(); + window.set_title("Gamepad Stick Input".to_owned()); + + // The monitor's size + let physical_width = window.physical_width(); + let physical_height = window.physical_height(); + + // The scaling factor the OS uses to account for pixel density or user preference + let scale_factor = window.scale_factor(); + + // The resizing constraints that limit the resolution we can set + let constraints = window.resize_constraints(); + + let min_physical_dimension: f32 = + (physical_height.min(physical_width) as f64 * scale_factor) as f32; + let min_constraint_dimension = constraints.max_height.min(constraints.max_width); + + // Keep the size of the window within constraints + let min_dimension = min_physical_dimension.min(min_constraint_dimension); + + window.set_resolution(min_dimension, min_dimension); + visualization_window.size = min_dimension; + // Spawn camera commands.spawn_bundle(Camera2dBundle::default()); @@ -88,10 +119,10 @@ fn setup( let deadzone_midpoint = (deadzone_lowerbound + deadzone_upperbound) / 2.0; let livezone_size = livezone_upperbound - livezone_lowerbound; let deadzone_size = deadzone_upperbound - deadzone_lowerbound; - let livezone_box_midpoint = livezone_midpoint * WINDOW_SIZE / 2.0; - let deadzone_box_midpoint = deadzone_midpoint * WINDOW_SIZE / 2.0; - let livezone_box_size = livezone_size * WINDOW_SIZE / 2.0; - let deadzone_box_size = deadzone_size * WINDOW_SIZE / 2.0; + let livezone_box_midpoint = livezone_midpoint * min_dimension / 2.0; + let deadzone_box_midpoint = deadzone_midpoint * min_dimension / 2.0; + let livezone_box_size = livezone_size * min_dimension / 2.0; + let deadzone_box_size = deadzone_size * min_dimension / 2.0; // For text placement let livezone_lower_left_corner = (livezone_box_midpoint - livezone_box_size) / 2.0; From 397c68639ba7d209b6a54e042aca5d3d880a717d Mon Sep 17 00:00:00 2001 From: targrub Date: Thu, 22 Sep 2022 13:46:58 -0400 Subject: [PATCH 11/11] Removed as much windows code as possible. Left in setting the window title, though. --- examples/input/gamepad_stick_input.rs | 47 ++++----------------------- 1 file changed, 7 insertions(+), 40 deletions(-) diff --git a/examples/input/gamepad_stick_input.rs b/examples/input/gamepad_stick_input.rs index 0d4fd27bd3323..da1e594562a96 100644 --- a/examples/input/gamepad_stick_input.rs +++ b/examples/input/gamepad_stick_input.rs @@ -1,5 +1,6 @@ use bevy::{input::gamepad::GamepadSettings, prelude::*}; +const FULL_RANGE_SIZE: f32 = 300.0; const CROSSHAIR_SIZE: f32 = 16.0; const FONT: &str = "fonts/FiraMono-Medium.ttf"; const FONT_SIZE: f32 = 18.0; @@ -12,20 +13,8 @@ struct Crosshair; #[derive(Component)] struct CoordinateText; -#[derive(Resource)] -struct VisualizationSize { - size: f32, -} - -impl Default for VisualizationSize { - fn default() -> Self { - VisualizationSize { size: 300.0 } - } -} - fn main() { App::new() - .insert_resource(VisualizationSize::default()) .insert_resource(ClearColor(DEADZONE_COLOR)) .add_plugins(DefaultPlugins) .add_startup_system(setup) @@ -38,7 +27,6 @@ fn update_position( mut text_query: Query<&mut Text, With>, gamepads: Res, axes: Res>, - visualization_size: Res, ) { let mut transform = crosshair_query.single_mut(); let mut text = text_query.single_mut(); @@ -56,8 +44,8 @@ fn update_position( axis_type: GamepadAxisType::LeftStickY, }) .unwrap(); - transform.translation.x = x * visualization_size.size / 2.0; - transform.translation.y = y * visualization_size.size / 2.0; + transform.translation.x = x * FULL_RANGE_SIZE / 2.0; + transform.translation.y = y * FULL_RANGE_SIZE / 2.0; text.sections[0].value = format!("({:6.3}, {:6.3})", x, y); } } @@ -66,32 +54,11 @@ fn setup( mut commands: Commands, asset_server: Res, gamepad_settings: Res, - mut visualization_window: ResMut, mut windows: ResMut, ) { let window = windows.get_primary_mut().unwrap(); window.set_title("Gamepad Stick Input".to_owned()); - // The monitor's size - let physical_width = window.physical_width(); - let physical_height = window.physical_height(); - - // The scaling factor the OS uses to account for pixel density or user preference - let scale_factor = window.scale_factor(); - - // The resizing constraints that limit the resolution we can set - let constraints = window.resize_constraints(); - - let min_physical_dimension: f32 = - (physical_height.min(physical_width) as f64 * scale_factor) as f32; - let min_constraint_dimension = constraints.max_height.min(constraints.max_width); - - // Keep the size of the window within constraints - let min_dimension = min_physical_dimension.min(min_constraint_dimension); - - window.set_resolution(min_dimension, min_dimension); - visualization_window.size = min_dimension; - // Spawn camera commands.spawn_bundle(Camera2dBundle::default()); @@ -119,10 +86,10 @@ fn setup( let deadzone_midpoint = (deadzone_lowerbound + deadzone_upperbound) / 2.0; let livezone_size = livezone_upperbound - livezone_lowerbound; let deadzone_size = deadzone_upperbound - deadzone_lowerbound; - let livezone_box_midpoint = livezone_midpoint * min_dimension / 2.0; - let deadzone_box_midpoint = deadzone_midpoint * min_dimension / 2.0; - let livezone_box_size = livezone_size * min_dimension / 2.0; - let deadzone_box_size = deadzone_size * min_dimension / 2.0; + let livezone_box_midpoint = livezone_midpoint * FULL_RANGE_SIZE / 2.0; + let deadzone_box_midpoint = deadzone_midpoint * FULL_RANGE_SIZE / 2.0; + let livezone_box_size = livezone_size * FULL_RANGE_SIZE / 2.0; + let deadzone_box_size = deadzone_size * FULL_RANGE_SIZE / 2.0; // For text placement let livezone_lower_left_corner = (livezone_box_midpoint - livezone_box_size) / 2.0;