Touchpad magnify and rotate events (#8791)

# 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.
This commit is contained in:
Jim Eckerlein 2023-06-08 22:31:43 +02:00 committed by GitHub
parent d6d25d8c78
commit 008030357b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 1 deletions

View file

@ -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::<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>()
@ -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>()

View file

@ -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);

View file

@ -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 {

View file

@ -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<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);
@ -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);
}
}