Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - 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

Closed
wants to merge 10 commits into from
Closed
10 changes: 5 additions & 5 deletions crates/bevy_input/src/gamepad.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_input/src/keyboard.rs
Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions crates/bevy_input/src/mouse.rs
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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.
///
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_window/Cargo.toml
Expand Up @@ -20,6 +20,7 @@ bevy_input = { path = "../bevy_input", version = "0.9.0-dev" }
raw-window-handle = "0.4.2"

# other
serde = { version = "1.0", features = ["derive"] }
targrub marked this conversation as resolved.
Show resolved Hide resolved

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = "0.3"
14 changes: 14 additions & 0 deletions crates/bevy_window/src/event.rs
Expand Up @@ -5,6 +5,7 @@ use bevy_math::{IVec2, Vec2};

/// A window event that is sent whenever a window's logical size has changed.
#[derive(Debug, Clone)]
targrub marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowResized {
pub id: WindowId,
/// The new logical width of the window.
Expand All @@ -15,6 +16,7 @@ pub struct WindowResized {

/// An event that indicates that a new window should be created.
#[derive(Debug, Clone)]
targrub marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateWindow {
pub id: WindowId,
pub descriptor: WindowDescriptor,
Expand All @@ -23,13 +25,15 @@ 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)]
#[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)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowCreated {
pub id: WindowId,
}
Expand All @@ -46,6 +50,7 @@ pub struct WindowCreated {
/// [`Window`]: crate::Window
/// [closing]: crate::Window::close
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowCloseRequested {
pub id: WindowId,
}
Expand All @@ -55,6 +60,7 @@ pub struct WindowCloseRequested {
///
/// [`Window::close`]: crate::Window::close
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowClosed {
pub id: WindowId,
}
Expand All @@ -68,6 +74,7 @@ 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)]
#[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,
Expand All @@ -77,17 +84,20 @@ pub struct CursorMoved {
}
/// An event that is sent whenever the user's cursor enters a window.
#[derive(Debug, Clone)]
#[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)]
#[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)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct ReceivedCharacter {
pub id: WindowId,
pub char: char,
Expand All @@ -102,19 +112,22 @@ pub struct WindowFocused {

/// An event that indicates a window's scale factor has changed.
#[derive(Debug, Clone)]
#[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)]
#[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)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum FileDragAndDrop {
DroppedFile { id: WindowId, path_buf: PathBuf },

Expand All @@ -125,6 +138,7 @@ pub enum FileDragAndDrop {

/// An event that is sent when a window is repositioned in physical pixels.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct WindowMoved {
pub id: WindowId,
pub position: IVec2,
Expand Down