bevy_input: allow use without bevy_reflect (#14167)

# Objective

Allow use of `bevy_input` types without needing `bevy_reflect`.

## Solution

Make `bevy_reflect` within `bevy_input` optional. It's compiled in by
default.
Turn on reflect in dependencies as well when this feature is on.

## Testing

- Did you test these changes? If so, how?

I did a `cargo hack -p bevy_input --each-feature build`.

Signed-off-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
Co-authored-by: Torstein Grindvik <torstein.grindvik@muybridge.com>
This commit is contained in:
Torstein Grindvik 2024-07-08 03:09:07 +02:00 committed by GitHub
parent faf3c175b8
commit 70c0223112
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 236 additions and 169 deletions

View File

@ -9,19 +9,30 @@ license = "MIT OR Apache-2.0"
keywords = ["bevy"] keywords = ["bevy"]
[features] [features]
default = [] default = ["bevy_reflect"]
serialize = ["serde"] bevy_reflect = [
"dep:bevy_reflect",
"bevy_app/bevy_reflect",
"bevy_ecs/bevy_reflect",
"bevy_math/bevy_reflect",
]
serialize = ["serde", "smol_str/serde"]
[dependencies] [dependencies]
# bevy # bevy
bevy_app = { path = "../bevy_app", version = "0.14.0-dev" } bevy_app = { path = "../bevy_app", version = "0.14.0-dev", default-features = false }
bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev", default-features = false, features = [
bevy_math = { path = "../bevy_math", version = "0.14.0-dev" } "serialize",
] }
bevy_math = { path = "../bevy_math", version = "0.14.0-dev", default-features = false, features = [
"rand",
"serialize",
] }
bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" }
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [ bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
"glam", "glam",
"smol_str", "smol_str",
] } ], optional = true }
# other # other
serde = { version = "1", features = ["derive"], optional = true } serde = { version = "1", features = ["derive"], optional = true }

View File

@ -1,6 +1,7 @@
//! The generic input type. //! The generic input type.
use bevy_ecs::system::Resource; use bevy_ecs::system::Resource;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_utils::HashSet; use bevy_utils::HashSet;
use std::hash::Hash; use std::hash::Hash;
@ -152,8 +153,8 @@ use std::hash::Hash;
/// ///
///[`ResMut`]: bevy_ecs::system::ResMut ///[`ResMut`]: bevy_ecs::system::ResMut
///[`DetectChangesMut::bypass_change_detection`]: bevy_ecs::change_detection::DetectChangesMut::bypass_change_detection ///[`DetectChangesMut::bypass_change_detection`]: bevy_ecs::change_detection::DetectChangesMut::bypass_change_detection
#[derive(Debug, Clone, Resource, Reflect)] #[derive(Debug, Clone, Resource)]
#[reflect(Default)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Default))]
pub struct ButtonInput<T: Copy + Eq + Hash + Send + Sync + 'static> { pub struct ButtonInput<T: Copy + Eq + Hash + Send + Sync + 'static> {
/// A collection of every button that is currently being pressed. /// A collection of every button that is currently being pressed.
pressed: HashSet<T>, pressed: HashSet<T>,
@ -298,12 +299,10 @@ where
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use bevy_reflect::TypePath;
use crate::ButtonInput; use crate::ButtonInput;
/// Used for testing the functionality of [`ButtonInput`]. /// Used for testing the functionality of [`ButtonInput`].
#[derive(TypePath, Copy, Clone, Eq, PartialEq, Hash)] #[derive(Copy, Clone, Eq, PartialEq, Hash)]
enum DummyInput { enum DummyInput {
Input1, Input1,
Input2, Input2,

View File

@ -6,6 +6,7 @@ use bevy_ecs::{
change_detection::DetectChangesMut, change_detection::DetectChangesMut,
system::{Res, ResMut, Resource}, system::{Res, ResMut, Resource},
}; };
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_utils::Duration; use bevy_utils::Duration;
use bevy_utils::{tracing::info, HashMap}; use bevy_utils::{tracing::info, HashMap};
@ -66,7 +67,7 @@ pub enum ButtonSettingsError {
}, },
} }
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// A gamepad with an associated `ID`. /// A gamepad with an associated `ID`.
@ -80,11 +81,15 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// ## Note /// ## Note
/// ///
/// The `ID` of a gamepad is fixed until the gamepad disconnects or the app is restarted. /// The `ID` of a gamepad is fixed until the gamepad disconnects or the app is restarted.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct Gamepad { pub struct Gamepad {
@ -100,11 +105,11 @@ impl Gamepad {
} }
/// Metadata associated with a [`Gamepad`]. /// Metadata associated with a [`Gamepad`].
#[derive(Debug, Clone, PartialEq, Eq, Reflect)] #[derive(Debug, Clone, PartialEq, Eq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadInfo { pub struct GamepadInfo {
@ -167,11 +172,15 @@ impl Gamepads {
/// [`GamepadButtonChangedEvent`]. It is also used in the [`GamepadButton`] /// [`GamepadButtonChangedEvent`]. It is also used in the [`GamepadButton`]
/// which in turn is used to create the [`ButtonInput<GamepadButton>`] or /// which in turn is used to create the [`ButtonInput<GamepadButton>`] or
/// [`Axis<GamepadButton>`] `bevy` resources. /// [`Axis<GamepadButton>`] `bevy` resources.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, PartialOrd, Ord)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum GamepadButtonType { pub enum GamepadButtonType {
@ -232,11 +241,15 @@ pub enum GamepadButtonType {
/// ## Updating /// ## Updating
/// ///
/// The gamepad button resources are updated inside of the [`gamepad_button_event_system`]. /// The gamepad button resources are updated inside of the [`gamepad_button_event_system`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadButton { pub struct GamepadButton {
@ -268,11 +281,11 @@ impl GamepadButton {
} }
/// A gamepad button input event. /// A gamepad button input event.
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadButtonInput { pub struct GamepadButtonInput {
@ -289,11 +302,11 @@ pub struct GamepadButtonInput {
/// This is used to determine which axis has changed its value when receiving a /// This is used to determine which axis has changed its value when receiving a
/// [`GamepadAxisChangedEvent`]. It is also used in the [`GamepadAxis`] /// [`GamepadAxisChangedEvent`]. It is also used in the [`GamepadAxis`]
/// which in turn is used to create the [`Axis<GamepadAxis>`] `bevy` resource. /// which in turn is used to create the [`Axis<GamepadAxis>`] `bevy` resource.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[reflect(Debug, Hash, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum GamepadAxisType { pub enum GamepadAxisType {
@ -325,11 +338,11 @@ pub enum GamepadAxisType {
/// ## Updating /// ## Updating
/// ///
/// The gamepad axes resources are updated inside of the [`gamepad_axis_event_system`]. /// The gamepad axes resources are updated inside of the [`gamepad_axis_event_system`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[reflect(Debug, Hash, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadAxis { pub struct GamepadAxis {
@ -370,8 +383,9 @@ impl GamepadAxis {
/// The [`GamepadSettings`] are used inside of `bevy_gilrs` to determine when raw gamepad events from `gilrs`, /// The [`GamepadSettings`] are used inside of `bevy_gilrs` to determine when raw gamepad events from `gilrs`,
/// should register as a [`GamepadEvent`]. Events that don't meet the change thresholds defined in [`GamepadSettings`] /// should register as a [`GamepadEvent`]. Events that don't meet the change thresholds defined in [`GamepadSettings`]
/// will not register. To modify these settings, mutate the corresponding resource. /// will not register. To modify these settings, mutate the corresponding resource.
#[derive(Resource, Default, Debug, Reflect)] #[derive(Resource, Default, Debug)]
#[reflect(Debug, Default)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Default))]
pub struct GamepadSettings { pub struct GamepadSettings {
/// The default button settings. /// The default button settings.
pub default_button_settings: ButtonSettings, pub default_button_settings: ButtonSettings,
@ -453,8 +467,8 @@ impl GamepadSettings {
/// value is surpassed and released if the `release_threshold` value is undercut. /// value is surpassed and released if the `release_threshold` value is undercut.
/// ///
/// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` /// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0`
#[derive(Debug, Clone, Reflect)] #[derive(Debug, Clone)]
#[reflect(Debug, Default)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Default))]
pub struct ButtonSettings { pub struct ButtonSettings {
press_threshold: f32, press_threshold: f32,
release_threshold: f32, release_threshold: f32,
@ -613,8 +627,8 @@ impl ButtonSettings {
/// Otherwise, values will not be rounded. /// Otherwise, values will not be rounded.
/// ///
/// The valid range is `[-1.0, 1.0]`. /// The valid range is `[-1.0, 1.0]`.
#[derive(Debug, Clone, Reflect, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[reflect(Debug, Default)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Default))]
pub struct AxisSettings { pub struct AxisSettings {
/// Values that are higher than `livezone_upperbound` will be rounded up to 1.0. /// Values that are higher than `livezone_upperbound` will be rounded up to 1.0.
livezone_upperbound: f32, livezone_upperbound: f32,
@ -945,8 +959,8 @@ impl AxisSettings {
/// ## Updating /// ## Updating
/// ///
/// The current value of a button is received through the [`GamepadButtonChangedEvent`]. /// The current value of a button is received through the [`GamepadButtonChangedEvent`].
#[derive(Debug, Clone, Reflect)] #[derive(Debug, Clone)]
#[reflect(Debug, Default)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, Default))]
pub struct ButtonAxisSettings { pub struct ButtonAxisSettings {
/// The high value at which to apply rounding. /// The high value at which to apply rounding.
pub high: f32, pub high: f32,
@ -1055,11 +1069,11 @@ pub fn gamepad_connection_system(
} }
/// The connection status of a gamepad. /// The connection status of a gamepad.
#[derive(Debug, Clone, PartialEq, Reflect)] #[derive(Debug, Clone, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum GamepadConnection { pub enum GamepadConnection {
@ -1071,11 +1085,11 @@ pub enum GamepadConnection {
/// A Gamepad connection event. Created when a connection to a gamepad /// A Gamepad connection event. Created when a connection to a gamepad
/// is established and when a gamepad is disconnected. /// is established and when a gamepad is disconnected.
#[derive(Event, Debug, Clone, PartialEq, Reflect)] #[derive(Event, Debug, Clone, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadConnectionEvent { pub struct GamepadConnectionEvent {
@ -1107,11 +1121,11 @@ impl GamepadConnectionEvent {
/// Gamepad event for when the "value" on the axis changes /// Gamepad event for when the "value" on the axis changes
/// by an amount larger than the threshold defined in [`GamepadSettings`]. /// by an amount larger than the threshold defined in [`GamepadSettings`].
#[derive(Event, Debug, Clone, PartialEq, Reflect)] #[derive(Event, Debug, Clone, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadAxisChangedEvent { pub struct GamepadAxisChangedEvent {
@ -1136,11 +1150,11 @@ impl GamepadAxisChangedEvent {
/// Gamepad event for when the "value" (amount of pressure) on the button /// Gamepad event for when the "value" (amount of pressure) on the button
/// changes by an amount larger than the threshold defined in [`GamepadSettings`]. /// changes by an amount larger than the threshold defined in [`GamepadSettings`].
#[derive(Event, Debug, Clone, PartialEq, Reflect)] #[derive(Event, Debug, Clone, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct GamepadButtonChangedEvent { pub struct GamepadButtonChangedEvent {
@ -1215,11 +1229,11 @@ pub fn gamepad_button_event_system(
/// This event type is used over the [`GamepadConnectionEvent`], /// This event type is used over the [`GamepadConnectionEvent`],
/// [`GamepadButtonChangedEvent`] and [`GamepadAxisChangedEvent`] when /// [`GamepadButtonChangedEvent`] and [`GamepadAxisChangedEvent`] when
/// the in-frame relative ordering of events is important. /// the in-frame relative ordering of events is important.
#[derive(Event, Debug, Clone, PartialEq, Reflect)] #[derive(Event, Debug, Clone, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum GamepadEvent { pub enum GamepadEvent {

View File

@ -2,9 +2,10 @@
use bevy_ecs::event::Event; use bevy_ecs::event::Event;
use bevy_math::Vec2; use bevy_math::Vec2;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect; use bevy_reflect::Reflect;
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// Two-finger pinch gesture, often used for magnifications. /// Two-finger pinch gesture, often used for magnifications.
@ -16,11 +17,11 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// ///
/// - Only available on **`macOS`** and **`iOS`**. /// - Only available on **`macOS`** and **`iOS`**.
/// - On **`iOS`**, must be enabled first /// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct PinchGesture(pub f32); pub struct PinchGesture(pub f32);
@ -34,11 +35,11 @@ pub struct PinchGesture(pub f32);
/// ///
/// - Only available on **`macOS`** and **`iOS`**. /// - Only available on **`macOS`** and **`iOS`**.
/// - On **`iOS`**, must be enabled first /// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct RotationGesture(pub f32); pub struct RotationGesture(pub f32);
@ -49,11 +50,11 @@ pub struct RotationGesture(pub f32);
/// ///
/// - Only available on **`macOS`** and **`iOS`**. /// - Only available on **`macOS`** and **`iOS`**.
/// - On **`iOS`**, must be enabled first /// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct DoubleTapGesture; pub struct DoubleTapGesture;
@ -63,11 +64,11 @@ pub struct DoubleTapGesture;
/// ## Platform-specific /// ## Platform-specific
/// ///
/// - On **`iOS`**, must be enabled first /// - On **`iOS`**, must be enabled first
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct PanGesture(pub Vec2); pub struct PanGesture(pub Vec2);

View File

@ -72,10 +72,11 @@ use bevy_ecs::{
event::{Event, EventReader}, event::{Event, EventReader},
system::ResMut, system::ResMut,
}; };
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect; use bevy_reflect::Reflect;
use smol_str::SmolStr; use smol_str::SmolStr;
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// A keyboard input event. /// A keyboard input event.
@ -87,11 +88,11 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// ///
/// The event is consumed inside of the [`keyboard_input_system`] /// The event is consumed inside of the [`keyboard_input_system`]
/// to update the [`ButtonInput<KeyCode>`](ButtonInput<KeyCode>) resource. /// to update the [`ButtonInput<KeyCode>`](ButtonInput<KeyCode>) resource.
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)] #[derive(Event, Debug, Clone, PartialEq, Eq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct KeyboardInput { pub struct KeyboardInput {
@ -111,10 +112,11 @@ pub struct KeyboardInput {
/// when, for example, switching between windows with 'Alt-Tab' or using any other /// when, for example, switching between windows with 'Alt-Tab' or using any other
/// OS specific key combination that leads to Bevy window losing focus and not receiving any /// OS specific key combination that leads to Bevy window losing focus and not receiving any
/// input events /// input events
#[derive(Event, Debug, Clone, PartialEq, Eq, Reflect)] #[derive(Event, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct KeyboardFocusLost; pub struct KeyboardFocusLost;
@ -160,10 +162,11 @@ pub fn keyboard_input_system(
/// ///
/// - Correctly match key press and release events. /// - Correctly match key press and release events.
/// - On non-web platforms, support assigning keybinds to virtually any key through a UI. /// - On non-web platforms, support assigning keybinds to virtually any key through a UI.
#[derive(Debug, Clone, Ord, PartialOrd, Copy, PartialEq, Eq, Hash, Reflect)] #[derive(Debug, Clone, Ord, PartialOrd, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum NativeKeyCode { pub enum NativeKeyCode {
@ -197,11 +200,15 @@ pub enum NativeKeyCode {
/// ## Updating /// ## Updating
/// ///
/// The resource is updated inside of the [`keyboard_input_system`]. /// The resource is updated inside of the [`keyboard_input_system`].
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect)] #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
#[allow(clippy::doc_markdown)] // Clippy doesn't like our use of <kbd>. #[allow(clippy::doc_markdown)] // Clippy doesn't like our use of <kbd>.
@ -686,11 +693,15 @@ pub enum KeyCode {
/// This enum is primarily used to store raw keysym when Winit doesn't map a given native logical /// This enum is primarily used to store raw keysym when Winit doesn't map a given native logical
/// key identifier to a meaningful [`Key`] variant. This lets you use [`Key`], and let the user /// key identifier to a meaningful [`Key`] variant. This lets you use [`Key`], and let the user
/// define keybinds which work in the presence of identifiers we haven't mapped for you yet. /// define keybinds which work in the presence of identifiers we haven't mapped for you yet.
#[derive(Debug, Clone, Ord, PartialOrd, PartialEq, Eq, Hash, Reflect)] #[derive(Debug, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum NativeKey { pub enum NativeKey {
@ -715,11 +726,15 @@ pub enum NativeKey {
/// ///
/// Its values map 1 to 1 to winit's Key. /// Its values map 1 to 1 to winit's Key.
#[non_exhaustive] #[non_exhaustive]
#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Reflect)] #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
#[allow(clippy::doc_markdown)] // Clippy doesn't like our use of <kbd>. #[allow(clippy::doc_markdown)] // Clippy doesn't like our use of <kbd>.

View File

@ -40,6 +40,7 @@ pub mod prelude {
use bevy_app::prelude::*; use bevy_app::prelude::*;
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect; use bevy_reflect::Reflect;
use gestures::*; use gestures::*;
use keyboard::{keyboard_input_system, KeyCode, KeyboardFocusLost, KeyboardInput}; use keyboard::{keyboard_input_system, KeyCode, KeyboardFocusLost, KeyboardInput};
@ -57,7 +58,7 @@ use gamepad::{
GamepadRumbleRequest, GamepadSettings, Gamepads, GamepadRumbleRequest, GamepadSettings, Gamepads,
}; };
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// Adds keyboard and mouse input to an App /// Adds keyboard and mouse input to an App
@ -127,29 +128,36 @@ impl Plugin for InputPlugin {
.init_resource::<Touches>() .init_resource::<Touches>()
.add_systems(PreUpdate, touch_screen_input_system.in_set(InputSystem)); .add_systems(PreUpdate, touch_screen_input_system.in_set(InputSystem));
// Register common types #[cfg(feature = "bevy_reflect")]
app.register_type::<ButtonState>() {
.register_type::<KeyboardInput>() // Register common types
.register_type::<MouseButtonInput>() app.register_type::<ButtonState>()
.register_type::<PinchGesture>() .register_type::<KeyboardInput>()
.register_type::<RotationGesture>() .register_type::<MouseButtonInput>()
.register_type::<DoubleTapGesture>() .register_type::<PinchGesture>()
.register_type::<PanGesture>() .register_type::<RotationGesture>()
.register_type::<TouchInput>() .register_type::<DoubleTapGesture>()
.register_type::<GamepadEvent>() .register_type::<PanGesture>()
.register_type::<GamepadButtonInput>() .register_type::<TouchInput>()
.register_type::<GamepadSettings>() .register_type::<GamepadEvent>()
.register_type::<AccumulatedMouseMotion>() .register_type::<GamepadButtonInput>()
.register_type::<AccumulatedMouseScroll>(); .register_type::<GamepadSettings>()
.register_type::<AccumulatedMouseMotion>()
.register_type::<AccumulatedMouseScroll>();
}
} }
} }
/// The current "press" state of an element /// The current "press" state of an element
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Reflect)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum ButtonState { pub enum ButtonState {

View File

@ -2,6 +2,7 @@
use crate::{ButtonInput, ButtonState}; use crate::{ButtonInput, ButtonState};
use bevy_ecs::entity::Entity; use bevy_ecs::entity::Entity;
#[cfg(feature = "bevy_reflect")]
use bevy_ecs::reflect::ReflectResource; use bevy_ecs::reflect::ReflectResource;
use bevy_ecs::system::Resource; use bevy_ecs::system::Resource;
use bevy_ecs::{ use bevy_ecs::{
@ -10,9 +11,10 @@ use bevy_ecs::{
system::ResMut, system::ResMut,
}; };
use bevy_math::Vec2; use bevy_math::Vec2;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::{std_traits::ReflectDefault, Reflect}; use bevy_reflect::{std_traits::ReflectDefault, Reflect};
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// A mouse button input event. /// A mouse button input event.
@ -23,11 +25,11 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// ///
/// The event is read inside of the [`mouse_button_input_system`] /// The event is read inside of the [`mouse_button_input_system`]
/// to update the [`ButtonInput<MouseButton>`] resource. /// to update the [`ButtonInput<MouseButton>`] resource.
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct MouseButtonInput { pub struct MouseButtonInput {
@ -49,11 +51,15 @@ pub struct MouseButtonInput {
/// ## Updating /// ## Updating
/// ///
/// The resource is updated inside of the [`mouse_button_input_system`]. /// The resource is updated inside of the [`mouse_button_input_system`].
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect)] #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum MouseButton { pub enum MouseButton {
@ -80,11 +86,11 @@ pub enum MouseButton {
/// However, the event data does not make it possible to distinguish which device it is referring to. /// 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 /// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct MouseMotion { pub struct MouseMotion {
@ -98,11 +104,11 @@ pub struct MouseMotion {
/// ///
/// The value of the event can either be interpreted as the amount of lines or the amount of pixels /// The value of the event can either be interpreted as the amount of lines or the amount of pixels
/// to scroll. /// to scroll.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum MouseScrollUnit { pub enum MouseScrollUnit {
@ -121,11 +127,11 @@ pub enum MouseScrollUnit {
/// A mouse wheel event. /// A mouse wheel event.
/// ///
/// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate. /// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate.
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct MouseWheel { pub struct MouseWheel {
@ -163,11 +169,15 @@ pub fn mouse_button_input_system(
/// This resource is reset to zero every frame. /// This resource is reset to zero every frame.
/// ///
/// This resource sums the total [`MouseMotion`] events received this frame. /// This resource sums the total [`MouseMotion`] events received this frame.
#[derive(Resource, Debug, Clone, Copy, PartialEq, Reflect, Default)] #[derive(Resource, Debug, Clone, Copy, PartialEq, Default)]
#[reflect(Debug, Default, Resource, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Default, Resource, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct AccumulatedMouseMotion { pub struct AccumulatedMouseMotion {
@ -180,11 +190,15 @@ pub struct AccumulatedMouseMotion {
/// This resource is reset to zero every frame. /// This resource is reset to zero every frame.
/// ///
/// This resource sums the total [`MouseWheel`] events received this frame. /// This resource sums the total [`MouseWheel`] events received this frame.
#[derive(Resource, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Resource, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, Default, Resource, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Default, Resource, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct AccumulatedMouseScroll { pub struct AccumulatedMouseScroll {

View File

@ -4,10 +4,11 @@ use bevy_ecs::entity::Entity;
use bevy_ecs::event::{Event, EventReader}; use bevy_ecs::event::{Event, EventReader};
use bevy_ecs::system::{ResMut, Resource}; use bevy_ecs::system::{ResMut, Resource};
use bevy_math::Vec2; use bevy_math::Vec2;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect; use bevy_reflect::Reflect;
use bevy_utils::HashMap; use bevy_utils::HashMap;
#[cfg(feature = "serialize")] #[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// A touch input event. /// A touch input event.
@ -33,11 +34,11 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
/// ///
/// This event is the translated version of the `WindowEvent::Touch` from the `winit` crate. /// 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. /// It is available to the end user and can be used for game logic.
#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Event, Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub struct TouchInput { pub struct TouchInput {
@ -57,11 +58,11 @@ pub struct TouchInput {
} }
/// A force description of a [`Touch`] input. /// A force description of a [`Touch`] input.
#[derive(Debug, Clone, Copy, PartialEq, Reflect)] #[derive(Debug, Clone, Copy, PartialEq)]
#[reflect(Debug, PartialEq)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr( #[cfg_attr(
feature = "serialize", all(feature = "serialize", feature = "bevy_reflect"),
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum ForceTouch { pub enum ForceTouch {
@ -103,11 +104,15 @@ pub enum ForceTouch {
/// This includes a phase that indicates that a touch input has started or ended, /// This includes a phase that indicates that a touch input has started or ended,
/// or that a finger has moved. There is also a canceled phase that indicates that /// or that a finger has moved. There is also a canceled phase that indicates that
/// the system canceled the tracking of the finger. /// the system canceled the tracking of the finger.
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect)] #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
#[reflect(Debug, Hash, PartialEq)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "bevy_reflect",
derive(serde::Serialize, serde::Deserialize), derive(Reflect),
reflect(Debug, Hash, PartialEq)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize) reflect(Serialize, Deserialize)
)] )]
pub enum TouchPhase { pub enum TouchPhase {