From 8b42a3f57a9c5a0a0352164c278366c2b392f682 Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 17:05:58 -0700 Subject: [PATCH 01/13] Derive `Reflect` + `FromReflect` for input types --- crates/bevy_input/src/gamepad.rs | 37 +++++++++++++++++++++---------- crates/bevy_input/src/keyboard.rs | 11 ++++----- crates/bevy_input/src/lib.rs | 6 +++-- crates/bevy_input/src/mouse.rs | 16 ++++++++----- crates/bevy_input/src/touch.rs | 10 ++++++--- 5 files changed, 53 insertions(+), 27 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 7756fc47af89b..2f6301e543f7d 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -1,6 +1,7 @@ use crate::{Axis, Input}; use bevy_ecs::event::{EventReader, EventWriter}; use bevy_ecs::system::{Res, ResMut, Resource}; +use bevy_reflect::{Reflect, FromReflect}; use bevy_utils::{tracing::info, HashMap, HashSet}; /// A gamepad with an associated `ID`. @@ -14,8 +15,9 @@ use bevy_utils::{tracing::info, HashMap, HashSet}; /// ## Note /// /// The `ID` of a gamepad is fixed until the gamepad disconnects or the app is restarted. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub struct Gamepad { /// The `ID` of the gamepad. pub id: usize, @@ -68,8 +70,9 @@ impl Gamepads { } /// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`]. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub enum GamepadEventType { /// A [`Gamepad`] has been connected. Connected, @@ -101,8 +104,9 @@ pub enum GamepadEventType { /// [`Axis`], and [`Axis`] resources won't be updated correctly. /// /// An example for gamepad input mocking can be seen in the documentation of the [`GamepadEventRaw`]. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub struct GamepadEvent { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -204,8 +208,9 @@ impl GamepadEvent { /// # /// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press); /// ``` -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub struct GamepadEventRaw { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -231,8 +236,9 @@ impl GamepadEventRaw { /// [`GamepadEventType::ButtonChanged`]. It is also used in the [`GamepadButton`] /// which in turn is used to create the [`Input`] or /// [`Axis`] `bevy` resources. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub enum GamepadButtonType { /// The bottom action button of the action pad (i.e. PS: Cross, Xbox: A). South, @@ -291,8 +297,9 @@ pub enum GamepadButtonType { /// ## Updating /// /// The resources are updated inside of the [`gamepad_event_system`]. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub struct GamepadButton { /// The gamepad on which the button is located on. pub gamepad: Gamepad, @@ -328,8 +335,9 @@ impl GamepadButton { /// This is used to determine which axis has changed its value when receiving a /// [`GamepadEventType::AxisChanged`]. It is also used in the [`GamepadAxis`] /// which in turn is used to create the [`Axis`] `bevy` resource. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub enum GamepadAxisType { /// The horizontal value of the left stick. LeftStickX, @@ -359,8 +367,9 @@ pub enum GamepadAxisType { /// ## Updating /// /// The resource is updated inside of the [`gamepad_event_system`]. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub struct GamepadAxis { /// The gamepad on which the axis is located on. pub gamepad: Gamepad, @@ -398,7 +407,8 @@ impl GamepadAxis { /// /// The [`GamepadSettings`] are used inside of the [`gamepad_event_system`], but are never written to /// inside of `bevy`. To modify these settings, mutate the corresponding resource. -#[derive(Resource, Default, Debug)] +#[derive(Resource, Default, Debug, Reflect, FromReflect)] +#[reflect(Debug)] pub struct GamepadSettings { /// The default button settings. pub default_button_settings: ButtonSettings, @@ -484,7 +494,8 @@ impl GamepadSettings { /// ## Updating /// /// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect, FromReflect)] +#[reflect(Debug)] pub struct ButtonSettings { /// The threshold for the button to be considered as pressed. pub press: f32, @@ -534,7 +545,8 @@ impl ButtonSettings { /// ## Updating /// /// The current value of an axis is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect, FromReflect)] +#[reflect(Debug)] pub struct AxisSettings { /// The positive high value at which to apply rounding. pub positive_high: f32, @@ -608,7 +620,8 @@ impl AxisSettings { /// ## Updating /// /// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect, FromReflect)] +#[reflect(Debug)] pub struct ButtonAxisSettings { /// The high value at which to apply rounding. pub high: f32, diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 0b6914cec440b..fd1e6af841bb6 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -11,8 +11,9 @@ use bevy_reflect::{FromReflect, Reflect}; /// /// The event is consumed inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system) /// to update the [`Input`](crate::Input) resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub struct KeyboardInput { /// The scan code of the key. pub scan_code: u32, @@ -62,9 +63,9 @@ pub fn keyboard_input_system( /// ## Updating /// /// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). -#[derive(Reflect, FromReflect, Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[reflect(Hash, PartialEq)] +#[reflect(Debug, Hash, PartialEq)] #[repr(u32)] pub enum KeyCode { /// The `1` key over the letters. @@ -425,7 +426,7 @@ pub enum KeyCode { /// ## Updating /// /// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). -#[derive(Reflect, FromReflect, Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -#[reflect(Hash, PartialEq)] +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] pub struct ScanCode(pub u32); diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index cebe484bce2bd..243ed7c783294 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -6,7 +6,6 @@ pub mod mouse; pub mod touch; pub use axis::*; -use bevy_ecs::schedule::{IntoSystemDescriptor, SystemLabel}; pub use input::*; pub mod prelude { @@ -24,6 +23,8 @@ pub mod prelude { } use bevy_app::prelude::*; +use bevy_ecs::schedule::{IntoSystemDescriptor, SystemLabel}; +use bevy_reflect::{FromReflect, Reflect}; use keyboard::{keyboard_input_system, KeyCode, KeyboardInput, ScanCode}; use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel}; use prelude::Gamepads; @@ -88,8 +89,9 @@ impl Plugin for InputPlugin { } /// The current "press" state of an element -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub enum ButtonState { Pressed, Released, diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index f77e89e097f47..4bc418172662d 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -1,6 +1,7 @@ use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_math::Vec2; +use bevy_reflect::{Reflect, FromReflect}; /// A mouse button input event. /// @@ -10,8 +11,9 @@ use bevy_math::Vec2; /// /// The event is read inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system) /// to update the [`Input`](crate::Input) resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub struct MouseButtonInput { /// The mouse button assigned to the event. pub button: MouseButton, @@ -29,8 +31,9 @@ pub struct MouseButtonInput { /// ## Updating /// /// The resource is updated inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system). -#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub enum MouseButton { /// The left mouse button. Left, @@ -51,8 +54,9 @@ pub enum MouseButton { /// However, the event data does not make it possible to distinguish which device it is referring to. /// /// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub struct MouseMotion { /// The change in the position of the pointing device since the last event was sent. pub delta: Vec2, @@ -64,8 +68,9 @@ pub struct MouseMotion { /// /// The value of the event can either be interpreted as the amount of lines or the amount of pixels /// to scroll. -#[derive(Debug, Clone, Copy, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub enum MouseScrollUnit { /// The line scroll unit. /// @@ -82,8 +87,9 @@ pub enum MouseScrollUnit { /// A mouse wheel event. /// /// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, PartialEq)] pub struct MouseWheel { /// The mouse scroll unit. pub unit: MouseScrollUnit, diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 3b43a7c7e377c..469e677ad7a3c 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -1,6 +1,7 @@ use bevy_ecs::event::EventReader; use bevy_ecs::system::{ResMut, Resource}; use bevy_math::Vec2; +use bevy_reflect::{Reflect, FromReflect}; use bevy_utils::HashMap; /// A touch input event. @@ -26,7 +27,8 @@ use bevy_utils::HashMap; /// /// This event is the translated version of the `WindowEvent::Touch` from the `winit` crate. /// It is available to the end user and can be used for game logic. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] pub struct TouchInput { /// The phase of the touch input. pub phase: TouchPhase, @@ -42,7 +44,8 @@ pub struct TouchInput { } /// A force description of a [`Touch`](crate::touch::Touch) input. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] pub enum ForceTouch { /// On iOS, the force is calibrated so that the same number corresponds to /// roughly the same amount of pressure on the screen regardless of the @@ -82,8 +85,9 @@ pub enum ForceTouch { /// This includes a phase that indicates that a touch input has started or ended, /// or that a finger has moved. There is also a cancelled phase that indicates that /// the system cancelled the tracking of the finger. -#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] pub enum TouchPhase { /// A finger started to touch the touchscreen. Started, From 0a46d5d6e92ed08b216e7a999d4bcdd3886992f5 Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 17:21:36 -0700 Subject: [PATCH 02/13] Add `register_type` for input types to plugin --- crates/bevy_input/src/lib.rs | 39 +++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index 243ed7c783294..e24f2e208235b 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -26,13 +26,16 @@ use bevy_app::prelude::*; use bevy_ecs::schedule::{IntoSystemDescriptor, SystemLabel}; use bevy_reflect::{FromReflect, Reflect}; 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}; +use mouse::{ + mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseScrollUnit, + MouseWheel, +}; +use touch::{touch_screen_input_system, ForceTouch, TouchInput, TouchPhase, Touches}; use gamepad::{ - gamepad_connection_system, gamepad_event_system, GamepadAxis, GamepadButton, GamepadEvent, - GamepadEventRaw, GamepadSettings, + gamepad_connection_system, gamepad_event_system, AxisSettings, ButtonAxisSettings, + ButtonSettings, Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, + GamepadEvent, GamepadEventRaw, GamepadEventType, GamepadSettings, Gamepads, }; /// Adds keyboard and mouse input to an App @@ -85,6 +88,32 @@ impl Plugin for InputPlugin { CoreStage::PreUpdate, touch_screen_input_system.label(InputSystem), ); + + // Register reflected types + app.register_type::(); + app.register_type::() + .register_type::() + .register_type::(); + app.register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::(); + app.register_type::() + .register_type::() + .register_type::(); + app.register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::(); } } From d45ac524dbe6fed8148193c3b7988be49146aacb Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 19:06:20 -0700 Subject: [PATCH 03/13] Fix rustfmt errors --- crates/bevy_input/src/gamepad.rs | 2 +- crates/bevy_input/src/mouse.rs | 2 +- crates/bevy_input/src/touch.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 2f6301e543f7d..cc131489fa7ec 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -1,7 +1,7 @@ use crate::{Axis, Input}; use bevy_ecs::event::{EventReader, EventWriter}; use bevy_ecs::system::{Res, ResMut, Resource}; -use bevy_reflect::{Reflect, FromReflect}; +use bevy_reflect::{FromReflect, Reflect}; use bevy_utils::{tracing::info, HashMap, HashSet}; /// A gamepad with an associated `ID`. diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 4bc418172662d..6ba880de6f54c 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -1,7 +1,7 @@ use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_math::Vec2; -use bevy_reflect::{Reflect, FromReflect}; +use bevy_reflect::{FromReflect, Reflect}; /// A mouse button input event. /// diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 03b1e2d0f4816..8315bdfaee730 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -1,7 +1,7 @@ use bevy_ecs::event::EventReader; use bevy_ecs::system::{ResMut, Resource}; use bevy_math::Vec2; -use bevy_reflect::{Reflect, FromReflect}; +use bevy_reflect::{FromReflect, Reflect}; use bevy_utils::HashMap; /// A touch input event. From ed1d7aeabf3a91a508bf58776b4ac145acd14acf Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 20:21:18 -0700 Subject: [PATCH 04/13] Separate type registrations Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> --- crates/bevy_input/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index e24f2e208235b..ed6fba18596a7 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -90,18 +90,23 @@ impl Plugin for InputPlugin { ); // Register reflected types + // Button types app.register_type::(); + // Keyboard types app.register_type::() .register_type::() .register_type::(); + // Mouse types app.register_type::() .register_type::() .register_type::() .register_type::() .register_type::(); + // Touch types app.register_type::() .register_type::() .register_type::(); + // Gamepad types app.register_type::() .register_type::() .register_type::() From e3cff4fc39a961b7c5013c680f4e0176a5674193 Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 20:24:54 -0700 Subject: [PATCH 05/13] Clean up comments --- crates/bevy_input/src/lib.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index ed6fba18596a7..a8d2fd905a9e6 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -89,24 +89,27 @@ impl Plugin for InputPlugin { touch_screen_input_system.label(InputSystem), ); - // Register reflected types - // Button types + // Register common types app.register_type::(); - // Keyboard types + + // Register keyboard types app.register_type::() .register_type::() .register_type::(); - // Mouse types + + // Register mouse types app.register_type::() .register_type::() .register_type::() .register_type::() .register_type::(); - // Touch types + + // Register touch types app.register_type::() .register_type::() .register_type::(); - // Gamepad types + + // Register gamepad types app.register_type::() .register_type::() .register_type::() From 7d24991ee0cb54ad0f4790f34f0e269e4b1af20d Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 20:25:16 -0700 Subject: [PATCH 06/13] Add `#[reflect(Default)]` --- crates/bevy_input/src/gamepad.rs | 10 +++++----- crates/bevy_input/src/input.rs | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index cc131489fa7ec..aeb3c3d6d60a5 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -1,7 +1,7 @@ use crate::{Axis, Input}; use bevy_ecs::event::{EventReader, EventWriter}; use bevy_ecs::system::{Res, ResMut, Resource}; -use bevy_reflect::{FromReflect, Reflect}; +use bevy_reflect::{std_traits::ReflectDefault, FromReflect, Reflect}; use bevy_utils::{tracing::info, HashMap, HashSet}; /// A gamepad with an associated `ID`. @@ -408,7 +408,7 @@ impl GamepadAxis { /// The [`GamepadSettings`] are used inside of the [`gamepad_event_system`], but are never written to /// inside of `bevy`. To modify these settings, mutate the corresponding resource. #[derive(Resource, Default, Debug, Reflect, FromReflect)] -#[reflect(Debug)] +#[reflect(Debug, Default)] pub struct GamepadSettings { /// The default button settings. pub default_button_settings: ButtonSettings, @@ -495,7 +495,7 @@ impl GamepadSettings { /// /// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. #[derive(Debug, Clone, Reflect, FromReflect)] -#[reflect(Debug)] +#[reflect(Debug, Default)] pub struct ButtonSettings { /// The threshold for the button to be considered as pressed. pub press: f32, @@ -546,7 +546,7 @@ impl ButtonSettings { /// /// The current value of an axis is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. #[derive(Debug, Clone, Reflect, FromReflect)] -#[reflect(Debug)] +#[reflect(Debug, Default)] pub struct AxisSettings { /// The positive high value at which to apply rounding. pub positive_high: f32, @@ -621,7 +621,7 @@ impl AxisSettings { /// /// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s. #[derive(Debug, Clone, Reflect, FromReflect)] -#[reflect(Debug)] +#[reflect(Debug, Default)] pub struct ButtonAxisSettings { /// The high value at which to apply rounding. pub high: f32, diff --git a/crates/bevy_input/src/input.rs b/crates/bevy_input/src/input.rs index 90e783ef9723d..76db7ab6d379d 100644 --- a/crates/bevy_input/src/input.rs +++ b/crates/bevy_input/src/input.rs @@ -1,5 +1,5 @@ use bevy_ecs::system::Resource; -use bevy_reflect::Reflect; +use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_utils::HashSet; use std::hash::Hash; @@ -35,7 +35,7 @@ use bevy_ecs::schedule::State; /// * Call the [`Input::release`] method for each release event. /// * Call the [`Input::clear`] method at each frame start, before processing events. #[derive(Debug, Clone, Resource, Reflect)] -#[reflect_value] +#[reflect_value(Default)] pub struct Input { /// A collection of every button that is currently being pressed. pressed: HashSet, From 768090ea795601020c552cf9ba38758a08ffa011 Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 20:45:49 -0700 Subject: [PATCH 07/13] Reflect `Serialize`+`Deserialize` on types --- crates/bevy_input/src/gamepad.rs | 11 +++++++++++ crates/bevy_input/src/keyboard.rs | 8 +++++++- crates/bevy_input/src/mouse.rs | 8 ++++++++ crates/bevy_input/src/touch.rs | 6 ++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index aeb3c3d6d60a5..14597c3302d30 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -4,6 +4,9 @@ use bevy_ecs::system::{Res, ResMut, Resource}; use bevy_reflect::{std_traits::ReflectDefault, FromReflect, Reflect}; use bevy_utils::{tracing::info, HashMap, HashSet}; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// A gamepad with an associated `ID`. /// /// ## Usage @@ -18,6 +21,7 @@ use bevy_utils::{tracing::info, HashMap, HashSet}; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct Gamepad { /// The `ID` of the gamepad. pub id: usize, @@ -73,6 +77,7 @@ impl Gamepads { #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum GamepadEventType { /// A [`Gamepad`] has been connected. Connected, @@ -107,6 +112,7 @@ pub enum GamepadEventType { #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct GamepadEvent { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -211,6 +217,7 @@ impl GamepadEvent { #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct GamepadEventRaw { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -239,6 +246,7 @@ impl GamepadEventRaw { #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum GamepadButtonType { /// The bottom action button of the action pad (i.e. PS: Cross, Xbox: A). South, @@ -300,6 +308,7 @@ pub enum GamepadButtonType { #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct GamepadButton { /// The gamepad on which the button is located on. pub gamepad: Gamepad, @@ -338,6 +347,7 @@ impl GamepadButton { #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum GamepadAxisType { /// The horizontal value of the left stick. LeftStickX, @@ -370,6 +380,7 @@ pub enum GamepadAxisType { #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct GamepadAxis { /// The gamepad on which the axis is located on. pub gamepad: Gamepad, diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index fd1e6af841bb6..c1f1a98404fad 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -2,6 +2,9 @@ use crate::{ButtonState, Input}; use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_reflect::{FromReflect, Reflect}; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// A keyboard input event. /// /// This event is the translated version of the `WindowEvent::KeyboardInput` from the `winit` crate. @@ -14,6 +17,7 @@ use bevy_reflect::{FromReflect, Reflect}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct KeyboardInput { /// The scan code of the key. pub scan_code: u32, @@ -66,6 +70,7 @@ pub fn keyboard_input_system( #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] #[repr(u32)] pub enum KeyCode { /// The `1` key over the letters. @@ -427,6 +432,7 @@ 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, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct ScanCode(pub u32); diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 6ba880de6f54c..32448172988b7 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -3,6 +3,9 @@ use bevy_ecs::{event::EventReader, system::ResMut}; use bevy_math::Vec2; use bevy_reflect::{FromReflect, Reflect}; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// A mouse button input event. /// /// This event is the translated version of the `WindowEvent::MouseInput` from the `winit` crate. @@ -14,6 +17,7 @@ use bevy_reflect::{FromReflect, Reflect}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct MouseButtonInput { /// The mouse button assigned to the event. pub button: MouseButton, @@ -34,6 +38,7 @@ pub struct MouseButtonInput { #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum MouseButton { /// The left mouse button. Left, @@ -57,6 +62,7 @@ pub enum MouseButton { #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct MouseMotion { /// The change in the position of the pointing device since the last event was sent. pub delta: Vec2, @@ -71,6 +77,7 @@ pub struct MouseMotion { #[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum MouseScrollUnit { /// The line scroll unit. /// @@ -90,6 +97,7 @@ pub enum MouseScrollUnit { #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct MouseWheel { /// The mouse scroll unit. pub unit: MouseScrollUnit, diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 8315bdfaee730..3cafbe43c2283 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -4,6 +4,9 @@ use bevy_math::Vec2; use bevy_reflect::{FromReflect, Reflect}; use bevy_utils::HashMap; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// A touch input event. /// /// ## Logic @@ -30,6 +33,7 @@ use bevy_utils::HashMap; #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub struct TouchInput { /// The phase of the touch input. pub phase: TouchPhase, @@ -48,6 +52,7 @@ pub struct TouchInput { #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum ForceTouch { /// On iOS, the force is calibrated so that the same number corresponds to /// roughly the same amount of pressure on the screen regardless of the @@ -90,6 +95,7 @@ pub enum ForceTouch { #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] pub enum TouchPhase { /// A finger started to touch the touchscreen. Started, From 0b41f8395ef41e64ec41a00f91504feada587a79 Mon Sep 17 00:00:00 2001 From: TehPers Date: Mon, 10 Oct 2022 20:46:08 -0700 Subject: [PATCH 08/13] Add "glam" feature to bevy_reflect dependency We're already using types from `glam` so this only brings in the reflection impls. --- 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 e3f500db46e09..7a9fbcddca741 100644 --- a/crates/bevy_input/Cargo.toml +++ b/crates/bevy_input/Cargo.toml @@ -18,7 +18,7 @@ bevy_app = { path = "../bevy_app", version = "0.9.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.9.0-dev" } bevy_math = { path = "../bevy_math", version = "0.9.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } -bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev" } +bevy_reflect = { path = "../bevy_reflect", version = "0.9.0-dev", features = ["glam"] } # other serde = { version = "1", features = ["derive"], optional = true } From 3abfcb1ebe0e4e73539849bada62335b77e9d705 Mon Sep 17 00:00:00 2001 From: TehPers Date: Tue, 11 Oct 2022 01:12:30 -0700 Subject: [PATCH 09/13] Combine related `#[cfg_attr]`s --- crates/bevy_input/src/gamepad.rs | 56 ++++++++++++++++++++++--------- crates/bevy_input/src/keyboard.rs | 21 ++++++++---- crates/bevy_input/src/lib.rs | 6 +++- crates/bevy_input/src/mouse.rs | 35 +++++++++++++------ crates/bevy_input/src/touch.rs | 21 ++++++++---- 5 files changed, 100 insertions(+), 39 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 14597c3302d30..4e46b4a1bfc5c 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -19,9 +19,12 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// /// The `ID` of a gamepad is fixed until the gamepad disconnects or the app is restarted. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct Gamepad { /// The `ID` of the gamepad. pub id: usize, @@ -75,9 +78,12 @@ impl Gamepads { /// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`]. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum GamepadEventType { /// A [`Gamepad`] has been connected. Connected, @@ -110,9 +116,12 @@ pub enum GamepadEventType { /// /// An example for gamepad input mocking can be seen in the documentation of the [`GamepadEventRaw`]. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadEvent { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -215,9 +224,12 @@ impl GamepadEvent { /// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press); /// ``` #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadEventRaw { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -244,9 +256,12 @@ impl GamepadEventRaw { /// which in turn is used to create the [`Input`] or /// [`Axis`] `bevy` resources. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum GamepadButtonType { /// The bottom action button of the action pad (i.e. PS: Cross, Xbox: A). South, @@ -306,9 +321,12 @@ pub enum GamepadButtonType { /// /// The resources are updated inside of the [`gamepad_event_system`]. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadButton { /// The gamepad on which the button is located on. pub gamepad: Gamepad, @@ -345,9 +363,12 @@ impl GamepadButton { /// [`GamepadEventType::AxisChanged`]. It is also used in the [`GamepadAxis`] /// which in turn is used to create the [`Axis`] `bevy` resource. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum GamepadAxisType { /// The horizontal value of the left stick. LeftStickX, @@ -378,9 +399,12 @@ pub enum GamepadAxisType { /// /// The resource is updated inside of the [`gamepad_event_system`]. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadAxis { /// The gamepad on which the axis is located on. pub gamepad: Gamepad, diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index c1f1a98404fad..e2080aae79339 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -15,9 +15,12 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// The event is consumed inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system) /// to update the [`Input`](crate::Input) resource. #[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct KeyboardInput { /// The scan code of the key. pub scan_code: u32, @@ -68,9 +71,12 @@ pub fn keyboard_input_system( /// /// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] #[repr(u32)] pub enum KeyCode { /// The `1` key over the letters. @@ -432,7 +438,10 @@ 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, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct ScanCode(pub u32); diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index a8d2fd905a9e6..7a931bdc489e7 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -127,8 +127,12 @@ impl Plugin for InputPlugin { /// The current "press" state of an element #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum ButtonState { Pressed, Released, diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 32448172988b7..50d92e52c0273 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -15,9 +15,12 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// The event is read inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system) /// to update the [`Input`](crate::Input) resource. #[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct MouseButtonInput { /// The mouse button assigned to the event. pub button: MouseButton, @@ -36,9 +39,12 @@ pub struct MouseButtonInput { /// /// The resource is updated inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system). #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum MouseButton { /// The left mouse button. Left, @@ -60,9 +66,12 @@ pub enum MouseButton { /// /// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct MouseMotion { /// The change in the position of the pointing device since the last event was sent. pub delta: Vec2, @@ -75,9 +84,12 @@ pub struct MouseMotion { /// The value of the event can either be interpreted as the amount of lines or the amount of pixels /// to scroll. #[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum MouseScrollUnit { /// The line scroll unit. /// @@ -95,9 +107,12 @@ pub enum MouseScrollUnit { /// /// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct MouseWheel { /// The mouse scroll unit. pub unit: MouseScrollUnit, diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 3cafbe43c2283..3f00d994fd3f0 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -31,9 +31,12 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// This event is the translated version of the `WindowEvent::Touch` from the `winit` crate. /// It is available to the end user and can be used for game logic. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct TouchInput { /// The phase of the touch input. pub phase: TouchPhase, @@ -50,9 +53,12 @@ pub struct TouchInput { /// A force description of a [`Touch`](crate::touch::Touch) input. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum ForceTouch { /// On iOS, the force is calibrated so that the same number corresponds to /// roughly the same amount of pressure on the screen regardless of the @@ -93,9 +99,12 @@ pub enum ForceTouch { /// or that a finger has moved. There is also a cancelled phase that indicates that /// the system cancelled the tracking of the finger. #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] #[reflect(Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum TouchPhase { /// A finger started to touch the touchscreen. Started, From 633523cccca1d8d159482e64236331c2cc51f06d Mon Sep 17 00:00:00 2001 From: TehPers Date: Tue, 18 Oct 2022 22:15:56 -0700 Subject: [PATCH 10/13] Add missing import --- crates/bevy_input/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index 7a931bdc489e7..23583667ae973 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -38,6 +38,9 @@ use gamepad::{ GamepadEvent, GamepadEventRaw, GamepadEventType, GamepadSettings, Gamepads, }; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// Adds keyboard and mouse input to an App #[derive(Default)] pub struct InputPlugin; From 2c798cd1eb5f1ba105c233c07b742be265174501 Mon Sep 17 00:00:00 2001 From: TehPers Date: Wed, 19 Oct 2022 14:32:45 -0700 Subject: [PATCH 11/13] Change Input from #[reflect_value] to #[reflect] --- crates/bevy_input/src/input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/input.rs b/crates/bevy_input/src/input.rs index 76db7ab6d379d..8e4556f9ae901 100644 --- a/crates/bevy_input/src/input.rs +++ b/crates/bevy_input/src/input.rs @@ -35,7 +35,7 @@ use bevy_ecs::schedule::State; /// * Call the [`Input::release`] method for each release event. /// * Call the [`Input::clear`] method at each frame start, before processing events. #[derive(Debug, Clone, Resource, Reflect)] -#[reflect_value(Default)] +#[reflect(Default)] pub struct Input { /// A collection of every button that is currently being pressed. pressed: HashSet, From 9c364b05f03fe6c8677bd97c638e124e209d961c Mon Sep 17 00:00:00 2001 From: TehPers Date: Wed, 26 Oct 2022 12:13:12 -0700 Subject: [PATCH 12/13] Reflect GamepadInfo --- crates/bevy_input/src/gamepad.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 5c90018ebb5db..7334e206121cc 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -88,8 +88,13 @@ impl Gamepad { } /// Metadata associated with a `Gamepad`. -#[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadInfo { pub name: String, } From e047e1119f28bafe291f7b388f7b8e5ff7bf2372 Mon Sep 17 00:00:00 2001 From: TehPers Date: Wed, 26 Oct 2022 12:31:57 -0700 Subject: [PATCH 13/13] Remove extra using --- crates/bevy_input/src/gamepad.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 7334e206121cc..8108ecb5bddc7 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -2,7 +2,7 @@ use crate::{Axis, Input}; use bevy_ecs::event::{EventReader, EventWriter}; use bevy_ecs::system::{Res, ResMut, Resource}; use bevy_reflect::{std_traits::ReflectDefault, FromReflect, Reflect}; -use bevy_utils::{tracing::info, HashMap, HashSet}; +use bevy_utils::{tracing::info, HashMap}; use thiserror::Error; /// Errors that occur when setting axis settings for gamepad input.