feat: scancode support

This commit is contained in:
2024-02-16 03:20:31 +01:00
parent 035ce77a35
commit 4996e4924e
3 changed files with 77 additions and 11 deletions
+47 -2
View File
@@ -58,8 +58,7 @@ impl InputsContainerInner<MyAction> for MyInputsContainer {
#[derive(Resource, Default, Deref, DerefMut)] #[derive(Resource, Default, Deref, DerefMut)]
struct JumpsCount(i32); struct JumpsCount(i32);
fn main() { fn main() { App::new()
App::new()
.add_plugins(( .add_plugins((
DefaultPlugins, DefaultPlugins,
ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(), ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(),
@@ -72,6 +71,7 @@ fn main() {
// TODO: default bingings... // TODO: default bingings...
fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) { fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) {
<<<<<<< HEAD
controls.force_push( controls.force_push(
MyAction::Left, MyAction::Left,
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))), Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))),
@@ -114,6 +114,51 @@ fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) {
KeyCode::Space, KeyCode::Space,
))), ))),
); );
=======
controls.force_push(
MyAction::Left,
// this way input will work only on latin layout only on `A`
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::KeyCode(KeyCode::A)))),
);
controls.force_push(
MyAction::Left,
// this way input will work only on any layout on same button, 35 is `H` for `us` layout or
// `О` for `ru` layout
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::ScanCode(ScanCode(35))))),
);
controls.force_push(
MyAction::Back,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::KeyCode(KeyCode::S)))),
);
controls.force_push(
MyAction::Back,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::ScanCode(ScanCode(36))))),
);
controls.force_push(
MyAction::Forward,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::KeyCode(KeyCode::W)))),
);
controls.force_push(
MyAction::Forward,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::ScanCode(ScanCode(37))))),
);
controls.force_push(
MyAction::Right,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::KeyCode(KeyCode::D)))),
);
controls.force_push(
MyAction::Right,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::ScanCode(ScanCode(38))))),
);
controls.force_push(
MyAction::Up,
Binding::new(ButtonCombination::Single(InputType::Keyboard(Keyboard::KeyCode(KeyCode::Space)))),
);
>>>>>>> b2e0bd2 (feat: scancode support)
} }
#[derive(Component)] #[derive(Component)]
+22 -7
View File
@@ -3,7 +3,7 @@ use bevy_ecs::{
schedule::State, schedule::State,
system::{Res, ResMut}, system::{Res, ResMut},
}; };
use bevy_input::{keyboard::KeyCode, mouse::MouseButton, Input}; use bevy_input::{keyboard::{KeyCode, ScanCode}, mouse::MouseButton, Input};
use crate::{ use crate::{
contract::{Action, GameState, InputsContainer}, contract::{Action, GameState, InputsContainer},
@@ -42,7 +42,8 @@ impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> Plugin for ControlsPlugin
impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs> { impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs> {
/// Process all hard inputs and bindings to update [`PlayerInputs`] /// Process all hard inputs and bindings to update [`PlayerInputs`]
fn save_input( fn save_input(
keyboard_input: Res<Input<KeyCode>>, keyboard_input_keycode: Res<Input<KeyCode>>,
keyboard_input_scancode: Res<Input<ScanCode>>,
mouse_input: Res<Input<MouseButton>>, mouse_input: Res<Input<MouseButton>>,
mut inputs_container: ResMut<Ic>, mut inputs_container: ResMut<Ic>,
controls: Res<Controls<A, Gs>>, controls: Res<Controls<A, Gs>>,
@@ -65,10 +66,19 @@ impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs>
match &binding.input { match &binding.input {
ButtonCombination::Single(button) => match button { ButtonCombination::Single(button) => match button {
InputType::Keyboard(key) => { InputType::Keyboard(Keyboard::KeyCode(key)) => {
log::trace!("presed?: {:?}", keyboard_input.pressed(*key)); log::trace!("presed?: {:?}", keyboard_input_keycode.pressed(*key));
log::trace!("active"); log::trace!("active");
if keyboard_input.pressed(*key) { if keyboard_input_keycode.pressed(*key) {
inputs.forced_set(*action, true);
break; // when on binding trigered no sence check another
}
inputs.forced_set(*action, false); // FIXME: this is happening on every pass so too frequently
}
InputType::Keyboard(Keyboard::ScanCode(code)) => {
log::trace!("presed?: {:?}", keyboard_input_scancode.pressed(*code));
log::trace!("active");
if keyboard_input_scancode.pressed(*code) {
inputs.forced_set(*action, true); inputs.forced_set(*action, true);
break; // when on binding trigered no sence check another break; // when on binding trigered no sence check another
} }
@@ -92,8 +102,13 @@ impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs>
// If any button in chord is not pressed we skip this `binding` // If any button in chord is not pressed we skip this `binding`
for button in buttons { for button in buttons {
match button { match button {
InputType::Keyboard(key) => { InputType::Keyboard(Keyboard::KeyCode(key)) => {
if !keyboard_input.pressed(*key) { if !keyboard_input_keycode.pressed(*key) {
continue 'bindings_loop;
}
}
InputType::Keyboard(Keyboard::ScanCode(code)) => {
if !keyboard_input_scancode.pressed(*code) {
continue 'bindings_loop; continue 'bindings_loop;
} }
} }
+8 -2
View File
@@ -2,7 +2,7 @@ use std::error;
use bevy_derive::{Deref, DerefMut}; use bevy_derive::{Deref, DerefMut};
use bevy_ecs::system::Resource; use bevy_ecs::system::Resource;
use bevy_input::{keyboard::KeyCode, mouse::MouseButton}; use bevy_input::{keyboard::{KeyCode, ScanCode}, mouse::MouseButton};
#[cfg(feature = "inspector-egui")] #[cfg(feature = "inspector-egui")]
use bevy_inspector_egui::prelude::*; use bevy_inspector_egui::prelude::*;
#[cfg(all(feature = "reflect", feature = "serialize"))] #[cfg(all(feature = "reflect", feature = "serialize"))]
@@ -199,12 +199,18 @@ impl<A: Action> PlayerInputs<A> {
common_traits_conditions! { common_traits_conditions! {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum InputType { pub enum InputType {
Keyboard(KeyCode), Keyboard(Keyboard),
Mouse(MouseInput), Mouse(MouseInput),
// TODO: Gamepad(GamepadButtonType), // TODO: Gamepad(GamepadButtonType),
// TODO: Touch screen // TODO: Touch screen
} }
#[derive(Debug, PartialEq, Clone)]
pub enum Keyboard {
KeyCode(KeyCode),
ScanCode(ScanCode),
}
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum MouseInput { pub enum MouseInput {
Button(MouseButton), Button(MouseButton),