Skip to content

Commit

Permalink
Touchpad magnify and rotate events (#8791)
Browse files Browse the repository at this point in the history
# Objective

The goal of this PR is to receive touchpad magnification and rotation
events.

## Solution

Implement pendants for winit's `TouchpadMagnify` and `TouchpadRotate`
events.

Adjust the `mouse_input_events.rs` example to debug magnify and rotate
events.

Since winit only reports these events on macOS, the Bevy events for
touchpad magnification and rotation are currently only fired on macOS.
  • Loading branch information
jim-ec authored Jun 8, 2023
1 parent d6d25d8 commit 0080303
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
8 changes: 8 additions & 0 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod input;
pub mod keyboard;
pub mod mouse;
pub mod touch;
pub mod touchpad;

pub use axis::*;
pub use input::*;
Expand All @@ -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,
Expand Down Expand Up @@ -67,6 +69,8 @@ impl Plugin for InputPlugin {
.add_event::<MouseWheel>()
.init_resource::<Input<MouseButton>>()
.add_systems(PreUpdate, mouse_button_input_system.in_set(InputSystem))
.add_event::<TouchpadMagnify>()
.add_event::<TouchpadRotate>()
// gamepad
.add_event::<GamepadConnectionEvent>()
.add_event::<GamepadButtonChangedEvent>()
Expand Down Expand Up @@ -112,6 +116,10 @@ impl Plugin for InputPlugin {
.register_type::<MouseScrollUnit>()
.register_type::<MouseWheel>();

// Register touchpad types
app.register_type::<TouchpadMagnify>()
.register_type::<TouchpadRotate>();

// Register touch types
app.register_type::<TouchInput>()
.register_type::<ForceTouch>()
Expand Down
39 changes: 39 additions & 0 deletions crates/bevy_input/src/touchpad.rs
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 13 additions & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion examples/input/mouse_input_events.rs
Original file line number Diff line number Diff line change
@@ -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::*,
};

Expand All @@ -18,6 +21,8 @@ fn print_mouse_events_system(
mut mouse_motion_events: EventReader<MouseMotion>,
mut cursor_moved_events: EventReader<CursorMoved>,
mut mouse_wheel_events: EventReader<MouseWheel>,
mut touchpad_magnify_events: EventReader<TouchpadMagnify>,
mut touchpad_rotate_events: EventReader<TouchpadRotate>,
) {
for event in mouse_button_input_events.iter() {
info!("{:?}", event);
Expand All @@ -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);
}
}

0 comments on commit 0080303

Please sign in to comment.