diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index 94ba5c771fd85..9abd064cb999d 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -8,6 +8,7 @@ mod input; pub mod keyboard; pub mod mouse; pub mod touch; +pub mod touchpad; pub use axis::*; pub use input::*; @@ -34,6 +35,7 @@ use mouse::{ MouseWheel, }; use touch::{touch_screen_input_system, ForceTouch, TouchInput, TouchPhase, Touches}; +use touchpad::{TouchpadMagnify, TouchpadRotate}; use gamepad::{ gamepad_axis_event_system, gamepad_button_event_system, gamepad_connection_system, @@ -67,6 +69,8 @@ impl Plugin for InputPlugin { .add_event::() .init_resource::>() .add_systems(PreUpdate, mouse_button_input_system.in_set(InputSystem)) + .add_event::() + .add_event::() // gamepad .add_event::() .add_event::() @@ -112,6 +116,10 @@ impl Plugin for InputPlugin { .register_type::() .register_type::(); + // Register touchpad types + app.register_type::() + .register_type::(); + // Register touch types app.register_type::() .register_type::() diff --git a/crates/bevy_input/src/touchpad.rs b/crates/bevy_input/src/touchpad.rs new file mode 100644 index 0000000000000..bc70267716028 --- /dev/null +++ b/crates/bevy_input/src/touchpad.rs @@ -0,0 +1,39 @@ +use bevy_ecs::event::Event; +use bevy_reflect::{FromReflect, Reflect, ReflectFromReflect}; + +#[cfg(feature = "serialize")] +use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; + +/// Touchpad magnification event with two-finger pinch gesture. +/// +/// Positive delta values indicate magnification (zooming in) and +/// negative delta values indicate shrinking (zooming out). +/// +/// ## Platform-specific +/// +/// - Only available on **`macOS`**. +#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq, FromReflect)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] +pub struct TouchpadMagnify(pub f32); + +/// Touchpad rotation event with two-finger rotation gesture. +/// +/// Positive delta values indicate rotation counterclockwise and +/// negative delta values indicate rotation clockwise. +/// +/// ## Platform-specific +/// +/// - Only available on **`macOS`**. +#[derive(Event, Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] +#[reflect(Debug, PartialEq, FromReflect)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] +pub struct TouchpadRotate(pub f32); diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 8957feac65c61..352e5b18b7fea 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -31,6 +31,7 @@ use bevy_input::{ keyboard::KeyboardInput, mouse::{MouseButtonInput, MouseMotion, MouseScrollUnit, MouseWheel}, touch::TouchInput, + touchpad::{TouchpadMagnify, TouchpadRotate}, }; use bevy_math::{ivec2, DVec2, Vec2}; use bevy_utils::{ @@ -236,6 +237,8 @@ struct InputEvents<'w> { keyboard_input: EventWriter<'w, KeyboardInput>, character_input: EventWriter<'w, ReceivedCharacter>, mouse_button_input: EventWriter<'w, MouseButtonInput>, + touchpad_magnify_input: EventWriter<'w, TouchpadMagnify>, + touchpad_rotate_input: EventWriter<'w, TouchpadRotate>, mouse_wheel_input: EventWriter<'w, MouseWheel>, touch_input: EventWriter<'w, TouchInput>, ime_input: EventWriter<'w, Ime>, @@ -481,6 +484,16 @@ pub fn winit_runner(mut app: App) { state: converters::convert_element_state(state), }); } + WindowEvent::TouchpadMagnify { delta, .. } => { + input_events + .touchpad_magnify_input + .send(TouchpadMagnify(delta as f32)); + } + WindowEvent::TouchpadRotate { delta, .. } => { + input_events + .touchpad_rotate_input + .send(TouchpadRotate(delta)); + } WindowEvent::MouseWheel { delta, .. } => match delta { event::MouseScrollDelta::LineDelta(x, y) => { input_events.mouse_wheel_input.send(MouseWheel { diff --git a/examples/input/mouse_input_events.rs b/examples/input/mouse_input_events.rs index c15a60b0e1486..61ba2a3d13703 100644 --- a/examples/input/mouse_input_events.rs +++ b/examples/input/mouse_input_events.rs @@ -1,7 +1,10 @@ //! Prints all mouse events to the console. use bevy::{ - input::mouse::{MouseButtonInput, MouseMotion, MouseWheel}, + input::{ + mouse::{MouseButtonInput, MouseMotion, MouseWheel}, + touchpad::{TouchpadMagnify, TouchpadRotate}, + }, prelude::*, }; @@ -18,6 +21,8 @@ fn print_mouse_events_system( mut mouse_motion_events: EventReader, mut cursor_moved_events: EventReader, mut mouse_wheel_events: EventReader, + mut touchpad_magnify_events: EventReader, + mut touchpad_rotate_events: EventReader, ) { for event in mouse_button_input_events.iter() { info!("{:?}", event); @@ -34,4 +39,14 @@ fn print_mouse_events_system( for event in mouse_wheel_events.iter() { info!("{:?}", event); } + + // This event will only fire on macOS + for event in touchpad_magnify_events.iter() { + info!("{:?}", event); + } + + // This event will only fire on macOS + for event in touchpad_rotate_events.iter() { + info!("{:?}", event); + } }