use bevy::prelude::*;
use bevy_input_bindings::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
keyboard::Plugin::<MyActions, NoValues, MyKeyboardBinding>::new(),
))
.add_observer(
|action: On<ActionEvent<MyActions>>, controllers: Query<&ControllerInterface>| {
let Ok(controller) = controllers.get(action.controller()) else {
return;
};
println!(
"Action from {}({:?}): {:?}",
action.controller(),
controller.layout(),
action.key()
);
},
)
.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Camera3d::default());
commands.spawn(MyKeyboardBinding::keyboard1());
commands.spawn(MyKeyboardBinding::keyboard2());
})
.run();
}
#[derive(Debug)]
enum MyActions {
Jump,
Shoot,
}
#[derive(Debug, Hash, PartialEq, Eq)]
struct NoValues;
#[derive(Component, Reflect)]
struct MyKeyboardBinding {
jump: keyboard::triggers::ButtonPressed,
shoot: keyboard::triggers::ButtonPressed,
}
impl MyKeyboardBinding {
fn keyboard1() -> Self {
Self {
jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyE),
shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyR),
}
}
fn keyboard2() -> Self {
Self {
jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyU),
shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyY),
}
}
}
impl keyboard::Binding<MyActions, NoValues> for MyKeyboardBinding {
fn get_action_bindings<'a>(
&'a mut self,
) -> Vec<(&'a mut dyn keyboard::TriggerTrait, MyActions)> {
return vec![(&mut self.jump, MyActions::Jump), (&mut self.shoot, MyActions::Shoot)];
}
}