diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 1a2734df0cc60..7756fc47af89b 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -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`], and [`Axis`] resources won't be updated correctly. /// /// An example for gamepad input mocking can be seen in the documentation of the [`GamepadEventRaw`]. -#[derive(Debug, Clone, PartialEq)] +#[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); diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index de0bdbbe3316a..0b6914cec440b 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -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`](crate::Input) 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, diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index b06a5c2ae355a..f77e89e097f47 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -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`](crate::Input) 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, diff --git a/crates/bevy_internal/Cargo.toml b/crates/bevy_internal/Cargo.toml index d4be55fc72878..f38c60c203f9a 100644 --- a/crates/bevy_internal/Cargo.toml +++ b/crates/bevy_internal/Cargo.toml @@ -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"] diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 462791d32bc95..0c021ba8c4c35 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -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::(); + type_registry.register::(); let reflect_serialize = type_registry - .get_type_data::(std::any::TypeId::of::()) + .get_type_data::(std::any::TypeId::of::()) .unwrap(); - let _serializable = reflect_serialize.get_serializable(&std::time::Duration::ZERO); + let _serializable = reflect_serialize.get_serializable(&Duration::ZERO); } #[test] diff --git a/crates/bevy_window/Cargo.toml b/crates/bevy_window/Cargo.toml index c974af23651ac..f1402447072de 100644 --- a/crates/bevy_window/Cargo.toml +++ b/crates/bevy_window/Cargo.toml @@ -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" diff --git a/crates/bevy_window/src/cursor.rs b/crates/bevy_window/src/cursor.rs index 56de60cdf01ad..de1fa563ba96b 100644 --- a/crates/bevy_window/src/cursor.rs +++ b/crates/bevy_window/src/cursor.rs @@ -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, diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index 0ac0d01c2d37d..a432e559c28cf 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -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, diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index 11e8bad32cc9f..127eb4ca5a2ca 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -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. ///