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 all 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
24 changes: 24 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 @@ -84,6 +86,28 @@ pub enum Event {
/// Whether or not this event already have been handled.
handled: 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 counterclockwise).
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,
/// Fired when the mouse leaves the window.
Expand Down
18 changes: 18 additions & 0 deletions src/renderer/control/camera_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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 @@ -216,6 +220,20 @@ impl CameraControl {
change |= *handled;
}
}
Event::PinchGesture { delta, handled, .. } => {
if !*handled {
self.pinch.apply(camera, *delta);
*handled = self.pinch.is_some();
change |= *handled;
}
}
Event::RotationGesture { delta, handled, .. } => {
if !*handled {
self.rotate.apply(camera, delta.0);
*handled = self.rotate.is_some();
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 = x + 0.1;
}
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 = x + 0.1;
}
self.control.handle_events(camera, events)
}
}
24 changes: 24 additions & 0 deletions src/window/winit_window/frame_input_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,30 @@ impl FrameInputGenerator {
}
}
}
WindowEvent::TouchpadMagnify { delta, .. } => {
// Renamed to PinchGesture in winit 0.30.0
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, .. } => {
// Renamed to RotationGesture in winit 0.30.0
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
Loading