feat: scancode support
This commit is contained in:
@@ -58,8 +58,7 @@ impl InputsContainerInner<MyAction> for MyInputsContainer {
|
||||
#[derive(Resource, Default, Deref, DerefMut)]
|
||||
struct JumpsCount(i32);
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
fn main() { App::new()
|
||||
.add_plugins((
|
||||
DefaultPlugins,
|
||||
ControlsPlugin::<MyAction, MyInputsContainer, MyGameState>::default(),
|
||||
@@ -72,6 +71,7 @@ fn main() {
|
||||
|
||||
// TODO: default bingings...
|
||||
fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) {
|
||||
<<<<<<< HEAD
|
||||
controls.force_push(
|
||||
MyAction::Left,
|
||||
Binding::new(ButtonCombination::Single(InputType::Keyboard(KeyCode::A))),
|
||||
@@ -114,6 +114,51 @@ fn add_bindings(mut controls: ResMut<Controls<MyAction, MyGameState>>) {
|
||||
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)]
|
||||
|
||||
@@ -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<A: Action, Ic: InputsContainer<A>, Gs: GameState> Plugin for ControlsPlugin
|
||||
impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs> {
|
||||
/// Process all hard inputs and bindings to update [`PlayerInputs`]
|
||||
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>>,
|
||||
mut inputs_container: ResMut<Ic>,
|
||||
controls: Res<Controls<A, Gs>>,
|
||||
@@ -65,10 +66,19 @@ impl<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs>
|
||||
|
||||
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<A: Action, Ic: InputsContainer<A>, Gs: GameState> ControlsPlugin<A, Ic, Gs>
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<A: Action> PlayerInputs<A> {
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user