diff --git a/crate/bevy_controls/example/basic/src/main.rs b/crate/bevy_controls/example/basic/src/main.rs index 7590cdf..ed23405 100644 --- a/crate/bevy_controls/example/basic/src/main.rs +++ b/crate/bevy_controls/example/basic/src/main.rs @@ -58,8 +58,7 @@ impl InputsContainerInner for MyInputsContainer { #[derive(Resource, Default, Deref, DerefMut)] struct JumpsCount(i32); -fn main() { - App::new() +fn main() { App::new() .add_plugins(( DefaultPlugins, ControlsPlugin::::default(), @@ -72,6 +71,7 @@ fn main() { // TODO: default bingings... fn add_bindings(mut controls: ResMut>) { +<<<<<<< HEAD controls.force_push( MyAction::Left, Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))), @@ -114,6 +114,51 @@ fn add_bindings(mut controls: ResMut>) { 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)] diff --git a/crate/bevy_controls/src/plugin.rs b/crate/bevy_controls/src/plugin.rs index 261c1e1..59f0963 100644 --- a/crate/bevy_controls/src/plugin.rs +++ b/crate/bevy_controls/src/plugin.rs @@ -3,7 +3,7 @@ use bevy_ecs::{ schedule::State, system::{Res, ResMut}, }; -use bevy_input::{keyboard::KeyCode, mouse::MouseButton, Input}; +use bevy_input::{keyboard::{KeyCode, ScanCode}, mouse::MouseButton, Input}; use crate::{ contract::{Action, GameState, InputsContainer}, @@ -42,7 +42,8 @@ impl, Gs: GameState> Plugin for ControlsPlugin impl, Gs: GameState> ControlsPlugin { /// Process all hard inputs and bindings to update [`PlayerInputs`] fn save_input( - keyboard_input: Res>, + keyboard_input_keycode: Res>, + keyboard_input_scancode: Res>, mouse_input: Res>, mut inputs_container: ResMut, controls: Res>, @@ -65,10 +66,19 @@ impl, Gs: GameState> ControlsPlugin match &binding.input { ButtonCombination::Single(button) => match button { - InputType::Keyboard(key) => { - log::trace!("presed?: {:?}", keyboard_input.pressed(*key)); + InputType::Keyboard(Keyboard::KeyCode(key)) => { + log::trace!("presed?: {:?}", keyboard_input_keycode.pressed(*key)); 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); break; // when on binding trigered no sence check another } @@ -92,8 +102,13 @@ impl, Gs: GameState> ControlsPlugin // If any button in chord is not pressed we skip this `binding` for button in buttons { match button { - InputType::Keyboard(key) => { - if !keyboard_input.pressed(*key) { + InputType::Keyboard(Keyboard::KeyCode(key)) => { + if !keyboard_input_keycode.pressed(*key) { + continue 'bindings_loop; + } + } + InputType::Keyboard(Keyboard::ScanCode(code)) => { + if !keyboard_input_scancode.pressed(*code) { continue 'bindings_loop; } } diff --git a/crate/bevy_controls/src/resource.rs b/crate/bevy_controls/src/resource.rs index 434f660..5822158 100644 --- a/crate/bevy_controls/src/resource.rs +++ b/crate/bevy_controls/src/resource.rs @@ -2,7 +2,7 @@ use std::error; use bevy_derive::{Deref, DerefMut}; use bevy_ecs::system::Resource; -use bevy_input::{keyboard::KeyCode, mouse::MouseButton}; +use bevy_input::{keyboard::{KeyCode, ScanCode}, mouse::MouseButton}; #[cfg(feature = "inspector-egui")] use bevy_inspector_egui::prelude::*; #[cfg(all(feature = "reflect", feature = "serialize"))] @@ -199,12 +199,18 @@ impl PlayerInputs { common_traits_conditions! { #[derive(Debug, PartialEq, Clone)] pub enum InputType { - Keyboard(KeyCode), + Keyboard(Keyboard), Mouse(MouseInput), // TODO: Gamepad(GamepadButtonType), // TODO: Touch screen } + #[derive(Debug, PartialEq, Clone)] + pub enum Keyboard { + KeyCode(KeyCode), + ScanCode(ScanCode), + } + #[derive(Debug, PartialEq, Clone)] pub enum MouseInput { Button(MouseButton),