mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 04:33:37 +00:00
Derived Copy
trait for bevy_input
events, Serialize
/Deserialize
for events in bevy_input
and bevy_windows
, PartialEq
for events in both, and Eq
where possible in both. (#6023)
# Objective Add traits to events in `bevy_input` and `bevy_windows`: `Copy`, `Serialize`/`Deserialize`, `PartialEq`, and `Eq`, as requested in https://github.com/bevyengine/bevy/issues/6022, https://github.com/bevyengine/bevy/issues/6023, https://github.com/bevyengine/bevy/issues/6024. ## Solution Added the traits to events in `bevy_input` and `bevy_windows`. Added dependency of `serde` in `Cargo.toml` of `bevy_input`. ## Migration Guide If one has been `.clone()`'ing `bevy_input` events, Clippy will now complain about that. Just remove `.clone()` to solve. ## Other Notes Some events in `bevy_input` had `f32` fields, so `Eq` trait was not derived for them. Some events in `bevy_windows` had `String` fields, so `Copy` trait was not derived for them. Co-authored-by: targrub <62773321+targrub@users.noreply.github.com>
This commit is contained in:
parent
2b80a3f279
commit
bc863cec4d
9 changed files with 69 additions and 34 deletions
|
@ -68,7 +68,7 @@ impl Gamepads {
|
|||
}
|
||||
|
||||
/// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`].
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum GamepadEventType {
|
||||
/// A [`Gamepad`] has been connected.
|
||||
|
@ -101,7 +101,7 @@ pub enum GamepadEventType {
|
|||
/// [`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`].
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct GamepadEvent {
|
||||
/// The gamepad this event corresponds to.
|
||||
|
@ -204,7 +204,7 @@ impl GamepadEvent {
|
|||
/// #
|
||||
/// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press);
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct GamepadEventRaw {
|
||||
/// The gamepad this event corresponds to.
|
||||
|
@ -703,7 +703,7 @@ pub fn gamepad_event_system(
|
|||
for event in raw_events.iter() {
|
||||
match event.event_type {
|
||||
GamepadEventType::Connected => {
|
||||
events.send(GamepadEvent::new(event.gamepad, event.event_type.clone()));
|
||||
events.send(GamepadEvent::new(event.gamepad, event.event_type));
|
||||
for button_type in &ALL_BUTTON_TYPES {
|
||||
let gamepad_button = GamepadButton::new(event.gamepad, *button_type);
|
||||
button_input.reset(gamepad_button);
|
||||
|
@ -714,7 +714,7 @@ pub fn gamepad_event_system(
|
|||
}
|
||||
}
|
||||
GamepadEventType::Disconnected => {
|
||||
events.send(GamepadEvent::new(event.gamepad, event.event_type.clone()));
|
||||
events.send(GamepadEvent::new(event.gamepad, event.event_type));
|
||||
for button_type in &ALL_BUTTON_TYPES {
|
||||
let gamepad_button = GamepadButton::new(event.gamepad, *button_type);
|
||||
button_input.reset(gamepad_button);
|
||||
|
|
|
@ -11,7 +11,8 @@ use bevy_reflect::{FromReflect, Reflect};
|
|||
///
|
||||
/// The event is consumed inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system)
|
||||
/// to update the [`Input<KeyCode>`](crate::Input<KeyCode>) resource.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct KeyboardInput {
|
||||
/// The scan code of the key.
|
||||
pub scan_code: u32,
|
||||
|
|
|
@ -10,7 +10,8 @@ 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<MouseButton>`](crate::Input<MouseButton>) resource.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct MouseButtonInput {
|
||||
/// The mouse button assigned to the event.
|
||||
pub button: MouseButton,
|
||||
|
@ -50,7 +51,8 @@ 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)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct MouseMotion {
|
||||
/// The change in the position of the pointing device since the last event was sent.
|
||||
pub delta: Vec2,
|
||||
|
@ -63,6 +65,7 @@ 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))]
|
||||
pub enum MouseScrollUnit {
|
||||
/// The line scroll unit.
|
||||
///
|
||||
|
@ -79,7 +82,8 @@ pub enum MouseScrollUnit {
|
|||
/// A mouse wheel event.
|
||||
///
|
||||
/// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct MouseWheel {
|
||||
/// The mouse scroll unit.
|
||||
pub unit: MouseScrollUnit,
|
||||
|
|
|
@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"]
|
|||
# Enable watching file system for asset hot reload
|
||||
filesystem_watcher = ["bevy_asset/filesystem_watcher"]
|
||||
|
||||
serialize = ["bevy_input/serialize"]
|
||||
serialize = ["bevy_input/serialize", "bevy_window/serialize"]
|
||||
|
||||
# Display server protocol support (X11 is enabled by default)
|
||||
wayland = ["bevy_winit/wayland"]
|
||||
|
|
|
@ -9,7 +9,8 @@ use crate::{
|
|||
|
||||
use crate::utility::{GenericTypeInfoCell, NonGenericTypeInfoCell};
|
||||
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
|
||||
use bevy_utils::{Duration, HashMap, HashSet, Instant};
|
||||
use bevy_utils::{Duration, Instant};
|
||||
use bevy_utils::{HashMap, HashSet};
|
||||
use std::{
|
||||
any::Any,
|
||||
borrow::Cow,
|
||||
|
@ -889,18 +890,19 @@ mod tests {
|
|||
Enum, FromReflect, Reflect, ReflectSerialize, TypeInfo, TypeRegistry, Typed, VariantInfo,
|
||||
VariantType,
|
||||
};
|
||||
use bevy_utils::{HashMap, Instant};
|
||||
use bevy_utils::HashMap;
|
||||
use bevy_utils::{Duration, Instant};
|
||||
use std::f32::consts::{PI, TAU};
|
||||
|
||||
#[test]
|
||||
fn can_serialize_duration() {
|
||||
let mut type_registry = TypeRegistry::default();
|
||||
type_registry.register::<std::time::Duration>();
|
||||
type_registry.register::<Duration>();
|
||||
|
||||
let reflect_serialize = type_registry
|
||||
.get_type_data::<ReflectSerialize>(std::any::TypeId::of::<std::time::Duration>())
|
||||
.get_type_data::<ReflectSerialize>(std::any::TypeId::of::<Duration>())
|
||||
.unwrap();
|
||||
let _serializable = reflect_serialize.get_serializable(&std::time::Duration::ZERO);
|
||||
let _serializable = reflect_serialize.get_serializable(&Duration::ZERO);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -8,6 +8,10 @@ repository = "https://github.com/bevyengine/bevy"
|
|||
license = "MIT OR Apache-2.0"
|
||||
keywords = ["bevy"]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
serialize = ["serde"]
|
||||
|
||||
[dependencies]
|
||||
# bevy
|
||||
bevy_app = { path = "../bevy_app", version = "0.9.0-dev" }
|
||||
|
@ -20,6 +24,7 @@ bevy_input = { path = "../bevy_input", version = "0.9.0-dev" }
|
|||
raw-window-handle = "0.4.2"
|
||||
|
||||
# other
|
||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
web-sys = "0.3"
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
/// This `enum` is simply a copy of a similar `enum` found in [`winit`](https://docs.rs/winit/latest/winit/window/enum.CursorIcon.html).
|
||||
/// `winit`, in turn, mostly copied cursor types available in the browser.
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum CursorIcon {
|
||||
/// The platform-dependent default cursor.
|
||||
Default,
|
||||
|
|
|
@ -4,7 +4,8 @@ use super::{WindowDescriptor, WindowId};
|
|||
use bevy_math::{IVec2, Vec2};
|
||||
|
||||
/// A window event that is sent whenever a window's logical size has changed.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowResized {
|
||||
pub id: WindowId,
|
||||
/// The new logical width of the window.
|
||||
|
@ -14,7 +15,8 @@ pub struct WindowResized {
|
|||
}
|
||||
|
||||
/// An event that indicates that a new window should be created.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct CreateWindow {
|
||||
pub id: WindowId,
|
||||
pub descriptor: WindowDescriptor,
|
||||
|
@ -22,14 +24,16 @@ pub struct CreateWindow {
|
|||
|
||||
/// An event that indicates the window should redraw, even if its control flow is set to `Wait` and
|
||||
/// there have been no window events.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct RequestRedraw;
|
||||
|
||||
/// An event that is sent whenever a new window is created.
|
||||
///
|
||||
/// To create a new window, send a [`CreateWindow`] event - this
|
||||
/// event will be sent in the handler for that event.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowCreated {
|
||||
pub id: WindowId,
|
||||
}
|
||||
|
@ -45,7 +49,8 @@ pub struct WindowCreated {
|
|||
/// [`WindowPlugin`]: crate::WindowPlugin
|
||||
/// [`Window`]: crate::Window
|
||||
/// [closing]: crate::Window::close
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowCloseRequested {
|
||||
pub id: WindowId,
|
||||
}
|
||||
|
@ -54,7 +59,8 @@ pub struct WindowCloseRequested {
|
|||
/// handler for [`Window::close`].
|
||||
///
|
||||
/// [`Window::close`]: crate::Window::close
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowClosed {
|
||||
pub id: WindowId,
|
||||
}
|
||||
|
@ -67,7 +73,8 @@ pub struct WindowClosed {
|
|||
///
|
||||
/// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved
|
||||
/// [`MouseMotion`]: bevy_input::mouse::MouseMotion
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct CursorMoved {
|
||||
/// The identifier of the window the cursor has moved on.
|
||||
pub id: WindowId,
|
||||
|
@ -76,45 +83,51 @@ pub struct CursorMoved {
|
|||
pub position: Vec2,
|
||||
}
|
||||
/// An event that is sent whenever the user's cursor enters a window.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct CursorEntered {
|
||||
pub id: WindowId,
|
||||
}
|
||||
/// An event that is sent whenever the user's cursor leaves a window.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct CursorLeft {
|
||||
pub id: WindowId,
|
||||
}
|
||||
|
||||
/// An event that is sent whenever a window receives a character from the OS or underlying system.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct ReceivedCharacter {
|
||||
pub id: WindowId,
|
||||
pub char: char,
|
||||
}
|
||||
|
||||
/// An event that indicates a window has received or lost focus.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct WindowFocused {
|
||||
pub id: WindowId,
|
||||
pub focused: bool,
|
||||
}
|
||||
|
||||
/// An event that indicates a window's scale factor has changed.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowScaleFactorChanged {
|
||||
pub id: WindowId,
|
||||
pub scale_factor: f64,
|
||||
}
|
||||
/// An event that indicates a window's OS-reported scale factor has changed.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowBackendScaleFactorChanged {
|
||||
pub id: WindowId,
|
||||
pub scale_factor: f64,
|
||||
}
|
||||
|
||||
/// Events related to files being dragged and dropped on a window.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum FileDragAndDrop {
|
||||
DroppedFile { id: WindowId, path_buf: PathBuf },
|
||||
|
||||
|
@ -124,7 +137,8 @@ pub enum FileDragAndDrop {
|
|||
}
|
||||
|
||||
/// An event that is sent when a window is repositioned in physical pixels.
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowMoved {
|
||||
pub id: WindowId,
|
||||
pub position: IVec2,
|
||||
|
|
|
@ -7,6 +7,7 @@ use raw_window_handle::RawWindowHandle;
|
|||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Reflect, FromReflect)]
|
||||
#[reflect_value(PartialEq, Hash)]
|
||||
/// A unique ID for a [`Window`].
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowId(Uuid);
|
||||
|
||||
/// Presentation mode for a window.
|
||||
|
@ -24,6 +25,7 @@ pub struct WindowId(Uuid);
|
|||
/// or updated on a [`Window`](Window::set_present_mode).
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
#[doc(alias = "vsync")]
|
||||
pub enum PresentMode {
|
||||
/// Chooses FifoRelaxed -> Fifo based on availability.
|
||||
|
@ -95,7 +97,8 @@ impl Default for WindowId {
|
|||
/// Please note that if the window is resizable, then when the window is
|
||||
/// maximized it may have a size outside of these limits. The functionality
|
||||
/// required to disable maximizing is not yet exposed by winit.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowResizeConstraints {
|
||||
pub min_width: f32,
|
||||
pub min_height: f32,
|
||||
|
@ -215,6 +218,7 @@ pub struct Window {
|
|||
/// Bevy apps don't interact with this `enum` directly. Instead, they should use the methods on [`Window`].
|
||||
/// This `enum` is meant for authors of windowing plugins. See the documentation on [`crate::WindowPlugin`] for more information.
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum WindowCommand {
|
||||
/// Set the window's [`WindowMode`].
|
||||
SetWindowMode {
|
||||
|
@ -288,6 +292,7 @@ pub enum WindowCommand {
|
|||
|
||||
/// Defines the way a window is displayed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum WindowMode {
|
||||
/// Creates a window that uses the given size.
|
||||
Windowed,
|
||||
|
@ -745,7 +750,8 @@ impl Window {
|
|||
}
|
||||
|
||||
/// Defines where window should be placed at on creation.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum WindowPosition {
|
||||
/// The position will be set by the window manager.
|
||||
Automatic,
|
||||
|
@ -762,7 +768,8 @@ pub enum WindowPosition {
|
|||
}
|
||||
|
||||
/// Defines which monitor to use.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum MonitorSelection {
|
||||
/// Uses current monitor of the window.
|
||||
///
|
||||
|
@ -782,7 +789,8 @@ pub enum MonitorSelection {
|
|||
/// See [`examples/window/window_settings.rs`] for usage.
|
||||
///
|
||||
/// [`examples/window/window_settings.rs`]: https://github.com/bevyengine/bevy/blob/latest/examples/window/window_settings.rs
|
||||
#[derive(Resource, Debug, Clone)]
|
||||
#[derive(Resource, Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct WindowDescriptor {
|
||||
/// The requested logical width of the window's client area.
|
||||
///
|
||||
|
|
Loading…
Reference in a new issue