From be55a06be0f3fa839d8eda95dfc135f68ca0fbb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jake=20Van=20Alstyne=20=F0=9F=8E=A9?= Date: Sat, 31 Oct 2020 01:24:27 -0600 Subject: [PATCH] Prefer using DeviceEvent over WindowEvent for mouse input handling --- .../tutorial12-camera/src/main.rs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/code/intermediate/tutorial12-camera/src/main.rs b/code/intermediate/tutorial12-camera/src/main.rs index 51f70dc5e..c65ed7899 100644 --- a/code/intermediate/tutorial12-camera/src/main.rs +++ b/code/intermediate/tutorial12-camera/src/main.rs @@ -3,7 +3,6 @@ use std::iter; use cgmath::prelude::*; use wgpu::util::DeviceExt; use winit::{ - dpi::PhysicalPosition, event::*, event_loop::{ControlFlow, EventLoop}, window::Window, @@ -103,7 +102,6 @@ struct State { #[allow(dead_code)] debug_material: model::Material, // NEW! - last_mouse_pos: PhysicalPosition, mouse_pressed: bool, } @@ -474,7 +472,6 @@ impl State { #[allow(dead_code)] debug_material, // NEW! - last_mouse_pos: (0.0, 0.0).into(), mouse_pressed: false, } } @@ -514,15 +511,6 @@ impl State { self.mouse_pressed = *state == ElementState::Pressed; true } - WindowEvent::CursorMoved { position, .. } => { - let mouse_dx = position.x - self.last_mouse_pos.x; - let mouse_dy = position.y - self.last_mouse_pos.y; - self.last_mouse_pos = *position; - if self.mouse_pressed { - self.camera_controller.process_mouse(mouse_dx, mouse_dy); - } - true - } _ => false, } } @@ -619,6 +607,15 @@ fn main() { *control_flow = ControlFlow::Poll; match event { Event::MainEventsCleared => window.request_redraw(), + Event::DeviceEvent { event, .. } => match event { + DeviceEvent::MouseMotion { delta } => { + let (x, y) = delta; + if state.mouse_pressed { + state.camera_controller.process_mouse(x, y); + } + } + _ => {} + }, Event::WindowEvent { ref event, window_id,