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

Feature gate input methods #579

Merged
merged 12 commits into from
Aug 8, 2024
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ opt-level = 3
members = ["./", "tools/ci", "macros"]

[features]
default = ['asset', 'ui', 'bevy/bevy_gilrs']
default = ["asset", "ui", "mouse", "keyboard", "gamepad"]

# Allow support for tracking timing information about actions (how long a button was pressed, etc.)
timing = []

# Adds support for mouse-based inputs.
mouse = []

# Adds support for keyboard-based inputs.
keyboard = []

# Adds support for gamepad-based inputs.
gamepad = ["bevy/bevy_gilrs"]

# Allow using the `InputMap` as `bevy::asset::Asset`.
asset = ['bevy/bevy_asset']

Expand Down Expand Up @@ -69,15 +78,6 @@ bevy = { version = "0.14.0-rc.3", default-features = false, features = [
"bevy_pbr",
] }
serde_test = "1.0"
criterion = "0.5"

[[bench]]
name = "action_state"
harness = false

[[bench]]
name = "input_map"
harness = false

[lib]
name = "leafwing_input_manager"
Expand Down
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
- `Axislike` inputs can no longer be inserted directly into an `InputMap`: instead, use the `insert_dual_axis` method
- `InputStreams` has been removed in favor of an extensible `CentralInputStore` type, which you can add your own raw input kinds to
- `RawInputs` has been removed to ensure that clashes for new raw input kinds can be handled correctly
- each of the built-in input methods (keyboard, mouse, gamepad) is now controlled by its own feature flag
- disable `default-features` to avoid paying the runtime and compile time costs for input kinds your project doesn't care about
- the `bevy_gilrs` feature of `bevy` is now enabled via the `gamepad` feature

#### More inputs

Expand Down
73 changes: 0 additions & 73 deletions benches/action_state.rs

This file was deleted.

97 changes: 0 additions & 97 deletions benches/input_map.rs

This file was deleted.

31 changes: 23 additions & 8 deletions src/action_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -992,18 +992,21 @@ impl<A: Actionlike> ActionState<A> {
mod tests {
use crate as leafwing_input_manager;
use crate::action_state::ActionState;
use crate::clashing_inputs::ClashStrategy;
use crate::input_map::InputMap;
use crate::plugin::{AccumulatorPlugin, CentralInputStorePlugin};
use crate::prelude::updating::CentralInputStore;
use crate::prelude::{Buttonlike, ButtonlikeChord};
use bevy::input::InputPlugin;
use bevy::prelude::*;
use bevy::utils::{Duration, Instant};
use leafwing_input_manager_macros::Actionlike;

#[cfg(feature = "keyboard")]
#[test]
fn press_lifecycle() {
use std::time::{Duration, Instant};

use crate::input_map::InputMap;
use crate::plugin::{AccumulatorPlugin, CentralInputStorePlugin};
use crate::prelude::updating::CentralInputStore;
use crate::prelude::ClashStrategy;
use crate::user_input::Buttonlike;
use bevy::input::InputPlugin;

let mut app = App::new();
app.add_plugins((InputPlugin, AccumulatorPlugin, CentralInputStorePlugin));

Expand Down Expand Up @@ -1126,9 +1129,22 @@ mod tests {
assert!(!action_state.just_released(&Action::Two));
}

#[cfg(feature = "keyboard")]
#[test]
#[ignore = "Clashing inputs for non-buttonlike inputs is broken."]
fn update_with_clashes_prioritizing_longest() {
use std::time::{Duration, Instant};

use crate::input_map::InputMap;
use crate::plugin::{AccumulatorPlugin, CentralInputStorePlugin};
use crate::prelude::updating::CentralInputStore;
use crate::prelude::ClashStrategy;
use crate::user_input::chord::ButtonlikeChord;
use crate::user_input::Buttonlike;
use bevy::input::InputPlugin;
use bevy::prelude::KeyCode::*;
use bevy::prelude::*;

#[derive(Actionlike, Clone, Copy, PartialEq, Eq, Hash, Debug, Reflect)]
enum Action {
One,
Expand All @@ -1137,7 +1153,6 @@ mod tests {
}

// Input map
use bevy::prelude::KeyCode::*;
let mut input_map = InputMap::default();
input_map.insert(Action::One, Digit1);
input_map.insert(Action::Two, Digit2);
Expand Down
1 change: 1 addition & 0 deletions src/clashing_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ fn resolve_clash<A: Actionlike>(
}
}

#[cfg(feature = "keyboard")]
#[cfg(test)]
mod tests {
use bevy::app::App;
Expand Down
18 changes: 14 additions & 4 deletions src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ use serde::{Deserialize, Serialize};

use crate::clashing_inputs::ClashStrategy;
use crate::prelude::updating::CentralInputStore;
use crate::prelude::{find_gamepad, UserInputWrapper};
use crate::prelude::UserInputWrapper;
use crate::user_input::{Axislike, Buttonlike, DualAxislike};
use crate::{Actionlike, InputControlKind};

#[cfg(feature = "gamepad")]
use crate::user_input::gamepad::find_gamepad;

#[cfg(not(feature = "gamepad"))]
fn find_gamepad(_gamepads: &Gamepads) -> Gamepad {
Gamepad::new(0)
}

/// A Multi-Map that allows you to map actions to multiple [`UserInputs`](crate::user_input::UserInput)s,
/// whether they are [`Buttonlike`], [`Axislike`] or [`DualAxislike`].
///
Expand Down Expand Up @@ -820,6 +828,7 @@ impl<A: Actionlike, U: Buttonlike> FromIterator<(A, U)> for InputMap<A> {
}
}

#[cfg(feature = "keyboard")]
mod tests {
use bevy::prelude::Reflect;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -951,7 +960,7 @@ mod tests {

#[test]
fn merging() {
use bevy::input::{gamepad::GamepadButtonType, keyboard::KeyCode};
use bevy::input::keyboard::KeyCode;

let mut input_map = InputMap::default();
let mut default_keyboard_map = InputMap::default();
Expand All @@ -962,8 +971,8 @@ mod tests {
);

let mut default_gamepad_map = InputMap::default();
default_gamepad_map.insert(Action::Run, GamepadButtonType::South);
default_gamepad_map.insert(Action::Hide, GamepadButtonType::East);
default_gamepad_map.insert(Action::Run, KeyCode::Numpad0);
default_gamepad_map.insert(Action::Hide, KeyCode::Numpad7);

// Merging works
input_map.merge(&default_keyboard_map);
Expand All @@ -974,6 +983,7 @@ mod tests {
assert_eq!(input_map, default_keyboard_map);
}

#[cfg(feature = "gamepad")]
#[test]
fn gamepad_swapping() {
use bevy::input::gamepad::Gamepad;
Expand Down
Loading