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

Trackpad-specific input event support #482

Merged
merged 7 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/gui/egui_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ impl GUI {
None
}
}
Event::PinchGesture { delta, handled, .. } => {
if !handled {
Some(egui::Event::Zoom(delta.exp()))
} else {
None
}
}
_ => None,
})
.collect::<Vec<_>>(),
Expand Down Expand Up @@ -223,6 +230,16 @@ impl GUI {
} => {
*handled = true;
}
Event::PinchGesture {
ref mut handled, ..
} => {
*handled = true;
}
Event::RotationGesture {
ref mut handled, ..
} => {
*handled = true;
}
_ => {}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/renderer/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub use fly_control::*;

pub use three_d_asset::PixelPoint as PhysicalPoint;

use three_d_asset::Radians;

/// Type of mouse button.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum MouseButton {
Expand Down Expand Up @@ -83,6 +85,30 @@ pub enum Event {
modifiers: Modifiers,
/// Whether or not this event already have been handled.
handled: bool,
/// Whether or not this event came from a gesture-capable input
gesture_capable: bool,
},
/// Fired continuously when a pinch input gesture is recognized, such as on a Mac trackpad
PinchGesture {
/// The relative pinching since the last [Event::PinchGesture] event (positive is zoom in).
delta: f32,
/// The screen position in physical pixels.
position: PhysicalPoint,
/// The state of modifiers.
modifiers: Modifiers,
/// Whether or not this event already have been handled.
handled: bool,
},
/// Fired continuously when a rotation input gesture is recognized, such as on a Mac trackpad
RotationGesture {
/// The relative rotation since the last [Event::RotationGesture] event (positive is zoom in).
delta: Radians,
/// The screen position in physical pixels.
position: PhysicalPoint,
/// The state of modifiers.
modifiers: Modifiers,
/// Whether or not this event already have been handled.
handled: bool,
},
/// Fired when the mouse enters the window.
MouseEnter,
Expand Down
33 changes: 30 additions & 3 deletions src/renderer/control/camera_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ pub struct CameraControl {
pub scroll_horizontal: CameraAction,
/// Specifies what happens when scrolling vertically.
pub scroll_vertical: CameraAction,
/// Specifies what happens when pinching on a gesture-capable device
pub pinch: CameraAction,
/// Specifies what happens when rotating on a gesture-capable device
pub rotate: CameraAction,
}

impl CameraControl {
Expand Down Expand Up @@ -144,10 +148,33 @@ impl CameraControl {
}
}
}
Event::MouseWheel { delta, handled, .. } => {
Event::MouseWheel {
delta,
modifiers,
gesture_capable,
handled,
..
} => {
if !*handled {
if !*gesture_capable && modifiers.ctrl {
// Remap vertical scrolling to the pinch event if the ctrl key is held (automatically triggered by some trackpads)
*handled = self.handle_action(camera, self.pinch, delta.1);
} else {
*handled = self.handle_action(camera, self.scroll_horizontal, delta.0);
*handled |= self.handle_action(camera, self.scroll_vertical, delta.1);
}
change |= *handled;
}
}
Event::PinchGesture { delta, handled, .. } => {
if !*handled {
*handled = self.handle_action(camera, self.pinch, *delta * 100.0);
change |= *handled;
}
}
Event::RotationGesture { delta, handled, .. } => {
if !*handled {
*handled = self.handle_action(camera, self.scroll_horizontal, delta.0);
*handled |= self.handle_action(camera, self.scroll_vertical, delta.1);
*handled = self.handle_action(camera, self.rotate, delta.0);
change |= *handled;
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/control/free_orbit_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl FreeOrbitControl {
speed: 0.1,
target,
},
pinch: CameraAction::Zoom {
min: min_distance,
max: max_distance,
speed: 0.1,
target,
},
..Default::default()
},
}
Expand All @@ -41,6 +47,10 @@ impl FreeOrbitControl {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
if let CameraAction::Zoom { target, speed, .. } = &mut self.control.pinch {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
self.control.handle_events(camera, events)
}
}
10 changes: 10 additions & 0 deletions src/renderer/control/orbit_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ impl OrbitControl {
speed: 0.1,
target,
},
pinch: CameraAction::Zoom {
min: min_distance,
max: max_distance,
speed: 0.1,
target,
},
..Default::default()
},
}
Expand All @@ -39,6 +45,10 @@ impl OrbitControl {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
if let CameraAction::Zoom { target, speed, .. } = &mut self.control.pinch {
let x = target.distance(*camera.position());
*speed = 0.01 * x + 0.001;
}
self.control.handle_events(camera, events)
}
}
42 changes: 41 additions & 1 deletion src/window/winit_window/frame_input_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use crate::control::*;
use crate::core::*;
#[cfg(target_arch = "wasm32")]
use instant::Instant;
use std::collections::HashSet;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant;
use winit::dpi::PhysicalSize;
use winit::event::DeviceId;
use winit::event::TouchPhase;
use winit::event::WindowEvent;

Expand All @@ -28,6 +30,7 @@ pub struct FrameInputGenerator {
secondary_finger_id: Option<u64>,
modifiers: Modifiers,
mouse_pressed: Option<MouseButton>,
gesture_input_devices: HashSet<DeviceId>,
thatcomputerguy0101 marked this conversation as resolved.
Show resolved Hide resolved
}

impl FrameInputGenerator {
Expand All @@ -52,6 +55,7 @@ impl FrameInputGenerator {
secondary_finger_id: None,
modifiers: Modifiers::default(),
mouse_pressed: None,
gesture_input_devices: HashSet::new(),
}
}

Expand Down Expand Up @@ -187,7 +191,9 @@ impl FrameInputGenerator {
}
}
}
WindowEvent::MouseWheel { delta, .. } => {
WindowEvent::MouseWheel {
delta, device_id, ..
} => {
if let Some(position) = self.cursor_pos {
match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => {
Expand All @@ -197,6 +203,7 @@ impl FrameInputGenerator {
position: position.into(),
modifiers: self.modifiers,
handled: false,
gesture_capable: self.gesture_input_devices.contains(device_id),
});
}
winit::event::MouseScrollDelta::PixelDelta(delta) => {
Expand All @@ -206,11 +213,42 @@ impl FrameInputGenerator {
position: position.into(),
modifiers: self.modifiers,
handled: false,
gesture_capable: self.gesture_input_devices.contains(device_id),
});
}
}
}
}
WindowEvent::TouchpadMagnify {
delta, device_id, ..
} => {
// Renamed to PinchGesture in winit 0.30.0
self.gesture_input_devices.insert(*device_id);
if let Some(position) = self.cursor_pos {
let d = *delta as f32;
self.events.push(crate::Event::PinchGesture {
delta: d,
position: position.into(),
modifiers: self.modifiers,
handled: false,
});
}
}
WindowEvent::TouchpadRotate {
delta, device_id, ..
} => {
// Renamed to RotationGesture in winit 0.30.0
self.gesture_input_devices.insert(*device_id);
if let Some(position) = self.cursor_pos {
let d = radians(*delta);
self.events.push(crate::Event::RotationGesture {
delta: d,
position: position.into(),
modifiers: self.modifiers,
handled: false,
});
}
}
WindowEvent::MouseInput { state, button, .. } => {
if let Some(position) = self.cursor_pos {
let button = match button {
Expand Down Expand Up @@ -330,6 +368,7 @@ impl FrameInputGenerator {
(position.x - p.x).abs() - (last_pos.x - p.x).abs(),
(position.y - p.y).abs() - (last_pos.y - p.y).abs(),
),
gesture_capable: false,
});
} else {
self.events.push(crate::Event::MouseMotion {
Expand All @@ -356,6 +395,7 @@ impl FrameInputGenerator {
(position.x - p.x).abs() - (last_pos.x - p.x).abs(),
(position.y - p.y).abs() - (last_pos.y - p.y).abs(),
),
gesture_capable: false,
});
}
self.secondary_cursor_pos = Some(position);
Expand Down
Loading