Skip to content

Commit

Permalink
Support Single Axis Inputs in Virtual DPads (#180)
Browse files Browse the repository at this point in the history
* Temporarily Comment Out Bevy Egui

* Make Axis Input Example Slightly More Readable

* Fix Bug in Symmetric Axis Constructor

Axis would always trigger because negative_low was positive.

* Return Axis Pair Results When Action Isn't Pressed

This makes sure that axis_pair() returns `Some` even if the value is
zero because the axis isn't moved.

* Set Dual Axis Value Zero if Not in Trigger Range

* Make Single-Axis Inputs Work in Virtual DPad

This makes sure that SingleAxis inputs will report a value of 0.0 if the
axis value is not withing the triggering zone. This makes it possible to
create a virtual gamepad made up of multiple single-axis inputs on the
same stick.

* Cargo Format
  • Loading branch information
zicklag authored Jul 15, 2022
1 parent 0ab4281 commit a38a98e
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 308 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ serde = {version = "1.0", features = ["derive"]}

[dev-dependencies]
bevy = {git = "https://github.com/bevyengine/bevy", default-features = false, features = ["bevy_gilrs", "bevy_sprite", "bevy_text", "bevy_ui", "bevy_render", "bevy_core_pipeline", "x11"]}
bevy_egui = { version="0.14", default-features = false }
# Uncomment once Bevy 0.8 is relese and bevy_egui supports it
#bevy_egui = "0.14.0"

[lib]
name = "leafwing_input_manager"
Expand Down
24 changes: 12 additions & 12 deletions examples/axis_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ fn spawn_player(mut commands: Commands) {
// Stores "which actions are currently activated"
action_state: ActionState::default(),
// Describes how to convert from player inputs into those actions
input_map: InputMap::new([
input_map: InputMap::default()
// Configure the left stick as a dual-axis
(DualAxis::left_stick(), Action::Move),
])
// Let's bind the right gamepad trigger to the throttle action
.insert(GamepadButtonType::RightTrigger2, Action::Throttle)
// And we'll use the right stick's x axis as a rudder control
.insert(
// This will trigger if the axis is moved 10% or more in either direction.
SingleAxis::symmetric(GamepadAxisType::RightStickX, 0.1),
Action::Rudder,
)
.build(),
.insert(DualAxis::left_stick(), Action::Move)
// Let's bind the right gamepad trigger to the throttle action
.insert(GamepadButtonType::RightTrigger2, Action::Throttle)
// And we'll use the right stick's x axis as a rudder control
.insert(
// This will trigger if the axis is moved 10% or more in either direction.
SingleAxis::symmetric(GamepadAxisType::RightStickX, 0.1),
Action::Rudder,
)
.build(),
});
}

// Query for the `ActionState` component in your game logic systems!
fn move_player(query: Query<&ActionState<Action>, With<Player>>) {
let action_state = query.single();

// Each action has a button-like state of its own that you can check
if action_state.pressed(Action::Move) {
// We're working with gamepads, so we want to defensively ensure that we're using the clamped values
Expand Down
Loading

0 comments on commit a38a98e

Please sign in to comment.