pinch zoom that (quite) works

This commit is contained in:
Arkitu 2024-09-17 19:32:50 +02:00
parent 8b54c3ce87
commit f7d1c4d96e

View File

@ -55,6 +55,9 @@ impl State {
s.render(PhysicalSize::new(1, 1));
s
}
fn set_zoom(&mut self, v: f32) {
self.zoom = v.clamp(1., 10.);
}
fn update_zooms(&mut self, screen_size: PhysicalSize<u32>) {
self.uniforms.zooms = [
(screen_size.height as f32 / screen_size.width as f32).max(1.) * self.zoom,
@ -74,8 +77,25 @@ impl State {
debug!(target:"app", "{:?}", w_size);
let old_touch_n = self.touches.iter().position(|t| t.id == touch.id).unwrap();
let old_touch = self.touches[old_touch_n];
self.uniforms.camera[0] -= (touch.location.x - old_touch.location.x) as f32/w_size.width as f32 * Map::WIDTH / self.uniforms.zooms[0];
self.uniforms.camera[1] += (touch.location.y - old_touch.location.y) as f32/w_size.height as f32 * Map::HEIGHT / self.uniforms.zooms[1];
let t = [
touch.location.x as f32/w_size.width as f32 * Map::WIDTH / self.uniforms.zooms[0],
touch.location.y as f32/w_size.height as f32 * Map::HEIGHT / self.uniforms.zooms[1]
];
let old_t = [
old_touch.location.x as f32/w_size.width as f32 * Map::WIDTH / self.uniforms.zooms[0],
old_touch.location.y as f32/w_size.height as f32 * Map::HEIGHT / self.uniforms.zooms[1]
];
// Handle pinch zoom
if self.touches.len() == 2 {
let old_touch2 = self.touches[if old_touch_n == 0 {1} else {0}];
let old_t2 = [
old_touch2.location.x as f32/w_size.width as f32 * Map::WIDTH / self.uniforms.zooms[0],
old_touch2.location.y as f32/w_size.height as f32 * Map::HEIGHT / self.uniforms.zooms[1]
];
self.set_zoom(self.zoom + (((((t[0]-old_t2[0])*self.zoom).powi(2)+((t[0]-old_t2[0])*self.zoom).powi(2)).sqrt() - (((old_t[0]-old_t2[0])*self.zoom).powi(2)+((old_t[0]-old_t2[0])*self.zoom).powi(2)).sqrt())*4.));
}
self.uniforms.camera[0] -= (t[0] - old_t[0]) / self.touches.len() as f32;
self.uniforms.camera[1] += (t[1] - old_t[1]) / self.touches.len() as f32;
debug!(target:"app", "{:?}", self.uniforms.camera);
self.touches[old_touch_n] = *touch;
},
@ -143,12 +163,10 @@ impl State {
self.uniforms.camera[0] += 0.1 / self.zoom;
},
KeyCode::KeyR => {
self.zoom += 0.1;
self.zoom = self.zoom.clamp(1., 10.);
self.set_zoom(self.zoom + 0.1);
},
KeyCode::KeyF => {
self.zoom -= 0.1;
self.zoom = self.zoom.clamp(1., 10.);
self.set_zoom(self.zoom - 0.1);
},
_ => {}
}