Crates.io | leafwing-input-manager |
lib.rs | leafwing-input-manager |
version | 0.15.1 |
source | src |
created_at | 2022-01-23 21:45:29.298399 |
updated_at | 2024-10-06 14:33:00.242466 |
description | A powerful, flexible and ergonomic way to manage action-input keybindings for the Bevy game engine. |
homepage | https://leafwing-studios.com/ |
repository | https://github.com/leafwing-studios/leafwing-input-manager |
max_upload_size | |
id | 519881 |
size | 826,508 |
A straightforward but robust input-action manager for Bevy.
Inputs from various input sources (keyboard, mouse and gamepad) are collected into a common ActionState
on your player entity,
which can be conveniently used in your game logic.
The mapping between inputs and actions is many-to-many, and easily configured and extended with the InputMap
components on each player entity.
A single action can be triggered by multiple inputs (or set directly by UI elements or gameplay logic),
and a single input can result in multiple actions being triggered, which can be handled contextually.
Bevy | leafwing-input-manager |
---|---|
0.14 | 0.14..0.15 |
0.13 | 0.13 |
0.12 | 0.11..0.12 |
0.11 | 0.10 |
0.10 | 0.9 |
0.9 | 0.7..0.8 |
InputMap
component
Keybindings<KeyCode>
, Keybindings<Gamepad>
headachesActionState
component
input_map.insert(Action::Jump, KeyCode::Space)
and input_map.insert(Action::Jump, GamepadButtonType::South)
? Have both!input_map.insert(Action::Console, ButtonlikeChord::new([KeyCode::ControlLeft, KeyCode::Shift, KeyCode::KeyC]))
ClashStrategy
enum: stop triggering individual buttons when you meant to press a chord!ActionDiff
representation to send on the wire#![forbid(missing_docs)]
Gamepads
resource and use InputMap::set_gamepad
.leafwing-input-manager
to your Cargo.toml
.Actionlike
trait for it.InputManagerPlugin
to your App
.InputManagerBundle
to your player entity (or entities!).InputMap
component on your player entity.ActionState
component on your player entity to check the collected input state!use bevy::prelude::*;
use leafwing_input_manager::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// This plugin maps inputs to an input-type agnostic action-state
// We need to provide it with an enum which stores the possible actions a player could take
.add_plugins(InputManagerPlugin::<Action>::default())
// The InputMap and ActionState components will be added to any entity with the Player component
.add_systems(Startup, spawn_player)
// Read the ActionState in your systems using queries!
.add_systems(Update, jump)
.run();
}
// This is the list of "things in the game I want to be able to do based on input"
#[derive(Actionlike, PartialEq, Eq, Hash, Clone, Copy, Debug, Reflect)]
enum Action {
Run,
Jump,
}
#[derive(Component)]
struct Player;
fn spawn_player(mut commands: Commands) {
// Describes how to convert from player inputs into those actions
let input_map = InputMap::new([(Action::Jump, KeyCode::Space)]);
commands
.spawn(InputManagerBundle::with_map(input_map))
.insert(Player);
}
// Query for the `ActionState` component in your game logic systems!
fn jump(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.just_pressed(&Action::Jump) {
println!("I'm jumping!");
}
}
This snippet is the minimal.rs
example from the examples
folder: check there for more in-depth learning materials!
Please refer to the [features]
section in the Cargo.toml
for information about the available crate features.