mouse movement

This commit is contained in:
Piotr 2020-09-29 21:15:05 +02:00
parent 637ffd06d6
commit 6ab1057199
2 changed files with 39 additions and 26 deletions

View File

@ -57,18 +57,18 @@ impl ExampleClient {
impl Client for ExampleClient {
fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState) {
match (code, state) {
(VirtualKeyCode::W, ElementState::Pressed) => {
self.camera.process_keyboard(CameraMovement::FORWARD, self.delta)
}
(VirtualKeyCode::S, ElementState::Pressed) => {
self.camera.process_keyboard(CameraMovement::BACKWARD, self.delta)
}
(VirtualKeyCode::A, ElementState::Pressed) => {
self.camera.process_keyboard(CameraMovement::LEFT, self.delta)
}
(VirtualKeyCode::D, ElementState::Pressed) => {
self.camera.process_keyboard(CameraMovement::RIGHT, self.delta)
}
(VirtualKeyCode::W, ElementState::Pressed) => self
.camera
.process_keyboard(CameraMovement::FORWARD, self.delta),
(VirtualKeyCode::S, ElementState::Pressed) => self
.camera
.process_keyboard(CameraMovement::BACKWARD, self.delta),
(VirtualKeyCode::A, ElementState::Pressed) => self
.camera
.process_keyboard(CameraMovement::LEFT, self.delta),
(VirtualKeyCode::D, ElementState::Pressed) => self
.camera
.process_keyboard(CameraMovement::RIGHT, self.delta),
(VirtualKeyCode::LControl, _) => self
.camera
.enable_mouse_movement(state == &ElementState::Released),

View File

@ -34,9 +34,7 @@ impl TimeStep {
pub fn delta(&mut self) -> f32 {
let current_time = Instant::now();
let delta = current_time.duration_since(self.last_time).as_micros()
as f32
* 0.001;
let delta = current_time.duration_since(self.last_time).as_micros() as f32 * 0.001;
self.last_time = current_time;
self.delta_time = delta;
delta
@ -58,7 +56,6 @@ impl TimeStep {
}
}
#[cfg(not(feature = "glfw_obsolete"))]
pub struct Engine {
title: String,
@ -72,7 +69,7 @@ impl Default for Engine {
Engine {
title: String::from("Doppler demo"),
size: (1280, 720),
debug_layer: true
debug_layer: true,
}
}
}
@ -198,7 +195,6 @@ impl Engine {
Event::NewEvents(_) => {
// other application-specific logic
last_frame = imgui.io_mut().update_delta_time(last_frame);
}
Event::MainEventsCleared => {
let delta = timestep.delta();
@ -211,6 +207,23 @@ impl Engine {
}
Event::LoopDestroyed => return,
Event::WindowEvent { event, .. } => match event {
WindowEvent::CursorMoved { position, .. } => {
let xpos = position.x as f32;
let ypos = position.y as f32;
if first_mouse {
last_x = xpos;
last_y = ypos;
first_mouse = false;
}
let xoffset = xpos - last_x;
let yoffset = last_y - ypos; // reversed since y-coordinates go from bottom to top
last_x = xpos;
last_y = ypos;
client.on_mouse_move(xoffset, yoffset);
}
WindowEvent::KeyboardInput {
input:
KeyboardInput {