BEVY CONTROLS
Bevy library for managing control and input mappings. This library abstracts different controls into different contexts where they are active, this allows for an easy check if controls are actually conflicting or just use the same keybind in different situations. The reading of controls also works in the FixedUpdate loop, they will keep their states.
How to use
You will need to provide your own Context and Actions types for the plugin:
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States, Reflect)]
enum ControlContext {
#[default]
MainMenu,
Game,
OpenInventory,
Paused,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect)]
enum ControlAction {
Test1,
TestAnalog1,
TestMovementX,
TestMovementY,
}
fn main() {
App::new()
.register_type::<ButtonControl<ControlContext, ControlAction>>()
.register_type::<AnalogControl<ControlContext, ControlAction>>()
.add_plugins((
DefaultPlugins,
EguiPlugin::default(),
WorldInspectorPlugin::new(),
ControlsPlugin::<ControlContext, ControlAction>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, (update_state, update))
.run();
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn((
Name::new("Test1 Control"),
ButtonControl::new(
ControlAction::Test1,
vec![ControlContext::MainMenu],
vec![
Buttons::Keyboard(KeyCode::Enter),
Buttons::Mouse(MouseButton::Left),
Buttons::Gamepad(GamepadButton::North),
],
),
));
}
fn update(
mut controls: Query<&mut ButtonControl<ControlContext, ControlAction>>,
) {
if controls.action_just_pressed(ControlAction::Test1) {
info!("Executed TEST Action!");
}
}