Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gamepad_stick_input example #6052

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,16 @@ 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"

[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"
Expand Down
Binary file added assets/textures/crosshair.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | 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)
Expand Down
139 changes: 139 additions & 0 deletions examples/input/gamepad_stick_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
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;
const LIVEZONE_COLOR: Color = Color::GRAY;
const DEADZONE_COLOR: Color = Color::rgb(0.4, 0.4, 0.4);

#[derive(Component)]
struct Crosshair;

#[derive(Component)]
struct CoordinateText;

fn main() {
App::new()
.insert_resource(ClearColor(DEADZONE_COLOR))
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(update_position)
.run();
}

fn update_position(
mut crosshair_query: Query<&mut Transform, With<Crosshair>>,
mut text_query: Query<&mut Text, With<CoordinateText>>,
gamepads: Res<Gamepads>,
axes: Res<Axis<GamepadAxis>>,
) {
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.
let x = axes
.get(GamepadAxis {
gamepad,
axis_type: GamepadAxisType::LeftStickX,
})
.unwrap();
let y = axes
.get(GamepadAxis {
gamepad,
axis_type: GamepadAxisType::LeftStickY,
})
.unwrap();
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);
}
}

fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
gamepad_settings: Res<GamepadSettings>,
mut windows: ResMut<Windows>,
) {
let window = windows.get_primary_mut().unwrap();
window.set_title("Gamepad Stick Input".to_owned());

// Spawn camera
commands.spawn_bundle(Camera2dBundle::default());

// Spawn crosshair
let texture = asset_server.load("textures/crosshair.png");
commands
.spawn_bundle(SpriteBundle {
texture,
sprite: Sprite {
custom_size: Some(Vec2::splat(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);

// 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 * 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;

// Spawn livezone box
commands.spawn_bundle(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::splat(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::splat(deadzone_box_size)),
color: DEADZONE_COLOR,
..Default::default()
},
transform: Transform::from_xyz(deadzone_box_midpoint, deadzone_box_midpoint, 0.1),
..Default::default()
});

// 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::from_section("( 0.000, 0.000)", text_style).with_alignment(text_alignment),
transform: Transform::from_xyz(
livezone_lower_left_corner,
livezone_lower_left_corner,
1.0,
),
..Default::default()
})
.insert(CoordinateText);
}