Bevy Version: | 0.14 | (current) |
---|
Gestures
Multi-finger gestures on a Touchpad or Touchscreen are a very common way to implement various operations, like panning, zooming, and rotating.
Platform Gesture Events
Bevy offers events that allow you to handle gestures as they are detected / implemented by the OS.
Currently, only macOS and iOS are supported. Other platforms may be supported in the future.
The supported gestures are:
RotationGesture
: rotating with two fingersPinchGesture
: pinch-to-zoom with two fingersPanGesture
: panning gestureDoubleTapGesture
: double-tap gesture
use bevy::input::gestures::{
DoubleTapGesture, PanGesture, PinchGesture, RotationGesture
};
// these only work on macOS and iOS
fn builtin_gestures(
mut evr_gesture_pinch: EventReader<PinchGesture>,
mut evr_gesture_rotate: EventReader<RotationGesture>,
mut evr_gesture_pan: EventReader<PanGesture>,
mut evr_gesture_doubletap: EventReader<PanGesture>,
) {
for ev_pinch in evr_gesture_pinch.read() {
// Positive numbers are zooming in
// Negative numbers are zooming out
println!("Two-finger zoom by {}", ev_pinch.0);
}
for ev_rotate in evr_gesture_rotate.read() {
// Positive numbers are anticlockwise
// Negative numbers are clockwise
println!("Two-finger rotate by {}", ev_rotate.0);
}
for ev_pan in evr_gesture_pan.read() {
// Each event is a Vec2 giving you the X/Y pan amount
println!("Two-finger pan by X: {}, Y: {}", ev_pan.0.x, ev_pan.0.y);
}
for ev_doubletap in evr_gesture_doubletap.read() {
// This one has no data
println!("Double-Tap gesture!");
}
}
Custom Touchpad Gestures
It is not currently possible to implement your own gestures on a touchpad, because there is no API to detect the individual fingers that are touching the touchpad.
Custom Touchscreen Gestures
You can (and probably should) implement your own touchscreen gestures. Bevy offers multi-touch detection, tracking each finger that is currently on the screen. Implementing your own gestures is be a good way to make touchscreen input behave appropriately to your application.