From 021876b280681d970dddce1958018eb3c42112d8 Mon Sep 17 00:00:00 2001 From: Perrelli9338 Date: Sun, 29 Sep 2024 14:02:42 +0200 Subject: [PATCH] Removed dummy third finger and use another approach --- src/system/camera.rs | 59 +++++++++----------------------------------- 1 file changed, 11 insertions(+), 48 deletions(-) diff --git a/src/system/camera.rs b/src/system/camera.rs index cb08e97..f92a4b6 100644 --- a/src/system/camera.rs +++ b/src/system/camera.rs @@ -1,5 +1,5 @@ -use bevy::input::touch::TouchPhase; use crate::AppState; +use bevy::input::touch::TouchPhase; use bevy::prelude::*; use bevy::window::PrimaryWindow; @@ -9,7 +9,7 @@ impl Plugin for CameraHandling { fn build(&self, app: &mut App) { app.add_systems( Update, - (handle_mouse, handle_touch.after(dummy_third_finger)).run_if(in_state(AppState::Playing)), + (handle_mouse, handle_touch).run_if(in_state(AppState::Playing)), ); } } @@ -36,53 +36,16 @@ pub fn handle_mouse( } } -pub fn dummy_third_finger( - mut touches: Res, - mut touch_events: EventWriter, - window_primary_query: Query>, -) { - let Ok(window) = window_primary_query.get_single() else { - return; - }; - if touches.iter().count() == 2 { - let mut fingers = touches - .iter() - .map(|touch| touch.position()) - .collect::>(); - touch_events.send(TouchInput { - phase: TouchPhase::Moved, - position: Vec2::new( - (fingers[0].x + fingers[1].x) / 2.0, - (fingers[0].y + fingers[1].y) / 2.0, - ), - window: window, - force: None, - id: 2, - }); - } -} +pub fn handle_touch(mut camera: Query<&mut Transform, With>, touch_input: Res) { + if touch_input.iter().count() == 2 { + let fingers: Vec<_> = touch_input.iter().collect(); + let (x1, y1) = (fingers[0].position().x, fingers[0].position().y); + let (x2, y2) = (fingers[1].position().x, fingers[1].position().y); + let delta = Vec2::new((x1 + x2) / 2.0, (y1 + y2) / 2.0); -pub fn handle_touch( - mut camera: Query<&mut Transform, With>, - mut touches: Res, - mut touch_events: EventReader, -) { - if touches.iter().count() == 2 { - for event in touch_events.read() { - let mut fingers = touches - .iter() - .map(|touch| touch.position()) - .collect::>(); - if event.id == 2 { - let delta = event.position - Vec2::new( - (fingers[0].x + fingers[1].x) / 2.0, - (fingers[0].y + fingers[1].y) / 2.0, - ); - for mut transform in camera.iter_mut() { - transform.translation.x += delta.x; - transform.translation.y -= delta.y; - } - } + for mut transform in camera.iter_mut() { + transform.translation.x += delta.x; + transform.translation.y -= delta.y; } } }