use bevy::prelude::*;
use bevy_input_bindings::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
keyboard::Plugin::<MyActions, MyValues, 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::new());
})
.add_systems(Update, |values: Query<&ControllerValues<MyValues>>| {
for value in values {
let Some((val0, val1)) = value.get_as_axis2d(&MyValues::Move) else {
continue;
};
if val0 == 0.0 && val1 == 0.0 {
continue;
}
println!("Move: {val0:0.2}:{val1:0.2}");
}
})
.run();
}
#[derive(Debug)]
enum MyActions {
Jump,
Shoot,
}
#[derive(Debug, Hash, PartialEq, Eq)]
enum MyValues {
Move,
}
#[derive(Component, Reflect)]
struct MyKeyboardBinding {
jump: keyboard::triggers::ButtonPressed,
shoot: keyboard::triggers::ButtonPressed,
}
impl MyKeyboardBinding {
fn new() -> Self {
Self {
jump: keyboard::triggers::ButtonPressed::new(KeyCode::KeyQ),
shoot: keyboard::triggers::ButtonPressed::new(KeyCode::KeyW),
}
}
}
impl keyboard::Binding<MyActions, MyValues> 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)];
}
fn get_value_bindings<'a>(
&'a mut self,
) -> Vec<(&'a mut dyn keyboard::FunctionTrait, MyValues)> {
return vec![];
}
}