diff --git a/crates/bevy_input/Cargo.toml b/crates/bevy_input/Cargo.toml index 29463fef08778..05978a4aa6252 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 } diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index d9d0a1d362a35..8108ecb5bddc7 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::{std_traits::ReflectDefault, FromReflect, Reflect}; use bevy_utils::{tracing::info, HashMap}; use thiserror::Error; @@ -53,6 +54,9 @@ pub enum ButtonSettingsError { }, } +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// A gamepad with an associated `ID`. /// /// ## Usage @@ -64,8 +68,13 @@ pub enum ButtonSettingsError { /// ## Note /// /// The `ID` of a gamepad is fixed until the gamepad disconnects or the app is restarted. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct Gamepad { /// The `ID` of the gamepad. pub id: usize, @@ -79,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, } @@ -129,8 +143,13 @@ impl Gamepads { } /// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`]. -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum GamepadEventType { /// A [`Gamepad`] has been connected. Connected(GamepadInfo), @@ -162,8 +181,13 @@ 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, PartialEq)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadEvent { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -266,8 +290,13 @@ impl GamepadEvent { /// # /// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press); /// ``` -#[derive(Debug, Clone, PartialEq)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct GamepadEventRaw { /// The gamepad this event corresponds to. pub gamepad: Gamepad, @@ -293,8 +322,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[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, @@ -353,8 +387,13 @@ pub enum GamepadButtonType { /// ## Updating /// /// The resources are updated inside of the [`gamepad_event_system`]. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[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, @@ -390,8 +429,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum GamepadAxisType { /// The horizontal value of the left stick. LeftStickX, @@ -421,8 +465,13 @@ pub enum GamepadAxisType { /// ## Updating /// /// The resource is updated inside of the [`gamepad_event_system`]. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[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, @@ -460,7 +509,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, Default)] pub struct GamepadSettings { /// The default button settings. pub default_button_settings: ButtonSettings, @@ -542,7 +592,8 @@ impl GamepadSettings { /// value is surpassed and released if the `release_threshold` value is undercut. /// /// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect, FromReflect)] +#[reflect(Debug, Default)] pub struct ButtonSettings { press_threshold: f32, release_threshold: f32, @@ -701,7 +752,8 @@ impl ButtonSettings { /// Otherwise, values will not be rounded. /// /// The valid range is `[-1.0, 1.0]`. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Reflect, FromReflect)] +#[reflect(Debug, Default)] pub struct AxisSettings { /// Values that are higher than `livezone_upperbound` will be rounded up to -1.0. livezone_upperbound: f32, @@ -1015,7 +1067,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, 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..8e4556f9ae901 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(Default)] pub struct Input { /// A collection of every button that is currently being pressed. pressed: HashSet, diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 0b6914cec440b..e2080aae79339 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. @@ -11,8 +14,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[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, @@ -62,9 +70,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[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), + reflect(Serialize, Deserialize) +)] #[repr(u32)] pub enum KeyCode { /// The `1` key over the letters. @@ -425,7 +437,11 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[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(Serialize, Deserialize) +)] pub struct ScanCode(pub u32); diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index cebe484bce2bd..23583667ae973 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,16 +23,24 @@ 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; -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, }; +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + /// Adds keyboard and mouse input to an App #[derive(Default)] pub struct InputPlugin; @@ -84,12 +91,51 @@ impl Plugin for InputPlugin { CoreStage::PreUpdate, touch_screen_input_system.label(InputSystem), ); + + // Register common types + app.register_type::(); + + // Register keyboard types + app.register_type::() + .register_type::() + .register_type::(); + + // Register mouse types + app.register_type::() + .register_type::() + .register_type::() + .register_type::() + .register_type::(); + + // Register touch types + app.register_type::() + .register_type::() + .register_type::(); + + // Register gamepad types + 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::(); } } /// The current "press" state of an element -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Reflect, FromReflect)] +#[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 f77e89e097f47..50d92e52c0273 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -1,6 +1,10 @@ use crate::{ButtonState, Input}; 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. /// @@ -10,8 +14,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[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, @@ -29,8 +38,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum MouseButton { /// The left mouse button. Left, @@ -51,8 +65,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[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, @@ -64,8 +83,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum MouseScrollUnit { /// The line scroll unit. /// @@ -82,8 +106,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[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 be88d897d1bbd..3f00d994fd3f0 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -1,8 +1,12 @@ use bevy_ecs::event::EventReader; use bevy_ecs::system::{ResMut, Resource}; 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 @@ -26,8 +30,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub struct TouchInput { /// The phase of the touch input. pub phase: TouchPhase, @@ -43,8 +52,13 @@ pub struct TouchInput { } /// A force description of a [`Touch`](crate::touch::Touch) input. -#[derive(Debug, Clone, Copy, PartialEq)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq)] +#[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 @@ -84,8 +98,13 @@ 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)] -#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] +#[reflect(Debug, Hash, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] pub enum TouchPhase { /// A finger started to touch the touchscreen. Started,