bevy_controls 1.0.0

Bevy controls library
Documentation
  • Coverage
  • 0%
    0 out of 39 items documented0 out of 16 items with examples
  • Size
  • Source code size: 177.29 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 3.93 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2m 42s Average build duration of successful builds.
  • all releases: 2m 42s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Schoggi0815

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:

// The context has to implement PartialEq and States.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Default, States, Reflect)]
enum ControlContext {
    #[default]
    MainMenu,
    Game,
    OpenInventory,
    Paused,
}

// The action has to implement PartialEq
#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect)]
enum ControlAction {
    Test1,
    TestAnalog1,
    TestMovementX,
    TestMovementY,
}

fn main() {
    App::new()
        // You can register the types for the Button and Analog controls to analyze them in the inspector.
        .register_type::<ButtonControl<ControlContext, ControlAction>>()
        .register_type::<AnalogControl<ControlContext, ControlAction>>()
        .add_plugins((
            DefaultPlugins,
            EguiPlugin::default(),
            WorldInspectorPlugin::new(),
            // Register the controls plugin
            ControlsPlugin::<ControlContext, ControlAction>::default(),
        ))
        .add_systems(Startup, setup)
        .add_systems(Update, (update_state, update))
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2d);

    // Spawn a new control like this:
    // The plugin will handle disabling and enabling it when you change the Context.
    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(
    // Use this exact query to make use of the trait methods.
    mut controls: Query<&mut ButtonControl<ControlContext, ControlAction>>,
) {
    // One line to check if the control was pressed.
    if controls.action_just_pressed(ControlAction::Test1) {
        info!("Executed TEST Action!");
    }
}