Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer using DeviceEvent over WindowEvent for mouse input handling #117

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions code/intermediate/tutorial12-camera/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -103,7 +102,6 @@ struct State {
#[allow(dead_code)]
debug_material: model::Material,
// NEW!
last_mouse_pos: PhysicalPosition<f64>,
mouse_pressed: bool,
}

Expand Down Expand Up @@ -474,7 +472,6 @@ impl State {
#[allow(dead_code)]
debug_material,
// NEW!
last_mouse_pos: (0.0, 0.0).into(),
mouse_pressed: false,
}
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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,
Expand Down