From 24afefe573e8f7aa6a086734549eaa915029c1d7 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sat, 30 Jul 2022 23:52:57 +0900 Subject: [PATCH 01/12] Added keyboard scan input event --- crates/bevy_input/Cargo.toml | 1 + crates/bevy_input/src/keyboard.rs | 124 +++++++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index 17db1fd140301..8cf615198d49c 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -21,3 +21,4 @@ bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" } # other serde = { version = "1", features = ["derive"], optional = true } +num_enum = "0.5.7" diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 40eec4b35a66b..85364ae61bd3d 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -1,5 +1,6 @@ use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; +use num_enum::FromPrimitive; /// A keyboard input event. /// @@ -27,10 +28,11 @@ pub struct KeyboardInput { /// The main difference between the [`KeyboardInput`] event and the [`Input`] resource is that /// the latter has convenient functions like [`Input::pressed`], [`Input::just_pressed`] and [`Input::just_released`]. pub fn keyboard_input_system( - mut keyboard_input: ResMut>, + mut scan_input: ResMut>, + mut key_input: ResMut>, mut keyboard_input_events: EventReader, ) { - keyboard_input.clear(); + key_input.clear(); for event in keyboard_input_events.iter() { if let KeyboardInput { key_code: Some(key_code), @@ -39,10 +41,17 @@ pub fn keyboard_input_system( } = event { match state { - ButtonState::Pressed => keyboard_input.press(*key_code), - ButtonState::Released => keyboard_input.release(*key_code), + ButtonState::Pressed => key_input.press(*key_code), + ButtonState::Released => key_input.release(*key_code), } } + let KeyboardInput { + scan_code, state, .. + } = event; + match state { + ButtonState::Pressed => scan_input.press(ScanCode::from(*scan_code)), + ButtonState::Released => scan_input.release(ScanCode::from(*scan_code)), + } } } @@ -407,3 +416,110 @@ pub enum KeyCode { /// The `Cut` key. Cut, } + +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, FromPrimitive)] +#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[repr(u32)] +pub enum ScanCode { + #[num_enum(default)] + Zero, + One, + Two, + Three, + Four, + Five, + Six, + Seven, + Eight, + Nine, + Ten, + Eleven, + Twelve, + Thirteen, + Fourteen, + Fifteen, + Sixteen, + Seventeen, + Eighteen, + Nineteen, + Twenty, + TwentyOne, + TwentyTwo, + TwentyThree, + TwentyFour, + TwentyFive, + TwentySix, + TwentySeven, + TwentyEight, + TwentyNine, + Thirty, + ThirtyOne, + ThirtyTwo, + ThirtyThree, + ThirtyFour, + ThirtyFive, + ThirtySix, + ThirtySeven, + ThirtyEight, + ThirtyNine, + Forty, + FortyOne, + FortyTwo, + FortyThree, + FortyFour, + FortyFive, + FortySix, + FortySeven, + FortyEight, + FortyNine, + Fifty, + FiftyOne, + FiftyTwo, + FiftyThree, + FiftyFour, + FiftyFive, + FiftySix, + FiftySeven, + FiftyEight, + FiftyNine, + Sixty, + SixtyOne, + SixtyTwo, + SixtyThree, + SixtyFour, + SixtyFive, + SixtySix, + SixtySeven, + SixtyEight, + SixtyNine, + Seventy, + SeventyOne, + SeventyTwo, + SeventyThree, + SeventyFour, + SeventyFive, + SeventySix, + SeventySeven, + SeventyEight, + SeventyNine, + Eighty, + EightyOne, + EightyTwo, + EightyThree, + EightyFour, + EightyFive, + EightySix, + EightySeven, + EightyEight, + EightyNine, + Ninety, + NinetyOne, + NinetyTwo, + NinetyThree, + NinetyFour, + NinetyFive, + NinetySix, + NinetySeven, + NinetyEight, + NinetyNine, +} From 1dd7f0ef1a9eb2d28a4e3d0619c72e632aac3e00 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 00:00:19 +0900 Subject: [PATCH 02/12] clear `scan_input` resource --- crates/bevy_input/src/keyboard.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 85364ae61bd3d..e0ba4021f1435 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -32,6 +32,7 @@ pub fn keyboard_input_system( mut key_input: ResMut>, mut keyboard_input_events: EventReader, ) { + scan_input.clear(); key_input.clear(); for event in keyboard_input_events.iter() { if let KeyboardInput { From f1f892e0d81e3390cbde16f52242d857b6167573 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 00:19:26 +0900 Subject: [PATCH 03/12] actually adding ScanCode resource --- crates/bevy_input/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index 625b8ed746a61..dca26c3edaaa2 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -16,7 +16,7 @@ pub mod prelude { Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, GamepadEvent, GamepadEventType, Gamepads, }, - keyboard::KeyCode, + keyboard::{KeyCode, ScanCode}, mouse::MouseButton, touch::{TouchInput, Touches}, Axis, Input, @@ -24,7 +24,7 @@ pub mod prelude { } use bevy_app::prelude::*; -use keyboard::{keyboard_input_system, KeyCode, KeyboardInput}; +use keyboard::{keyboard_input_system, KeyCode, KeyboardInput, ScanCode}; use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel}; use prelude::Gamepads; use touch::{touch_screen_input_system, TouchInput, Touches}; @@ -47,6 +47,7 @@ impl Plugin for InputPlugin { // keyboard .add_event::() .init_resource::>() + .init_resource::>() .add_system_to_stage( CoreStage::PreUpdate, keyboard_input_system.label(InputSystem), From eb7c0f78eea47ddf5eb8b87f0e3d3ffdeed930a7 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 02:03:14 +0900 Subject: [PATCH 04/12] Thanks! --- crates/bevy_input/Cargo.toml | 3 +- crates/bevy_input/src/keyboard.rs | 112 ++---------------------------- 2 files changed, 5 insertions(+), 110 deletions(-) diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index 8cf615198d49c..cc9ba8514ffd8 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -20,5 +20,4 @@ bevy_math = { path = "../bevy_math", version = "0.8.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" } # other -serde = { version = "1", features = ["derive"], optional = true } -num_enum = "0.5.7" +serde = { version = "1", features = ["derive"], optional = true } \ No newline at end of file diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index e0ba4021f1435..d934dc772ab5c 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -1,6 +1,5 @@ use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; -use num_enum::FromPrimitive; /// A keyboard input event. /// @@ -50,8 +49,8 @@ pub fn keyboard_input_system( scan_code, state, .. } = event; match state { - ButtonState::Pressed => scan_input.press(ScanCode::from(*scan_code)), - ButtonState::Released => scan_input.release(ScanCode::from(*scan_code)), + ButtonState::Pressed => scan_input.press(ScanCode(*scan_code)), + ButtonState::Released => scan_input.release(ScanCode(*scan_code)), } } } @@ -418,109 +417,6 @@ pub enum KeyCode { Cut, } -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, FromPrimitive)] +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[repr(u32)] -pub enum ScanCode { - #[num_enum(default)] - Zero, - One, - Two, - Three, - Four, - Five, - Six, - Seven, - Eight, - Nine, - Ten, - Eleven, - Twelve, - Thirteen, - Fourteen, - Fifteen, - Sixteen, - Seventeen, - Eighteen, - Nineteen, - Twenty, - TwentyOne, - TwentyTwo, - TwentyThree, - TwentyFour, - TwentyFive, - TwentySix, - TwentySeven, - TwentyEight, - TwentyNine, - Thirty, - ThirtyOne, - ThirtyTwo, - ThirtyThree, - ThirtyFour, - ThirtyFive, - ThirtySix, - ThirtySeven, - ThirtyEight, - ThirtyNine, - Forty, - FortyOne, - FortyTwo, - FortyThree, - FortyFour, - FortyFive, - FortySix, - FortySeven, - FortyEight, - FortyNine, - Fifty, - FiftyOne, - FiftyTwo, - FiftyThree, - FiftyFour, - FiftyFive, - FiftySix, - FiftySeven, - FiftyEight, - FiftyNine, - Sixty, - SixtyOne, - SixtyTwo, - SixtyThree, - SixtyFour, - SixtyFive, - SixtySix, - SixtySeven, - SixtyEight, - SixtyNine, - Seventy, - SeventyOne, - SeventyTwo, - SeventyThree, - SeventyFour, - SeventyFive, - SeventySix, - SeventySeven, - SeventyEight, - SeventyNine, - Eighty, - EightyOne, - EightyTwo, - EightyThree, - EightyFour, - EightyFive, - EightySix, - EightySeven, - EightyEight, - EightyNine, - Ninety, - NinetyOne, - NinetyTwo, - NinetyThree, - NinetyFour, - NinetyFive, - NinetySix, - NinetySeven, - NinetyEight, - NinetyNine, -} +pub struct ScanCode(u32); From dc4bc86f5e9440b221cab0fc47c239352a65c888 Mon Sep 17 00:00:00 2001 From: Bleb1k <91003089+Bleb1k@users.noreply.github.com> Date: Sun, 31 Jul 2022 10:53:43 +0900 Subject: [PATCH 05/12] Update crates/bevy_input/Cargo.toml Co-authored-by: Alice Cecile --- crates/bevy_input/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index cc9ba8514ffd8..17db1fd140301 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -20,4 +20,4 @@ bevy_math = { path = "../bevy_math", version = "0.8.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" } # other -serde = { version = "1", features = ["derive"], optional = true } \ No newline at end of file +serde = { version = "1", features = ["derive"], optional = true } From e6a31f49a912b2642815065a33074a1505b87956 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 14:07:43 +0900 Subject: [PATCH 06/12] docs --- crates/bevy_input/src/keyboard.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index d934dc772ab5c..7cfe05033984b 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -1,3 +1,5 @@ +use std::collections::linked_list; + use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; @@ -34,20 +36,15 @@ pub fn keyboard_input_system( scan_input.clear(); key_input.clear(); for event in keyboard_input_events.iter() { - if let KeyboardInput { - key_code: Some(key_code), - state, - .. - } = event - { - match state { - ButtonState::Pressed => key_input.press(*key_code), - ButtonState::Released => key_input.release(*key_code), - } - } let KeyboardInput { scan_code, state, .. } = event; + if let Some(key_code) = event.key_code { + match state { + ButtonState::Pressed => key_input.press(key_code), + ButtonState::Released => key_input.release(key_code), + } + } match state { ButtonState::Pressed => scan_input.press(ScanCode(*scan_code)), ButtonState::Released => scan_input.release(ScanCode(*scan_code)), @@ -417,6 +414,16 @@ pub enum KeyCode { Cut, } +/// The scan code of a [`KeyboardInput`](crate::keyboard::KeyboardInput). +/// +/// ## Usage +/// +/// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. +/// The resource stores the numeration of the buttons of a keyboard and can be accessed inside of a system. +/// +/// ## Updating +/// +/// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub struct ScanCode(u32); From 500d4be425b63619258975fe75bf26aae7c95bc8 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 14:10:06 +0900 Subject: [PATCH 07/12] removed unused import --- crates/bevy_input/src/keyboard.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 7cfe05033984b..07c731161dc12 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -1,5 +1,3 @@ -use std::collections::linked_list; - use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; @@ -426,4 +424,4 @@ pub enum KeyCode { /// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -pub struct ScanCode(u32); +pub struct ScanCode(pub u32); From 3da669918182a966314a9754c42ca75d9c5df662 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 14:46:06 +0900 Subject: [PATCH 08/12] docs tweaks --- crates/bevy_input/src/keyboard.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 07c731161dc12..45a766d25df40 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -24,8 +24,8 @@ pub struct KeyboardInput { /// /// ## Differences /// -/// The main difference between the [`KeyboardInput`] event and the [`Input`] resource is that -/// the latter has convenient functions like [`Input::pressed`], [`Input::just_pressed`] and [`Input::just_released`]. +/// The main difference between the [`KeyboardInput`] event and the [`Input`] or [`Input`] resources is that +/// the latter have convenient functions such as [`Input::pressed`], [`Input::just_pressed`] and [`Input::just_released`]. pub fn keyboard_input_system( mut scan_input: ResMut>, mut key_input: ResMut>, @@ -418,6 +418,7 @@ pub enum KeyCode { /// /// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. /// The resource stores the numeration of the buttons of a keyboard and can be accessed inside of a system. +/// Can be used instead of [`KeyCode`](KeyCode) for keyboard-layout independent controls /// /// ## Updating /// From 2a78b2aa82cf1ce30471bf37680de11fbba37081 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 14:56:44 +0900 Subject: [PATCH 09/12] more docs tweaks --- crates/bevy_input/src/keyboard.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 45a766d25df40..47750bb4fba41 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -56,6 +56,7 @@ pub fn keyboard_input_system( /// /// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. /// The resource stores the data of the buttons of a keyboard and can be accessed inside of a system. +/// Use [`KeyCode`](ScanCode) for keyboard-layout independent controls /// /// ## Updating /// @@ -418,7 +419,7 @@ pub enum KeyCode { /// /// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. /// The resource stores the numeration of the buttons of a keyboard and can be accessed inside of a system. -/// Can be used instead of [`KeyCode`](KeyCode) for keyboard-layout independent controls +/// Use ['KeyCode'](KeyCode) for more meaningful/readable code /// /// ## Updating /// From ed841d6af05321e28d09f72135c885e6cd6a647c Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Sun, 31 Jul 2022 15:00:56 +0900 Subject: [PATCH 10/12] semicolons typo --- crates/bevy_input/src/keyboard.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 47750bb4fba41..0fb97728d4088 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -419,7 +419,7 @@ pub enum KeyCode { /// /// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. /// The resource stores the numeration of the buttons of a keyboard and can be accessed inside of a system. -/// Use ['KeyCode'](KeyCode) for more meaningful/readable code +/// Use [`KeyCode`](KeyCode) for more meaningful/readable code /// /// ## Updating /// From cc493961ae357a7b7505366dda8287378a3e5517 Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Mon, 1 Aug 2022 13:46:44 +0900 Subject: [PATCH 11/12] docs changes --- crates/bevy_input/src/keyboard.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 0fb97728d4088..fdbf738702eb7 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -55,8 +55,7 @@ pub fn keyboard_input_system( /// ## Usage /// /// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. -/// The resource stores the data of the buttons of a keyboard and can be accessed inside of a system. -/// Use [`KeyCode`](ScanCode) for keyboard-layout independent controls +/// The resource values are mapped to the current layout of the keyboard and correlate to an [`ScanCode`](ScanCode). /// /// ## Updating /// @@ -417,9 +416,8 @@ pub enum KeyCode { /// /// ## Usage /// -/// It is used as the generic `T` value of an [`Input`](crate::Input) to create a `Res>`. -/// The resource stores the numeration of the buttons of a keyboard and can be accessed inside of a system. -/// Use [`KeyCode`](KeyCode) for more meaningful/readable code +/// It is used as the `ScanCode(u32)` value of an [`Input`](crate::Input) to create a `Res>`. +/// The resource values are mapped to the physical location of a key on the keyboard and correlate to an [`KeyCode`](KeyCode) /// /// ## Updating /// From c62cf20793e869aeb80f40b0dfe6630c01e98b2d Mon Sep 17 00:00:00 2001 From: Bleb1k Date: Mon, 1 Aug 2022 13:52:35 +0900 Subject: [PATCH 12/12] docs tweaks --- crates/bevy_input/src/keyboard.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index fdbf738702eb7..98abd1bdd025b 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -416,7 +416,7 @@ pub enum KeyCode { /// /// ## Usage /// -/// It is used as the `ScanCode(u32)` value of an [`Input`](crate::Input) to create a `Res>`. +/// It is used as the generic value of an [`Input`](crate::Input) to create a `Res>`. /// The resource values are mapped to the physical location of a key on the keyboard and correlate to an [`KeyCode`](KeyCode) /// /// ## Updating