Derive Reflect
+ FromReflect
for input types (#6232)
# Objective Adds support for reflecting many more of the input types. This allows those types to be used via scripting, `bevy-inspector-egui`, etc. These types are registered by the `InputPlugin` so that they're automatically available to anyone who wants to use them Closes #6223 ## Solution Many types now have `#[derive(Reflect, FromReflect)]` added to them in `bevy_input`. Additionally, `#[reflect(traits...)]` has been added for applicable traits to the types. This PR does not add reflection support for types which have private fields. Notably, `Touch` and `Touches` don't implement `Reflect`/`FromReflect`. This adds the "glam" feature to the `bevy_reflect` dependency for package `bevy_input`. Since `bevy_input` transitively depends on `glam` already, all this brings in are the reflection `impl`s. ## Migration Guide - `Input<T>` now implements `Reflect` via `#[reflect]` instead of `#[reflect_value]`. This means it now exposes its private fields via the `Reflect` trait rather than being treated as a value type. For code that relies on the `Input<T>` struct being treated as a value type by reflection, it is still possible to wrap the `Input<T>` type with a wrapper struct and apply `#[reflect_value]` to it. - As a reminder, private fields exposed via reflection are not subject to any stability guarantees. --- ## Changelog Added - Implemented `Reflect` + `FromReflect` for many input-related types. These types are automatically registered when adding the `InputPlugin`.
This commit is contained in:
parent
c18b1a839b
commit
2023ce63c7
@ -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_ecs = { path = "../bevy_ecs", version = "0.9.0-dev" }
|
||||||
bevy_math = { path = "../bevy_math", 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_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
|
# other
|
||||||
serde = { version = "1", features = ["derive"], optional = true }
|
serde = { version = "1", features = ["derive"], optional = true }
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use crate::{Axis, Input};
|
use crate::{Axis, Input};
|
||||||
use bevy_ecs::event::{EventReader, EventWriter};
|
use bevy_ecs::event::{EventReader, EventWriter};
|
||||||
use bevy_ecs::system::{Res, ResMut, Resource};
|
use bevy_ecs::system::{Res, ResMut, Resource};
|
||||||
|
use bevy_reflect::{std_traits::ReflectDefault, FromReflect, Reflect};
|
||||||
use bevy_utils::{tracing::info, HashMap};
|
use bevy_utils::{tracing::info, HashMap};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
@ -53,6 +54,9 @@ pub enum ButtonSettingsError {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "serialize")]
|
||||||
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
||||||
|
|
||||||
/// A gamepad with an associated `ID`.
|
/// A gamepad with an associated `ID`.
|
||||||
///
|
///
|
||||||
/// ## Usage
|
/// ## Usage
|
||||||
@ -64,8 +68,13 @@ pub enum ButtonSettingsError {
|
|||||||
/// ## 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)]
|
#[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",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct Gamepad {
|
pub struct Gamepad {
|
||||||
/// The `ID` of the gamepad.
|
/// The `ID` of the gamepad.
|
||||||
pub id: usize,
|
pub id: usize,
|
||||||
@ -79,8 +88,13 @@ impl Gamepad {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Metadata associated with a `Gamepad`.
|
/// Metadata associated with a `Gamepad`.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct GamepadInfo {
|
pub struct GamepadInfo {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
@ -129,8 +143,13 @@ impl Gamepads {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`].
|
/// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`].
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum GamepadEventType {
|
pub enum GamepadEventType {
|
||||||
/// A [`Gamepad`] has been connected.
|
/// A [`Gamepad`] has been connected.
|
||||||
Connected(GamepadInfo),
|
Connected(GamepadInfo),
|
||||||
@ -162,8 +181,13 @@ pub enum GamepadEventType {
|
|||||||
/// [`Axis<GamepadAxis>`], and [`Axis<GamepadButton>`] resources won't be updated correctly.
|
/// [`Axis<GamepadAxis>`], and [`Axis<GamepadButton>`] resources won't be updated correctly.
|
||||||
///
|
///
|
||||||
/// An example for gamepad input mocking can be seen in the documentation of the [`GamepadEventRaw`].
|
/// An example for gamepad input mocking can be seen in the documentation of the [`GamepadEventRaw`].
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct GamepadEvent {
|
pub struct GamepadEvent {
|
||||||
/// The gamepad this event corresponds to.
|
/// The gamepad this event corresponds to.
|
||||||
pub gamepad: Gamepad,
|
pub gamepad: Gamepad,
|
||||||
@ -266,8 +290,13 @@ impl GamepadEvent {
|
|||||||
/// #
|
/// #
|
||||||
/// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press);
|
/// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press);
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct GamepadEventRaw {
|
pub struct GamepadEventRaw {
|
||||||
/// The gamepad this event corresponds to.
|
/// The gamepad this event corresponds to.
|
||||||
pub gamepad: Gamepad,
|
pub gamepad: Gamepad,
|
||||||
@ -293,8 +322,13 @@ impl GamepadEventRaw {
|
|||||||
/// [`GamepadEventType::ButtonChanged`]. It is also used in the [`GamepadButton`]
|
/// [`GamepadEventType::ButtonChanged`]. It is also used in the [`GamepadButton`]
|
||||||
/// which in turn is used to create the [`Input<GamepadButton>`] or
|
/// which in turn is used to create the [`Input<GamepadButton>`] or
|
||||||
/// [`Axis<GamepadButton>`] `bevy` resources.
|
/// [`Axis<GamepadButton>`] `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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum GamepadButtonType {
|
pub enum GamepadButtonType {
|
||||||
/// The bottom action button of the action pad (i.e. PS: Cross, Xbox: A).
|
/// The bottom action button of the action pad (i.e. PS: Cross, Xbox: A).
|
||||||
South,
|
South,
|
||||||
@ -353,8 +387,13 @@ pub enum GamepadButtonType {
|
|||||||
/// ## Updating
|
/// ## Updating
|
||||||
///
|
///
|
||||||
/// The resources are updated inside of the [`gamepad_event_system`].
|
/// 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct GamepadButton {
|
pub struct GamepadButton {
|
||||||
/// The gamepad on which the button is located on.
|
/// The gamepad on which the button is located on.
|
||||||
pub gamepad: Gamepad,
|
pub gamepad: Gamepad,
|
||||||
@ -390,8 +429,13 @@ impl GamepadButton {
|
|||||||
/// 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
|
||||||
/// [`GamepadEventType::AxisChanged`]. It is also used in the [`GamepadAxis`]
|
/// [`GamepadEventType::AxisChanged`]. 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)]
|
#[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",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum GamepadAxisType {
|
pub enum GamepadAxisType {
|
||||||
/// The horizontal value of the left stick.
|
/// The horizontal value of the left stick.
|
||||||
LeftStickX,
|
LeftStickX,
|
||||||
@ -421,8 +465,13 @@ pub enum GamepadAxisType {
|
|||||||
/// ## Updating
|
/// ## Updating
|
||||||
///
|
///
|
||||||
/// The resource is updated inside of the [`gamepad_event_system`].
|
/// 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct GamepadAxis {
|
pub struct GamepadAxis {
|
||||||
/// The gamepad on which the axis is located on.
|
/// The gamepad on which the axis is located on.
|
||||||
pub gamepad: Gamepad,
|
pub gamepad: Gamepad,
|
||||||
@ -460,7 +509,8 @@ impl GamepadAxis {
|
|||||||
///
|
///
|
||||||
/// The [`GamepadSettings`] are used inside of the [`gamepad_event_system`], but are never written to
|
/// 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.
|
/// 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 {
|
pub struct GamepadSettings {
|
||||||
/// The default button settings.
|
/// The default button settings.
|
||||||
pub default_button_settings: ButtonSettings,
|
pub default_button_settings: ButtonSettings,
|
||||||
@ -542,7 +592,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)]
|
#[derive(Debug, Clone, Reflect, FromReflect)]
|
||||||
|
#[reflect(Debug, Default)]
|
||||||
pub struct ButtonSettings {
|
pub struct ButtonSettings {
|
||||||
press_threshold: f32,
|
press_threshold: f32,
|
||||||
release_threshold: f32,
|
release_threshold: f32,
|
||||||
@ -701,7 +752,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)]
|
#[derive(Debug, Clone, Reflect, FromReflect)]
|
||||||
|
#[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,
|
||||||
@ -1015,7 +1067,8 @@ impl AxisSettings {
|
|||||||
/// ## Updating
|
/// ## Updating
|
||||||
///
|
///
|
||||||
/// The current value of a button is received through the [`GamepadEvent`]s or [`GamepadEventRaw`]s.
|
/// 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 {
|
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,
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use bevy_ecs::system::Resource;
|
use bevy_ecs::system::Resource;
|
||||||
use bevy_reflect::Reflect;
|
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
||||||
use bevy_utils::HashSet;
|
use bevy_utils::HashSet;
|
||||||
use std::hash::Hash;
|
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::release`] method for each release event.
|
||||||
/// * Call the [`Input::clear`] method at each frame start, before processing events.
|
/// * Call the [`Input::clear`] method at each frame start, before processing events.
|
||||||
#[derive(Debug, Clone, Resource, Reflect)]
|
#[derive(Debug, Clone, Resource, Reflect)]
|
||||||
#[reflect_value]
|
#[reflect(Default)]
|
||||||
pub struct Input<T: Copy + Eq + Hash + Send + Sync + 'static> {
|
pub struct Input<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>,
|
||||||
|
@ -2,6 +2,9 @@ use crate::{ButtonState, Input};
|
|||||||
use bevy_ecs::{event::EventReader, system::ResMut};
|
use bevy_ecs::{event::EventReader, system::ResMut};
|
||||||
use bevy_reflect::{FromReflect, Reflect};
|
use bevy_reflect::{FromReflect, Reflect};
|
||||||
|
|
||||||
|
#[cfg(feature = "serialize")]
|
||||||
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
||||||
|
|
||||||
/// A keyboard input event.
|
/// A keyboard input event.
|
||||||
///
|
///
|
||||||
/// This event is the translated version of the `WindowEvent::KeyboardInput` from the `winit` crate.
|
/// 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)
|
/// The event is consumed inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system)
|
||||||
/// to update the [`Input<KeyCode>`](crate::Input<KeyCode>) resource.
|
/// to update the [`Input<KeyCode>`](crate::Input<KeyCode>) 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct KeyboardInput {
|
pub struct KeyboardInput {
|
||||||
/// The scan code of the key.
|
/// The scan code of the key.
|
||||||
pub scan_code: u32,
|
pub scan_code: u32,
|
||||||
@ -62,9 +70,13 @@ pub fn keyboard_input_system(
|
|||||||
/// ## Updating
|
/// ## Updating
|
||||||
///
|
///
|
||||||
/// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system).
|
/// 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(Debug, Hash, PartialEq)]
|
||||||
#[reflect(Hash, PartialEq)]
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum KeyCode {
|
pub enum KeyCode {
|
||||||
/// The `1` key over the letters.
|
/// The `1` key over the letters.
|
||||||
@ -425,7 +437,11 @@ pub enum KeyCode {
|
|||||||
/// ## Updating
|
/// ## Updating
|
||||||
///
|
///
|
||||||
/// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system).
|
/// 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)]
|
||||||
#[reflect(Hash, PartialEq)]
|
#[reflect(Debug, Hash, PartialEq)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct ScanCode(pub u32);
|
pub struct ScanCode(pub u32);
|
||||||
|
@ -6,7 +6,6 @@ pub mod mouse;
|
|||||||
pub mod touch;
|
pub mod touch;
|
||||||
|
|
||||||
pub use axis::*;
|
pub use axis::*;
|
||||||
use bevy_ecs::schedule::{IntoSystemDescriptor, SystemLabel};
|
|
||||||
pub use input::*;
|
pub use input::*;
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
@ -24,16 +23,24 @@ pub mod prelude {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use bevy_app::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 keyboard::{keyboard_input_system, KeyCode, KeyboardInput, ScanCode};
|
||||||
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
|
use mouse::{
|
||||||
use prelude::Gamepads;
|
mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseScrollUnit,
|
||||||
use touch::{touch_screen_input_system, TouchInput, Touches};
|
MouseWheel,
|
||||||
|
};
|
||||||
|
use touch::{touch_screen_input_system, ForceTouch, TouchInput, TouchPhase, Touches};
|
||||||
|
|
||||||
use gamepad::{
|
use gamepad::{
|
||||||
gamepad_connection_system, gamepad_event_system, GamepadAxis, GamepadButton, GamepadEvent,
|
gamepad_connection_system, gamepad_event_system, AxisSettings, ButtonAxisSettings,
|
||||||
GamepadEventRaw, GamepadSettings,
|
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
|
/// Adds keyboard and mouse input to an App
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct InputPlugin;
|
pub struct InputPlugin;
|
||||||
@ -84,12 +91,51 @@ impl Plugin for InputPlugin {
|
|||||||
CoreStage::PreUpdate,
|
CoreStage::PreUpdate,
|
||||||
touch_screen_input_system.label(InputSystem),
|
touch_screen_input_system.label(InputSystem),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Register common types
|
||||||
|
app.register_type::<ButtonState>();
|
||||||
|
|
||||||
|
// Register keyboard types
|
||||||
|
app.register_type::<KeyboardInput>()
|
||||||
|
.register_type::<KeyCode>()
|
||||||
|
.register_type::<ScanCode>();
|
||||||
|
|
||||||
|
// Register mouse types
|
||||||
|
app.register_type::<MouseButtonInput>()
|
||||||
|
.register_type::<MouseButton>()
|
||||||
|
.register_type::<MouseMotion>()
|
||||||
|
.register_type::<MouseScrollUnit>()
|
||||||
|
.register_type::<MouseWheel>();
|
||||||
|
|
||||||
|
// Register touch types
|
||||||
|
app.register_type::<TouchInput>()
|
||||||
|
.register_type::<ForceTouch>()
|
||||||
|
.register_type::<TouchPhase>();
|
||||||
|
|
||||||
|
// Register gamepad types
|
||||||
|
app.register_type::<Gamepad>()
|
||||||
|
.register_type::<GamepadEventType>()
|
||||||
|
.register_type::<GamepadEvent>()
|
||||||
|
.register_type::<GamepadEventRaw>()
|
||||||
|
.register_type::<GamepadButtonType>()
|
||||||
|
.register_type::<GamepadButton>()
|
||||||
|
.register_type::<GamepadAxisType>()
|
||||||
|
.register_type::<GamepadAxis>()
|
||||||
|
.register_type::<GamepadSettings>()
|
||||||
|
.register_type::<ButtonSettings>()
|
||||||
|
.register_type::<AxisSettings>()
|
||||||
|
.register_type::<ButtonAxisSettings>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The current "press" state of an element
|
/// 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum ButtonState {
|
pub enum ButtonState {
|
||||||
Pressed,
|
Pressed,
|
||||||
Released,
|
Released,
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
use crate::{ButtonState, Input};
|
use crate::{ButtonState, Input};
|
||||||
use bevy_ecs::{event::EventReader, system::ResMut};
|
use bevy_ecs::{event::EventReader, system::ResMut};
|
||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
|
use bevy_reflect::{FromReflect, Reflect};
|
||||||
|
|
||||||
|
#[cfg(feature = "serialize")]
|
||||||
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
||||||
|
|
||||||
/// A mouse button input event.
|
/// 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)
|
/// The event is read inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system)
|
||||||
/// to update the [`Input<MouseButton>`](crate::Input<MouseButton>) resource.
|
/// to update the [`Input<MouseButton>`](crate::Input<MouseButton>) 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct MouseButtonInput {
|
pub struct MouseButtonInput {
|
||||||
/// The mouse button assigned to the event.
|
/// The mouse button assigned to the event.
|
||||||
pub button: MouseButton,
|
pub button: MouseButton,
|
||||||
@ -29,8 +38,13 @@ pub struct MouseButtonInput {
|
|||||||
/// ## Updating
|
/// ## Updating
|
||||||
///
|
///
|
||||||
/// The resource is updated inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system).
|
/// 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum MouseButton {
|
pub enum MouseButton {
|
||||||
/// The left mouse button.
|
/// The left mouse button.
|
||||||
Left,
|
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.
|
/// 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(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct MouseMotion {
|
pub struct MouseMotion {
|
||||||
/// The change in the position of the pointing device since the last event was sent.
|
/// The change in the position of the pointing device since the last event was sent.
|
||||||
pub delta: Vec2,
|
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
|
/// 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)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum MouseScrollUnit {
|
pub enum MouseScrollUnit {
|
||||||
/// The line scroll unit.
|
/// The line scroll unit.
|
||||||
///
|
///
|
||||||
@ -82,8 +106,13 @@ 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(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct MouseWheel {
|
pub struct MouseWheel {
|
||||||
/// The mouse scroll unit.
|
/// The mouse scroll unit.
|
||||||
pub unit: MouseScrollUnit,
|
pub unit: MouseScrollUnit,
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
use bevy_ecs::event::EventReader;
|
use bevy_ecs::event::EventReader;
|
||||||
use bevy_ecs::system::{ResMut, Resource};
|
use bevy_ecs::system::{ResMut, Resource};
|
||||||
use bevy_math::Vec2;
|
use bevy_math::Vec2;
|
||||||
|
use bevy_reflect::{FromReflect, Reflect};
|
||||||
use bevy_utils::HashMap;
|
use bevy_utils::HashMap;
|
||||||
|
|
||||||
|
#[cfg(feature = "serialize")]
|
||||||
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
||||||
|
|
||||||
/// A touch input event.
|
/// A touch input event.
|
||||||
///
|
///
|
||||||
/// ## Logic
|
/// ## Logic
|
||||||
@ -26,8 +30,13 @@ use bevy_utils::HashMap;
|
|||||||
///
|
///
|
||||||
/// 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(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub struct TouchInput {
|
pub struct TouchInput {
|
||||||
/// The phase of the touch input.
|
/// The phase of the touch input.
|
||||||
pub phase: TouchPhase,
|
pub phase: TouchPhase,
|
||||||
@ -43,8 +52,13 @@ pub struct TouchInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A force description of a [`Touch`](crate::touch::Touch) input.
|
/// A force description of a [`Touch`](crate::touch::Touch) input.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)]
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[reflect(Debug, PartialEq)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum ForceTouch {
|
pub enum ForceTouch {
|
||||||
/// On iOS, the force is calibrated so that the same number corresponds to
|
/// 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
|
/// 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,
|
/// 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
|
/// or that a finger has moved. There is also a cancelled phase that indicates that
|
||||||
/// the system cancelled the tracking of the finger.
|
/// 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)]
|
||||||
|
#[cfg_attr(
|
||||||
|
feature = "serialize",
|
||||||
|
derive(serde::Serialize, serde::Deserialize),
|
||||||
|
reflect(Serialize, Deserialize)
|
||||||
|
)]
|
||||||
pub enum TouchPhase {
|
pub enum TouchPhase {
|
||||||
/// A finger started to touch the touchscreen.
|
/// A finger started to touch the touchscreen.
|
||||||
Started,
|
Started,
|
||||||
|
Loading…
Reference in New Issue
Block a user