Created
January 6, 2024 08:58
-
-
Save miketwenty1/0463460175ac34f6d059f79355a1299c to your computer and use it in GitHub Desktop.
Multi-Touch Pinch Zoom Bevy System Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[allow(clippy::type_complexity, clippy::too_many_arguments)] | |
pub fn pinch_system( | |
touches: Res<Touches>, | |
mut touch_e: EventReader<TouchInput>, | |
time: Res<Time>, | |
mut cam_query: Query<&mut OrthographicProjection, With<Camera>>, | |
mut multitouch_distance: Local<f32>, | |
) { | |
let mut zoom_amount: f32 = 0.0; | |
for _e in touch_e.read() { | |
if touches.iter().count() == 2 { | |
let first = touches | |
.first_pressed_position() | |
.unwrap_or(Vec2 { x: 0.0, y: 0.0 }); | |
for touch in touches.iter() { | |
if touch.position() != first { | |
let diff2 = distance_between_vecs(&touch.position(), &first); | |
if diff2 > 110.0 { | |
if (*multitouch_distance - diff2).abs() > 2.0 { | |
if *multitouch_distance == 0.0 { | |
} else if *multitouch_distance > diff2 { | |
zoom_amount = 0.25; | |
info!("zooming out"); | |
} else { | |
zoom_amount = -0.25; | |
info!("zooming in"); | |
} | |
} | |
*multitouch_distance = diff2; | |
} | |
} | |
} | |
if zoom_amount != 0.0 { | |
let time_adjusted = if time.delta_seconds() > 0.01 { | |
0.01 | |
} else { | |
time.delta_seconds() | |
}; | |
for mut ortho in cam_query.iter_mut() { | |
ortho.scale += zoom_amount * time_adjusted * 30.0; | |
if ortho.scale > ZOOM_OUT_MAX { | |
ortho.scale = ZOOM_OUT_MAX; | |
} else if ortho.scale < ZOOM_IN_MAX { | |
ortho.scale = ZOOM_IN_MAX; | |
} | |
} | |
} | |
} else { | |
*multitouch_distance = 0.0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment